Java Code Examples for com.gargoylesoftware.htmlunit.html.DomNode#getNodeType()
The following examples show how to use
com.gargoylesoftware.htmlunit.html.DomNode#getNodeType() .
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: XPathHelper.java From htmlunit with Apache License 2.0 | 6 votes |
/** * Evaluates an XPath expression to an XObject. * @param contextNode the node to start searching from * @param str a valid XPath string * @param a prefix resolver to use for resolving namespace prefixes, or null * @return an XObject, which can be used to obtain a string, number, nodelist, etc (should never be {@code null}) * @throws TransformerException if a syntax or other error occurs */ private static XObject evaluateXPath(final DomNode contextNode, final String str, final PrefixResolver prefixResolver) throws TransformerException { final XPathContext xpathSupport = new XPathContext(); final Node xpathExpressionContext; if (contextNode.getNodeType() == Node.DOCUMENT_NODE) { xpathExpressionContext = ((Document) contextNode).getDocumentElement(); } else { xpathExpressionContext = contextNode; } PrefixResolver resolver = prefixResolver; if (resolver == null) { resolver = new HtmlUnitPrefixResolver(xpathExpressionContext); } final boolean caseSensitive = contextNode.getPage().hasCaseSensitiveTagNames(); final XPathAdapter xpath = new XPathAdapter(str, null, resolver, null, caseSensitive); final int ctxtNode = xpathSupport.getDTMHandleFromNode(contextNode); return xpath.execute(xpathSupport, ctxtNode, prefixResolver); }
Example 2
Source File: XPathUtils.java From HtmlUnit-Android with Apache License 2.0 | 6 votes |
/** * Evaluates an XPath expression to an XObject. * @param contextNode the node to start searching from * @param str a valid XPath string * @param a prefix resolver to use for resolving namespace prefixes, or null * @return an XObject, which can be used to obtain a string, number, nodelist, etc (should never be {@code null}) * @throws TransformerException if a syntax or other error occurs */ private static XObject evaluateXPath(final DomNode contextNode, final String str, final PrefixResolver prefixResolver) throws TransformerException { final XPathContext xpathSupport = new XPathContext(); final Node xpathExpressionContext; if (contextNode.getNodeType() == Node.DOCUMENT_NODE) { xpathExpressionContext = ((Document) contextNode).getDocumentElement(); } else { xpathExpressionContext = contextNode; } PrefixResolver resolver = prefixResolver; if (resolver == null) { resolver = new HtmlUnitPrefixResolver(xpathExpressionContext); } final boolean caseSensitive = contextNode.getPage().hasCaseSensitiveTagNames(); final boolean attributeCaseSensitive = caseSensitive || contextNode.getPage().getWebClient() .getBrowserVersion().hasFeature(XPATH_ATTRIBUTE_CASE_SENSITIVE); final XPathAdapter xpath = new XPathAdapter(str, null, resolver, null, caseSensitive, attributeCaseSensitive); final int ctxtNode = xpathSupport.getDTMHandleFromNode(contextNode); return xpath.execute(xpathSupport, ctxtNode, prefixResolver); }
Example 3
Source File: XMLDOMElement.java From htmlunit with Apache License 2.0 | 5 votes |
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 4
Source File: XMLDOMElement.java From HtmlUnit-Android with Apache License 2.0 | 5 votes |
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 5
Source File: HtmlUnitNekoHtmlParser.java From htmlunit with Apache License 2.0 | 4 votes |
/** * Parses the HTML content from the given string into an object tree representation. * * @param parent where the new parsed nodes will be added to * @param context the context to build the fragment context stack * @param source the (X)HTML to be parsed * @throws SAXException if a SAX error occurs * @throws IOException if an IO error occurs */ @Override public void parseFragment(final DomNode parent, final DomNode context, final String source) throws SAXException, IOException { final Page page = parent.getPage(); if (!(page instanceof HtmlPage)) { return; } final HtmlPage htmlPage = (HtmlPage) page; final URL url = htmlPage.getUrl(); final HtmlUnitNekoDOMBuilder domBuilder = new HtmlUnitNekoDOMBuilder(this, parent, url, source); domBuilder.setFeature("http://cyberneko.org/html/features/balance-tags/document-fragment", true); // build fragment context stack DomNode node = context; final List<QName> ancestors = new ArrayList<>(); while (node != null && node.getNodeType() != Node.DOCUMENT_NODE) { ancestors.add(0, new QName(null, node.getNodeName(), null, null)); node = node.getParentNode(); } if (ancestors.isEmpty() || !"html".equals(ancestors.get(0).localpart)) { ancestors.add(0, new QName(null, "html", null, null)); } if (ancestors.size() == 1 || !"body".equals(ancestors.get(1).localpart)) { ancestors.add(1, new QName(null, "body", null, null)); } domBuilder.setFeature(HTMLScanner.ALLOW_SELFCLOSING_TAGS, true); domBuilder.setProperty(HTMLTagBalancer.FRAGMENT_CONTEXT_STACK, ancestors.toArray(new QName[ancestors.size()])); final XMLInputSource in = new XMLInputSource(null, url.toString(), null, new StringReader(source), null); htmlPage.registerParsingStart(); htmlPage.registerSnippetParsingStart(); try { domBuilder.parse(in); } finally { htmlPage.registerParsingEnd(); htmlPage.registerSnippetParsingEnd(); } }
Example 6
Source File: XMLSerializer.java From htmlunit with Apache License 2.0 | 4 votes |
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 7
Source File: XMLDOMNode.java From htmlunit with Apache License 2.0 | 4 votes |
/** * Returns the XML Document Object Model (DOM) node type, which determines valid values and whether the node can * have child nodes. * @return the XML Document Object Model (DOM) node type */ @JsxGetter public short getNodeType() { final DomNode domNode = getDomNodeOrDie(); return domNode.getNodeType(); }
Example 8
Source File: XMLSerializer.java From HtmlUnit-Android with Apache License 2.0 | 4 votes |
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: XMLDOMNode.java From HtmlUnit-Android with Apache License 2.0 | 4 votes |
/** * Returns the XML Document Object Model (DOM) node type, which determines valid values and whether the node can * have child nodes. * @return the XML Document Object Model (DOM) node type */ @JsxGetter public short getNodeType() { final DomNode domNode = getDomNodeOrDie(); return domNode.getNodeType(); }