Example usage for org.dom4j Element content

List of usage examples for org.dom4j Element content

Introduction

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

Prototype

List<Node> content();

Source Link

Document

Returns the content nodes of this branch as a backed List so that the content of this branch may be modified directly using the List interface.

Usage

From source file:org.jetbrains.kotlin.projectsextensions.maven.buildextender.PomXmlModifier.java

License:Apache License

private Element createPluginsElement(Element root) {
    QName qname = new QName("plugins", root.getQName().getNamespace());
    DefaultElement pluginsElement = new DefaultElement(qname);

    root.content().add(pluginsElement);

    return root.element("plugins");
}

From source file:org.jetbrains.kotlin.projectsextensions.maven.buildextender.PomXmlModifier.java

License:Apache License

private void createStdlibDependency(Element dependencies) {
    QName dependencyQname = new QName("dependency", dependencies.getQName().getNamespace());
    DefaultElement dependency = new DefaultElement(dependencyQname);

    QName groupIdQname = new QName("groupId", dependencies.getQName().getNamespace());
    DefaultElement groupId = new DefaultElement(groupIdQname);
    groupId.addText(groupIdName);//ww  w . jav a  2  s . co m

    QName artifactIdQname = new QName("artifactId", dependencies.getQName().getNamespace());
    DefaultElement artifactId = new DefaultElement(artifactIdQname);
    artifactId.addText("kotlin-stdlib");

    QName versionQname = new QName("version", dependencies.getQName().getNamespace());
    DefaultElement version = new DefaultElement(versionQname);
    version.addText(kotlinVersion);

    dependency.add(groupId);
    dependency.add(artifactId);
    dependency.add(version);

    dependencies.content().add(dependency);
}

From source file:org.jvnet.maven.jellydoc.ReferenceRenderer.java

License:Apache License

private String docXml(Element parent) {
    Element doc = parent.element("doc");
    // remove all javadoc tags that don't belong.
    doc.content().removeAll(doc.elements("authortag"));
    String xml = doc.getText();// w w  w.  j a va 2  s  . com

    StringWriter w = new StringWriter();
    MarkupParser parser = new MarkupParser(new ConfluenceDialect());
    HtmlDocumentBuilder builder = new HtmlDocumentBuilder(w) {
        @Override
        public void lineBreak() {
            // no line break since IDEs usually don't wrap text.
        }
    };

    builder.setEmitAsDocument(false);
    parser.setBuilder(builder);
    parser.parse(xml);
    return w.toString();
}

From source file:org.olat.ims.qti.qpool.QTIExportProcessor.java

License:Apache License

/**
 * Collect the file and rewrite the /*  w  w  w .j  ava  2 s  .  c  o m*/
 * @param el
 * @param container
 * @param materials
 * @param paths
 */
private void collectResourcesInMatText(Element el, VFSContainer container, ItemsAndMaterials materials) {
    //mattext
    @SuppressWarnings("unchecked")
    List<Element> mattextList = el.selectNodes(".//mattext");
    for (Element mat : mattextList) {
        Attribute texttypeAttr = mat.attribute("texttype");
        String texttype = texttypeAttr.getValue();
        if ("text/html".equals(texttype)) {
            @SuppressWarnings("unchecked")
            List<Node> childElList = new ArrayList<Node>(mat.content());
            for (Node childEl : childElList) {
                mat.remove(childEl);
            }

            for (Node childEl : childElList) {
                if (Node.CDATA_SECTION_NODE == childEl.getNodeType()) {
                    CDATA data = (CDATA) childEl;
                    boolean changed = false;
                    String text = data.getText();
                    List<String> materialPaths = findMaterialInMatText(text);
                    for (String materialPath : materialPaths) {
                        VFSItem matVfsItem = container.resolve(materialPath);
                        if (matVfsItem instanceof VFSLeaf) {
                            String exportUri = generateExportPath(materials.getPaths(), matVfsItem);
                            materials.addMaterial(new ItemMaterial((VFSLeaf) matVfsItem, exportUri));
                            text = text.replaceAll(materialPath, exportUri);
                            changed = true;
                        }
                    }
                    if (changed) {
                        mat.addCDATA(text);
                    } else {
                        mat.add(childEl);
                    }
                } else {
                    mat.add(childEl);
                }
            }
        }
    }
}

From source file:org.onosproject.yms.app.ych.defaultcodecs.xml.XmlCodecListener.java

License:Apache License

@Override
public void enterXmlElement(Element element, XmlNodeType nodeType, Element rootElement) {
    if (element.equals(rootElement)) {
        return;//from   www .  ja  va2s. co m
    }

    YdtContextOperationType opType = null;

    for (Iterator iter = element.attributeIterator(); iter.hasNext();) {
        Attribute attr = (Attribute) iter.next();
        if (attr.getName().equals(OPERATION)) {
            opType = YdtContextOperationType.valueOf(attr.getValue().toUpperCase());
        }
    }

    String nameSpace = null;
    if (element.getNamespace() != null) {
        nameSpace = element.getNamespace().getURI();
    }

    /*
     * When new module has to be added, and if curnode has reference of
     * previous module, then we need to traverse back to parent(logical root
     * node).
     */
    if (ydtExtBuilder.getRootNode() == ydtExtBuilder.getCurNode().getParent() && prevNodeNamespace != null
            && !prevNodeNamespace.equals(nameSpace)) {
        ydtExtBuilder.traverseToParent();
    }

    if (nodeType == OBJECT_NODE && element.content() == null || element.content().isEmpty()) {
        nodeType = TEXT_NODE;
    }
    if (nodeType == OBJECT_NODE) {
        if (ydtExtBuilder != null) {
            if (ydtExtBuilder.getCurNode() == ydtExtBuilder.getRootNode()) {
                ydtExtBuilder.addChild(null, nameSpace, opType);
            }
            ydtExtBuilder.addChild(element.getName(), nameSpace, opType);
        }
    } else if (nodeType == TEXT_NODE) {

        if (ydtExtBuilder != null) {
            if (ydtExtBuilder.getCurNode() == ydtExtBuilder.getRootNode()) {
                ydtExtBuilder.addChild(null, nameSpace, opType);
            }
            ydtExtBuilder.addLeaf(element.getName(), nameSpace, element.getText());
        }
    }

    if (nameSpace != null) {
        prevNodeNamespace = nameSpace;
    }
}

From source file:org.opencms.setup.xml.CmsSetupXmlHelper.java

License:Open Source License

/**
 * Sets the given value in all nodes identified by the given xpath of the given xml file.<p>
 * /*from   w  ww  .  j a va2 s  .  c  o m*/
 * If value is <code>null</code>, all nodes identified by the given xpath will be deleted.<p>
 * 
 * If the node identified by the given xpath does not exists, the missing nodes will be created
 * (if <code>value</code> not <code>null</code>).<p>
 * 
 * @param document the xml document
 * @param xPath the xpath to set
 * @param value the value to set (can be <code>null</code> for deletion)
 * @param nodeToInsert optional, if given it will be inserted after xPath with the given value
 * 
 * @return the number of successful changed or deleted nodes
 */
public static int setValue(Document document, String xPath, String value, String nodeToInsert) {

    int changes = 0;
    // be naive and try to find the node
    Iterator<Node> itNodes = CmsCollectionsGenericWrapper.<Node>list(document.selectNodes(xPath)).iterator();

    // if not found
    if (!itNodes.hasNext()) {
        if (value == null) {
            // if no node found for deletion
            return 0;
        }
        // find the node creating missing nodes in the way
        Iterator<String> it = CmsStringUtil.splitAsList(xPath, "/", false).iterator();
        Node currentNode = document;
        while (it.hasNext()) {
            String nodeName = it.next();
            // if a string condition contains '/'
            while ((nodeName.indexOf("='") > 0) && (nodeName.indexOf("']") < 0)) {
                nodeName += "/" + it.next();
            }
            Node node = currentNode.selectSingleNode(nodeName);
            if (node != null) {
                // node found
                currentNode = node;
                if (!it.hasNext()) {
                    currentNode.setText(value);
                }
            } else if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
                Element elem = (Element) currentNode;
                if (!nodeName.startsWith("@")) {
                    elem = handleNode(elem, nodeName);
                    if (!it.hasNext() && CmsStringUtil.isNotEmptyOrWhitespaceOnly(value)) {
                        elem.setText(value);
                    }
                } else {
                    // if node is attribute create it with given value
                    elem.addAttribute(nodeName.substring(1), value);
                }
                currentNode = elem;
            } else {
                // should never happen
                if (LOG.isDebugEnabled()) {
                    LOG.debug(Messages.get().getBundle().key(Messages.ERR_XML_SET_VALUE_2, xPath, value));
                }
                break;
            }
        }
        if (nodeToInsert == null) {
            // if not inserting we are done
            return 1;
        }
        // if inserting, we just created the insertion point, so continue
        itNodes = CmsCollectionsGenericWrapper.<Node>list(document.selectNodes(xPath)).iterator();
    }

    // if found 
    while (itNodes.hasNext()) {
        Node node = itNodes.next();
        if (nodeToInsert == null) {
            // if not inserting
            if (value != null) {
                // if found, change the value
                node.setText(value);
            } else {
                // if node for deletion is found
                node.getParent().remove(node);
            }
        } else {
            // first create the node to insert
            Element parent = node.getParent();
            Element elem = handleNode(parent, nodeToInsert);
            if (value != null) {
                elem.setText(value);
            }
            // get the parent element list
            List<Node> list = CmsCollectionsGenericWrapper.<Node>list(parent.content());
            // remove the just created element
            list.remove(list.size() - 1);
            // insert it back to the right position
            int pos = list.indexOf(node);
            list.add(pos + 1, elem); // insert after
        }
        changes++;
    }
    return changes;
}

From source file:org.opencms.util.ant.CmsSetupXmlHelper.java

License:Open Source License

/**
 * Sets the given value in all nodes identified by the given xpath of the given xml file.<p>
 * //  w ww  . j  ava  2 s. c  o m
 * If value is <code>null</code>, all nodes identified by the given xpath will be deleted.<p>
 * 
 * If the node identified by the given xpath does not exists, the missing nodes will be created
 * (if <code>value</code> not <code>null</code>).<p>
 * 
 * @param document the xml document
 * @param xPath the xpath to set
 * @param value the value to set (can be <code>null</code> for deletion)
 * @param nodeToInsert optional, if given it will be inserted after xPath with the given value
 * 
 * @return the number of successful changed or deleted nodes
 */
@SuppressWarnings("unchecked")
public static int setValue(Document document, String xPath, String value, String nodeToInsert) {

    int changes = 0;
    // be naive and try to find the node
    Iterator<Node> itNodes = document.selectNodes(xPath).iterator();

    // if not found
    if (!itNodes.hasNext()) {
        if (value == null) {
            // if no node found for deletion
            return 0;
        }
        // find the node creating missing nodes in the way
        Iterator<String> it = CmsStringUtil.splitAsList(xPath, "/", false).iterator();
        Node currentNode = document;
        while (it.hasNext()) {
            String nodeName = it.next();
            // if a string condition contains '/'
            while ((nodeName.indexOf("='") > 0) && (nodeName.indexOf("']") < 0)) {
                nodeName += "/" + it.next();
            }
            Node node = currentNode.selectSingleNode(nodeName);
            if (node != null) {
                // node found
                currentNode = node;
                if (!it.hasNext()) {
                    currentNode.setText(value);
                }
            } else if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
                Element elem = (Element) currentNode;
                if (!nodeName.startsWith("@")) {
                    elem = handleNode(elem, nodeName);
                    if (!it.hasNext() && !CmsStringUtil.isEmptyOrWhitespaceOnly(value)) {
                        elem.setText(value);
                    }
                } else {
                    // if node is attribute create it with given value
                    elem.addAttribute(nodeName.substring(1), value);
                }
                currentNode = elem;
            } else {
                // should never happen
                break;
            }
        }
        if (nodeToInsert == null) {
            // if not inserting we are done
            return 1;
        }
        // if inserting, we just created the insertion point, so continue
        itNodes = document.selectNodes(xPath).iterator();
    }

    // if found 
    while (itNodes.hasNext()) {
        Node node = itNodes.next();
        if (nodeToInsert == null) {
            // if not inserting
            if (value != null) {
                // if found, change the value
                node.setText(value);
            } else {
                // if node for deletion is found
                node.getParent().remove(node);
            }
        } else {
            // first create the node to insert
            Element parent = node.getParent();
            Element elem = handleNode(parent, nodeToInsert);
            if (value != null) {
                elem.setText(value);
            }
            // get the parent element list
            List<Node> list = parent.content();
            // remove the just created element
            list.remove(list.size() - 1);
            // insert it back to the right position
            int pos = list.indexOf(node);
            list.add(pos + 1, elem); // insert after
        }
        changes++;
    }
    return changes;
}

From source file:org.opencms.xml.CmsXmlGenericWrapper.java

License:Open Source License

/**
 * Provides a type safe / generic wrapper for {@link Element#content()}.<p>
 * /*  ww w  .  j a va 2s  . c om*/
 * @param element the element to get the content for
 * 
 * @return type safe access to {@link Element#content()}.<p>
 */
@SuppressWarnings("unchecked")
public static List<Node> content(Element element) {

    return element.content();
}

From source file:org.openkoala.koala.deploy.curd.module.util.CURDConfigUpdate.java

License:Open Source License

private static void jpaDBUpdate(List<String> entitys, String config) {
    Document jpaDocument = DocumentUtil.readDocument(config);
    String parent = "/xmlns:persistence/xmlns:persistence-unit";
    Element parentElement = XPathQueryUtil.querySingle(JPA_XMLS, parent, jpaDocument);
    for (String entity : entitys) {
        String entityQueryString = "/xmlns:persistence/xmlns:persistence-unit[xmlns:class='" + entity + "']";
        Element entityElement = XPathQueryUtil.querySingle(JPA_XMLS, entityQueryString, jpaDocument);

        if (entityElement == null) {
            entityElement = parentElement.addElement("class", JPA_XMLS);
            entityElement.setText(entity);
            Element add = entityElement;
            parentElement.remove(entityElement);
            parentElement.content().add(0, add);
        }//from   ww w .j  a v  a2 s .  c o m
    }
    DocumentUtil.document2Xml(config, jpaDocument);
}

From source file:org.orbeon.oxf.processor.tamino.dom4j.TDOM4JInputStreamInterpreter.java

License:Open Source License

private void setResponseQueryContent(Document document, String collection, String doctype) {
    // Get all the Elements returned from Tamino
    Element re = document.getRootElement();
    // Get the xql/xq:result element as the container for the querried XML instances.
    QName qname = new QName(TXQNamespace.RESULT.getName(), xqNamespace);
    Element qc = re.element(qname);
    if (qc == null) {
        qname = new QName(TXQLNamespace.RESULT.getName(), xqlNamespace);
        qc = re.element(qname);//w ww . j  a va 2  s .  co  m
    }

    if (qc != null) {
        // Get the list of all result DOM4J element instances referring to the XML instances. Currently getMixedContent is used, will be changed later.
        //List list  = qc.elements();
        List list = qc.content();
        if (list.size() != 0) {
            // Obtain the response query content.
            TResponseQueryContent responseQueryContent = getResponseQueryContent();
            Iterator iter = list.iterator();
            while (iter.hasNext()) {
                Object item = iter.next();
                // Well, if the item happens to be a string then there is either mixed content or only text content.
                if (item instanceof String) {
                    responseQueryContent.setText(item.toString());
                } else if (item instanceof DefaultText) {
                    responseQueryContent.setText(((DefaultText) item).getText());
                } else if (item instanceof Element) {
                    Element element = (Element) item;
                    // Create the appropiate Tamino XML object with the given ino:id, collection and schema information.
                    TXMLObject xmlObject = TXMLObject.newInstance(element);
                    xmlObject.setCollection(collection);
                    xmlObject.setDoctype(doctype);
                    // Add the obtained TXMLObject to the result set.
                    responseQueryContent.add(xmlObject);
                }
            }
        }
    }
}