Example usage for org.dom4j DocumentFactory createDocument

List of usage examples for org.dom4j DocumentFactory createDocument

Introduction

In this page you can find the example usage for org.dom4j DocumentFactory createDocument.

Prototype

public Document createDocument(Element rootElement) 

Source Link

Usage

From source file:org.eclipse.birt.build.VersionUpdater.java

License:Open Source License

/**
 * @author /*from  w  w w .jav  a  2  s.c o m*/
 * @param folderPath
 * @param plug_id
 * @param lastDate
 * @param dayInpast
 */
private void genVersionLog(File folderPath, String plug_id, String lastDate, int dayInpast) {
    // gen the dest file path
    String parentPath = folderPath.getAbsolutePath();
    String fileName = plug_id + "_DayInPast" + ".xml";
    String fullName = parentPath + "/" + fileName;
    File dest = new File(fullName);
    System.out.println("dest file full path:\t" + fullName);
    try {
        //genarate document factory
        DocumentFactory factory = new DocumentFactory();
        //create root element
        DOMElement rootElement = new DOMElement("plugin");
        rootElement.setAttribute("id", plug_id);
        //add child:lastdate
        DOMElement dateElement = new DOMElement("LastDate");
        dateElement.setText(lastDate);
        rootElement.add(dateElement);
        //add child:dayinpast
        DOMElement dayElement = new DOMElement("DayInPast");
        dayElement.setText(Integer.toString(dayInpast));
        rootElement.add(dayElement);
        //gen the doc
        Document doc = factory.createDocument(rootElement);

        //PrettyFormat
        OutputFormat format = OutputFormat.createPrettyPrint();
        XMLWriter writer = new XMLWriter(new FileWriter(dest), format);
        writer.write(doc);
        writer.close();

    } catch (Exception ex) {
        ex.printStackTrace();
    }

}

From source file:org.frogx.service.maven.plugin.games.GenerateFrogxResourcesMojo.java

License:Open Source License

public boolean generatePluginDescriptor() {
    // Check requirements
    if (match == null || namespace == null) {
        return false;
    }//ww w  . j a v a2  s . c  om

    // Create XML data
    getLog().info("Generating a game descriptor (" + frogxPluginDescriptor + ").");
    DocumentFactory documentFactory = DocumentFactory.getInstance();
    Element gameEl = documentFactory.createElement("plugin", pluginDescriptorNS);

    Element element = gameEl.addElement("type");
    element.setText("match");

    element = gameEl.addElement("match");
    element.setText(match);

    element = gameEl.addElement("namespace");
    element.setText(namespace);

    if (description != null) {
        element = gameEl.addElement("description");
        element.setText(description);
    }

    if (category != null) {
        element = gameEl.addElement("category");
        element.setText(category);
    }

    Document doc = documentFactory.createDocument(gameEl);
    doc.setXMLEncoding("UTF-8");

    // Check and create the necessary folder
    File genResourceDir = getGeneratedFrogxResourcesDirectory();
    if (!genResourceDir.exists()) {
        genResourceDir.mkdirs();
    }

    // Write the game descriptor
    try {
        writeDocument(doc, new File(getGeneratedFrogxResourcesDirectory(), frogxPluginDescriptor));
    } catch (IOException e) {
        getLog().warn("An Error occured while writing the generated " + "game descriptor. ", e);
        return false;
    }
    return true;
}

From source file:org.orbeon.oxf.xml.dom4j.Dom4jUtils.java

License:Open Source License

/**
 * Return a new document with a copy of newRoot as its root.
 *//*w  ww.j  av a2 s  . c om*/
public static Document createDocumentCopyElement(final Element newRoot) {
    final Element copy = newRoot.createCopy();
    final DocumentFactory factory = NonLazyUserDataDocumentFactory.getInstance();
    return factory.createDocument(copy);
}

From source file:org.talend.updates.runtime.nexus.component.ComponentIndexManager.java

License:Open Source License

public boolean createIndexFile(File indexFile, List<ComponentIndexBean> newIndexList) throws IOException {
    if (newIndexList == null || newIndexList.isEmpty() || indexFile == null) {
        return false;
    }/*from ww  w  . j  a  va  2 s . c o  m*/

    XMLWriter xmlWriter = null;
    boolean created = false;
    try {
        // write to index
        final DocumentFactory docFactory = DocumentFactory.getInstance();
        final Element components = docFactory.createElement(ELEM_COMPONENTS);
        Document newDoc = docFactory.createDocument(components);
        for (ComponentIndexBean b : newIndexList) {
            final Element elem = createXmlElement(b);
            if (elem != null) {
                components.add(elem);
            }
        }

        // 4 spaces
        OutputFormat format = new OutputFormat();
        format.setEncoding("UTF-8"); //$NON-NLS-1$
        format.setIndentSize(4);
        format.setNewlines(true);
        xmlWriter = new XMLWriter(new FileOutputStream(indexFile), format);

        xmlWriter.write(newDoc);

        created = true;
        return true;
    } finally {
        if (xmlWriter != null) {
            try {
                xmlWriter.close();
            } catch (IOException e) {
                //
            }
        }
        if (!created && indexFile.exists()) {
            indexFile.delete(); // remove the wrong file.
        }
    }
}