Example usage for org.dom4j Document setRootElement

List of usage examples for org.dom4j Document setRootElement

Introduction

In this page you can find the example usage for org.dom4j Document setRootElement.

Prototype

void setRootElement(Element rootElement);

Source Link

Document

Sets the root element for this document

Usage

From source file:cc.warlock.core.configuration.WarlockConfiguration.java

License:Open Source License

public void save() {
    Document document = DocumentHelper.createDocument();
    Element warlockConfig = DocumentHelper.createElement("warlock-config");

    document.setRootElement(warlockConfig);

    for (IConfigurationProvider provider : providers) {
        List<Element> elements = provider.getTopLevelElements();

        for (Element element : elements) {
            warlockConfig.add(element);//from w ww. j  ava 2 s .  co m
        }
    }

    for (Element unhandled : unhandledElements) {
        // Make sure to resave unhandled elements, just in case the corresponding handler wasn't instantiated
        warlockConfig.add(unhandled.createCopy());
    }

    try {
        if (configFile.exists()) {
            File backupFile = new File(configFile.getPath() + ".bak");
            if (backupFile.exists())
                backupFile.renameTo(new File(backupFile.getPath() + ".1"));
            configFile.renameTo(backupFile);
        }
        OutputFormat format = OutputFormat.createPrettyPrint();
        FileOutputStream stream = new FileOutputStream(configFile);
        XMLWriter writer = new XMLWriter(stream, format);
        writer.write(document);
        stream.close();

    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:ch.javasoft.xml.config.XmlConfig.java

License:BSD License

private static Document createDocument(Element configElement, String configName) {
    final Document doc = DocumentHelper.createDocument();
    final Element configList = DocumentHelper.createElement(XmlElement.config_list.getXmlName());
    configElement.addAttribute(XmlAttribute.name.getXmlName(), configName);
    configList.addAttribute(XmlAttribute.def.getXmlName(), configName);
    configList.add(configElement);// w  w w .  j  a  v a  2s  .c om
    doc.setRootElement(configList);
    return doc;
}

From source file:com.arc.mw.util.StateSaver.java

License:Open Source License

/**
 * Save the state of the root object into the given file.
 * @param out ascii stream to write to.// w ww  .ja v  a  2 s  .  c  o m
 * @param object object whose state is to be saved.
 * @exception IOException error occurred in writing file.
 */
public static void saveState(Writer out, IXMLSavable object) throws IOException {
    DocumentFactory factory = DocumentFactory.getInstance();
    Document document = factory.createDocument();
    Element e = factory.createElement("root");
    object.saveState(e);
    document.setRootElement(e);
    XMLWriter writer = new XMLWriter(out, OutputFormat.createPrettyPrint());
    writer.write(document);
}

From source file:com.cladonia.xml.XDocumentFactory.java

License:Open Source License

/** 
 * Creates the XDocument. //from   w  w  w.  ja  v a 2  s.c o m
*
* @param root the root element.
 *
 * @return the XDocument.
 */
public Document createDocument(Element root) {
    Document answer = createDocument();
    answer.setRootElement(root);

    return answer;
}

From source file:com.dtolabs.client.services.RundeckAPICentralDispatcher.java

License:Apache License

@Override
public void createProject(final String project, final Properties projectProperties)
        throws CentralDispatcherException {
    final HashMap<String, String> params = new HashMap<String, String>();

    Document document = DocumentFactory.getInstance().createDocument();
    Element project1 = DocumentFactory.getInstance().createElement("project");
    document.setRootElement(project1);
    project1.addElement("name").addText(project);
    Element config = project1.addElement("config");

    for (Object o : projectProperties.keySet()) {
        config.addElement("property").addAttribute("key", o.toString()).addAttribute("value",
                projectProperties.getProperty(o.toString()));
    }//from   w ww  . java 2 s .co  m
    //serialize to temp file
    File temp = null;
    try {
        temp = File.createTempFile("rundeck-api", ".xml");
        temp.deleteOnExit();
        try (FileOutputStream fos = new FileOutputStream(temp)) {
            serialize(document, fos);
        } catch (IOException e) {
            throw new CentralDispatcherServerRequestException("Failed to serialize request document", e);
        }
    } catch (IOException e) {
        throw new CentralDispatcherServerRequestException("Failed to serialize request document", e);
    }

    /*
     * Send the request bean and the file as a multipart request.
     */

    //2. send request via ServerService
    final WebserviceResponse response;
    try {
        response = getServerService().makeRundeckRequest(RUNDECK_API_PROJECTS, params, temp, "POST", "text/xml",
                "text/xml");
    } catch (MalformedURLException e) {
        throw new CentralDispatcherServerRequestException("Failed to make request", e);
    } finally {
        temp.delete();
    }
    if (response.getResultCode() != 201) {
        throw new CentralDispatcherServerRequestException("Failed to create the project, result code: "
                + response.getResultCode() + " " + response.getResponseMessage());
    }

    validateResponse(response);
}

From source file:com.haulmont.cuba.gui.presentations.PresentationsImpl.java

License:Apache License

@Override
public Element getSettings(Presentation p) {
    p = getPresentation(p.getId());//from ww  w.jav  a 2 s .c o m
    if (p != null) {
        Document doc;
        if (!StringUtils.isEmpty(p.getXml())) {
            doc = Dom4j.readDocument(p.getXml());
        } else {
            doc = DocumentHelper.createDocument();
            doc.setRootElement(doc.addElement("presentation"));
        }
        return doc.getRootElement();
    } else {
        return null;
    }
}

From source file:com.haulmont.cuba.web.gui.components.presentations.PresentationEditor.java

License:Apache License

protected void commit() {
    Presentations presentations = component.getPresentations();

    Document doc = DocumentHelper.createDocument();
    doc.setRootElement(doc.addElement("presentation"));

    component.saveSettings(doc.getRootElement());

    String xml = Dom4j.writeDocument(doc, false);
    presentation.setXml(xml);/* ww  w  .  ja  v a 2  s.c om*/

    presentation.setName(nameField.getValue());
    presentation.setAutoSave(autoSaveField.getValue());
    presentation.setDefault(defaultField.getValue());

    User user = sessionSource.getUserSession().getCurrentOrSubstitutedUser();

    boolean userOnly = !allowGlobalPresentations || !BooleanUtils.isTrue(globalField.getValue());
    presentation.setUser(userOnly ? user : null);

    if (log.isTraceEnabled()) {
        log.trace(String.format("XML: %s", Dom4j.writeDocument(doc, true)));
    }

    if (isNew) {
        presentations.add(presentation);
    } else {
        presentations.modify(presentation);
    }
    presentations.commit();

    addCloseListener(e -> {
        if (isNew) {
            component.applyPresentation(presentation.getId());
        }
    });
}

From source file:com.heren.turtle.server.utils.XmlUtils.java

License:Open Source License

/**
 * back failure information/*from   w w  w  .java 2 s  .co  m*/
 *
 * @param message
 * @return
 */
public static String errorMessage(String message) {
    Document document = DocumentHelper.createDocument();
    Element payload = DocumentHelper.createElement("payload");
    document.setRootElement(payload);
    Element response = payload.addElement("response");
    Element result = response.addElement("result");
    result.setText("true");
    Element resultText = response.addElement("resultText");
    resultText.setText(message == null ? "" : message);
    Element userId = response.addElement("userId");
    userId.setText("0001");
    return document.asXML();
}

From source file:com.heren.turtle.server.utils.XmlUtils.java

License:Open Source License

/**
 * back correct information//  www  .  ja  v  a2  s  . c o  m
 *
 * @param flag 0 no data 1 some exception
 * @return
 */
public static String correctMessage(int flag) {
    Document document = DocumentHelper.createDocument();
    Element payload = DocumentHelper.createElement("payload");
    document.setRootElement(payload);
    Element response = payload.addElement("response");
    Element result = response.addElement("result");
    result.setText("true");
    Element resultText = response.addElement("resultText");
    switch (flag) {
    case 0:
        resultText.setText("no data found!");
        break;
    case -1:
        resultText.setText("success!");
        break;
    default:
        break;
    }
    Element userId = response.addElement("userId");
    userId.setText("0001");
    return document.asXML();
}

From source file:com.heren.turtle.server.utils.XmlUtils.java

License:Open Source License

/**
 * back successful information/*ww  w  . ja  va 2  s.  co m*/
 *
 * @return
 */
public static String resultMessage() {
    Document document = DocumentHelper.createDocument();
    Element payload = DocumentHelper.createElement("payload");
    document.setRootElement(payload);
    Element response = payload.addElement("response");
    Element result = response.addElement("result");
    result.setText("true");
    Element resultText = response.addElement("resultText");
    resultText.setText("??");
    Element userId = response.addElement("userId");
    userId.setText("0001");
    return document.asXML();
}