How to create DCRs from a java class

Posted: 7 May 2009 in CSSDK, Data Content Records, FormsPublisher, Java, Teamsite

Imagine for a second you want to create a DCR which produces this XML:

<?xml version="1.0" encoding="UTF-8"?>
<article>
    <title>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</title>
    <date>20 January 2009</date>
    <content>Proin bibendum lectus et sem faucibus vitae aliquet sapien condimentum. Morbi dignissim elit eget mauris iaculis ac tincidunt nisl ultrices. Vivamus eu nisi ac massa semper sollicitudin eget lacinia sem. Praesent nisl risus, feugiat a luctus at, dignissim quis sapien. Fusce ut ligula vitae dui dapibus aliquet non sit amet sem. Sed blandit accumsan dictum. Sed mollis volutpat ultricies. Nam arcu erat, convallis non placerat dignissim, fringilla in nulla. Aliquam pulvinar nisi est, vel venenatis ipsum. Maecenas facilisis, purus sit amet rhoncus congue, ante nisi dignissim risus, sed consequat ipsum sapien viverra massa. Praesent ac viverra dui. Vestibulum nec nibh mauris, a convallis eros. Proin pulvinar, tellus sed pulvinar pulvinar, orci risus dapibus enim, sit amet tempus odio purus quis nibh. Vestibulum sed nunc ante, a faucibus leo. Pellentesque rutrum sapien eu massa bibendum convallis. Nullam erat orci, iaculis in aliquet et, fringilla in tellus. Nunc quis dui libero, nec tempor arcu. Sed bibendum, magna feugiat lobortis gravida, lacus ipsum aliquet enim, sed adipiscing mi quam fringilla turpis. In convallis felis pellentesque erat dignissim et pellentesque tortor porttitor.</content>
</article>

With a class containing the following code, this would be possible:

// creating DCR for news/article
try {
    CSExtendedAttribute[] articlesExtendedAttributes = {new CSExtendedAttribute("TeamSite/Templating/DCR/Type","news/article")};
    Transformer transformer = transformerFactory.newTransformer();
    docBuilder = docBuilderFactory.newDocumentBuilder();
    Document doc = docBuilder.newDocument();
    doc.setXmlVersion("1.0");
    Element rootElement = doc.createElement("Content");
    doc.appendChild(rootElement);

    Element articleElement = doc.createElement("article");
    Element titleElement = doc.createElement("title");
    Element dateElement = doc.createElement("date");
    Element contentElement = doc.createElement("content");

    titleElement.setTextContent("Lorem ipsum dolor sit amet, consectetur adipiscing elit.");
    dateElement.setTextContent(new Date().toString());
    contentElement.setTextContent("Proin bibendum lectus et sem faucibus vitae aliquet sapien condimentum. Morbi dignissim elit eget mauris iaculis ac tincidunt nisl ultrices. Vivamus eu nisi ac massa semper sollicitudin eget lacinia sem. Praesent nisl risus, feugiat a luctus at, dignissim quis sapien. Fusce ut ligula vitae dui dapibus aliquet non sit amet sem. Sed blandit accumsan dictum. Sed mollis volutpat ultricies. Nam arcu erat, convallis non placerat dignissim, fringilla in nulla. Aliquam pulvinar nisi est, vel venenatis ipsum. Maecenas facilisis, purus sit amet rhoncus congue, ante nisi dignissim risus, sed consequat ipsum sapien viverra massa. Praesent ac viverra dui. Vestibulum nec nibh mauris, a convallis eros. Proin pulvinar, tellus sed pulvinar pulvinar, orci risus dapibus enim, sit amet tempus odio purus quis nibh. Vestibulum sed nunc ante, a faucibus leo. Pellentesque rutrum sapien eu massa bibendum convallis. Nullam erat orci, iaculis in aliquet et, fringilla in tellus. Nunc quis dui libero, nec tempor arcu. Sed bibendum, magna feugiat lobortis gravida, lacus ipsum aliquet enim, sed adipiscing mi quam fringilla turpis. In convallis felis pellentesque erat dignissim et pellentesque tortor porttitor.");
    rootElement.appendChild(articleElement);
    articleElement.appendChild(titleElement);
    articleElement.appendChild(dateElement);
    articleElement.appendChild(contentElement);

    String outputFileName = workarea.getVPath().getPathNoServer() + "/templatedata/news/article/data/" + "Lorem_ipsum_dolor_sit_amet.xml";
    Source source = new DOMSource(doc);
    OutputStream outputStream = new FileOutputStream(outputFileName);
    transformer.transform(source, new StreamResult(outputStream));
    CSSimpleFile articleDCRFile = (CSSimpleFile) client.getFile(new CSVPath(workarea.getVPath().getPathNoServer() + "/templatedata/news/article/data/" + "Lorem_ipsum_dolor_sit_amet.xml"));
    articleDCRFile.setExtendedAttributes(articlesExtendedAttributes);
} catch (Exception ex) {
    Logger.getLogger(MyClass.class.getName()).log(Level.SEVERE, null, ex);
}
Advertisements
Comments
  1. cgdm4884 says:

    Hi Laurent,
    if I wanted to create a Page from a java class?
    or, if you wanted to add a component to a Page always a java class?
    and if I wanted to change the content of a component in a Page?
    I treat the Page as an XML file or are there methods (API) suitable?

  2. a page is just an XML document so you can create them from java like you would any other XML document.
    It may be easier to create some kind of master page, copy it and edit it via java. I have done this before as a workflow which would create entire sites ready to be edited.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s