Example usage for org.dom4j Node getText

List of usage examples for org.dom4j Node getText

Introduction

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

Prototype

String getText();

Source Link

Document

Returns the text of this node.

Usage

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

License:Mozilla Public License

protected Line parseMixedElement2(Vector lines, Line current, XElement elem) {

    current = parseStartTag(lines, current, elem);

    if (isMixed2(elem)) {

        int previousText = 0;

        for (int i = 0; i < elem.nodeCount(); i++) {
            Node node = elem.node(i);

            if (node instanceof XElement) {
                previousText = 0;/*from  w  w w. j a v a 2  s  .  c o  m*/

                current = parseMixedElement2(lines, current, (XElement) node);
                XElement ele = (XElement) node;
                if (isMixed2(ele)) {
                    if (requiresEndTag(ele)) {
                        current = parseEndTag(lines, current, (XElement) node);
                    }
                }
            } else if ((node instanceof Text) || (node instanceof Entity)) {
                if (_viewer.showValues() && !isWhiteSpace(node)) {
                    String text = node.getText().trim();

                    if (!"".equals(text)) {
                        if (previousText == 1) {
                            text = " " + text;
                        }
                        previousText = 1;
                    }

                    current = parseContent(lines, current, text);
                }
            } else if ((node instanceof Comment) && _viewer.showComments()) {
                previousText = 0;
                current = parseComment(lines, current, (Comment) node);
            } else if ((node instanceof CDATA) && _viewer.showValues()) {
                previousText = 0;
                current = parseCDATA(lines, current, (CDATA) node);
            } else if ((node instanceof ProcessingInstruction) && _viewer.showPI()) {
                previousText = 0;
                current = parseProcessingInstruction(lines, current, (ProcessingInstruction) node);
            }
        }
    } else {
        List elements = (List) elem.elements();

        if (elements != null && elements.size() > 0) {
            Iterator iterator = elements.iterator();

            while (iterator.hasNext()) {
                current = parseMixedElement2(lines, current, (XElement) iterator.next());
            }
            current = parseEndTag(lines, current, elem);
        } else if (elem.hasContent()) {
            if (_viewer.showValues()) {
                current = parseContent(lines, current, elem.getText());
                if (requiresEndTag(elem)) {
                    current = parseEndTag(lines, current, elem);
                }
            }
        }
    }

    return current;
}

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

License:Mozilla Public License

protected Line parseMixedElement(Vector lines, Line current, XElement elem) {

    current = parseStartTag(lines, current, elem);

    if (_viewer.showValues()) {
        if (isMixed(elem)) {
            for (int i = 0; i < elem.nodeCount(); i++) {
                Node node = elem.node(i);

                if (node instanceof XElement) {
                    current = parseMixedElement(lines, current, (XElement) node);
                } else if ((node instanceof Text) || (node instanceof Entity)) {
                    String text = node.getText();
                    current = parseContent(lines, current, text);
                } else if ((node instanceof Comment) && _viewer.showComments()) {
                    current = parseComment(lines, current, (Comment) node);
                }//from   w  w w.j a  va2s.  c  o m
            }
        } else {
            List elements = (List) elem.elements();

            if (elements != null && elements.size() > 0) {
                Iterator iterator = elements.iterator();

                while (iterator.hasNext()) {
                    current = parseMixedElement(lines, current, (XElement) iterator.next());
                }

                current = parseEndTag(lines, current, elem);
            } else if (elem.hasContent()) {
                current = parseContent(lines, current, elem.getText());
                current = parseEndTag(lines, current, elem);
            }
        }
    }

    return current;
}

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

License:Mozilla Public License

private static boolean isWhiteSpace(Node node) {
    return node.getText().trim().length() == 0;
}

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

License:Open Source License

protected Line parseElement(Vector lines, Line current, XElement elem) {

    // need to parse PI diff instruction
    if (hasProcessingInstruction(elem)) {
        parseDiffPIs(elem);//w  w w  . j a v  a 2s  . co  m
    }

    current = parseStartTag(lines, current, elem);

    // if its mixed content then let text nodes take care of that
    if (isMixedElementAndContent(elem)) {
        // let the text nodes take care of it
        return current;
    }

    if (hasTextOrCDATA(elem)) {
        // parse the content, note do not strip whitespace as xdiff detects these
        if (!hasCDATA(elem)) {
            if (insertElement || deleteElement) {
                // then we don't want whitespace
                current = parseContent(lines, current, elem.getText().trim());
            } else
                current = parseContent(lines, current, elem.getText());
        } else {
            for (int i = 0; i < elem.nodeCount(); i++) {
                Node node = elem.node(i);

                if ((node instanceof Text) || (node instanceof Entity)) {
                    String text;
                    if (insertElement || deleteElement) {
                        // can delete trailing whitspace in these cases
                        text = node.getText().trim();
                    } else {
                        // xdiff includes trailing whitespace at the moment
                        text = node.getText();
                    }

                    current = parseContent(lines, current, text);
                } else if ((node instanceof CDATA)) {
                    current = parseCDATA(lines, current, (CDATA) node);
                }
            }
        }

        if (!elem.hasChildElements()) {
            current = parseEndTag(lines, current, elem);
        }
    } else {
        // check blanked nodes
        if (updateElementFrom != null) {
            // element content must have been blanked
            current = parseContent(lines, current, "");

            if (!elem.hasChildElements()) {
                current = parseEndTag(lines, current, elem);
            }
        }
    }

    //clearDiffPIValues();

    return current;
}

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

License:Open Source License

protected Line parseMixedElement(Vector lines, Line current, XElement elem) {

    current = parseStartTag(lines, current, elem);

    if (isMixed(elem)) {
        for (int i = 0; i < elem.nodeCount(); i++) {
            Node node = elem.node(i);

            if (node instanceof XElement) {
                current = parseMixedElement(lines, current, (XElement) node);
            } else if ((node instanceof Text) || (node instanceof CDATA) || (node instanceof Entity)) {
                String text = node.getText();
                current = parseContent(lines, current, text);
            } else if ((node instanceof Comment)) {
                current = parseComment(lines, current, (Comment) node);
            }/* w  ww  . j av  a2  s.  c om*/
        }
    } else {
        List elements = (List) elem.elements();

        if (elements != null && elements.size() > 0) {
            Iterator iterator = elements.iterator();

            while (iterator.hasNext()) {
                current = parseMixedElement(lines, current, (XElement) iterator.next());
            }

            current = parseEndTag(lines, current, elem);
        } else if (elem.hasContent()) {
            current = parseContent(lines, current, elem.getText());
            current = parseEndTag(lines, current, elem);
        }
    }

    return current;
}

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

License:Open Source License

private boolean isWhiteSpace(Node node) {
    return node.getText().trim().length() == 0;
}

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

License:Mozilla Public License

protected void writeElement(Element element) throws IOException {
    if (DEBUG)//from  w w w .  j av a2 s  .  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 writePreservedElement(Element element) throws IOException {
    if (DEBUG)//from w ww. j a va2s  . 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;
}

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.//from   ww w.jav  a2s  .  c  om
 */
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)//from   www. j  a  va 2s  . 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;
}