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

The following examples show how to use com.gargoylesoftware.htmlunit.html.DomNode#getNodeName() . 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: XMLDocument.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
@JsxFunction
public HTMLCollection getElementsByTagName(final String tagName) {
    final DomNode firstChild = getDomNodeOrDie().getFirstChild();
    if (firstChild == null) {
        return HTMLCollection.emptyCollection(getWindow().getDomNodeOrDie());
    }

    final HTMLCollection collection = new HTMLCollection(getDomNodeOrDie(), false) {
        @Override
        protected boolean isMatching(final DomNode node) {
            final String nodeName;
            if (getBrowserVersion().hasFeature(JS_XML_GET_ELEMENTS_BY_TAG_NAME_LOCAL)) {
                nodeName = node.getLocalName();
            }
            else {
                nodeName = node.getNodeName();
            }

            return nodeName.equals(tagName);
        }
    };

    return collection;
}
 
Example 2
Source File: XMLDocument.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
@JsxFunction
public HTMLCollection getElementsByTagName(final String tagName) {
    final DomNode firstChild = getDomNodeOrDie().getFirstChild();
    if (firstChild == null) {
        return HTMLCollection.emptyCollection(getWindow().getDomNodeOrDie());
    }

    final HTMLCollection collection = new HTMLCollection(getDomNodeOrDie(), false) {
        @Override
        protected boolean isMatching(final DomNode node) {
            final String nodeName;
            if (getBrowserVersion().hasFeature(JS_XML_GET_ELEMENTS_BY_TAG_NAME_LOCAL)) {
                nodeName = node.getLocalName();
            }
            else {
                nodeName = node.getNodeName();
            }

            return nodeName.equals(tagName);
        }
    };

    return collection;
}
 
Example 3
Source File: HTMLElement.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public String getNodeName() {
    final DomNode domNode = getDomNodeOrDie();
    String nodeName = domNode.getNodeName();
    if (domNode.getHtmlPageOrNull() != null) {
        nodeName = nodeName.toUpperCase(Locale.ROOT);
    }
    return nodeName;
}
 
Example 4
Source File: HTMLElement.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public String getNodeName() {
    final DomNode domNode = getDomNodeOrDie();
    String nodeName = domNode.getNodeName();
    if (domNode.getHtmlPageOrNull() != null) {
        nodeName = nodeName.toUpperCase(Locale.ROOT);
    }
    return nodeName;
}
 
Example 5
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 6
Source File: XMLDOMNode.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the qualified name for attribute, document type, element, entity, or notation nodes. Returns a fixed
 * string for all other node types.
 * @return the qualified name
 */
@JsxGetter
public String getNodeName() {
    final DomNode domNode = getDomNodeOrDie();
    return domNode.getNodeName();
}
 
Example 7
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 8
Source File: XMLDOMNode.java    From HtmlUnit-Android with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the qualified name for attribute, document type, element, entity, or notation nodes. Returns a fixed
 * string for all other node types.
 * @return the qualified name
 */
@JsxGetter
public String getNodeName() {
    final DomNode domNode = getDomNodeOrDie();
    return domNode.getNodeName();
}