Java Code Examples for com.gargoylesoftware.htmlunit.html.DomNode#getChildren()

The following examples show how to use com.gargoylesoftware.htmlunit.html.DomNode#getChildren() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Node.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the child nodes of the current element.
 * @return the child nodes of the current element
 */
@JsxGetter
public NodeList getChildNodes() {
    if (childNodes_ == null) {
        final DomNode node = getDomNodeOrDie();
        childNodes_ = new NodeList(node, false) {
            @Override
            protected List<DomNode> computeElements() {
                final List<DomNode> response = new ArrayList<>();
                for (final DomNode child : node.getChildren()) {
                    response.add(child);
                }

                return response;
            }
        };
    }
    return childNodes_;
}
 
Example 2
Source File: Node.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the child nodes of the current element.
 * @return the child nodes of the current element
 */
@JsxGetter
public NodeList getChildNodes() {
    if (childNodes_ == null) {
        final DomNode node = getDomNodeOrDie();
        childNodes_ = new NodeList(node, false) {
            @Override
            protected List<DomNode> computeElements() {
                final List<DomNode> response = new ArrayList<>();
                for (final DomNode child : node.getChildren()) {
                    response.add(child);
                }

                return response;
            }
        };
    }
    return childNodes_;
}
 
Example 3
Source File: XSLTProcessor.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
private static DomNode findOutputNode(final DomNode xsltDomNode) {
    for (DomNode child : xsltDomNode.getChildren()) {
        if ("output".equals(child.getLocalName())) {
            return child;
        }

        for (DomNode child1 : child.getChildren()) {
            if ("output".equals(child1.getLocalName())) {
                return child1;
            }
        }
    }
    return null;
}
 
Example 4
Source File: XMLDOMNode.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a node list containing the child nodes.
 * @return a node list containing the child nodes
 */
@JsxGetter
public XMLDOMNodeList getChildNodes() {
    if (childNodes_ == null) {
        final DomNode domNode = getDomNodeOrDie();
        final boolean isXmlPage = domNode.getOwnerDocument() instanceof XmlPage;
        final Boolean xmlSpaceDefault = isXMLSpaceDefault(domNode);
        final boolean skipEmptyTextNode = isXmlPage && !Boolean.FALSE.equals(xmlSpaceDefault);

        childNodes_ = new XMLDOMNodeList(domNode, false, "XMLDOMNode.childNodes") {
            @Override
            protected List<DomNode> computeElements() {
                final List<DomNode> response = new ArrayList<>();
                for (final DomNode child : domNode.getChildren()) {
                    //IE: XmlPage ignores all empty text nodes
                    if (skipEmptyTextNode && child instanceof DomText && !(child instanceof DomCDataSection)
                        && StringUtils.isBlank(((DomText) child).getNodeValue())) { //and 'xml:space' is 'default'
                        continue;
                    }
                    response.add(child);
                }

                return response;
            }
        };
    }
    return childNodes_;
}
 
Example 5
Source File: XMLDOMElement.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
private void toText(final DomNode node, final StringBuilder builder) {
    switch (node.getNodeType()) {
        case Node.DOCUMENT_TYPE_NODE:
        case Node.NOTATION_NODE:
            return;
        case Node.TEXT_NODE:
        case Node.CDATA_SECTION_NODE:
        case Node.COMMENT_NODE:
        case Node.PROCESSING_INSTRUCTION_NODE:
            builder.append(node.getNodeValue());
            break;
        default:
    }
    boolean lastWasElement = false;
    for (final DomNode child : node.getChildren()) {
        switch (child.getNodeType()) {
            case Node.ELEMENT_NODE:
                lastWasElement = true;
                toText(child, builder);
                break;

            case Node.TEXT_NODE:
            case Node.CDATA_SECTION_NODE:
                if (StringUtils.isBlank(child.getNodeValue())) {
                    if (lastWasElement) {
                        builder.append(' ');
                    }
                    lastWasElement = false;
                    break;
                }
                lastWasElement = false;
                builder.append(child.getNodeValue());
                break;
            default:
                lastWasElement = false;
        }
    }
}
 
Example 6
Source File: XMLDOMNode.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a node list containing the child nodes.
 * @return a node list containing the child nodes
 */
@JsxGetter
public XMLDOMNodeList getChildNodes() {
    if (childNodes_ == null) {
        final DomNode domNode = getDomNodeOrDie();
        final boolean isXmlPage = domNode.getOwnerDocument() instanceof XmlPage;
        final Boolean xmlSpaceDefault = isXMLSpaceDefault(domNode);
        final boolean skipEmptyTextNode = isXmlPage && !Boolean.FALSE.equals(xmlSpaceDefault);

        childNodes_ = new XMLDOMNodeList(domNode, false, "XMLDOMNode.childNodes") {
            @Override
            protected List<DomNode> computeElements() {
                final List<DomNode> response = new ArrayList<>();
                for (final DomNode child : domNode.getChildren()) {
                    //IE: XmlPage ignores all empty text nodes
                    if (skipEmptyTextNode && child instanceof DomText && !(child instanceof DomCDataSection)
                        && StringUtils.isBlank(((DomText) child).getNodeValue())) { //and 'xml:space' is 'default'
                        continue;
                    }
                    response.add(child);
                }

                return response;
            }
        };
    }
    return childNodes_;
}
 
Example 7
Source File: XMLDOMElement.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
private void toText(final DomNode node, final StringBuilder builder) {
    switch (node.getNodeType()) {
        case Node.DOCUMENT_TYPE_NODE:
        case Node.NOTATION_NODE:
            return;
        case Node.TEXT_NODE:
        case Node.CDATA_SECTION_NODE:
        case Node.COMMENT_NODE:
        case Node.PROCESSING_INSTRUCTION_NODE:
            builder.append(node.getNodeValue());
            break;
        default:
    }
    boolean lastWasElement = false;
    for (final DomNode child : node.getChildren()) {
        switch (child.getNodeType()) {
            case Node.ELEMENT_NODE:
                lastWasElement = true;
                toText(child, builder);
                break;

            case Node.TEXT_NODE:
            case Node.CDATA_SECTION_NODE:
                if (StringUtils.isBlank(child.getNodeValue())) {
                    if (lastWasElement) {
                        builder.append(' ');
                    }
                    lastWasElement = false;
                    break;
                }
                lastWasElement = false;
                builder.append(child.getNodeValue());
                break;
            default:
                lastWasElement = false;
        }
    }
}
 
Example 8
Source File: XMLSerializer.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
private void toXml(final int indent,
        final DomNode node, final StringBuilder builder) {
    final String nodeName = node.getNodeName();
    builder.append('<').append(nodeName);

    final String optionalPrefix = "";
    final String namespaceURI = node.getNamespaceURI();
    final String prefix = node.getPrefix();
    if (namespaceURI != null && prefix != null) {
        boolean sameNamespace = false;
        for (DomNode parentNode = node.getParentNode(); parentNode instanceof DomElement;
                parentNode = parentNode.getParentNode()) {
            if (namespaceURI.equals(parentNode.getNamespaceURI())) {
                sameNamespace = true;
            }
        }
        if (node.getParentNode() == null || !sameNamespace) {
            ((DomElement) node).setAttribute("xmlns:" + prefix, namespaceURI);
        }
    }

    final NamedNodeMap attributesMap = node.getAttributes();
    for (int i = 0; i < attributesMap.getLength(); i++) {
        final DomAttr attrib = (DomAttr) attributesMap.item(i);
        builder.append(' ').append(attrib.getQualifiedName()).append('=')
            .append('"').append(attrib.getValue()).append('"');
    }
    boolean startTagClosed = false;
    for (final DomNode child : node.getChildren()) {
        if (!startTagClosed) {
            builder.append(optionalPrefix).append('>');
            startTagClosed = true;
        }
        switch (child.getNodeType()) {
            case Node.ELEMENT_NODE:
                toXml(indent + 1, child, builder);
                break;

            case Node.TEXT_NODE:
                String value = child.getNodeValue();
                value = StringUtils.escapeXmlChars(value);
                if (preserveWhiteSpace_) {
                    builder.append(value.replace("\n", "\r\n"));
                }
                else if (org.apache.commons.lang3.StringUtils.isBlank(value)) {
                    builder.append("\r\n");
                    final DomNode sibling = child.getNextSibling();
                    if (sibling != null && sibling.getNodeType() == Node.ELEMENT_NODE) {
                        for (int i = 0; i < indent; i++) {
                            builder.append('\t');
                        }
                    }
                }
                else {
                    builder.append(value.replace("\n", "\r\n"));
                }
                break;

            case Node.CDATA_SECTION_NODE:
            case Node.COMMENT_NODE:
                if (!preserveWhiteSpace_ && builder.charAt(builder.length() - 1) == '\n') {
                    for (int i = 0; i < indent; i++) {
                        builder.append('\t');
                    }
                }
                builder.append(child.asXml());
                break;

            default:

        }
    }
    if (!startTagClosed) {
        builder.append(optionalPrefix).append("/>");
    }
    else {
        if (!preserveWhiteSpace_ && builder.charAt(builder.length() - 1) == '\n') {
            for (int i = 0; i < indent - 1; i++) {
                builder.append('\t');
            }
        }
        builder.append('<').append('/').append(nodeName).append('>');
    }
}
 
Example 9
Source File: XMLSerializer.java    From HtmlUnit-Android with Apache License 2.0 4 votes vote down vote up
private void toXml(final int indent,
        final DomNode node, final StringBuilder builder) {
    final String nodeName = node.getNodeName();
    builder.append('<').append(nodeName);

    final String optionalPrefix = "";
    final String namespaceURI = node.getNamespaceURI();
    final String prefix = node.getPrefix();
    if (namespaceURI != null && prefix != null) {
        boolean sameNamespace = false;
        for (DomNode parentNode = node.getParentNode(); parentNode instanceof DomElement;
                parentNode = parentNode.getParentNode()) {
            if (namespaceURI.equals(parentNode.getNamespaceURI())) {
                sameNamespace = true;
            }
        }
        if (node.getParentNode() == null || !sameNamespace) {
            ((DomElement) node).setAttribute("xmlns:" + prefix, namespaceURI);
        }
    }

    final NamedNodeMap attributesMap = node.getAttributes();
    for (int i = 0; i < attributesMap.getLength(); i++) {
        final DomAttr attrib = (DomAttr) attributesMap.item(i);
        builder.append(' ').append(attrib.getQualifiedName()).append('=')
            .append('"').append(attrib.getValue()).append('"');
    }
    boolean startTagClosed = false;
    for (final DomNode child : node.getChildren()) {
        if (!startTagClosed) {
            builder.append(optionalPrefix).append('>');
            startTagClosed = true;
        }
        switch (child.getNodeType()) {
            case Node.ELEMENT_NODE:
                toXml(indent + 1, child, builder);
                break;

            case Node.TEXT_NODE:
                String value = child.getNodeValue();
                value = StringUtils.escapeXmlChars(value);
                if (preserveWhiteSpace_) {
                    builder.append(value.replace("\n", "\r\n"));
                }
                else if (org.apache.commons.lang3.StringUtils.isBlank(value)) {
                    builder.append("\r\n");
                    final DomNode sibling = child.getNextSibling();
                    if (sibling != null && sibling.getNodeType() == Node.ELEMENT_NODE) {
                        for (int i = 0; i < indent; i++) {
                            builder.append('\t');
                        }
                    }
                }
                else {
                    builder.append(value.replace("\n", "\r\n"));
                }
                break;

            case Node.CDATA_SECTION_NODE:
            case Node.COMMENT_NODE:
                if (!preserveWhiteSpace_ && builder.charAt(builder.length() - 1) == '\n') {
                    for (int i = 0; i < indent; i++) {
                        builder.append('\t');
                    }
                }
                builder.append(child.asXml());
                break;

            default:

        }
    }
    if (!startTagClosed) {
        builder.append(optionalPrefix).append("/>");
    }
    else {
        if (!preserveWhiteSpace_ && builder.charAt(builder.length() - 1) == '\n') {
            for (int i = 0; i < indent - 1; i++) {
                builder.append('\t');
            }
        }
        builder.append('<').append('/').append(nodeName).append('>');
    }
}
 
Example 10
Source File: Element.java    From HtmlUnit-Android with Apache License 2.0 2 votes vote down vote up
/**
 * Helper for getting code back from nodes.
 * @param builder the builder to write to
 * @param node the node to be serialized
 * @param html flag
 */
protected void printChildren(final StringBuilder builder, final DomNode node, final boolean html) {
    for (final DomNode child : node.getChildren()) {
        printNode(builder, child, html);
    }
}