Search in sources :

Example 1 with MicroText

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;
}
Also used : ProcessingInstruction(org.w3c.dom.ProcessingInstruction) XMLHelper(com.helger.xml.XMLHelper) MicroComment(com.helger.xml.microdom.MicroComment) IMicroDocument(com.helger.xml.microdom.IMicroDocument) MicroDocument(com.helger.xml.microdom.MicroDocument) MicroContainer(com.helger.xml.microdom.MicroContainer) MicroDocumentType(com.helger.xml.microdom.MicroDocumentType) IMicroElement(com.helger.xml.microdom.IMicroElement) Node(org.w3c.dom.Node) XMLConstants(javax.xml.XMLConstants) ReturnsMutableCopy(com.helger.commons.annotation.ReturnsMutableCopy) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) ArrayHelper(com.helger.commons.collection.ArrayHelper) MicroProcessingInstruction(com.helger.xml.microdom.MicroProcessingInstruction) IMicroContainer(com.helger.xml.microdom.IMicroContainer) MicroElement(com.helger.xml.microdom.MicroElement) DocumentType(org.w3c.dom.DocumentType) ValueEnforcer(com.helger.commons.ValueEnforcer) IMicroNode(com.helger.xml.microdom.IMicroNode) Element(org.w3c.dom.Element) MicroCDATA(com.helger.xml.microdom.MicroCDATA) MicroEntityReference(com.helger.xml.microdom.MicroEntityReference) Immutable(javax.annotation.concurrent.Immutable) MicroText(com.helger.xml.microdom.MicroText) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) PresentForCodeCoverage(com.helger.commons.annotation.PresentForCodeCoverage) IMicroElement(com.helger.xml.microdom.IMicroElement) MicroElement(com.helger.xml.microdom.MicroElement) MicroComment(com.helger.xml.microdom.MicroComment) IMicroElement(com.helger.xml.microdom.IMicroElement) MicroElement(com.helger.xml.microdom.MicroElement) Element(org.w3c.dom.Element) MicroProcessingInstruction(com.helger.xml.microdom.MicroProcessingInstruction) IMicroNode(com.helger.xml.microdom.IMicroNode) MicroDocumentType(com.helger.xml.microdom.MicroDocumentType) DocumentType(org.w3c.dom.DocumentType) MicroEntityReference(com.helger.xml.microdom.MicroEntityReference) MicroDocumentType(com.helger.xml.microdom.MicroDocumentType) IMicroDocument(com.helger.xml.microdom.IMicroDocument) MicroDocument(com.helger.xml.microdom.MicroDocument) MicroCDATA(com.helger.xml.microdom.MicroCDATA) IMicroElement(com.helger.xml.microdom.IMicroElement) MicroText(com.helger.xml.microdom.MicroText) ProcessingInstruction(org.w3c.dom.ProcessingInstruction) MicroProcessingInstruction(com.helger.xml.microdom.MicroProcessingInstruction) Nonnull(javax.annotation.Nonnull)

Aggregations

ValueEnforcer (com.helger.commons.ValueEnforcer)1 PresentForCodeCoverage (com.helger.commons.annotation.PresentForCodeCoverage)1 ReturnsMutableCopy (com.helger.commons.annotation.ReturnsMutableCopy)1 ArrayHelper (com.helger.commons.collection.ArrayHelper)1 XMLHelper (com.helger.xml.XMLHelper)1 IMicroContainer (com.helger.xml.microdom.IMicroContainer)1 IMicroDocument (com.helger.xml.microdom.IMicroDocument)1 IMicroElement (com.helger.xml.microdom.IMicroElement)1 IMicroNode (com.helger.xml.microdom.IMicroNode)1 MicroCDATA (com.helger.xml.microdom.MicroCDATA)1 MicroComment (com.helger.xml.microdom.MicroComment)1 MicroContainer (com.helger.xml.microdom.MicroContainer)1 MicroDocument (com.helger.xml.microdom.MicroDocument)1 MicroDocumentType (com.helger.xml.microdom.MicroDocumentType)1 MicroElement (com.helger.xml.microdom.MicroElement)1 MicroEntityReference (com.helger.xml.microdom.MicroEntityReference)1 MicroProcessingInstruction (com.helger.xml.microdom.MicroProcessingInstruction)1 MicroText (com.helger.xml.microdom.MicroText)1 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 Nonnull (javax.annotation.Nonnull)1