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:architecture.common.xml.XmlWriter.java

License:Apache License

protected void writeElement(Element element) throws IOException {
    int size = element.nodeCount();
    String qualifiedName = element.getQualifiedName();

    writePrintln();//from  w  ww.java2s  .c  o m
    indent();

    writer.write("<");
    writer.write(qualifiedName);

    int previouslyDeclaredNamespaces = namespaceStack.size();
    Namespace ns = element.getNamespace();
    if (isNamespaceDeclaration(ns)) {
        namespaceStack.push(ns);
        writeNamespace(ns);
    }

    // Print out additional namespace declarations
    boolean textOnly = true;
    for (int i = 0; i < size; i++) {
        Node node = element.node(i);
        if (node instanceof Namespace) {
            Namespace additional = (Namespace) node;
            if (isNamespaceDeclaration(additional)) {
                namespaceStack.push(additional);
                writeNamespace(additional);
            }
        } else if (node instanceof Element) {
            textOnly = false;
        } else if (node instanceof Comment) {
            textOnly = false;
        }
    }

    writeAttributes(element);

    lastOutputNodeType = Node.ELEMENT_NODE;

    if (size <= 0) {
        writeEmptyElementClose(qualifiedName);
    } else {
        writer.write(">");
        if (textOnly) {
            // we have at least one text node so lets assume
            // that its non-empty
            writeElementContent(element);
        } else {
            // we know it's not null or empty from above
            ++indentLevel;

            writeElementContent(element);

            --indentLevel;

            writePrintln();
            indent();
        }
        writer.write("</");
        writer.write(qualifiedName);
        writer.write(">");
    }

    // remove declared namespaceStack from stack
    while (namespaceStack.size() > previouslyDeclaredNamespaces) {
        namespaceStack.pop();
    }

    lastOutputNodeType = Node.ELEMENT_NODE;
}

From source file:architecture.common.xml.XmlWriter.java

License:Apache 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.//from w ww  .  j av a2s. c o  m
 */
protected void writeElementContent(Element element) throws IOException {
    boolean trim = format.isTrimText();
    boolean oldPreserve = preserve;
    if (trim) { // verify we have to before more expensive test
        preserve = isElementSpacePreserved(element);
        trim = !preserve;
    }
    if (trim) {
        // concatenate adjacent text nodes together
        // so that whitespace trimming works properly
        Text lastTextNode = null;
        StringBuilder buffer = null;
        boolean textOnly = true;
        for (int i = 0, size = element.nodeCount(); i < size; i++) {
            Node node = element.node(i);
            if (node instanceof Text) {
                if (lastTextNode == null) {
                    lastTextNode = (Text) node;
                } else {
                    if (buffer == null) {
                        buffer = new StringBuilder(lastTextNode.getText());
                    }
                    buffer.append(((Text) node).getText());
                }
            } else {
                if (!textOnly && format.isPadText()) {
                    writer.write(PAD_TEXT);
                }

                textOnly = false;

                if (lastTextNode != null) {
                    if (buffer != null) {
                        writeString(buffer.toString());
                        buffer = null;
                    } else {
                        writeString(lastTextNode.getText());
                    }
                    lastTextNode = null;

                    if (format.isPadText()) {
                        writer.write(PAD_TEXT);
                    }
                }
                writeNode(node);
            }
        }
        if (lastTextNode != null) {
            if (!textOnly && format.isPadText()) {
                writer.write(PAD_TEXT);
            }
            if (buffer != null) {
                writeString(buffer.toString());
                buffer = null;
            } else {
                writeString(lastTextNode.getText());
            }
            lastTextNode = null;
        }
    } else {
        Node lastTextNode = null;
        for (int i = 0, size = element.nodeCount(); i < size; i++) {
            Node node = element.node(i);
            if (node instanceof Text) {
                writeNode(node);
                lastTextNode = node;
            } else {
                if ((lastTextNode != null) && format.isPadText()) {
                    writer.write(PAD_TEXT);
                }
                writeNode(node);
                if ((lastTextNode != null) && format.isPadText()) {
                    writer.write(PAD_TEXT);
                }
                lastTextNode = null;
            }
        }
    }
    preserve = oldPreserve;
}

From source file:architecture.ee.util.xml.XmlWriter.java

License:Apache 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./*from  w  w w  .  j  a  va 2  s .c o m*/
 */
protected void writeElementContent(Element element) throws IOException {
    boolean trim = format.isTrimText();
    boolean oldPreserve = preserve;
    if (trim) { //verify we have to before more expensive test
        preserve = isElementSpacePreserved(element);
        trim = !preserve;
    }
    if (trim) {
        // concatenate adjacent text nodes together
        // so that whitespace trimming works properly
        Text lastTextNode = null;
        StringBuilder buffer = null;
        boolean textOnly = true;
        for (int i = 0, size = element.nodeCount(); i < size; i++) {
            Node node = element.node(i);
            if (node instanceof Text) {
                if (lastTextNode == null) {
                    lastTextNode = (Text) node;
                } else {
                    if (buffer == null) {
                        buffer = new StringBuilder(lastTextNode.getText());
                    }
                    buffer.append(node.getText());
                }
            } else {
                if (!textOnly && format.isPadText()) {
                    writer.write(PAD_TEXT);
                }

                textOnly = false;

                if (lastTextNode != null) {
                    if (buffer != null) {
                        writeString(buffer.toString());
                        buffer = null;
                    } else {
                        writeString(lastTextNode.getText());
                    }
                    lastTextNode = null;

                    if (format.isPadText()) {
                        writer.write(PAD_TEXT);
                    }
                }
                writeNode(node);
            }
        }
        if (lastTextNode != null) {
            if (!textOnly && format.isPadText()) {
                writer.write(PAD_TEXT);
            }
            if (buffer != null) {
                writeString(buffer.toString());
                buffer = null;
            } else {
                writeString(lastTextNode.getText());
            }
            lastTextNode = null;
        }
    } else {
        Node lastTextNode = null;
        for (int i = 0, size = element.nodeCount(); i < size; i++) {
            Node node = element.node(i);
            if (node instanceof Text) {
                writeNode(node);
                lastTextNode = node;
            } else {
                if ((lastTextNode != null) && format.isPadText()) {
                    writer.write(PAD_TEXT);
                }
                writeNode(node);
                if ((lastTextNode != null) && format.isPadText()) {
                    writer.write(PAD_TEXT);
                }
                lastTextNode = null;
            }
        }
    }
    preserve = oldPreserve;
}

From source file:com.ah.be.ls.stat.StatManager.java

private static List<StatConfig> readStatConfig() {
    if (stats != null && !stats.isEmpty()) {
        return stats;
    }//from   ww w  . java2 s .c  o m
    stats = new ArrayList<StatConfig>();
    // read from stat-config.xml
    SAXReader reader = new SAXReader();
    File ff = new File(System.getenv("HM_ROOT") + "/resources/stat-config.xml");
    if (!ff.exists()) {
        // for test
        ff = new File("stat-config.xml");
    }
    try {
        Document doc = reader.read(ff);

        Element roota = doc.getRootElement();
        log.info("StatManager", "roota..nodcount=" + roota.nodeCount());

        Iterator<?> iters = roota.elementIterator("feature");
        while (iters.hasNext()) {
            StatConfig stat = new StatConfig();
            Element foo = (Element) iters.next();
            if (foo.attribute("ignore") != null) {
                continue;
            }
            stat.setFeatureId(Integer.valueOf(foo.attributeValue("id")));
            stat.setFeatureName(foo.attributeValue("name"));

            Element e2 = foo.element("bo-class");
            Element e4 = foo.element("search-rule");
            stat.setBoClassName(e2.attributeValue("name"));
            stat.setSearchRule(e4.attributeValue("value"));
            stat.setSearchType(e4.attributeValue("type"));

            stats.add(stat);
        }

        return stats;
    } catch (ClassNotFoundException e) {
        log.error("StatManager", "readStatConfig: ClassNotFoundException", e);
    } catch (Exception e) {
        log.error("StatManager", "readStatConfig: Exception", e);
    }

    return null;
}

From source file:com.amalto.workbench.utils.XmlUtil.java

License:Open Source License

public static void treeWalk(Element element, NodeProcess nodeProcess) {
    for (int i = 0, size = element.nodeCount(); i < size; i++) {
        Node node = element.node(i);

        if (node instanceof Element) {
            treeWalk((Element) node, nodeProcess);
        } else {//from  w w  w .j  a  va2s  .  c om
            nodeProcess.process(node);
        }
    }
}

From source file:com.chinarewards.license.util.XmlUtil_dom4j.java

public static void searchElement(Element paramElement) {
    int i = 0;/* w  w w. j  a va  2 s  .  c  o m*/
    int j = paramElement.nodeCount();
    while (i < j) {
        Node localNode = paramElement.node(i);
        if (localNode instanceof Element) {
            Element localElement = (Element) localNode;
            searchAttribute(localElement);
            searchElement((Element) localNode);
        }
        ++i;
    }
}

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

License:Open Source 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  ww .ja  v  a  2 s . c  o m
 */
protected void writeElementContent(Element element) throws IOException {
    CDATA lastCDATANode = null;
    StringBuffer cdataBuffer = null;

    if (format.isTrimText()) {
        // concatenate adjacent text nodes together
        // so that whitespace trimming works properly
        Text lastTextNode = null;
        StringBuffer textBuffer = null;
        for (int i = 0, size = element.nodeCount(); i < size; i++) {
            Node node = element.node(i);

            if (node instanceof Text) {
                if (lastCDATANode != null) {
                    if (cdataBuffer != null) {
                        writeCDATA(cdataBuffer.toString());
                        cdataBuffer = null;
                    } else {
                        writeCDATA(lastCDATANode.getText());
                    }

                    lastCDATANode = null;
                }

                if (lastTextNode == null) {
                    lastTextNode = (Text) node;
                } else {
                    if (textBuffer == null) {
                        textBuffer = new StringBuffer(lastTextNode.getText());
                    }
                    textBuffer.append(((Text) node).getText());
                }
            } else if (node instanceof CDATA) {
                if (lastTextNode != null) {
                    if (textBuffer != null) {
                        writeString(textBuffer.toString());
                        textBuffer = null;
                    } else {
                        writeString(lastTextNode.getText());
                    }

                    lastTextNode = null;
                }

                if (lastCDATANode == null) {
                    lastCDATANode = (CDATA) node;
                } else {
                    if (cdataBuffer == null) {
                        cdataBuffer = new StringBuffer(lastCDATANode.getText());
                    }

                    cdataBuffer.append(((CDATA) node).getText());
                }
            } else {
                if (lastCDATANode != null) {
                    if (cdataBuffer != null) {
                        writeCDATA(cdataBuffer.toString());
                        cdataBuffer = null;
                    } else {
                        writeCDATA(lastCDATANode.getText());
                    }

                    lastCDATANode = null;
                }

                if (lastTextNode != null) {
                    if (textBuffer != null) {
                        writeString(textBuffer.toString());
                        textBuffer = null;
                    } else {
                        writeString(lastTextNode.getText());
                    }

                    lastTextNode = null;
                }

                writeNode(node);
            }
        }

        if (lastCDATANode != null) {
            if (cdataBuffer != null) {
                writeCDATA(cdataBuffer.toString());
                cdataBuffer = null;
            } else {
                writeCDATA(lastCDATANode.getText());
            }

            lastCDATANode = null;
        }

        if (lastTextNode != null) {
            if (textBuffer != null) {
                writeString(textBuffer.toString());
                textBuffer = null;
            } else {
                writeString(lastTextNode.getText());
            }

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

            if (node instanceof CDATA) {
                if (lastCDATANode == null) {
                    lastCDATANode = (CDATA) node;
                } else {
                    if (cdataBuffer == null) {
                        cdataBuffer = new StringBuffer(lastCDATANode.getText());
                    }

                    cdataBuffer.append(((CDATA) node).getText());
                }
            } else {
                if (lastCDATANode != null) {
                    if (cdataBuffer != null) {
                        writeCDATA(cdataBuffer.toString());
                        cdataBuffer = null;
                    } else {
                        writeCDATA(lastCDATANode.getText());
                    }

                    lastCDATANode = null;
                }

                writeNode(node);
            }
        }

        if (lastCDATANode != null) {
            if (cdataBuffer != null) {
                writeCDATA(cdataBuffer.toString());
                cdataBuffer = null;
            } else {
                writeCDATA(lastCDATANode.getText());
            }

            lastCDATANode = null;
        }
    }
}

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

License:Mozilla Public License

protected void writeElement(Element element) throws IOException {
    if (DEBUG)/*from   www .  j  av  a2s  .c o  m*/
        System.out.println("XMLFormatter.writeElement( " + element + ")");
    //      if ( indentMixed) {
    //         super.writeElement( element);
    //      } else {
    int size = element.nodeCount();
    String qualifiedName = element.getQualifiedName();

    boolean hasElement = false;
    boolean hasText = false;

    // first test to see if this element has mixed content, 
    // if whitespace is significant ...
    for (int i = 0; i < size; i++) {
        Node node = element.node(i);

        if (node instanceof Element) {
            hasElement = true;
        } else if (node instanceof Text) {
            String text = node.getText();

            if (text != null && text.trim().length() > 0) {
                hasText = true;
            }
        }
    }

    Attribute space = element.attribute("space");
    boolean preserveSpace = false;

    if (space != null) {
        String prefix = space.getNamespacePrefix();
        String value = space.getValue();
        //         System.out.println( "prefix = "+prefix+" value = "+value);
        if (prefix != null && "xml".equals(prefix) && "preserve".equals(value)) {
            preserveSpace = true;
        }
    }

    writePrintln();
    indent();

    writer.write("<");
    writer.write(qualifiedName);

    int previouslyDeclaredNamespaces = namespaceStack.size();

    Namespace ns = element.getNamespace();

    if (isNamespaceDeclaration(ns)) {
        namespaceStack.push(ns);
        writeNamespace(ns);
    }

    // Print out additional namespace declarations
    for (int i = 0; i < size; i++) {
        Node node = element.node(i);

        if (node instanceof Namespace) {
            Namespace additional = (Namespace) node;

            if (isNamespaceDeclaration(additional)) {
                namespaceStack.push(additional);
                writeNamespace(additional);
            }
        }
    }

    writeAttributes(element);

    lastOutputNodeType = Node.ELEMENT_NODE;

    if (size <= 0) {
        writeEmptyElementClose(qualifiedName);
    } else {
        writer.write(">");

        if (!hasElement && !preserveSpace) { // text only
            // we have at least one text node so lets assume
            // that its non-empty
            //            System.out.println( "writeElementContent (Text) ...");
            boolean previousWrapText = wrapText;
            wrapText = true;
            writeElementContent(element);
            wrapText = previousWrapText;
        } else if (preserveMixedContent && hasElement && hasText) { // preserve space
            // Mixed content
            //                System.out.println( "writeMixedElementContent ...");
            Node lastNode = writeMixedElementContent(element);

        } else if (preserveSpace) { // preserve space
            // Mixed content
            //                System.out.println( "writePreserveElementContent ...");
            Node lastNode = writePreservedElementContent(element);

        } else { // hasElement && !hasText
            //               System.out.println( "writeElementContent (Element) ...");

            boolean previousWrapText = wrapText;
            wrapText = true;
            ++indentLevel;
            writeElementContent(element);
            --indentLevel;
            wrapText = previousWrapText;

            writePrintln();
            indent();
        }

        writer.write("</");
        writer.write(qualifiedName);
        writer.write(">");
        //         writePrintln();
    }

    // remove declared namespaceStack from stack
    while (namespaceStack.size() > previouslyDeclaredNamespaces) {
        namespaceStack.pop();
    }

    lastOutputNodeType = Node.ELEMENT_NODE;
    //      }
}

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

License:Mozilla Public License

protected void writeMixedElement(Element element) throws IOException {
    if (DEBUG)/* ww w .  ja  v  a  2s  .  c o  m*/
        System.out.println("XMLFormatter.writeMixedElement( " + element + ")");

    int size = element.nodeCount();
    String qualifiedName = element.getQualifiedName();

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

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

    writer.write("<");
    writer.write(qualifiedName);

    int previouslyDeclaredNamespaces = namespaceStack.size();
    Namespace ns = element.getNamespace();

    if (isNamespaceDeclaration(ns)) {
        namespaceStack.push(ns);
        writeNamespace(ns);
    }

    // Print out additional namespace declarations
    boolean textOnly = true;
    for (int i = 0; i < size; i++) {
        Node node = element.node(i);
        if (node instanceof Namespace) {
            Namespace additional = (Namespace) node;
            if (isNamespaceDeclaration(additional)) {
                namespaceStack.push(additional);
                writeNamespace(additional);
            }
        } else if (node instanceof Element) {
            textOnly = false;
        }
    }

    writeAttributes(element);

    lastOutputNodeType = Node.ELEMENT_NODE;

    if (size <= 0) {
        writeEmptyElementClose(qualifiedName);
        //           writePrintln();
    } else {
        writer.write(">");
        writeMixedElementContent(element);
        writer.write("</");
        writer.write(qualifiedName);
        writer.write(">");
    }

    // remove declared namespaceStack from stack
    while (namespaceStack.size() > previouslyDeclaredNamespaces) {
        namespaceStack.pop();
    }

    lastOutputNodeType = Node.ELEMENT_NODE;

    format.setTrimText(previousTrim);
    wrapText = previousWrapText;
}

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

License:Mozilla Public License

protected void writePreservedElement(Element element) throws IOException {
    if (DEBUG)//from  ww  w  .  j  a v a2  s  . com
        System.out.println("XMLFormatter.writePreservedElement( " + element + ")");
    int size = element.nodeCount();
    String qualifiedName = element.getQualifiedName();

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

    format.setTrimText(false);
    wrapText = false;

    boolean hasElement = false;
    boolean hasText = false;

    // first test to see if this element has mixed content, 
    // if whitespace is significant ...
    for (int i = 0; i < size; i++) {
        Node node = element.node(i);

        if (node instanceof Element) {
            hasElement = true;
        } else if (node instanceof Text) {
            String text = node.getText();

            if (text != null && text.trim().length() > 0) {
                hasText = true;
            }
        }
    }

    Attribute space = element.attribute("space");
    boolean defaultSpace = false;

    if (space != null) {
        String prefix = space.getNamespacePrefix();
        String value = space.getValue();
        //            System.out.println( "prefix = "+prefix+" value = "+value);
        if (prefix != null && "xml".equals(prefix) && "default".equals(value)) {
            defaultSpace = true;
        }
    }

    writer.write("<");
    writer.write(qualifiedName);

    int previouslyDeclaredNamespaces = namespaceStack.size();
    Namespace ns = element.getNamespace();

    if (isNamespaceDeclaration(ns)) {
        namespaceStack.push(ns);
        writeNamespace(ns);
    }

    // Print out additional namespace declarations
    for (int i = 0; i < size; i++) {
        Node node = element.node(i);
        if (node instanceof Namespace) {
            Namespace additional = (Namespace) node;
            if (isNamespaceDeclaration(additional)) {
                namespaceStack.push(additional);
                writeNamespace(additional);
            }
        }
    }

    writeAttributes(element);

    lastOutputNodeType = Node.ELEMENT_NODE;

    if (size <= 0) {
        writeEmptyElementClose(qualifiedName);
    } else {
        writer.write(">");

        if (preserveMixedContent && hasElement && hasText) { // mixed content
            //                System.out.println( "writeMixedElementContent ...");

            Node lastNode = writeMixedElementContent(element);
        } else if (!defaultSpace) { // preserve space
            //                System.out.println( "writePreservedElementContent ...");

            Node lastNode = writePreservedElementContent(element);
        } else {
            //                System.out.println( "writeElementContent ...");

            format.setTrimText(isTrimText());
            boolean prevWrapText = wrapText;
            wrapText = true;
            writeElementContent(element);
            wrapText = prevWrapText;
            format.setTrimText(false);
        }

        writer.write("</");
        writer.write(qualifiedName);
        writer.write(">");
    }

    // remove declared namespaceStack from stack
    while (namespaceStack.size() > previouslyDeclaredNamespaces) {
        namespaceStack.pop();
    }

    lastOutputNodeType = Node.ELEMENT_NODE;

    format.setTrimText(previousTrim);
    wrapText = previousWrapText;
}