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.apache.taglibs.xtags.xpath.AddTag.java

License:Apache License

public int doEndTag() throws JspException {
    Object element = TagHelper.getInputNodes(pageContext, this, false);
    if (element == null) {
        throw new JspException("No current node to add content to");
    }/*from  w  w w  .ja  v  a  2 s  . c  om*/
    if (!(element instanceof Element)) {
        throw new JspException("Current node is not an Element");
    }
    if (bodyContent != null) {
        try {
            StringReader sreader = new StringReader("<dummy>" + bodyContent.getString() + "</dummy>");
            SAXReader reader = new SAXReader();
            Document doc = reader.read(sreader);
            Element root = doc.getRootElement();
            List nodes = root.content();
            while (!nodes.isEmpty()) {
                Node node = (Node) nodes.remove(0);
                node.detach();
                ((Element) element).add(node);
            }
        } catch (DocumentException e) {
            handleException(e);
        }
    }

    return EVAL_PAGE;
}

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 .  j a va 2 s. com*/

    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.codehaus.cargo.container.weblogic.WebLogic9x10x103x12xConfigXmlInstalledLocalDeployer.java

License:Apache License

/**
 * Per current schema of the weblogic domain, app-deployment elements need to come directly
 * after the configuration-version element.
 * //  w w w.java 2 s . co m
 * @param domain - domain to re-order
 */
protected void reorderAppDeploymentsAfterConfigurationVersion(Element domain) {
    Element configurationVersion = xmlTool.selectElementMatchingXPath("weblogic:configuration-version", domain);

    List<Element> appDeployments = xmlTool.selectElementsMatchingXPath("weblogic:app-deployment", domain);

    List<Element> domainElements = domain.content();
    int indexOfConfigurationVersion = domainElements.indexOf(configurationVersion);

    domainElements.removeAll(appDeployments);
    domainElements.addAll(indexOfConfigurationVersion + 1, appDeployments);

}

From source file:org.codehaus.cargo.container.weblogic.WebLogic9xConfigXmlInstalledLocalDeployer.java

License:Apache License

/**
 * Per current schema of the weblogic domain, app-deployment elements need to come directly
 * after the configuration-version element.
 * //from   w  w w .  java 2 s.co m
 * @param domain - domain to re-order
 */
protected void reorderAppDeploymentsAfterConfigurationVersion(Element domain) {
    Element configurationVersion = xmlTool.selectElementMatchingXPath("weblogic:configuration-version", domain);

    List appDeployments = xmlTool.selectElementsMatchingXPath("weblogic:app-deployment", domain);

    List domainElements = domain.content();
    int indexOfConfigurationVersion = domainElements.indexOf(configurationVersion);

    domainElements.removeAll(appDeployments);
    domainElements.addAll(indexOfConfigurationVersion + 1, appDeployments);

}

From source file:org.collectionspace.chain.util.xtmpl.XTmplDocument.java

License:Educational Community License

@SuppressWarnings("unchecked")
public void setTexts(String key, String[] texts) {
    Node basis = document.selectSingleNode(attach.get(key));
    Element parent = basis.getParent();
    int pos = parent.indexOf(basis);
    List<Node> nodes = (List<Node>) parent.content();
    List<Node> after = new ArrayList(nodes.subList(pos + 1, nodes.size()));
    basis.detach();//from   w w w .  j  a  va 2s  .  c  om
    if (texts.length > 0) {
        for (Node n : after)
            n.detach();
        for (String text : texts) {
            Node renewed = (Node) basis.clone();
            renewed.setText(text);
            parent.add(renewed);
        }
        for (Node n : after)
            parent.add(n);
    }
}

From source file:org.dcm4chex.archive.hl7.HL7SendService.java

License:LGPL

/** Parse the PID entries into a list of maps */
public static final List<Map<String, String>> parsePDQ(Document msg) {
    List<Map<String, String>> ret = new ArrayList<Map<String, String>>();
    Element root = msg.getRootElement();
    List<?> content = root.content();
    for (Object c : content) {
        if (!(c instanceof Element))
            continue;
        Element e = (Element) c;
        if (e.getName().equals("PID")) {
            Map<String, String> pid = new HashMap<String, String>();
            pid.put("Type", "Patient");
            List<?> fields = e.elements(HL7XMLLiterate.TAG_FIELD);

            int pidNo = 0;
            for (Object f : fields) {
                pidNo++;/*from w  ww . ja v a  2s .com*/
                if (pidNo >= FIELD_NAMES.length)
                    continue;
                String fieldName = FIELD_NAMES[pidNo];
                if (fieldName == null)
                    continue;
                Element field = (Element) f;
                if (field.isTextOnly()) {
                    String txt = field.getText();
                    if (txt == null || txt.length() == 0)
                        continue;
                    pid.put(fieldName, txt);
                    continue;
                }
                if (pidNo == 3) {
                    List<?> comps = field.elements(HL7XMLLiterate.TAG_COMPONENT);
                    if (comps.size() < 3) {
                        throw new IllegalArgumentException("Missing Authority in PID-3");
                    }
                    Element authority = (Element) comps.get(2);
                    List<?> authorityUID = authority.elements(HL7XMLLiterate.TAG_SUBCOMPONENT);
                    pid.put("PatientID", field.getText());
                    StringBuffer issuer = new StringBuffer(authority.getText());
                    for (int i = 0; i < authorityUID.size(); i++) {
                        issuer.append("&").append(((Element) authorityUID.get(i)).getText());
                    }
                    pid.put("IssuerOfPatientID", issuer.toString());
                    continue;
                }
                if (pidNo == 5) {
                    String name = field.getText() + "^" + field.elementText(HL7XMLLiterate.TAG_COMPONENT);
                    pid.put(fieldName, name);
                    continue;
                }
                pid.put(fieldName, field.asXML());
            }
            ret.add(pid);
        }
    }
    return ret;
}

From source file:org.dom4j.samples.LargeDocumentDemo.java

License:Open Source License

public void onEnd(ElementPath path) {
    Element element = path.getCurrent();

    println("onEnd: of parsing element: " + element + " with: " + element.content().size()
            + " content node(s)");

    // now prune the current element to reduce memory
    element.detach();/* w w w .  j av  a  2s.  c o m*/
}

From source file:org.etudes.component.app.melete.MeleteImportServiceImpl.java

License:Apache License

private void removeNamespaces(Element elem) {
    elem.setQName(QName.get(elem.getName(), Namespace.NO_NAMESPACE, elem.getQualifiedName()));
    Node n = null;/*from  ww  w.  j  av a2  s .c o m*/
    for (int i = 0; i < elem.content().size(); i++) {
        n = (Node) elem.content().get(i);
        if (n.getNodeType() == Node.ATTRIBUTE_NODE)
            ((Attribute) n).setNamespace(Namespace.NO_NAMESPACE);
        if (n.getNodeType() == Node.ELEMENT_NODE)
            removeNamespaces((Element) n);
    }
}

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

License:Open Source License

private int getRelativePositionOfPI() {
    String target = ((ProcessingInstruction) node).getTarget();
    Element parent = (Element) ((ProcessingInstruction) node).getParent();
    if (parent == null) {
        return 1;
    }//  ww w  . j  a  v a2s. co m
    List children = parent.content();
    int count = 0;
    for (int i = 0; i < children.size(); i++) {
        Object child = children.get(i);
        if (child instanceof ProcessingInstruction
                && (target == null || target.equals(((ProcessingInstruction) child).getTarget()))) {
            count++;
        }
        if (child == node) {
            break;
        }
    }
    return count;
}

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

License:Open Source License

private int getRelativePositionOfTextNode() {
    Element parent;
    if (node instanceof Text) {
        parent = (Element) ((Text) node).getParent();
    } else {/*from   w  w  w .ja  v a2  s. c  om*/
        parent = (Element) ((CDATA) node).getParent();
    }
    if (parent == null) {
        return 1;
    }
    List children = parent.content();
    int count = 0;
    for (int i = 0; i < children.size(); i++) {
        Object child = children.get(i);
        if (child instanceof Text || child instanceof CDATA) {
            count++;
        }
        if (child == node) {
            break;
        }
    }
    return count;
}