Example usage for org.dom4j Document add

List of usage examples for org.dom4j Document add

Introduction

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

Prototype

void add(Node node);

Source Link

Document

Adds the given Node or throws IllegalAddException if the given node is not of a valid type.

Usage

From source file:no.met.jtimeseries.service.ServiceDescriptionGenerator.java

License:Open Source License

/**
 * Create a description of the web service offered by a class in XML. The
 * documentation is created by looking at the web service annotations.
 *
 * @param c//  w  w  w  .j  a va 2 s .  c o  m
 *            The class to generate documentation for.
 * @return A XML document object.
 */
public static Document getXMLServiceDescription(Class<? extends Object> c) {

    List<Method> serviceMethods = getServiceMethods(c);

    DocumentFactory df = new DocumentFactory();
    Document xmlDoc = df.createDocument();

    Element rootElement = df.createElement("services");
    xmlDoc.add(rootElement);
    for (Method m : serviceMethods) {

        Element service = rootElement.addElement("service");
        MethodInfo mi = getMethodInfo(m);
        service.addAttribute("path", mi.path);

        if (mi.produces != null) {
            service.addAttribute("returmMimeType", StringUtils.join(mi.produces, ','));
        }

        if (mi.description != null) {
            service.addAttribute("description", mi.description);
        }

        List<ParameterInfo> params = getParameters(m);
        for (ParameterInfo pi : params) {
            Element param = service.addElement("parameter");
            param.addAttribute("name", pi.name);
            if (pi.defaultValue != null) {
                param.addAttribute("defaultValue", pi.defaultValue);
            }
        }
    }

    return xmlDoc;
}

From source file:org.apache.taglibs.xtags.xpath.ReplaceTag.java

License:Apache License

public int doEndTag() throws JspException {
    Object context = TagHelper.getInputNodes(pageContext, this, false);
    if (context == null) {
        logInfo("No current node to replace");
        return EVAL_PAGE;
    }/*from w w  w .  jav  a2  s  .c  om*/

    try {
        String xmlFragment = null;
        if (bodyContent != null) {
            xmlFragment = bodyContent.getString();
        }
        if (context instanceof List) {
            List els = (List) context;
            if (els.size() > 1) {
                throw new JspException("Current context contains more than one node");
            }
            if (els.size() == 1) {
                context = els.get(0);
            }
        }
        if (context instanceof Document) {
            if (xmlFragment == null) {
                throw new JspException("Cannot replace document with empty body");
            }
            Document sourceDoc = (Document) context;
            Document newDoc = DocumentHelper.parseText(xmlFragment);

            // clear source doc contents
            sourceDoc.clearContent();
            for (int i = 0, size = newDoc.nodeCount(); i < size; i++) {
                Node node = newDoc.node(i);
                // detach from new doc
                node.detach();
                // add to source
                sourceDoc.add(node);
            }
        } else {
            if (!(context instanceof Element)) {
                throw new JspException("Current node is not an Element: " + context.getClass().getName());
            }
            Element element = (Element) context;

            SAXReader reader = new SAXReader();

            if (element.isRootElement()) {
                if (xmlFragment == null) {
                    throw new JspException("Cannot replace root element with empty body");
                }
                Document newDoc = DocumentHelper.parseText(xmlFragment);
                Document sourceDoc = element.getDocument();
                Element newRoot = newDoc.getRootElement();
                newRoot.detach();
                sourceDoc.setRootElement(newRoot);
            } else {
                Element parent = element.getParent();
                List parentContent = parent.content();
                int index = parentContent.indexOf(element);
                parentContent.remove(index);
                if (xmlFragment != null) {
                    Document newDoc = DocumentHelper.parseText("<dummy>" + xmlFragment + "</dummy>");
                    parentContent.addAll(index, newDoc.getRootElement().content());
                }
            }
        }
    } catch (DocumentException e) {
        handleException(e);
    }
    return EVAL_PAGE;
}

From source file:org.b5chat.crossfire.plugin.admin.AdminConsole.java

License:Open Source License

/**
 * Rebuilds the generated model./*from ww  w  .j  a v a2 s .  com*/
 */
private static synchronized void rebuildModel() {
    Document doc = DocumentFactory.getInstance().createDocument();
    generatedModel = coreModel.createCopy();
    doc.add(generatedModel);

    // Add in all overrides.
    for (Element element : overrideModels.values()) {
        // See if global settings are overriden.
        Element appName = (Element) element.selectSingleNode("//adminconsole/global/appname");
        if (appName != null) {
            Element existingAppName = (Element) generatedModel
                    .selectSingleNode("//adminconsole/global/appname");
            existingAppName.setText(appName.getText());
            if (appName.attributeValue("plugin") != null) {
                existingAppName.addAttribute("plugin", appName.attributeValue("plugin"));
            }
        }
        Element appLogoImage = (Element) element.selectSingleNode("//adminconsole/global/logo-image");
        if (appLogoImage != null) {
            Element existingLogoImage = (Element) generatedModel
                    .selectSingleNode("//adminconsole/global/logo-image");
            existingLogoImage.setText(appLogoImage.getText());
            if (appLogoImage.attributeValue("plugin") != null) {
                existingLogoImage.addAttribute("plugin", appLogoImage.attributeValue("plugin"));
            }
        }
        Element appLoginImage = (Element) element.selectSingleNode("//adminconsole/global/login-image");
        if (appLoginImage != null) {
            Element existingLoginImage = (Element) generatedModel
                    .selectSingleNode("//adminconsole/global/login-image");
            existingLoginImage.setText(appLoginImage.getText());
            if (appLoginImage.attributeValue("plugin") != null) {
                existingLoginImage.addAttribute("plugin", appLoginImage.attributeValue("plugin"));
            }
        }
        Element appVersion = (Element) element.selectSingleNode("//adminconsole/global/version");
        if (appVersion != null) {
            Element existingVersion = (Element) generatedModel
                    .selectSingleNode("//adminconsole/global/version");
            if (existingVersion != null) {
                existingVersion.setText(appVersion.getText());
                if (appVersion.attributeValue("plugin") != null) {
                    existingVersion.addAttribute("plugin", appVersion.attributeValue("plugin"));
                }
            } else {
                ((Element) generatedModel.selectSingleNode("//adminconsole/global"))
                        .add(appVersion.createCopy());
            }
        }
        // Tabs
        for (Object o : element.selectNodes("//tab")) {
            Element tab = (Element) o;
            String id = tab.attributeValue("id");
            Element existingTab = getElemnetByID(id);
            // Simple case, there is no existing tab with the same id.
            if (existingTab == null) {
                // Make sure that the URL on the tab is set. If not, default to the
                // url of the first item.
                if (tab.attributeValue("url") == null) {
                    Element firstItem = (Element) tab.selectSingleNode("//item[@url]");
                    if (firstItem != null) {
                        tab.addAttribute("url", firstItem.attributeValue("url"));
                    }
                }
                generatedModel.add(tab.createCopy());
            }
            // More complex case -- a tab with the same id already exists.
            // In this case, we have to overrite only the difference between
            // the two elements.
            else {
                overrideTab(existingTab, tab);
            }
        }
    }
}

From source file:org.craftercms.core.xml.mergers.impl.DescriptorMergerImpl.java

License:Open Source License

@Override
public Document merge(List<Document> descriptorsToMerge) throws XmlMergeException {
    Document merged = DocumentHelper.createDocument();

    if (CollectionUtils.isNotEmpty(descriptorsToMerge)) {
        Element mergedRoot = descriptorsToMerge.get(0).getRootElement().createCopy();

        for (Iterator<Document> i = descriptorsToMerge.listIterator(1); i.hasNext();) {
            Element descriptorRoot = i.next().getRootElement().createCopy();

            mergedRoot = initialMergeCue.merge(mergedRoot, descriptorRoot, initialMergeCueParams);
        }//w w  w. j av a  2 s. c  om

        merged.add(mergedRoot);
    }

    return merged;
}

From source file:org.etudes.api.app.melete.util.XMLHelper.java

License:Apache License

/**
 * Creates xml docuemnt and adds element
 * @param element/* w  w w .  j av  a 2 s .  com*/
 */
static public Document createXMLDocument(Element element) {
    Document document = DocumentHelper.createDocument();
    document.add(element);

    return document;
}

From source file:org.firesoa.common.jxpath.model.dom4j.Dom4JNodePointer.java

License:Open Source License

@Override
public void setValue(Object value) {
    if (value == null)
        value = "";//null??
    if ((node instanceof org.dom4j.CharacterData || node instanceof Attribute || node instanceof DocumentType
            || node instanceof Entity || node instanceof ProcessingInstruction)) {
        String string = (String) TypeUtils.convert(value, String.class);
        if (string != null && !string.equals("")) {
            ((Node) node).setText(string);
        } else {/*from  w w w  .  j a v  a2  s . c o  m*/
            ((Node) node).getParent().remove((Node) node);
        }
    } else if (node instanceof Document) {
        Document theOriginalDoc = (Document) node;
        Element theOrigialRoot = theOriginalDoc.getRootElement();

        if (value instanceof Document) {//?document

            Document valueDoc = (Document) value;
            Element valueRoot = valueDoc.getRootElement();

            if (theOrigialRoot == null || valueRoot == null
                    || theOrigialRoot.getQName().equals(valueRoot.getQName())) {

                theOriginalDoc.clearContent();

                List content = valueDoc.content();
                if (content != null) {
                    for (int i = 0; i < content.size(); i++) {
                        Node dom4jNode = (Node) content.get(i);
                        Node newDom4jNode = (Node) dom4jNode.clone();
                        theOriginalDoc.add(newDom4jNode);
                    }
                }
            } else {
                throw new RuntimeException(
                        "Can NOT assign " + valueRoot.getQName() + " to " + theOrigialRoot.getQName());

            }

        } else if (value instanceof Element) {
            Element valueElem = (Element) value;
            if (valueElem.getQName().equals(theOrigialRoot.getQName())) {
                theOriginalDoc.clearContent();
                Element newValueElem = (Element) valueElem.clone();
                theOriginalDoc.setRootElement(newValueElem);
            } else {
                throw new RuntimeException(
                        "Can NOT assign " + valueElem.getQName() + " to " + theOrigialRoot.getQName());
            }
        } else {
            throw new RuntimeException("Can NOT assign " + value + " to " + theOrigialRoot.getQName());

        }
        //         else if (value instanceof Comment){
        //            Comment cmmt = (Comment)((Comment)value).clone();
        //            theOriginalDoc.add(cmmt);
        //            
        //         }else if (value instanceof ProcessingInstruction){
        //            ProcessingInstruction instru = (ProcessingInstruction)((ProcessingInstruction)value).clone();
        //            theOriginalDoc.add(instru);
        //         }

    } else if (node instanceof Element) {
        Element originalElem = ((Element) node);

        if (value != null && value instanceof Element) {
            Element valueElm = (Element) value;
            if (originalElem.getQName().equals(valueElm.getQName())) {
                originalElem.clearContent();
                List content = valueElm.content();
                if (content != null) {
                    for (int i = 0; i < content.size(); i++) {
                        Node dom4jNode = (Node) content.get(i);
                        Node newDom4jNode = (Node) dom4jNode.clone();
                        originalElem.add(newDom4jNode);
                    }
                }
            } else {
                throw new RuntimeException(
                        "Can NOT assign " + valueElm.getQName() + " to " + originalElem.getQName());
            }

        } else if (value != null && value instanceof Text) {
            originalElem.clearContent();
            Text txt = (Text) ((Text) value).clone();
            originalElem.add(txt);
        } else if (value != null && value instanceof CDATA) {
            originalElem.clearContent();
            CDATA cdata = (CDATA) ((CDATA) value).clone();
            originalElem.add(cdata);
        } else if (value != null && value instanceof java.util.Date) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dateStr = format.format((java.util.Date) value);
            originalElem.clearContent();
            originalElem.addText(dateStr);
        } else {
            String string = (String) TypeUtils.convert(value, String.class);
            originalElem.clearContent();
            originalElem.addText(string);
        }

    }

}

From source file:org.foxbpm.bpmn.converter.BpmnXMLConverter.java

License:Apache License

/**
 * bpmnModel??documnet/*from   w  w w . j a v a  2  s .  co  m*/
 * 
 * @param model
 *            bpmn
 */
public Document convertToXML(BpmnModel model) {

    if (null == model) {
        throw new BpmnConverterException("?XML?!");
    }

    DocumentFactory factory = DocumentFactory.getInstance();
    Document doc = factory.createDocument();
    Element element = factory.createElement(
            BpmnXMLConstants.BPMN2_PREFIX + ':' + BpmnXMLConstants.ELEMENT_DEFINITIONS,
            BpmnXMLConstants.BPMN2_NAMESPACE);
    element.addNamespace(BpmnXMLConstants.XSI_PREFIX, BpmnXMLConstants.XSI_NAMESPACE);
    element.addNamespace(BpmnXMLConstants.DC_PREFIX, BpmnXMLConstants.DC_NAMESPACE);
    element.addNamespace(BpmnXMLConstants.DI_PREFIX, BpmnXMLConstants.DI_NAMESPACE);
    element.addNamespace(BpmnXMLConstants.BPMNDI_PREFIX, BpmnXMLConstants.BPMNDI_NAMESPACE);
    element.addNamespace(BpmnXMLConstants.FOXBPM_PREFIX, BpmnXMLConstants.FOXBPM_NAMESPACE);
    element.addNamespace(BpmnXMLConstants.XSD_PREFIX, BpmnXMLConstants.XSD_NAMESPACE);
    element.addNamespace(BpmnXMLConstants.EMPTY_STRING, BpmnXMLConstants.XMLNS_NAMESPACE);
    // 
    element.addAttribute(BpmnXMLConstants.TARGET_NAMESPACE_ATTRIBUTE, BpmnXMLConstants.XMLNS_NAMESPACE);
    element.addAttribute(BpmnXMLConstants.ATTRIBUTE_ID, "Definitions_1");
    doc.add(element);
    // ??
    try {
        for (Iterator<Process> iterator = model.getProcesses().iterator(); iterator.hasNext();) {
            ProcessExport.writeProcess(iterator.next(), element);
        }
        // ????
        BPMNDIExport.writeBPMNDI(model, element);
    } catch (Exception e) {
        LOGGER.error("?XML???" + model.getProcesses().get(0).getName(), e);
        if (e instanceof BpmnConverterException) {
            throw (BpmnConverterException) e;
        } else {
            throw new BpmnConverterException(
                    "?XML???" + model.getProcesses().get(0).getName(), e);
        }
    }
    return doc;
}

From source file:org.gbif.portal.util.mhf.message.impl.xml.XMLMessageFactory.java

License:Open Source License

/**
 * Creates a new Message based on the node that is passed in
 *
 * @param node   To copy or detach/*from  ww w.  ja va 2  s. c  o m*/
 * @param detach If the node may be detached from the parent document
 * @return A new message
 */
public XMLMessage build(Node node, boolean detach) {
    Document document = DocumentFactory.getInstance().createDocument();
    if (detach) {
        document.add(node.detach());
    } else {
        Node cloned = (Node) node.clone();
        document.add(cloned);
    }
    return new XMLMessage(document);
}

From source file:org.infoglue.cms.applications.contenttool.actions.ContentTreeXMLAction.java

License:Open Source License

public String doContentVersion() throws Exception {
    Document doc = DocumentHelper.createDocument();
    doc.add(getContentVersionElement(parent));
    return out(getFormattedDocument(doc));
}

From source file:org.infoglue.cms.applications.contenttool.actions.SimpleContentXmlAction.java

License:Open Source License

public String doContent() throws Exception {
    Document doc = DocumentHelper.createDocument();
    doc.add(getContentElement(parent));
    return out(getFormattedDocument(doc));
}