Example usage for org.dom4j Node getParent

List of usage examples for org.dom4j Node getParent

Introduction

In this page you can find the example usage for org.dom4j Node getParent.

Prototype

Element getParent();

Source Link

Document

getParent returns the parent Element if this node supports the parent relationship or null if it is the root element or does not support the parent relationship.

Usage

From source file:ch.javasoft.xml.config.XmlUtil.java

License:BSD License

/**
 * Returns an xpath like string for the given xml node
 * /*www .j  a  va2s  .c  om*/
 * @param node            the node to convert to a string
 * @param recurseParents   true if parents should be included
 * 
 * @return an xpath like string for the given node
 */
public static String getNodePath(Node node, boolean recurseParents) {
    if (node instanceof Element) {
        return getElementPath((Element) node, recurseParents);
    }
    final StringBuilder sb = new StringBuilder(node.getName());
    if (recurseParents && node.getParent() != null) {
        sb.insert(0, node instanceof Attribute ? '@' : '/');
        sb.insert(0, getElementPath(node.getParent(), recurseParents));
    }
    return sb.toString();
}

From source file:com.cladonia.xml.helper.Helper.java

License:Open Source License

private void setElementInternal(Node baseNode, ElementInformation ei) {
    try {//from w  w w .  j a  v  a  2 s.co  m
        XElement node = null;

        if (baseNode.getNodeType() == org.dom4j.Node.ATTRIBUTE_NODE) {
            node = (XElement) baseNode.getParent();
        } else if (baseNode.getNodeType() == org.dom4j.Node.ELEMENT_NODE) {
            node = (XElement) baseNode;
        }

        SchemaElement element = null;

        if (ei != null) {

            if (ei instanceof SchemaElement) {
                element = (SchemaElement) ei;
                attributeList.setEnabled(true);
                elementList.setEnabled(true);

                setElementDetails(node.getName(), node.getNamespace().getPrefix(), false);

                setNamespace(node.getNamespace().getURI());

                Vector elementAttributes = new Vector();
                Vector schemaAttributes = new Vector();

                Vector elementChildren = new Vector();
                Vector schemaChildren = new Vector();

                if (element != null) {
                    setText(type, element.getType());

                    Vector modAtts = element.getAttributes();
                    if (modAtts != null) {

                        for (int cnt = 0; cnt < modAtts.size(); ++cnt) {
                            SchemaAttribute sa = (SchemaAttribute) modAtts.get(cnt);
                            schemaAttributes.add(sa);
                        }

                    }

                    Vector modElements = element.getChildElements();
                    if (modElements != null) {

                        for (int cnt = 0; cnt < modElements.size(); ++cnt) {
                            SchemaElement sa = (SchemaElement) modElements.get(cnt);
                            schemaChildren.add(sa);
                        }

                    }

                } else {
                    setType(null);
                }

                for (int cnt = 0; cnt < node.attributeCount(); ++cnt) {

                    elementAttributes.add((XAttribute) node.attribute(cnt));
                }

                XElement[] childElements = node.getElements();
                for (int cnt = 0; cnt < childElements.length; ++cnt) {

                    elementChildren.add((XElement) childElements[cnt]);
                }

                //this.ementList.setElements( element.getElements());
                //this.elementList.setElements( newElementArray);
                //attributeList.setAttributes( element.getAttributes());
                //attributeList.setAttributes( newAttArray);
                attributeList.setAttributes(schemaAttributes, elementAttributes);
                elementList.setElements(schemaChildren, elementChildren);
            } else if (ei instanceof DTDElement) {
                DTDElement dtdElement = (DTDElement) ei;
                attributeList.setEnabled(true);
                elementList.setEnabled(true);

                setElementDetails(node.getName(), node.getNamespace().getPrefix(), false);

                setNamespace(node.getNamespace().getURI());

                Vector elementAttributes = new Vector();
                Vector schemaAttributes = new Vector();

                Vector elementChildren = new Vector();
                Vector schemaChildren = new Vector();

                if (dtdElement != null) {
                    setText(type, dtdElement.getType());

                    Vector modAtts = dtdElement.getAttributes();
                    if (modAtts != null) {

                        for (int cnt = 0; cnt < modAtts.size(); ++cnt) {
                            DTDAttribute sa = (DTDAttribute) modAtts.get(cnt);
                            schemaAttributes.add(sa);
                        }

                    }

                    Vector modElements = dtdElement.getChildElements();
                    if (modElements != null) {

                        for (int cnt = 0; cnt < modElements.size(); ++cnt) {
                            DTDElement sa = (DTDElement) modElements.get(cnt);
                            schemaChildren.add(sa);
                        }

                    }

                } else {
                    setType(null);
                }

                for (int cnt = 0; cnt < node.attributeCount(); ++cnt) {

                    elementAttributes.add((XAttribute) node.attribute(cnt));
                }

                XElement[] childElements = node.getElements();
                for (int cnt = 0; cnt < childElements.length; ++cnt) {

                    elementChildren.add((XElement) childElements[cnt]);
                }

                //this.ementList.setElements( element.getElements());
                //this.elementList.setElements( newElementArray);
                //attributeList.setAttributes( element.getAttributes());
                //attributeList.setAttributes( newAttArray);
                attributeList.setAttributes(schemaAttributes, elementAttributes);
                elementList.setElements(schemaChildren, elementChildren);
            } else if (ei instanceof RNGElement) {
                RNGElement rngElement = (RNGElement) ei;
                attributeList.setEnabled(true);
                elementList.setEnabled(true);

                setElementDetails(node.getName(), node.getNamespace().getPrefix(), false);

                setNamespace(node.getNamespace().getURI());

                Vector elementAttributes = new Vector();
                Vector schemaAttributes = new Vector();

                Vector elementChildren = new Vector();
                Vector schemaChildren = new Vector();

                if (rngElement != null) {
                    setText(type, rngElement.getType());

                    Vector modAtts = rngElement.getAttributes();
                    if (modAtts != null) {

                        for (int cnt = 0; cnt < modAtts.size(); ++cnt) {
                            RNGAttribute sa = (RNGAttribute) modAtts.get(cnt);
                            schemaAttributes.add(sa);
                        }

                    }

                    Vector modElements = rngElement.getChildElements();
                    if (modElements != null) {

                        for (int cnt = 0; cnt < modElements.size(); ++cnt) {
                            RNGElement sa = (RNGElement) modElements.get(cnt);
                            schemaChildren.add(sa);
                        }

                    }

                } else {
                    setType(null);
                }

                for (int cnt = 0; cnt < node.attributeCount(); ++cnt) {

                    elementAttributes.add((XAttribute) node.attribute(cnt));
                }

                XElement[] childElements = node.getElements();
                for (int cnt = 0; cnt < childElements.length; ++cnt) {

                    elementChildren.add((XElement) childElements[cnt]);
                }

                //this.ementList.setElements( element.getElements());
                //this.elementList.setElements( newElementArray);
                //attributeList.setAttributes( element.getAttributes());
                //attributeList.setAttributes( newAttArray);
                attributeList.setAttributes(schemaAttributes, elementAttributes);
                elementList.setElements(schemaChildren, elementChildren);
            } else {

            }
        } else {
            //System.out.println("one is null");
        }

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:com.cladonia.xml.navigator.XmlElementNode.java

License:Open Source License

private void findChildren(XElement parent, Vector nodes, boolean all) {
    if (DEBUG)/* ww  w . ja v a  2s  . c  o m*/
        System.out.println(
                "XmlElementNode.findChildren( " + parent.getName() + ") [" + parent.getTextTrim() + "]");

    if (parent != null) {
        if (this.element != parent && navigator.showAttributes()) {
            XAttribute[] attributes = parent.getAttributes();

            for (int i = 0; i < attributes.length; i++) {
                if (contains(nodes, attributes[i])) {
                    add(new XmlElementNode(navigator, parent, this.nodes, nodes, namespaces, all));
                    return;
                }
            }
        }

        for (int i = 0; i < parent.nodeCount(); i++) {
            Node node = parent.node(i);
            XElement element = null;

            if (node instanceof XElement) {
                element = (XElement) node;
            } else {
                element = (XElement) node.getParent();
            }

            if (this.element != element && element != null && !contains(element)) {
                if (all || contains(nodes, node)) {
                    add(new XmlElementNode(navigator, element, this.nodes, nodes, namespaces, all));

                    if (element == parent) {
                        return;
                    }
                } else if (element != parent) {
                    findChildren(element, nodes, all);
                }
            }
        }
    }
}

From source file:com.cladonia.xml.viewer.XmlElementNode.java

License:Mozilla Public License

/**
 * Constructs the the XML element node./*from   ww  w  . jav  a  2 s .c  o  m*/
 *
 * @param element the XML element.
 */
public XmlElementNode(Viewer viewer, XElement element, boolean end) {
    this.element = element;
    _viewer = viewer;

    isEndTag = end;

    if (!isEndTag()) {
        if (!isMixed2(element) || !_viewer.showInline()) {
            for (int i = 0; i < element.nodeCount(); i++) {
                Node node = element.node(i);

                if (node instanceof XElement) {
                    add(new XmlElementNode(_viewer, (XElement) node));
                } else if ((node instanceof Comment) && _viewer.showComments()) {
                    add(new XmlCommentNode(_viewer, (Comment) node));
                } else if ((node instanceof ProcessingInstruction) && _viewer.showPI()) {
                    add(new XmlProcessingInstructionNode(_viewer, (ProcessingInstruction) node));
                } else if ((node instanceof CDATA) && _viewer.showValues()) {
                    add(new XmlCDATANode(_viewer, (CDATA) node));
                } else if ((node instanceof Text) && _viewer.showValues()) {
                    if (!isWhiteSpace(node)) {
                        if (!hasTextOnly((XElement) node.getParent())) {
                            add(new XmlTextNode(_viewer, (Text) node));
                        }
                    }
                }
            }

            // check to see if we need to add end element
            if (!hasTextOnly(element) && element.hasContent() && !element.getName().equals(XNGR_DUMMY_ROOT)) {
                add(new XmlElementNode(_viewer, element, true));
            }
        }
    }

    format();
}

From source file:com.cladonia.xngreditor.actions.ToolsCapitalizeAction.java

License:Open Source License

private String capitalize(ExchangerDocument document, String xpath, boolean capitalizeElements,
        boolean capitalizeAttributes, boolean capitalizeElementsAndAttributes, boolean traverseChildren) {
    //used for xpath expressions
    try {//from w  w w.  ja va  2 s  .  c  o m
        Vector nodes = document.search(xpath);
        if (nodes.size() < 1) {
            MessageHandler.showError(parent, "Error - Cannot Resolve XPath", "Tools Capitalize Error");
            return (null);
        }
        for (int cnt = 0; cnt < nodes.size(); ++cnt) {
            //for each element
            //Capitalize the attributes
            Node node = (Node) nodes.get(cnt);
            if (node instanceof Element) {
                XElement root = (XElement) nodes.get(cnt);
                if ((capitalizeAttributes) || (capitalizeElementsAndAttributes)) {

                    if (root.attributeCount() > 0) {
                        root.setAttributes(this.capitalizeAttributes(root));
                    }
                }
                //capitalize the element
                if ((capitalizeElements) || (capitalizeElementsAndAttributes)) {
                    if (root.getName() != null) {

                        String name = root.getName();
                        name = capitalizeString(name);
                        Namespace ns = root.getNamespace();

                        root.setQName(new QName(name, ns));
                    }
                }
                if (traverseChildren) {
                    //then capitalize its children           
                    iterateTree(root, capitalizeElements, capitalizeAttributes,
                            capitalizeElementsAndAttributes);
                }
            }

            else if (((capitalizeAttributes) || (capitalizeElementsAndAttributes))
                    && (node instanceof Attribute)) {
                Attribute att = (Attribute) node;
                node.getParent().setAttributes(capitalizeAttributes((XElement) (node.getParent()), att));

            }
        }
        document.update();
    } catch (NullPointerException e) {
        MessageHandler.showError(parent, "Error - Cannot Capitalize,\nElements or Attributes not found",
                "Tools Capitalize Error");
        return (null);
    } catch (Exception e) {
        MessageHandler.showError(parent, "Error - Cannot Capitalize Document", "Tools Capitalize Error");
        return (null);
    }
    return document.getText();
}

From source file:com.cladonia.xngreditor.actions.ToolsDeCapitalizeAction.java

License:Open Source License

/**
 * /*from   w  ww .  j a  v  a 2s .  co m*/
 * Decapitalize a document based on the various flags
 * @param document the document to be decapitalized
 * @param xpath the xpath filter
 * @param deCapitalizeElements flag as to whether to decapitalize elements
 * @param deCapitalizeAttributes flag as to whether to decapitalize attributes
 * @param deCapitalizeElementsAndAttributes flag as to whether to decapitalize elements and attributes
 * @param traverseChildren process sub elements flag
 * @return the decapitalized document as a string
 */
private String deCapitalize(ExchangerDocument document, String xpath, boolean deCapitalizeElements,
        boolean deCapitalizeAttributes, boolean deCapitalizeElementsAndAttributes, boolean traverseChildren) {
    //used for xpath expressions
    try {
        //XElement[] root = document.getElements(xpath);
        Vector nodes = document.search(xpath);
        if (nodes.size() < 1) {
            MessageHandler.showError(parent, "Could Not Resolve XPath:\n" + xpath, "Tools DeCapitalize Error");
            return (null);
        }
        for (int cnt = 0; cnt < nodes.size(); ++cnt) {
            //for each element
            //DeCapitalize the attributes
            Node node = (Node) nodes.get(cnt);
            if (node instanceof Element) {
                XElement root = (XElement) nodes.get(cnt);
                if ((deCapitalizeAttributes) || (deCapitalizeElementsAndAttributes)) {
                    if (root.attributeCount() > 0) {
                        root.setAttributes(this.deCapitalizeAttributes(root));
                    }
                }
                if ((deCapitalizeElements) || (deCapitalizeElementsAndAttributes)) {
                    //DeCapitalize the element
                    if (root.getName() != null) {

                        String name = root.getName();
                        name = deCapitalizeString(name);
                        Namespace ns = root.getNamespace();

                        root.setQName(new QName(name, ns));
                    }
                }
                if (traverseChildren) {
                    //then DeCapitalize its children           
                    iterateTree(root, deCapitalizeElements, deCapitalizeAttributes,
                            deCapitalizeElementsAndAttributes);
                }
            } else if (((deCapitalizeAttributes) || (deCapitalizeElementsAndAttributes))
                    && (node instanceof Attribute)) {
                Attribute att = (Attribute) node;
                node.getParent().setAttributes(deCapitalizeAttributes((XElement) (node.getParent()), att));

            }
        }
        document.update();
    } catch (NullPointerException e) {
        MessageHandler.showError(parent, "Error - Cannot DeCapitalize,\nElements or Attributes not found",
                "Tools DeCapitalize Error");
        return (null);

    } catch (Exception e) {
        MessageHandler.showError(parent, "Error - Cannot DeCapitalize Document", "Tools DeCapitalize Error");
        return (null);
    }
    return document.getText();
}

From source file:com.cladonia.xngreditor.actions.ToolsLowercaseAction.java

License:Open Source License

private String lowercase(ExchangerDocument document, String xpath, boolean lowercaseElements,
        boolean lowercaseAttributes, boolean lowercaseElementsAndAttributes, boolean traverseChildren) {
    //used for xpath expressions
    try {/*ww  w . ja  va2s  . co  m*/
        //XElement[] root = document.getElements(xpath);
        Vector nodes = document.search(xpath);
        if (nodes.size() < 1) {
            MessageHandler.showError(parent, "Could Not Resolve XPath:\n" + xpath, "Tools Lowercase Error");
            return (null);
        }
        for (int cnt = 0; cnt < nodes.size(); ++cnt) {
            //for each element
            //lowercase the attributes
            Node node = (Node) nodes.get(cnt);
            if (node instanceof Element) {
                XElement root = (XElement) nodes.get(cnt);
                if ((lowercaseAttributes) || (lowercaseElementsAndAttributes)) {
                    if (root.attributeCount() > 0) {
                        root.setAttributes(this.lowercaseAttributes(root));
                    }
                }
                if ((lowercaseElements) || (lowercaseElementsAndAttributes)) {
                    //lowercase the element
                    if (root.getName() != null) {

                        String name = root.getName();
                        name = lowercaseString(name);
                        Namespace ns = root.getNamespace();

                        root.setQName(new QName(name, ns));
                    }
                }
                if (traverseChildren) {
                    //then lowercase its children           
                    iterateTree(root, lowercaseElements, lowercaseAttributes, lowercaseElementsAndAttributes);
                }
            } else if (((lowercaseAttributes) || (lowercaseElementsAndAttributes))
                    && (node instanceof Attribute)) {
                Attribute att = (Attribute) node;
                node.getParent().setAttributes(lowercaseAttributes((XElement) (node.getParent()), att));

            }
        }
        document.update();
    } catch (NullPointerException e) {
        MessageHandler.showError(parent, "Error - Cannot Lowercase Document,\nElements or Attributes not found",
                "Tools Lowercase Error");
        return (null);
    } catch (Exception e) {
        MessageHandler.showError(parent, "Error - Cannot Lowercase Document", "Tools Lowercase Error");
        return (null);
    }
    return document.getText();
}

From source file:com.cladonia.xngreditor.actions.ToolsRenameNodeAction.java

License:Open Source License

private String renameByXPath(ExchangerDocument document, String xpathPredicate, String newName)
        throws Exception {

    //List nodeList = document.getDocument().selectNodes( xpathPredicate );
    Vector nodeList = document.search(xpathPredicate);

    try {//from  w w w.j a v  a2s  .c  o m

        for (int cnt = 0; cnt < nodeList.size(); ++cnt) {
            Node node = (Node) nodeList.get(cnt);
            //              node.setName(newName);

            if (node instanceof Element) {

                XElement e = (XElement) node;

                e.setQName(new QName(newName, e.getNamespace()));
            } else if (node instanceof Attribute) {

                XAttribute newAtt = (XAttribute) node;
                /*int fcnt = findInList(newAtt,attributeList);
                if(fcnt > -1) {*/
                //if the attribute matches the one we are looking for
                //replace it with the new qname
                String name = newAtt.getName();
                String oldValue = newAtt.getValue();
                Namespace ns = newAtt.getNamespace();
                XElement parent = (XElement) node.getParent();
                parent.remove(newAtt);
                parent.add(new XAttribute(new QName(newName, ns), oldValue));

                //now remove it from the attributeList to speed up the next searches
                //attributeList.remove(fcnt);

                //}
                //attributeList.add((XAttribute) node);
            }

        }
    } catch (NullPointerException e) {
        MessageHandler.showError(parent, "XPath: " + xpathPredicate + "\nCannot be resolved",
                "Tools Rename Node Error");
        return (null);
    } catch (Exception e) {
        MessageHandler.showError(parent, "Cannot Rename Node", "Tools Rename Node Error");
        return (null);
    }
    document.update();

    return document.getText();
}

From source file:com.cladonia.xngreditor.actions.ToolsSetNodeValueAction.java

License:Open Source License

/**
 * sets the value of the xpath - selected nodes
 * //from ww w .j a va 2 s . c om
 * @param document the exchanger document
 * @param xpathPredicate - the xpath predicate to resolve
 * @param value - the new value for the nodes
 * @return the documents text
 */
private String setNodeValue(ExchangerDocument document, String xpathPredicate, String value) throws Exception {

    /*XPath xpathSelector = DocumentHelper.createXPath(xpathPredicate);
     List nodeList = xpathSelector.selectNodes(document.getDocument());*/
    Vector nodeList = document.search(xpathPredicate);
    Vector attributeList = new Vector();

    String warning = "";
    if (nodeList.size() > 0) {
        try {

            for (int cnt = 0; cnt < nodeList.size(); ++cnt) {

                Node node = (Node) nodeList.get(cnt);

                if (node instanceof Element) {

                    XElement e = (XElement) node;
                    e.setValue(value);
                } else if (node instanceof Attribute) {

                    XAttribute newAtt = (XAttribute) node;

                    //if the attribute matches the one we are looking for
                    //replace it with the new qname
                    String name = newAtt.getName();
                    String oldValue = newAtt.getValue();
                    Namespace ns = newAtt.getNamespace();
                    XElement parent = (XElement) node.getParent();
                    parent.remove(newAtt);
                    parent.add(new XAttribute(new QName(name, ns), value));

                }

            }
            //now change all the attributes
            changeAttribute(document, attributeList, value);

        } catch (NullPointerException e) {
            MessageHandler.showError(parent, "XPath: " + xpathPredicate + "\nCannot be resolved",
                    "Tools Set Node Value Error");
            return (null);
        } catch (Exception e) {
            MessageHandler.showError(parent, "Error setting node value", "Tools Set Node Value Error");
            return (null);
        }

        document.update();
    } else {
        MessageHandler.showError(parent, "No nodes could be found for:\n" + xpathPredicate,
                "Tools Set Node Value Error");
        return (null);
    }

    return (document.getText());
}

From source file:com.cladonia.xngreditor.actions.ToolsSortNodeAction.java

License:Open Source License

private Vector treeWalk(Element element, List nodeList, Vector nodes) throws Exception {

    for (int i = 0, size = element.nodeCount(); i < size; i++) {
        Node node = element.node(i);

        //search to see if this node is in the nodelist
        for (int cnt = 0; cnt < nodeList.size(); ++cnt) {
            if (node == (Node) nodeList.get(cnt)) {

                //make sure it doesn't already exist in the nodes vector
                boolean wasFound = false;
                for (int icnt = 0; icnt < nodes.size(); ++icnt) {
                    if (node == ((NodeInfo) nodes.get(icnt)).getNode()) {
                        wasFound = true;
                    }/*from w ww  .  j  a  v a  2  s . c om*/
                }
                if (!wasFound) {
                    //set this to the next id
                    Element parent = node.getParent();
                    int index = parent.indexOf(node);
                    NodeInfo ni = new NodeInfo(originalCounter, parent, node, index);
                    nodes.add(ni);
                    originalCounter++;
                }
            }
        }

        if (node instanceof Element) {
            treeWalk((Element) node, nodeList, nodes);
        }

    }

    return (nodes);
}