use of com.helger.xml.microdom.MicroText in project ph-commons by phax.
the class MicroHelper method convertToMicroNode.
@Nonnull
public static IMicroNode convertToMicroNode(@Nonnull final Node aNode) {
ValueEnforcer.notNull(aNode, "Node");
final IMicroNode ret;
final short nNodeType = aNode.getNodeType();
switch(nNodeType) {
case Node.DOCUMENT_NODE:
{
ret = new MicroDocument();
break;
}
case Node.DOCUMENT_TYPE_NODE:
{
final DocumentType aDT = (DocumentType) aNode;
// inline DTDs are not supported yet
// aDT.getEntities ();
ret = new MicroDocumentType(aDT.getName(), aDT.getPublicId(), aDT.getSystemId());
break;
}
case Node.ELEMENT_NODE:
{
final Element aElement = (Element) aNode;
final String sNamespaceURI = aElement.getNamespaceURI();
final IMicroElement eElement = sNamespaceURI != null ? new MicroElement(sNamespaceURI, aElement.getLocalName()) : new MicroElement(aElement.getTagName());
XMLHelper.forAllAttributes(aElement, aAttr -> {
final String sAttrNamespaceURI = aAttr.getNamespaceURI();
if (sAttrNamespaceURI != null) {
// Ignore all "xmlns" attributes (special namespace URI!)
if (!XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(sAttrNamespaceURI))
eElement.setAttribute(sAttrNamespaceURI, aAttr.getLocalName(), aAttr.getValue());
} else
eElement.setAttribute(aAttr.getName(), aAttr.getValue());
});
ret = eElement;
break;
}
case Node.CDATA_SECTION_NODE:
ret = new MicroCDATA(aNode.getNodeValue());
break;
case Node.TEXT_NODE:
ret = new MicroText(aNode.getNodeValue());
break;
case Node.COMMENT_NODE:
ret = new MicroComment(aNode.getNodeValue());
break;
case Node.ENTITY_REFERENCE_NODE:
ret = new MicroEntityReference(aNode.getNodeValue());
break;
case Node.PROCESSING_INSTRUCTION_NODE:
final ProcessingInstruction aPI = (ProcessingInstruction) aNode;
ret = new MicroProcessingInstruction(aPI.getTarget(), aPI.getData());
break;
case Node.ATTRIBUTE_NODE:
throw new IllegalArgumentException("Unknown/unsupported node type: ATTRIBUTE_NODE");
case Node.ENTITY_NODE:
throw new IllegalArgumentException("Unknown/unsupported node type: ENTITY_NODE");
case Node.DOCUMENT_FRAGMENT_NODE:
throw new IllegalArgumentException("Unknown/unsupported node type: DOCUMENT_FRAGMENT_NODE");
case Node.NOTATION_NODE:
throw new IllegalArgumentException("Unknown/unsupported node type: NOTATION_NODE");
default:
throw new IllegalArgumentException("Unknown/unsupported node type: " + nNodeType);
}
// handle children recursively (works for different node types)
XMLHelper.iterateChildren(aNode, x -> ret.appendChild(convertToMicroNode(x)));
return ret;
}
Aggregations