use of com.gargoylesoftware.htmlunit.html.DomProcessingInstruction in project htmlunit by HtmlUnit.
the class XmlUtils method createFrom.
private static DomNode createFrom(final SgmlPage page, final Node source, final boolean handleXHTMLAsHTML, final Map<Integer, List<String>> attributesOrderMap) {
if (source.getNodeType() == Node.TEXT_NODE) {
return new DomText(page, source.getNodeValue());
}
if (source.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) {
return new DomProcessingInstruction(page, source.getNodeName(), source.getNodeValue());
}
if (source.getNodeType() == Node.COMMENT_NODE) {
return new DomComment(page, source.getNodeValue());
}
if (source.getNodeType() == Node.DOCUMENT_TYPE_NODE) {
final DocumentType documentType = (DocumentType) source;
return new DomDocumentType(page, documentType.getName(), documentType.getPublicId(), documentType.getSystemId());
}
final String ns = source.getNamespaceURI();
String localName = source.getLocalName();
if (handleXHTMLAsHTML && Html.XHTML_NAMESPACE.equals(ns)) {
final ElementFactory factory = page.getWebClient().getPageCreator().getHtmlParser().getFactory(localName);
return factory.createElementNS(page, ns, localName, namedNodeMapToSaxAttributes(source.getAttributes(), attributesOrderMap, source));
}
final NamedNodeMap nodeAttributes = source.getAttributes();
if (page != null && page.isHtmlPage()) {
localName = localName.toUpperCase(Locale.ROOT);
}
final String qualifiedName;
if (source.getPrefix() == null) {
qualifiedName = localName;
} else {
qualifiedName = source.getPrefix() + ':' + localName;
}
final String namespaceURI = source.getNamespaceURI();
if (Html.SVG_NAMESPACE.equals(namespaceURI)) {
return page.getWebClient().getPageCreator().getHtmlParser().getSvgFactory().createElementNS(page, namespaceURI, qualifiedName, namedNodeMapToSaxAttributes(nodeAttributes, attributesOrderMap, source));
}
final Map<String, DomAttr> attributes = new LinkedHashMap<>();
for (int i = 0; i < nodeAttributes.getLength(); i++) {
final int orderedIndex = getIndex(nodeAttributes, attributesOrderMap, source, i);
final Attr attribute = (Attr) nodeAttributes.item(orderedIndex);
final String attributeNamespaceURI = attribute.getNamespaceURI();
final String attributeQualifiedName;
if (attribute.getPrefix() == null) {
attributeQualifiedName = attribute.getLocalName();
} else {
attributeQualifiedName = attribute.getPrefix() + ':' + attribute.getLocalName();
}
final String value = attribute.getNodeValue();
final boolean specified = attribute.getSpecified();
final DomAttr xmlAttribute = new DomAttr(page, attributeNamespaceURI, attributeQualifiedName, value, specified);
attributes.put(attribute.getNodeName(), xmlAttribute);
}
return new DomElement(namespaceURI, qualifiedName, page, attributes);
}
use of com.gargoylesoftware.htmlunit.html.DomProcessingInstruction in project htmlunit by HtmlUnit.
the class XMLDOMProcessingInstruction method setNodeValue.
/**
* Attempting to set the value of document fragments generates an error.
* @param newValue the new value to set
*/
@Override
public void setNodeValue(final String newValue) {
final DomProcessingInstruction domProcessingInstruction = getDomNodeOrDie();
if (XML_DECLARATION_TARGET.equalsIgnoreCase(domProcessingInstruction.getTarget())) {
throw Context.reportRuntimeError("This operation cannot be performed with a node of type XMLDECL.");
}
super.setNodeValue(newValue);
}
use of com.gargoylesoftware.htmlunit.html.DomProcessingInstruction in project htmlunit by HtmlUnit.
the class XMLDOMProcessingInstruction method setData.
/**
* Sets the content of the processing instruction, excluding the target.
* @param data the content of the processing instruction, excluding the target
*/
@JsxSetter
public void setData(final String data) {
if (data == null || "null".equals(data)) {
throw Context.reportRuntimeError("Type mismatch.");
}
final DomProcessingInstruction domProcessingInstruction = getDomNodeOrDie();
if (XML_DECLARATION_TARGET.equalsIgnoreCase(domProcessingInstruction.getTarget())) {
throw Context.reportRuntimeError("This operation cannot be performed with a node of type XMLDECL.");
}
domProcessingInstruction.setData(data);
}
use of com.gargoylesoftware.htmlunit.html.DomProcessingInstruction in project htmlunit by HtmlUnit.
the class XMLDOMProcessingInstruction method getAttributes.
/**
* Returns the list of attributes for this element.
* @return the list of attributes for this element
*/
@Override
public Object getAttributes() {
if (!attributesComputed_) {
final DomProcessingInstruction domProcessingInstruction = getDomNodeOrDie();
if (XML_DECLARATION_TARGET.equalsIgnoreCase(domProcessingInstruction.getTarget())) {
attributes_ = new XMLDOMNamedNodeMap(getDomNodeOrDie());
}
attributesComputed_ = true;
}
return attributes_;
}
use of com.gargoylesoftware.htmlunit.html.DomProcessingInstruction in project htmlunit by HtmlUnit.
the class XmlUtils method copy.
/**
* Copy all children from 'source' to 'dest', within the context of the specified page.
* @param page the page which the nodes belong to
* @param source the node to copy from
* @param dest the node to copy to
* @param handleXHTMLAsHTML if true elements from the XHTML namespace are handled as HTML elements instead of
* DOM elements
*/
private static void copy(final SgmlPage page, final Node source, final DomNode dest, final boolean handleXHTMLAsHTML, final Map<Integer, List<String>> attributesOrderMap) {
final NodeList nodeChildren = source.getChildNodes();
for (int i = 0; i < nodeChildren.getLength(); i++) {
final Node child = nodeChildren.item(i);
switch(child.getNodeType()) {
case Node.ELEMENT_NODE:
final DomNode childXml = createFrom(page, child, handleXHTMLAsHTML, attributesOrderMap);
dest.appendChild(childXml);
copy(page, child, childXml, handleXHTMLAsHTML, attributesOrderMap);
break;
case Node.TEXT_NODE:
dest.appendChild(new DomText(page, child.getNodeValue()));
break;
case Node.CDATA_SECTION_NODE:
dest.appendChild(new DomCDataSection(page, child.getNodeValue()));
break;
case Node.COMMENT_NODE:
dest.appendChild(new DomComment(page, child.getNodeValue()));
break;
case Node.PROCESSING_INSTRUCTION_NODE:
dest.appendChild(new DomProcessingInstruction(page, child.getNodeName(), child.getNodeValue()));
break;
default:
if (LOG.isWarnEnabled()) {
LOG.warn("NodeType " + child.getNodeType() + " (" + child.getNodeName() + ") is not yet supported.");
}
}
}
}
Aggregations