Example usage for org.dom4j Element add

List of usage examples for org.dom4j Element add

Introduction

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

Prototype

void add(Namespace namespace);

Source Link

Document

Adds the given Namespace to this element.

Usage

From source file:com.jonschang.ai.ga.GenericPhenotype.java

License:LGPL

public Element getXml() throws XmlException {
    DocumentFactory f = DocumentFactory.getInstance();
    Element toRet = f.createElement("phenotype");
    toRet.addAttribute("class", "com.jonschang.ai.ga.GenericPhenotype");
    toRet.addAttribute("score", ((Double) (score != null ? score : 0.0)).toString());
    Element geneEle = null;// w  w  w. j a v  a  2 s.  c o  m
    for (Gene gene : this.genes) {
        toRet.add(gene.getXml());
    }
    return toRet;
}

From source file:com.liferay.alloy.tools.builder.base.BaseBuilder.java

License:Open Source License

protected List<Component> getAllComponents() throws Exception {
    DocumentFactory factory = SAXReaderUtil.getDocumentFactory();

    Document doc = factory.createDocument();

    String taglibsXML = "<components></components>";

    Document taglibsDoc = SAXReaderUtil
            .read(new InputSource(new ByteArrayInputStream(taglibsXML.getBytes("utf-8"))));

    Element root = taglibsDoc.getRootElement();

    for (Document currentDoc : getComponentDefinitionDocs()) {
        currentDoc = _getExtendedDocument(currentDoc);

        Element currentRoot = currentDoc.getRootElement();

        String defaultPackage = currentRoot.attributeValue("short-name");
        List<Element> extComponentNodes = currentRoot.elements("component");

        for (Element extComponent : extComponentNodes) {
            String extComponentPackage = Convert.toString(extComponent.attributeValue("package"),
                    defaultPackage);/*from   w w w.ja v a 2  s.c  o  m*/

            extComponent.addAttribute("package", extComponentPackage);
        }

        Element authors = currentRoot.element("author");

        List<Element> components = currentRoot.elements("component");

        for (Element component : components) {
            Element copy = component.createCopy();
            Element componentAuthors = copy.element("authors");

            if ((authors != null) && (componentAuthors == null)) {
                copy.add(authors.createCopy());
            }

            root.add(copy);
        }

        List<org.dom4j.Attribute> attributes = currentRoot.attributes();

        for (org.dom4j.Attribute attribute : attributes) {
            root.addAttribute(attribute.getName(), attribute.getValue());
        }
    }

    doc.add(root.createCopy());

    return getComponents(doc);
}

From source file:com.liferay.alloy.tools.builder.base.BaseBuilder.java

License:Open Source License

protected Document mergeXMLAttributes(Document doc1, Document doc2) {
    Element doc2Root = doc2.getRootElement();
    Element doc1Root = doc1.getRootElement();
    Element docRoot = doc2Root.createCopy();

    docRoot.clearContent();/*ww w  .  j a  v a2  s .  c  o m*/

    if (doc1Root != null) {
        Iterator<Object> attributesIterator = doc1Root.attributeIterator();

        while (attributesIterator.hasNext()) {
            org.dom4j.Attribute attribute = (org.dom4j.Attribute) attributesIterator.next();

            if (attribute.getName().equals("extends")) {
                continue;
            }

            docRoot.addAttribute(attribute.getName(), attribute.getValue());
        }

        Element descriptionElement = doc1Root.element("description");

        if (descriptionElement != null) {
            docRoot.add(descriptionElement.createCopy());
        }
    }

    DocumentFactory factory = SAXReaderUtil.getDocumentFactory();

    Document doc = factory.createDocument();
    doc.setRootElement(docRoot);

    List<Element> doc2Components = doc2Root.elements(_COMPONENT);

    for (Element doc2Component : doc2Components) {
        Element component = doc2Component.createCopy();

        String name = doc2Component.attributeValue("name");

        Element doc1Component = getComponentNode(doc1, name);

        if (doc1Component != null) {
            Element doc1ComponentDescriptionElement = doc1Component.element("description");

            if (doc1ComponentDescriptionElement != null) {
                Element descriptionElement = component.element("description");

                if (descriptionElement != null) {
                    component.remove(descriptionElement);
                }

                component.add(doc1ComponentDescriptionElement.createCopy());
            }

            Iterator<Object> attributesIterator = doc1Component.attributeIterator();

            while (attributesIterator.hasNext()) {
                org.dom4j.Attribute attribute = (org.dom4j.Attribute) attributesIterator.next();

                component.addAttribute(attribute.getName(), attribute.getValue());
            }

            Element doc1AttributesNode = doc1Component.element(_ATTRIBUTES);

            Element attributesNode = component.element(_ATTRIBUTES);

            if ((doc1AttributesNode != null) && (attributesNode != null)) {
                List<Element> doc1Attributes = doc1AttributesNode.elements(_ATTRIBUTE);

                List<Element> attributes = attributesNode.elements(_ATTRIBUTE);

                for (Element doc1Attribute : doc1Attributes) {
                    Element attribute = getElementByName(attributes, doc1Attribute.elementText("name"));

                    if (attribute != null) {
                        attributesNode.remove(attribute);
                    }

                    attributesNode.add(doc1Attribute.createCopy());
                }
            }

            Element doc1EventsNode = doc1Component.element(_EVENTS);

            Element eventsNode = component.element(_EVENTS);

            if ((doc1EventsNode != null) && (eventsNode != null)) {
                List<Element> doc1Events = doc1EventsNode.elements(_EVENT);

                List<Element> events = eventsNode.elements(_EVENT);

                for (Element doc1Event : doc1Events) {
                    Element event = getElementByName(events, doc1Event.elementText("name"));

                    if (event != null) {
                        eventsNode.add(event);
                    }

                    eventsNode.add(doc1Event.createCopy());
                }
            }
        }

        doc.getRootElement().add(component);
    }

    if (doc1Root != null) {
        List<Element> doc1Components = doc1Root.elements(_COMPONENT);

        for (Element doc1Component : doc1Components) {
            Element component = doc1Component.createCopy();

            String name = doc1Component.attributeValue("name");

            Element doc2Component = getComponentNode(doc2, name);

            if (doc2Component == null) {
                doc.getRootElement().add(component);
            }
        }
    }

    return doc;
}

From source file:com.liferay.alloy.tools.builder.taglib.TagBuilder.java

License:Open Source License

protected Document mergeTlds(Document sourceDoc, Document targetDoc) {
    Element targetRoot = targetDoc.getRootElement();

    DocumentFactory factory = SAXReaderUtil.getDocumentFactory();

    XPath xpathTags = factory.createXPath("//tld:tag");

    Map<String, String> namespaceContextMap = new HashMap<String, String>();

    namespaceContextMap.put(_TLD_XPATH_PREFIX, _TLD_XPATH_URI);

    NamespaceContext namespaceContext = new AlloyGeneratorNamespaceContext(namespaceContextMap);

    xpathTags.setNamespaceContext(namespaceContext);

    List<Node> sources = xpathTags.selectNodes(sourceDoc);

    for (Node source : sources) {
        Element sourceElement = (Element) source;

        String sourceName = sourceElement.elementText("name");

        String xpathTagValue = "//tld:tag[tld:name='" + sourceName + "']";

        XPath xpathTag = factory.createXPath(xpathTagValue);

        xpathTag.setNamespaceContext(namespaceContext);

        List<Node> targets = xpathTag.selectNodes(targetDoc);

        if (targets.size() > 0) {
            Element targetElement = (Element) targets.get(0);

            XPath xpathAttributes = factory.createXPath(xpathTagValue + "//tld:attribute");

            Map<String, String> namespaces = new HashMap<String, String>();

            namespaces.put("tld", StringPool.EMPTY);

            xpathAttributes.setNamespaceURIs(namespaces);

            List<Node> sourceAttributes = xpathAttributes.selectNodes(source);

            for (Node sourceAttribute : sourceAttributes) {
                Element sourceAttributeElement = (Element) sourceAttribute;

                String attributeName = sourceAttributeElement.elementText("name");

                String xpathAttributeValue = "//tld:attribute[tld:name='" + attributeName + "']";

                XPath xpathAttribute = factory.createXPath(xpathTagValue + xpathAttributeValue);

                xpathAttribute.setNamespaceContext(namespaceContext);

                Node targetAttribute = xpathAttribute.selectSingleNode(targetElement);

                if (targetAttribute != null) {
                    targetAttribute.detach();
                }/*w w  w  . java  2  s  .  c o m*/

                targetElement.add(sourceAttributeElement.createCopy());
            }

            Element dynamicAttrElement = targetElement.element("dynamic-attributes");

            if (dynamicAttrElement != null) {
                targetElement.add(dynamicAttrElement.detach());
            }
        } else {
            targetRoot.add(sourceElement.createCopy());
        }
    }

    return targetDoc;
}

From source file:com.liferay.portal.tools.service.builder.ServiceBuilder.java

License:Open Source License

public static String getContent(String fileName) throws Exception {
    Document document = _getContentDocument(fileName);

    Element rootElement = document.getRootElement();

    Element authorElement = null;
    Element namespaceElement = null;
    Map<String, Element> entityElements = new TreeMap<>();
    Map<String, Element> exceptionElements = new TreeMap<>();

    List<Element> elements = rootElement.elements();

    for (Element element : elements) {
        String elementName = element.getName();

        if (elementName.equals("author")) {
            element.detach();//from  w  w  w .ja  v  a 2s.  c o m

            if (authorElement != null) {
                throw new IllegalArgumentException("There can only be one author element");
            }

            authorElement = element;
        } else if (elementName.equals("namespace")) {
            element.detach();

            if (namespaceElement != null) {
                throw new IllegalArgumentException("There can only be one namespace element");
            }

            namespaceElement = element;
        } else if (elementName.equals("entity")) {
            element.detach();

            String name = element.attributeValue("name");

            entityElements.put(StringUtil.toLowerCase(name), element);
        } else if (elementName.equals("exceptions")) {
            element.detach();

            List<Element> exceptionElementsList = element.elements("exception");

            for (Element exceptionElement : exceptionElementsList) {
                exceptionElement.detach();

                exceptionElements.put(exceptionElement.getText(), exceptionElement);
            }
        }
    }

    if (authorElement != null) {
        rootElement.add(authorElement);
    }

    if (namespaceElement == null) {
        throw new IllegalArgumentException("The namespace element is required");
    } else {
        rootElement.add(namespaceElement);
    }

    _addElements(rootElement, entityElements);

    if (!exceptionElements.isEmpty()) {
        Element exceptionsElement = rootElement.addElement("exceptions");

        _addElements(exceptionsElement, exceptionElements);
    }

    return document.asXML();
}

From source file:com.liferay.portal.tools.service.builder.ServiceBuilder.java

License:Open Source License

private static void _addElements(Element element, Map<String, Element> elements) {

    for (Map.Entry<String, Element> entry : elements.entrySet()) {
        Element childElement = entry.getValue();

        element.add(childElement);
    }/*from   w w w  . j av  a2s.  c  om*/
}

From source file:com.liferay.portal.tools.service.builder.ServiceBuilder.java

License:Open Source License

private static Document _getContentDocument(String fileName) throws Exception {

    SAXReader saxReader = _getSAXReader();

    Document document = saxReader.read(new File(fileName));

    Element rootElement = document.getRootElement();

    List<Element> elements = rootElement.elements();

    for (Element element : elements) {
        String elementName = element.getName();

        if (!elementName.equals("service-builder-import")) {
            continue;
        }/*w w  w.  ja v  a2s  .c o m*/

        element.detach();

        String dirName = fileName.substring(0, fileName.lastIndexOf(StringPool.SLASH) + 1);
        String serviceBuilderImportFileName = element.attributeValue("file");

        Document serviceBuilderImportDocument = _getContentDocument(dirName + serviceBuilderImportFileName);

        Element serviceBuilderImportRootElement = serviceBuilderImportDocument.getRootElement();

        List<Element> serviceBuilderImportElements = serviceBuilderImportRootElement.elements();

        for (Element serviceBuilderImportElement : serviceBuilderImportElements) {

            serviceBuilderImportElement.detach();

            rootElement.add(serviceBuilderImportElement);
        }
    }

    return document;
}

From source file:com.liferay.portlet.PortletPreferencesSerializer.java

License:Open Source License

public static String toXML(PortletPreferencesImpl prefs) throws SystemException {

    try {//  w w  w. j av  a2 s .  co  m
        Map preferences = prefs.getPreferences();

        DocumentFactory docFactory = DocumentFactory.getInstance();

        Element portletPreferences = docFactory.createElement("portlet-preferences");

        Iterator itr = preferences.entrySet().iterator();

        while (itr.hasNext()) {
            Map.Entry entry = (Map.Entry) itr.next();

            Preference preference = (Preference) entry.getValue();

            Element prefEl = docFactory.createElement("preference");

            Element nameEl = docFactory.createElement("name");
            nameEl.addText(preference.getName());

            prefEl.add(nameEl);

            String[] values = preference.getValues();

            for (int i = 0; i < values.length; i++) {
                Element valueEl = docFactory.createElement("value");
                valueEl.addText(values[i]);

                prefEl.add(valueEl);
            }

            if (preference.isReadOnly()) {
                Element valueEl = docFactory.createElement("read-only");
                valueEl.addText("true");
            }

            portletPreferences.add(prefEl);
        }

        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        XMLWriter writer = new XMLWriter(baos, OutputFormat.createCompactFormat());

        writer.write(portletPreferences);

        return baos.toString();
    } catch (IOException ioe) {
        throw new SystemException(ioe);
    }
}

From source file:com.liferay.util.xml.XMLMerger.java

License:Open Source License

public Document merge(Document masterDoc, Document slaveDoc) {
    Document mergedDoc = (Document) masterDoc.clone();

    Element root1 = mergedDoc.getRootElement();
    Element root2 = slaveDoc.getRootElement();

    List children1 = root1.elements();
    List children2 = root2.elements();

    for (int i = 0; i < children2.size(); i++) {
        Element el2 = (Element) children2.get(i);

        Element el2Clone = (Element) el2.clone();
        el2Clone.detach();//from   w  ww.j a  va 2s. c o m

        root1.add(el2Clone);
    }

    organizeXML(mergedDoc);

    return mergedDoc;
}

From source file:com.liferay.util.xml.XMLMerger.java

License:Open Source License

private void _orderChildren(Element parent, String[] orderedChildrenNames) {

    if (orderedChildrenNames == null) {
        return;//from   w  w  w .ja v  a2s  .com
    }

    List elements = new ArrayList();

    for (int i = 0; i < orderedChildrenNames.length; i++) {
        elements.addAll(parent.elements(orderedChildrenNames[i]));
    }

    for (int i = 0; i < elements.size(); i++) {
        Element el = (Element) elements.get(i);
        el.detach();
        parent.add(el);
    }
}