Search in sources :

Example 1 with EXMLVersion

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

the class MainFindInvalidXMLChars method main.

public static void main(final String[] args) {
    final EXMLVersion eXMLVersion = EXMLVersion.XML_10;
    final int nMax = Character.MAX_VALUE + 1;
    final ICommonsList<Integer> aForbiddenE1 = new CommonsArrayList<>();
    for (int i = 0; i < nMax; ++i) {
        final Document aDoc = XMLFactory.newDocument(eXMLVersion);
        try {
            aDoc.appendChild(aDoc.createElement(Character.toString((char) i)));
            final NonBlockingStringWriter aSW = new NonBlockingStringWriter();
            XMLTransformerFactory.newTransformer().transform(new DOMSource(aDoc), new StreamResult(aSW));
            DOMReader.readXMLDOM(aSW.getAsString());
        } catch (final Exception ex) {
            aForbiddenE1.add(Integer.valueOf(i));
        }
    }
    final ICommonsList<Integer> aForbiddenE2 = new CommonsArrayList<>();
    for (int i = 0; i < nMax; ++i) {
        final Document aDoc = XMLFactory.newDocument(eXMLVersion);
        try {
            aDoc.appendChild(aDoc.createElement("a" + Character.toString((char) i)));
            final NonBlockingStringWriter aSW = new NonBlockingStringWriter();
            XMLTransformerFactory.newTransformer().transform(new DOMSource(aDoc), new StreamResult(aSW));
            DOMReader.readXMLDOM(aSW.getAsString());
        } catch (final Exception ex) {
            aForbiddenE2.add(Integer.valueOf(i));
        }
    }
    final ICommonsList<Integer> aForbiddenAN1 = new CommonsArrayList<>();
    for (int i = 0; i < nMax; ++i) {
        final Document aDoc = XMLFactory.newDocument(eXMLVersion);
        try {
            final Element aElement = (Element) aDoc.appendChild(aDoc.createElement("abc"));
            aElement.setAttribute(Character.toString((char) i), "xyz");
            final NonBlockingStringWriter aSW = new NonBlockingStringWriter();
            XMLTransformerFactory.newTransformer().transform(new DOMSource(aDoc), new StreamResult(aSW));
            DOMReader.readXMLDOM(aSW.getAsString());
        } catch (final Exception ex) {
            aForbiddenAN1.add(Integer.valueOf(i));
        }
    }
    final ICommonsList<Integer> aForbiddenAN2 = new CommonsArrayList<>();
    for (int i = 0; i < nMax; ++i) {
        final Document aDoc = XMLFactory.newDocument(eXMLVersion);
        try {
            final Element aElement = (Element) aDoc.appendChild(aDoc.createElement("abc"));
            aElement.setAttribute("a" + Character.toString((char) i), "xyz");
            final NonBlockingStringWriter aSW = new NonBlockingStringWriter();
            XMLTransformerFactory.newTransformer().transform(new DOMSource(aDoc), new StreamResult(aSW));
            DOMReader.readXMLDOM(aSW.getAsString());
        } catch (final Exception ex) {
            aForbiddenAN2.add(Integer.valueOf(i));
        }
    }
    final ICommonsList<Integer> aForbiddenAV = new CommonsArrayList<>();
    for (int i = 0; i < nMax; ++i) {
        final Document aDoc = XMLFactory.newDocument(eXMLVersion);
        try {
            final Element aElement = (Element) aDoc.appendChild(aDoc.createElement("abc"));
            aElement.setAttribute("a", Character.toString((char) i));
            final NonBlockingStringWriter aSW = new NonBlockingStringWriter();
            XMLTransformerFactory.newTransformer().transform(new DOMSource(aDoc), new StreamResult(aSW));
            DOMReader.readXMLDOM(aSW.getAsString());
        } catch (final Exception ex) {
            aForbiddenAV.add(Integer.valueOf(i));
        }
    }
    final ICommonsList<Integer> aForbiddenTV = new CommonsArrayList<>();
    for (int i = 0; i < nMax; ++i) {
        final Document aDoc = XMLFactory.newDocument(eXMLVersion);
        try {
            final Element aElement = (Element) aDoc.appendChild(aDoc.createElement("abc"));
            aElement.appendChild(aDoc.createTextNode(Character.toString((char) i)));
            final NonBlockingStringWriter aSW = new NonBlockingStringWriter();
            XMLTransformerFactory.newTransformer().transform(new DOMSource(aDoc), new StreamResult(aSW));
            DOMReader.readXMLDOM(aSW.getAsString());
        } catch (final Exception ex) {
            aForbiddenTV.add(Integer.valueOf(i));
        }
    }
    final ICommonsList<Integer> aForbiddenCV = new CommonsArrayList<>();
    for (int i = 0; i < nMax; ++i) {
        final Document aDoc = XMLFactory.newDocument(eXMLVersion);
        try {
            final Element aElement = (Element) aDoc.appendChild(aDoc.createElement("abc"));
            aElement.appendChild(aDoc.createCDATASection(Character.toString((char) i)));
            final NonBlockingStringWriter aSW = new NonBlockingStringWriter();
            XMLTransformerFactory.newTransformer().transform(new DOMSource(aDoc), new StreamResult(aSW));
            DOMReader.readXMLDOM(aSW.getAsString());
        } catch (final Exception ex) {
            aForbiddenCV.add(Integer.valueOf(i));
        }
    }
    LOGGER.info("Forbidden Element Name Start:       " + _getFormatted(aForbiddenE1));
    LOGGER.info("Forbidden Element Name InBetween:   " + _getFormatted(aForbiddenE2));
    LOGGER.info("Forbidden Attribute Name Start:     " + _getFormatted(aForbiddenAN1));
    LOGGER.info("Forbidden Attribute Name InBetween: " + _getFormatted(aForbiddenAN2));
    LOGGER.info("Forbidden Attribute Value: " + _getFormatted(aForbiddenAV));
    LOGGER.info("Forbidden Text Value:      " + _getFormatted(aForbiddenTV));
    LOGGER.info("Forbidden CDATA Value:     " + _getFormatted(aForbiddenCV));
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) StreamResult(javax.xml.transform.stream.StreamResult) Element(org.w3c.dom.Element) EXMLVersion(com.helger.xml.EXMLVersion) Document(org.w3c.dom.Document) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) NonBlockingStringWriter(com.helger.commons.io.stream.NonBlockingStringWriter)

Example 2 with EXMLVersion

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

the class XMLSerializer method _writeDocument.

private void _writeDocument(@Nonnull final XMLEmitter aXMLWriter, @Nonnull final Document aDocument) {
    if (m_aSettings.getSerializeXMLDeclaration().isEmit()) {
        String sXMLVersion = null;
        ETriState eDocumentStandalone = ETriState.UNDEFINED;
        try {
            sXMLVersion = aDocument.getXmlVersion();
            eDocumentStandalone = ETriState.valueOf(aDocument.getXmlStandalone());
        } catch (final LinkageError | DOMException ex) {
        // LinkageError Happens e.g. in dom4j 1.6.1:
        // AbstractMethodError: getXmlVersion and getXmlStandalone
        // DOMException in dom4j 2.0.3
        }
        final EXMLVersion eXMLVersion = EXMLVersion.getFromVersionOrDefault(sXMLVersion, m_aSettings.getXMLVersion());
        aXMLWriter.onXMLDeclaration(eXMLVersion, m_aSettings.getCharset().name(), m_aSettings.getSerializeXMLDeclaration().isEmitStandalone() ? eDocumentStandalone : ETriState.UNDEFINED, m_aSettings.isNewLineAfterXMLDeclaration());
    }
    _writeNodeList(aXMLWriter, aDocument, aDocument.getChildNodes());
}
Also used : DOMException(org.w3c.dom.DOMException) ETriState(com.helger.commons.state.ETriState) EXMLVersion(com.helger.xml.EXMLVersion)

Example 3 with EXMLVersion

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

the class MainFindMaskedXMLChars method main.

/**
 * XML 1.0:
 *
 * <pre>
 * Masked Element Name Start:       false
 * Masked Element Name InBetween:   false
 * Masked Attribute Name Start:     false
 * Masked Attribute Name InBetween: false
 * Masked Attribute Value: (c >= 0x9 && c <= 0xa) || (c == 0xd) || (c == 0x22) || (c == 0x26) || (c == 0x3c)
 * Masked Text Value:      (c == 0xd) || (c == 0x26) || (c == 0x3c) || (c == 0x3e) || (c >= 0x7f && c <= 0x9f)
 * Masked CDATA Value:     false
 * </pre>
 *
 * XML 1.1:
 *
 * <pre>
 * Masked Element Name Start:       false
 * Masked Element Name InBetween:   false
 * Masked Attribute Name Start:     false
 * Masked Attribute Name InBetween: false
 * Masked Attribute Value: (c >= 0x1 && c <= 0x1f) || (c == 0x22) || (c == 0x26) || (c == 0x3c)
 * Masked Text Value:      (c >= 0x1 && c <= 0x8) || (c >= 0xb && c <= 0x1f) || (c == 0x26) || (c == 0x3c) || (c == 0x3e) || (c >= 0x7f && c <= 0x9f)
 * Masked CDATA Value:     (c >= 0x1 && c <= 0x8) || (c >= 0xb && c <= 0xc) || (c >= 0xe && c <= 0x1f)
 * </pre>
 *
 * @param args
 *        main args
 * @throws Exception
 *         in case of err
 */
public static void main(final String[] args) throws Exception {
    final EXMLVersion eXMLVersion = EXMLVersion.XML_11;
    final EXMLSerializeVersion eXMLSerializeVersion = EXMLSerializeVersion.getFromXMLVersionOrThrow(eXMLVersion);
    final int nMax = Character.MAX_VALUE + 1;
    final ICommonsList<Integer> aMaskedE1 = new CommonsArrayList<>();
    for (int i = 0; i < nMax; ++i) if (!XMLCharHelper.isInvalidXMLNameStartChar(eXMLSerializeVersion, (char) i)) {
        final Document aDoc = XMLFactory.newDocument(eXMLVersion);
        aDoc.appendChild(aDoc.createElement(Character.toString((char) i)));
        final NonBlockingStringWriter aSW = new NonBlockingStringWriter();
        XMLTransformerFactory.newTransformer().transform(new DOMSource(aDoc), new StreamResult(aSW));
        if (_containsER(aSW.getAsString(), i))
            aMaskedE1.add(Integer.valueOf(i));
    }
    final ICommonsList<Integer> aMaskedE2 = new CommonsArrayList<>();
    for (int i = 0; i < nMax; ++i) if (!XMLCharHelper.isInvalidXMLNameChar(eXMLSerializeVersion, (char) i)) {
        final Document aDoc = XMLFactory.newDocument(eXMLVersion);
        aDoc.appendChild(aDoc.createElement("a" + Character.toString((char) i)));
        final NonBlockingStringWriter aSW = new NonBlockingStringWriter();
        XMLTransformerFactory.newTransformer().transform(new DOMSource(aDoc), new StreamResult(aSW));
        if (_containsER(aSW.getAsString(), i))
            aMaskedE2.add(Integer.valueOf(i));
    }
    final ICommonsList<Integer> aMaskedAN1 = new CommonsArrayList<>();
    for (int i = 0; i < nMax; ++i) if (!XMLCharHelper.isInvalidXMLNameStartChar(eXMLSerializeVersion, (char) i)) {
        final Document aDoc = XMLFactory.newDocument(eXMLVersion);
        final Element aElement = (Element) aDoc.appendChild(aDoc.createElement("abc"));
        aElement.setAttribute(Character.toString((char) i), "xyz");
        final NonBlockingStringWriter aSW = new NonBlockingStringWriter();
        XMLTransformerFactory.newTransformer().transform(new DOMSource(aDoc), new StreamResult(aSW));
        if (_containsER(aSW.getAsString(), i))
            aMaskedAN1.add(Integer.valueOf(i));
    }
    final ICommonsList<Integer> aMaskedAN2 = new CommonsArrayList<>();
    for (int i = 0; i < nMax; ++i) if (!XMLCharHelper.isInvalidXMLNameChar(eXMLSerializeVersion, (char) i)) {
        final Document aDoc = XMLFactory.newDocument(eXMLVersion);
        final Element aElement = (Element) aDoc.appendChild(aDoc.createElement("abc"));
        aElement.setAttribute("a" + Character.toString((char) i), "xyz");
        final NonBlockingStringWriter aSW = new NonBlockingStringWriter();
        XMLTransformerFactory.newTransformer().transform(new DOMSource(aDoc), new StreamResult(aSW));
        if (_containsER(aSW.getAsString(), i))
            aMaskedAN2.add(Integer.valueOf(i));
    }
    final ICommonsList<Integer> aMaskedAV = new CommonsArrayList<>();
    for (int i = 0; i < nMax; ++i) if (!XMLCharHelper.isInvalidXMLAttributeValueChar(eXMLSerializeVersion, (char) i)) {
        final Document aDoc = XMLFactory.newDocument(eXMLVersion);
        final Element aElement = (Element) aDoc.appendChild(aDoc.createElement("abc"));
        aElement.setAttribute("a", Character.toString((char) i));
        final NonBlockingStringWriter aSW = new NonBlockingStringWriter();
        XMLTransformerFactory.newTransformer().transform(new DOMSource(aDoc), new StreamResult(aSW));
        if (_containsER(aSW.getAsString(), i))
            aMaskedAV.add(Integer.valueOf(i));
    }
    final ICommonsList<Integer> aMaskedTV = new CommonsArrayList<>();
    for (int i = 0; i < nMax; ++i) if (!XMLCharHelper.isInvalidXMLTextChar(eXMLSerializeVersion, (char) i)) {
        final Document aDoc = XMLFactory.newDocument(eXMLVersion);
        final Element aElement = (Element) aDoc.appendChild(aDoc.createElement("abc"));
        aElement.appendChild(aDoc.createTextNode(Character.toString((char) i)));
        final NonBlockingStringWriter aSW = new NonBlockingStringWriter();
        XMLTransformerFactory.newTransformer().transform(new DOMSource(aDoc), new StreamResult(aSW));
        if (_containsER(aSW.getAsString(), i))
            aMaskedTV.add(Integer.valueOf(i));
    }
    final ICommonsList<Integer> aMaskedCV = new CommonsArrayList<>();
    for (int i = 0; i < nMax; ++i) {
        final Document aDoc = XMLFactory.newDocument(eXMLVersion);
        final Element aElement = (Element) aDoc.appendChild(aDoc.createElement("abc"));
        aElement.appendChild(aDoc.createCDATASection(Character.toString((char) i)));
        final NonBlockingStringWriter aSW = new NonBlockingStringWriter();
        XMLTransformerFactory.newTransformer().transform(new DOMSource(aDoc), new StreamResult(aSW));
        final String sXML = aSW.getAsString();
        if (sXML.indexOf("<[CDATA[") >= 0 && _containsER(sXML, i))
            aMaskedCV.add(Integer.valueOf(i));
    }
    LOGGER.info("Masked Element Name Start:       " + _getFormatted(aMaskedE1));
    LOGGER.info("Masked Element Name InBetween:   " + _getFormatted(aMaskedE2));
    LOGGER.info("Masked Attribute Name Start:     " + _getFormatted(aMaskedAN1));
    LOGGER.info("Masked Attribute Name InBetween: " + _getFormatted(aMaskedAN2));
    LOGGER.info("Masked Attribute Value: " + _getFormatted(aMaskedAV));
    LOGGER.info("Masked Text Value:      " + _getFormatted(aMaskedTV));
    LOGGER.info("Masked CDATA Value:     " + _getFormatted(aMaskedCV));
}
Also used : EXMLSerializeVersion(com.helger.xml.serialize.write.EXMLSerializeVersion) DOMSource(javax.xml.transform.dom.DOMSource) StreamResult(javax.xml.transform.stream.StreamResult) Element(org.w3c.dom.Element) EXMLVersion(com.helger.xml.EXMLVersion) Document(org.w3c.dom.Document) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) NonBlockingStringWriter(com.helger.commons.io.stream.NonBlockingStringWriter)

Example 4 with EXMLVersion

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

the class XMLTransformerFactoryTest method testSpecialChars.

@Test
public void testSpecialChars() throws Exception {
    final EXMLVersion eXMLVersion = EXMLVersion.XML_10;
    final EXMLSerializeVersion eXMLSerializeVersion = EXMLSerializeVersion.getFromXMLVersionOrThrow(eXMLVersion);
    final StringBuilder aAttrVal = new StringBuilder();
    final StringBuilder aText = new StringBuilder();
    for (char i = 0; i < 256; ++i) {
        if (!XMLCharHelper.isInvalidXMLAttributeValueChar(eXMLSerializeVersion, i))
            aAttrVal.append(i);
        if (!XMLCharHelper.isInvalidXMLTextChar(eXMLSerializeVersion, i))
            aText.append(i);
    }
    final Document aDoc = XMLFactory.newDocument(eXMLVersion);
    final Element eRoot = (Element) aDoc.appendChild(aDoc.createElement("root"));
    eRoot.setAttribute("test", aAttrVal.toString());
    final Element e1 = (Element) eRoot.appendChild(aDoc.createElement("a"));
    e1.appendChild(aDoc.createTextNode(aText.toString()));
    final Element e2 = (Element) eRoot.appendChild(aDoc.createElement("b"));
    e2.appendChild(aDoc.createCDATASection("aaaaaaaaaaa]]>bbbbbbbbbbb]]>ccccccccc"));
    final Element e3 = (Element) eRoot.appendChild(aDoc.createElement("c"));
    e3.appendChild(aDoc.createCDATASection("]]>"));
    if (false)
        e3.appendChild(aDoc.createComment("<!--"));
    e3.appendChild(aDoc.createTextNode("abc"));
    if (false)
        e3.appendChild(aDoc.createComment("-->"));
    final NonBlockingStringWriter aSW = new NonBlockingStringWriter();
    XMLTransformerFactory.newTransformer().transform(new DOMSource(aDoc), new StreamResult(aSW));
    final String sTransform = aSW.getAsString();
    final Document aDoc2 = DOMReader.readXMLDOM(sTransform);
    final Node e3a = aDoc2.getDocumentElement().getChildNodes().item(2);
    aSW.reset();
    XMLTransformerFactory.newTransformer().transform(new DOMSource(e3a), new StreamResult(aSW));
    final String sXML = XMLWriter.getNodeAsString(aDoc, new XMLWriterSettings().setIncorrectCharacterHandling(EXMLIncorrectCharacterHandling.WRITE_TO_FILE_NO_LOG).setIndent(EXMLSerializeIndent.NONE));
    assertNotNull(sXML);
    assertNotNull("Failed to read: " + sXML, DOMReader.readXMLDOM(sXML));
}
Also used : EXMLSerializeVersion(com.helger.xml.serialize.write.EXMLSerializeVersion) DOMSource(javax.xml.transform.dom.DOMSource) XMLWriterSettings(com.helger.xml.serialize.write.XMLWriterSettings) StreamResult(javax.xml.transform.stream.StreamResult) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) EXMLVersion(com.helger.xml.EXMLVersion) Document(org.w3c.dom.Document) NonBlockingStringWriter(com.helger.commons.io.stream.NonBlockingStringWriter) Test(org.junit.Test)

Aggregations

EXMLVersion (com.helger.xml.EXMLVersion)4 NonBlockingStringWriter (com.helger.commons.io.stream.NonBlockingStringWriter)3 DOMSource (javax.xml.transform.dom.DOMSource)3 StreamResult (javax.xml.transform.stream.StreamResult)3 Document (org.w3c.dom.Document)3 Element (org.w3c.dom.Element)3 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)2 EXMLSerializeVersion (com.helger.xml.serialize.write.EXMLSerializeVersion)2 ETriState (com.helger.commons.state.ETriState)1 XMLWriterSettings (com.helger.xml.serialize.write.XMLWriterSettings)1 Test (org.junit.Test)1 DOMException (org.w3c.dom.DOMException)1 Node (org.w3c.dom.Node)1