Example usage for org.dom4j Document getRootElement

List of usage examples for org.dom4j Document getRootElement

Introduction

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

Prototype

Element getRootElement();

Source Link

Document

Returns the root Element for this document.

Usage

From source file:com.laudandjolynn.mytv.MyTvData.java

License:Apache License

public void writeData(String parent, String tag, String value) {
    logger.debug("write data to my tv data file: " + Constant.MY_TV_DATA_FILE_PATH);
    File file = new File(Constant.MY_TV_DATA_FILE_PATH);
    if (!file.exists()) {
        Document doc = DocumentHelper.createDocument();
        doc.addElement(Constant.APP_NAME);
        try {/*from  ww  w.j  av  a2s  .c om*/
            FileUtils.writeWithNIO(doc.asXML().getBytes(), Constant.MY_TV_DATA_FILE_PATH);
        } catch (IOException e) {
            throw new MyTvException(
                    "error occur while write data to file. -- " + Constant.MY_TV_DATA_FILE_PATH);
        }
    }
    SAXReader reader = new SAXReader();
    try {
        Document xmlDoc = reader.read(file);
        Element parentElement = xmlDoc.getRootElement();
        if (parent != null) {
            List<?> nodes = xmlDoc.selectNodes("//" + parent);
            if (nodes != null && nodes.size() > 0) {
                parentElement = (Element) nodes.get(0);
            }
        }
        parentElement.addElement(tag).setText(value);
        try {
            XMLWriter writer = new XMLWriter(new FileWriter(file));
            writer.write(xmlDoc);
            writer.close();
        } catch (IOException e) {
            throw new MyTvException(
                    "error occur while write data to file. -- " + Constant.MY_TV_DATA_FILE_PATH);
        }
    } catch (DocumentException e) {
        String msg = "can't parse xml file. -- " + Constant.MY_TV_DATA_FILE_PATH;
        throw new MyTvException(msg);
    }
}

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);// 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 Element getComponentNode(Document doc, String name) {
    List<Element> components = doc.getRootElement().elements(_COMPONENT);

    for (Element component : components) {
        if (component.attributeValue("name").equals(name)) {
            return component;
        }//  w w  w .ja va2  s . c om
    }

    return null;
}

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

License:Open Source License

protected Document getComponentsDocByShortName(String name) {
    for (Document doc : getComponentDefinitionDocs()) {
        Element root = doc.getRootElement();

        if (root.attributeValue("short-name").equals(name)) {
            return doc;
        }/*from  www  .  java2  s  .c  o m*/
    }

    return null;
}

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();/*w  w  w . jav  a 2 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.base.BaseBuilder.java

License:Open Source License

private Document _getExtendedDocument(Document document) {
    String parentXMLPath = document.getRootElement().attributeValue("extends");

    File parentXML = null;//  ww w  . j  a v a2 s.  co  m

    if (StringUtil.isNotBlank(parentXMLPath)) {
        parentXML = new File(parentXMLPath);

        if (!parentXML.exists()) {
            parentXMLPath = PropsUtil.getString(parentXMLPath);
            parentXML = new File(parentXMLPath);
        }

        if (parentXML.exists()) {
            try {
                Document parentDoc = SAXReaderUtil.read(parentXML);

                Element extensionElement = document.getRootElement().element("extension");

                document = mergeXMLAttributes(document, parentDoc);

                if (extensionElement != null) {
                    document.getRootElement().add(extensionElement.createCopy());
                }
            } catch (DocumentException e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("Could not extend from: " + parentXMLPath + ". File does not exist.");
        }
    }

    return document;
}

From source file:com.liferay.alloy.tools.builder.faces.FacesBuilder.java

License:Open Source License

@Override
protected List<Component> getComponents(Document doc) throws Exception {
    Element root = doc.getRootElement();

    Map<String, Component> facesComponentsMap = new HashMap<>();

    String defaultYUIRendererParentClass = root.attributeValue("defaultYUIRendererParentClass");
    String defaultSince = root.attributeValue("defaultSince");
    List<Element> allComponentNodes = root.elements("component");

    for (Element node : allComponentNodes) {
        FacesComponent facesComponent = new FacesComponent();
        facesComponent.initialize(node, _COMPONENTS_PACKAGE, defaultYUIRendererParentClass, defaultSince);
        facesComponentsMap.put(facesComponent.getName(), facesComponent);
    }//from   w  w w .j a  v  a2 s  .c o  m

    List<Component> facesComponents = new ArrayList<>(facesComponentsMap.values());

    for (Component facesComponent : facesComponents) {
        recursivelyAddExtensionAttributesAndEvents(facesComponent, facesComponentsMap);
    }

    return facesComponents;
}

From source file:com.liferay.alloy.tools.builder.faces.FacesBuilder.java

License:Open Source License

private void _buildTaglibXMLFile(List<Component> components, Map<String, Object> context) throws Exception {

    context.put("components", components);
    context.put("version", PropsUtil.getString("builder.faces.version"));

    for (Document doc : getComponentDefinitionDocs()) {
        Element root = doc.getRootElement();

        Element descriptionElement = root.element("description");
        String description = null;

        if (descriptionElement != null) {
            description = descriptionElement.getTextTrim();
        }//from  ww w .j ava2 s .  c  om

        context.put("description", description);

        Element extensionElement = root.element("extension");

        if (extensionElement != null) {
            List<Element> extensionElements = extensionElement.elements();
            context.put("extensionElements", extensionElements);
        }

        String taglibXMLContent = processTemplate("taglib.xml.ftl", context);

        StringBuilder sb = new StringBuilder(4);
        sb.append(_TAGLIB_XML_OUTPUT_DIR);
        sb.append("/");
        sb.append(_NAMESPACE);
        sb.append(".taglib.xml");

        File taglibXMLFile = new File(sb.toString());
        writeFile(taglibXMLFile, taglibXMLContent, true);
    }
}

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

License:Open Source License

@Override
protected List<Component> getComponents(Document doc) throws Exception {
    Element root = doc.getRootElement();

    List<Component> components = new ArrayList<Component>();

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

    for (Element node : allComponentNodes) {
        TagComponent tagComponent = new TagComponent();
        tagComponent.initialize(node, defaultPackage);
        components.add(tagComponent);/*  w  w  w. jav  a  2  s.c  o m*/
    }

    return components;
}

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();
                }//from  w w  w  .  jav a2 s  .  c  om

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

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

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

    return targetDoc;
}