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

The following examples show how to use com.gargoylesoftware.htmlunit.html.DomNode#getPrefix() . 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: Document.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the value of the {@code forms} property.
 * @return the value of the {@code forms} property
 */
@JsxGetter({CHROME, IE})
public Object getForms() {
    return new HTMLCollection(getDomNodeOrDie(), false) {
        @Override
        protected boolean isMatching(final DomNode node) {
            return node instanceof HtmlForm && node.getPrefix() == null;
        }

        @Override
        public Object call(final Context cx, final Scriptable scope,
                final Scriptable thisObj, final Object[] args) {
            if (getBrowserVersion().hasFeature(JS_DOCUMENT_FORMS_FUNCTION_SUPPORTED)) {
                return super.call(cx, scope, thisObj, args);
            }
            throw Context.reportRuntimeError("TypeError: document.forms is not a function");
        }
    };
}
 
Example 2
Source File: HTMLElement.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public String getLocalName() {
    final DomNode domNode = getDomNodeOrDie();
    if (domNode.getHtmlPageOrNull() != null) {
        final String prefix = domNode.getPrefix();
        if (prefix != null) {
            // create string builder only if needed (performance)
            final StringBuilder localName = new StringBuilder(prefix.toLowerCase(Locale.ROOT))
                .append(':')
                .append(domNode.getLocalName().toLowerCase(Locale.ROOT));
            return localName.toString();
        }
        return domNode.getLocalName().toLowerCase(Locale.ROOT);
    }
    return domNode.getLocalName();
}
 
Example 3
Source File: Document.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the value of the {@code forms} property.
 * @return the value of the {@code forms} property
 */
@JsxGetter({CHROME, IE})
public Object getForms() {
    return new HTMLCollection(getDomNodeOrDie(), false) {
        @Override
        protected boolean isMatching(final DomNode node) {
            return node instanceof HtmlForm && node.getPrefix() == null;
        }

        @Override
        public Object call(final Context cx, final Scriptable scope,
                final Scriptable thisObj, final Object[] args) {
            if (getBrowserVersion().hasFeature(JS_DOCUMENT_FORMS_FUNCTION_SUPPORTED)) {
                return super.call(cx, scope, thisObj, args);
            }
            throw Context.reportRuntimeError("TypeError: document.forms is not a function");
        }
    };
}
 
Example 4
Source File: HTMLElement.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public String getLocalName() {
    final DomNode domNode = getDomNodeOrDie();
    if (domNode.getHtmlPageOrNull() != null) {
        final String prefix = domNode.getPrefix();
        if (prefix != null) {
            // create string builder only if needed (performance)
            final StringBuilder localName = new StringBuilder(prefix.toLowerCase(Locale.ROOT));
            localName.append(':');
            localName.append(domNode.getLocalName().toLowerCase(Locale.ROOT));
            return localName.toString();
        }
        return domNode.getLocalName().toLowerCase(Locale.ROOT);
    }
    return domNode.getLocalName();
}
 
Example 5
Source File: XMLDOMNode.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the namespace prefix.
 * @return the namespace prefix
 */
@JsxGetter
public String getPrefix() {
    final DomNode domNode = getDomNodeOrDie();
    final String prefix = domNode.getPrefix();
    if (prefix == null || domNode.getHtmlPageOrNull() != null) {
        return "";
    }
    return prefix;
}
 
Example 6
Source File: XMLDOMNode.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the namespace prefix.
 * @return the namespace prefix
 */
@JsxGetter
public String getPrefix() {
    final DomNode domNode = getDomNodeOrDie();
    final String prefix = domNode.getPrefix();
    if (prefix == null || domNode.getHtmlPageOrNull() != null) {
        return "";
    }
    return prefix;
}
 
Example 7
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 8
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('>');
    }
}