Example usage for org.dom4j Element nodeCount

List of usage examples for org.dom4j Element nodeCount

Introduction

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

Prototype

int nodeCount();

Source Link

Document

Returns the number of Node instances that this branch contains.

Usage

From source file:com.cladonia.xml.XMLFormatter.java

License:Mozilla Public License

/** Outputs the content of the given element. If whitespace trimming is
 * enabled then all adjacent text nodes are appended together before
 * the whitespace trimming occurs to avoid problems with multiple
 * text nodes being created due to text content that spans parser buffers
 * in a SAX parser./* w w  w . j  a  va 2  s  .co  m*/
 */
protected Node writeMixedElementContent(Element element) throws IOException {
    if (DEBUG)
        System.out.println("XMLFormatter.writeMixedElementContent( " + element + ")");
    Node previousNode = null;

    boolean previousTrim = format.isTrimText();
    boolean previousWrapText = wrapText;

    if (isPreserveMixedContent()) {
        format.setTrimText(false);
        wrapText = false;
    } else {
        wrapText = true;
    }

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

        if (i + 1 < size) {
            nextNode = element.node(i + 1);
        }

        if (node instanceof Text) {
            writeString(node.getText());
        } else {
            writeMixedNode(node);
        }

        previousNode = node;
    }

    format.setTrimText(previousTrim);
    wrapText = previousWrapText;

    return previousNode;
}

From source file:com.cladonia.xml.XMLFormatter.java

License:Mozilla Public License

protected Node writePreservedElementContent(Element element) throws IOException {
    if (DEBUG)/*www  .jav a 2 s. c  o m*/
        System.out.println("XMLFormatter.writePreservedElementContent( " + element + ")");
    Node previousNode = null;

    boolean previousTrim = format.isTrimText();
    boolean previousWrapText = wrapText;
    format.setTrimText(false);
    wrapText = false;

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

        if (i + 1 < size) {
            nextNode = element.node(i + 1);
        }

        if (node instanceof Text) {
            String text = node.getText();

            // previous node was an empty element-node <element/>
            writeString(text);
        } else {
            writePreservedNode(node);
        }

        previousNode = node;
    }

    format.setTrimText(previousTrim);
    wrapText = previousWrapText;

    return previousNode;
}

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

License:Open Source License

/**
 * Walk through the document tree and change the attributes if they are the selected ones
 * //from   w  w w .  j a v  a2  s .  c  om
 * @param document the exchanger document
 * @param attributeList the list of attributes that match the xpath result
 * @param newNs the new namespace for the nodes
 * @throws Exception
 */
public void treeWalk(Element element, List attributeList, Namespace newNs) throws Exception {

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

        if (node instanceof Element) {
            //if it's an element
            XElement e = (XElement) node;
            if (e.attributeCount() > 0) {
                //and it has attributes

                List attList = e.attributes();

                for (int cnt = 0; cnt < attList.size(); ++cnt) {
                    //for each attribute
                    XAttribute newAtt = (XAttribute) attList.get(cnt);
                    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 value = newAtt.getValue();
                        Namespace ns = newNs;

                        attList.set(cnt, new XAttribute(new QName(name, ns), value));

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

                    }
                }
            }
            treeWalk((Element) node, attributeList, newNs);
        }
    }

}

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

License:Open Source License

private void iterateTree(Element element, boolean capitalizeElements, boolean capitalizeAttributes,
        boolean capitalizeElementsAndAttributes) throws Exception {

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

            XElement oldElement = (XElement) oldNode;

            if (((capitalizeAttributes) || (capitalizeElementsAndAttributes))
                    && (oldElement.attributeCount() > 0)) {
                oldElement.setAttributes(this.capitalizeAttributes(oldElement));

            }/* www  . j  a va 2 s.c o  m*/

            //capitalize the element
            if (((capitalizeElements) || (capitalizeElementsAndAttributes)) && (oldElement.getName() != null)) {

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

                oldElement.setQName(new QName(name, ns));

            }
            iterateTree(oldElement, capitalizeElements, capitalizeAttributes, capitalizeElementsAndAttributes);

        }
    }
}

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

License:Open Source License

/**
 * Iterate through the tree to decapitalize elements and attributes based on the flags
 * @param element the element the method is working on
  * @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
 * @throws Exception// w  w w .  j a  v  a  2  s  .c  om
 */
private void iterateTree(Element element, boolean deCapitalizeElements, boolean deCapitalizeAttributes,
        boolean deCapitalizeElementsAndAttributes) throws Exception {

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

            XElement oldElement = (XElement) oldNode;

            if ((deCapitalizeAttributes) || (deCapitalizeElementsAndAttributes)) {

                oldElement.setAttributes(this.deCapitalizeAttributes(oldElement));

            }

            //DeCapitalize the element
            if (((deCapitalizeElements) || (deCapitalizeElementsAndAttributes))
                    && (oldElement.getName() != null)) {

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

                oldElement.setQName(new QName(name, ns));

            }
            iterateTree(oldElement, deCapitalizeElements, deCapitalizeAttributes,
                    deCapitalizeElementsAndAttributes);

        }
    }
}

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

License:Open Source License

private void iterateTree(Element element, boolean lowercaseElements, boolean lowercaseAttributes,
        boolean lowercaseElementsAndAttributes) throws Exception {

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

            XElement oldElement = (XElement) oldNode;

            if ((lowercaseAttributes) || (lowercaseElementsAndAttributes)) {

                oldElement.setAttributes(this.lowercaseAttributes(oldElement));

            }//  w  w  w .  j av a 2  s.  co  m

            //Lowercase the element
            if (((lowercaseElements) || (lowercaseElementsAndAttributes)) && (oldElement.getName() != null)) {

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

                oldElement.setQName(new QName(name, ns));

            }
            iterateTree(oldElement, lowercaseElements, lowercaseAttributes, lowercaseElementsAndAttributes);

        }
    }
}

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

License:Open Source License

private Vector iterateTree(XElement root, Element element, Vector allNamespaces) throws Exception {

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

            XElement oldElement = (XElement) oldNode;
            this.removeNamespacesFromElement(oldElement);

            iterateTree(root, oldElement, allNamespaces);

        }//from w  w  w  .j av a  2  s .  co  m
    }
    //return the vector of namespaces
    return (allNamespaces);
}

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

License:Open Source License

/**
 * Walk through the document tree and change the attributes if they are the selected ones
 * //from www  . j a  v  a  2 s. c  o m
 * @param document the exchanger document
 * @param attributeList the list of attributes that match the xpath result
 * @param value the new value for the nodes
 * @throws Exception
 */
private void treeWalk(Element element, List attributeList, String value) throws Exception {

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

        if (node instanceof Element) {
            //if it's an element
            XElement e = (XElement) node;
            if (e.attributeCount() > 0) {
                //and it has attributes

                List attList = e.attributes();

                for (int cnt = 0; cnt < attList.size(); ++cnt) {
                    //for each attribute
                    XAttribute newAtt = (XAttribute) attList.get(cnt);
                    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();

                        attList.set(cnt, new XAttribute(new QName(name, ns), value));

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

                    }
                }
            }
            treeWalk((Element) node, attributeList, value);
        }
    }

}

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  .jav a2s .  co m*/
                }
                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);
}

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

License:Open Source License

/**
 * Iterate through the tree and uppercase each node depending on the flags
 * @param element the element node/*  w  w  w  .  j  a va 2  s .c om*/
 * @param uppercaseElements uppercase elements flag
 * @param uppercaseAttributes uppercase attributes flag
 * @param uppercaseElementsAndAttributes uppercase elements and attributes flag
 * @throws Exception
 */
private void iterateTree(Element element, boolean uppercaseElements, boolean uppercaseAttributes,
        boolean uppercaseElementsAndAttributes) throws Exception {

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

            XElement oldElement = (XElement) oldNode;

            if ((uppercaseAttributes) || (uppercaseElementsAndAttributes)) {

                oldElement.setAttributes(this.uppercaseAttributes(oldElement));

            }

            //Uppercase the element
            if (((uppercaseElements) || (uppercaseElementsAndAttributes)) && (oldElement.getName() != null)) {

                String name = oldElement.getName();
                name = uppercaseString(name);
                Namespace ns = oldElement.getNamespace();

                oldElement.setQName(new QName(name, ns));

            }
            iterateTree(oldElement, uppercaseElements, uppercaseAttributes, uppercaseElementsAndAttributes);

        }
    }
}