Search in sources :

Example 1 with MicroComment

use of com.helger.xml.microdom.MicroComment 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)

Example 2 with MicroComment

use of com.helger.xml.microdom.MicroComment in project ph-commons by phax.

the class MicroWriterTest method testGetXHTMLString.

@Test
public void testGetXHTMLString() {
    final IMicroDocument aDoc = MicroReader.readMicroXML(TEST_XML);
    _testGetNodeAsXHTMLString(aDoc);
    _testGetNodeAsXHTMLString(aDoc.getDocumentElement());
    _testGetNodeAsXHTMLString(new MicroDocument());
    _testGetNodeAsXHTMLString(new MicroElement("xyz"));
    _testGetNodeAsXHTMLString(new MicroContainer());
    _testGetNodeAsXHTMLString(new MicroComment("useless"));
    _testGetNodeAsXHTMLString(new MicroEntityReference("xyz"));
    try {
        _testGetNodeAsXHTMLString(null);
        fail();
    } catch (final NullPointerException ex) {
    }
}
Also used : IMicroDocument(com.helger.xml.microdom.IMicroDocument) MicroDocument(com.helger.xml.microdom.MicroDocument) IMicroElement(com.helger.xml.microdom.IMicroElement) MicroElement(com.helger.xml.microdom.MicroElement) MicroContainer(com.helger.xml.microdom.MicroContainer) MicroComment(com.helger.xml.microdom.MicroComment) MicroEntityReference(com.helger.xml.microdom.MicroEntityReference) IMicroDocument(com.helger.xml.microdom.IMicroDocument) Test(org.junit.Test)

Example 3 with MicroComment

use of com.helger.xml.microdom.MicroComment in project ph-commons by phax.

the class AbstractSimpleDAO method modifyWriteData.

/**
 * Modify the created document by e.g. adding some comment or digital
 * signature or whatsoever.
 *
 * @param aDoc
 *        The created non-<code>null</code> document.
 */
@OverrideOnDemand
@MustBeLocked(ELockType.WRITE)
protected void modifyWriteData(@Nonnull final IMicroDocument aDoc) {
    final IMicroComment aComment = new MicroComment("This file was generated automatically - do NOT modify!\n" + "Written at " + PDTToString.getAsString(ZonedDateTime.now(Clock.systemUTC()), Locale.US));
    final IMicroElement eRoot = aDoc.getDocumentElement();
    // Add a small comment
    if (eRoot != null)
        aDoc.insertBefore(aComment, eRoot);
    else
        aDoc.appendChild(aComment);
}
Also used : MicroComment(com.helger.xml.microdom.MicroComment) IMicroComment(com.helger.xml.microdom.IMicroComment) IMicroElement(com.helger.xml.microdom.IMicroElement) IMicroComment(com.helger.xml.microdom.IMicroComment) MustBeLocked(com.helger.commons.annotation.MustBeLocked) OverrideOnDemand(com.helger.commons.annotation.OverrideOnDemand)

Example 4 with MicroComment

use of com.helger.xml.microdom.MicroComment in project ph-commons by phax.

the class AbstractWALDAO method modifyWriteData.

/**
 * Modify the created document by e.g. adding some comment or digital
 * signature or whatsoever.
 *
 * @param aDoc
 *        The created non-<code>null</code> document.
 */
@OverrideOnDemand
@MustBeLocked(ELockType.WRITE)
protected void modifyWriteData(@Nonnull final IMicroDocument aDoc) {
    final IMicroComment aComment = new MicroComment("This file was generated automatically - do NOT modify!\n" + "Written at " + PDTToString.getAsString(ZonedDateTime.now(Clock.systemUTC()), Locale.US));
    final IMicroElement eRoot = aDoc.getDocumentElement();
    // Add a small comment
    if (eRoot != null)
        aDoc.insertBefore(aComment, eRoot);
    else
        aDoc.appendChild(aComment);
}
Also used : IMicroComment(com.helger.xml.microdom.IMicroComment) MicroComment(com.helger.xml.microdom.MicroComment) IMicroElement(com.helger.xml.microdom.IMicroElement) IMicroComment(com.helger.xml.microdom.IMicroComment) MustBeLocked(com.helger.commons.annotation.MustBeLocked) OverrideOnDemand(com.helger.commons.annotation.OverrideOnDemand)

Example 5 with MicroComment

use of com.helger.xml.microdom.MicroComment in project ph-commons by phax.

the class MicroWriterTest method testGetXMLString.

@Test
public void testGetXMLString() {
    final IMicroDocument aDoc = MicroReader.readMicroXML(TEST_XML);
    _testGetNodeAsXMLString(aDoc);
    _testGetNodeAsXMLString(aDoc.getDocumentElement());
    _testGetNodeAsXMLString(new MicroElement("xyz"));
    _testGetNodeAsXMLString(new MicroElement("xyz").setAttribute("nsuri", "attr", "1"));
    _testGetNodeAsXMLString(new MicroContainer());
    _testGetNodeAsXMLString(new MicroContainer(new MicroComment("useless"), new MicroElement("xyz")));
    _testGetNodeAsXMLString(new MicroComment("useless"));
    _testGetNodeAsXMLString(new MicroEntityReference("xyz"));
    _testGetNodeAsXMLString(new MicroCDATA("xyz"));
    try {
        _testGetNodeAsXMLString(null);
        fail();
    } catch (final NullPointerException ex) {
    }
    try {
        MicroWriter.getNodeAsString(null, XMLWriterSettings.DEFAULT_XML_SETTINGS);
        fail();
    } catch (final NullPointerException ex) {
    }
    try {
        MicroWriter.getNodeAsString(aDoc, null);
        fail();
    } catch (final NullPointerException ex) {
    }
}
Also used : IMicroElement(com.helger.xml.microdom.IMicroElement) MicroElement(com.helger.xml.microdom.MicroElement) MicroContainer(com.helger.xml.microdom.MicroContainer) MicroCDATA(com.helger.xml.microdom.MicroCDATA) MicroComment(com.helger.xml.microdom.MicroComment) MicroEntityReference(com.helger.xml.microdom.MicroEntityReference) IMicroDocument(com.helger.xml.microdom.IMicroDocument) Test(org.junit.Test)

Aggregations

IMicroElement (com.helger.xml.microdom.IMicroElement)5 MicroComment (com.helger.xml.microdom.MicroComment)5 IMicroDocument (com.helger.xml.microdom.IMicroDocument)3 MicroContainer (com.helger.xml.microdom.MicroContainer)3 MicroElement (com.helger.xml.microdom.MicroElement)3 MicroEntityReference (com.helger.xml.microdom.MicroEntityReference)3 MustBeLocked (com.helger.commons.annotation.MustBeLocked)2 OverrideOnDemand (com.helger.commons.annotation.OverrideOnDemand)2 IMicroComment (com.helger.xml.microdom.IMicroComment)2 MicroCDATA (com.helger.xml.microdom.MicroCDATA)2 MicroDocument (com.helger.xml.microdom.MicroDocument)2 Test (org.junit.Test)2 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 IMicroNode (com.helger.xml.microdom.IMicroNode)1 MicroDocumentType (com.helger.xml.microdom.MicroDocumentType)1