Search in sources :

Example 1 with StringSAXInputSource

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

the class MicroWriterTest method _testC14.

private static void _testC14(final String sSrc, final String sDst) {
    final IMicroDocument aDoc = MicroReader.readMicroXML(sSrc, new SAXReaderSettings().setEntityResolver((x, y) -> "world.txt".equals(y) ? new StringSAXInputSource("world") : new StringSAXInputSource("")));
    assertNotNull(aDoc);
    final MapBasedNamespaceContext aCtx = new MapBasedNamespaceContext();
    aCtx.addMapping("a", "http://www.w3.org");
    aCtx.addMapping("b", "http://www.ietf.org");
    final String sC14 = MicroWriter.getNodeAsString(aDoc, XMLWriterSettings.createForCanonicalization().setIndentationString("   ").setNamespaceContext(aCtx).setSerializeComments(EXMLSerializeComments.IGNORE));
    assertEquals(sDst, sC14);
}
Also used : EXMLSerializeDocType(com.helger.xml.serialize.write.EXMLSerializeDocType) MicroComment(com.helger.xml.microdom.MicroComment) IMicroDocument(com.helger.xml.microdom.IMicroDocument) MicroDocument(com.helger.xml.microdom.MicroDocument) MicroContainer(com.helger.xml.microdom.MicroContainer) EXMLSerializeVersion(com.helger.xml.serialize.write.EXMLSerializeVersion) StringSAXInputSource(com.helger.xml.sax.StringSAXInputSource) NonBlockingByteArrayOutputStream(com.helger.commons.io.stream.NonBlockingByteArrayOutputStream) IMicroElement(com.helger.xml.microdom.IMicroElement) XMLWriterSettings(com.helger.xml.serialize.write.XMLWriterSettings) Assert.fail(org.junit.Assert.fail) SAXReaderSettings(com.helger.xml.serialize.read.SAXReaderSettings) EXMLSerializeComments(com.helger.xml.serialize.write.EXMLSerializeComments) EXMLVersion(com.helger.xml.EXMLVersion) XMLCharHelper(com.helger.xml.serialize.write.XMLCharHelper) Assert.assertNotNull(org.junit.Assert.assertNotNull) MapBasedNamespaceContext(com.helger.xml.namespace.MapBasedNamespaceContext) Assert.assertTrue(org.junit.Assert.assertTrue) MicroElement(com.helger.xml.microdom.MicroElement) Test(org.junit.Test) StandardCharsets(java.nio.charset.StandardCharsets) IMicroNode(com.helger.xml.microdom.IMicroNode) Ignore(org.junit.Ignore) MicroCDATA(com.helger.xml.microdom.MicroCDATA) MicroEntityReference(com.helger.xml.microdom.MicroEntityReference) EXMLSerializeIndent(com.helger.xml.serialize.write.EXMLSerializeIndent) Assert.assertEquals(org.junit.Assert.assertEquals) MapBasedNamespaceContext(com.helger.xml.namespace.MapBasedNamespaceContext) StringSAXInputSource(com.helger.xml.sax.StringSAXInputSource) SAXReaderSettings(com.helger.xml.serialize.read.SAXReaderSettings) IMicroDocument(com.helger.xml.microdom.IMicroDocument)

Example 2 with StringSAXInputSource

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

the class MicroReaderTest method testSimple.

@Test
public void testSimple() {
    final String s = "<?xml version=\"1.0\"?>" + "<verrryoot>" + "<root xmlns=\"myuri\">" + "<child xmlns=\"\">" + "<a:child2 xmlns:a=\"foo\">Value text - no entities!</a:child2>" + "</child>" + "</root>" + "</verrryoot>";
    IMicroDocument aDoc = MicroReader.readMicroXML(s);
    assertNotNull(aDoc);
    aDoc = MicroReader.readMicroXML(new StringSAXInputSource(s));
    assertNotNull(aDoc);
    aDoc = MicroReader.readMicroXML(new NonBlockingStringReader(s));
    assertNotNull(aDoc);
    aDoc = MicroReader.readMicroXML(new StringInputStreamProvider(s, StandardCharsets.ISO_8859_1));
    assertNotNull(aDoc);
    aDoc = MicroReader.readMicroXML(new NonBlockingByteArrayInputStream(s.getBytes(StandardCharsets.ISO_8859_1)));
    assertNotNull(aDoc);
    aDoc = MicroReader.readMicroXML(s, new SAXReaderSettings().setErrorHandler(new LoggingSAXErrorHandler()));
    assertNotNull(aDoc);
    final NonBlockingByteArrayOutputStream baos = new NonBlockingByteArrayOutputStream();
    new MicroSerializer().write(aDoc, baos);
    assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + CRLF + "<verrryoot>" + CRLF + INDENT + "<root xmlns=\"myuri\">" + CRLF + INDENT + INDENT + "<ns0:child xmlns:ns0=\"\">" + CRLF + INDENT + INDENT + INDENT + "<ns1:child2 xmlns:ns1=\"foo\">Value text - no entities!</ns1:child2>" + CRLF + INDENT + INDENT + "</ns0:child>" + CRLF + INDENT + "</root>" + CRLF + "</verrryoot>" + CRLF, baos.getAsString(StandardCharsets.UTF_8));
    final String sXHTML = "<content>" + "<div class=\"css1\">" + "<span class=\"css2\">" + "<span>Text1 " + "<span>Text1b</span>" + "</span>" + " " + "<span>Text1c</span>" + "<span class=\"css3\">" + "<span>Text2</span>" + "</span>" + "</span>" + "</div>" + "</content>";
    final IMicroDocument docXHTML = MicroReader.readMicroXML(new NonBlockingStringReader(sXHTML));
    assertNotNull(docXHTML);
    final String sResult = MicroWriter.getNodeAsString(docXHTML, new XMLWriterSettings().setIndent(EXMLSerializeIndent.NONE));
    assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<content>" + "<div class=\"css1\">" + "<span class=\"css2\"><span>Text1 <span>Text1b</span></span> <span>Text1c</span>" + "<span class=\"css3\"><span>Text2</span></span>" + "</span>" + "</div>" + "</content>", sResult);
}
Also used : NonBlockingByteArrayInputStream(com.helger.commons.io.stream.NonBlockingByteArrayInputStream) NonBlockingStringReader(com.helger.commons.io.stream.NonBlockingStringReader) XMLWriterSettings(com.helger.xml.serialize.write.XMLWriterSettings) StringSAXInputSource(com.helger.xml.sax.StringSAXInputSource) LoggingSAXErrorHandler(com.helger.xml.sax.LoggingSAXErrorHandler) NonBlockingByteArrayOutputStream(com.helger.commons.io.stream.NonBlockingByteArrayOutputStream) StringInputStreamProvider(com.helger.commons.io.streamprovider.StringInputStreamProvider) ISAXReaderSettings(com.helger.xml.serialize.read.ISAXReaderSettings) SAXReaderSettings(com.helger.xml.serialize.read.SAXReaderSettings) IMicroDocument(com.helger.xml.microdom.IMicroDocument) Test(org.junit.Test)

Example 3 with StringSAXInputSource

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

the class XMLWriterTest method _testC14.

private static void _testC14(final String sSrc, final String sDst) {
    final Document aDoc = DOMReader.readXMLDOM(sSrc, new DOMReaderSettings().setEntityResolver((x, y) -> {
        return "world.txt".equals(new File(y).getName()) ? new StringSAXInputSource("world") : new StringSAXInputSource("");
    }));
    assertNotNull(aDoc);
    final MapBasedNamespaceContext aCtx = new MapBasedNamespaceContext();
    aCtx.addMapping("a", "http://www.w3.org");
    aCtx.addMapping("b", "http://www.ietf.org");
    final String sC14 = XMLWriter.getNodeAsString(aDoc, XMLWriterSettings.createForCanonicalization().setIndentationString("   ").setNamespaceContext(aCtx).setSerializeComments(EXMLSerializeComments.IGNORE));
    assertEquals(sDst, sC14);
}
Also used : EXMLVersion(com.helger.xml.EXMLVersion) Transformer(javax.xml.transform.Transformer) DOMSource(javax.xml.transform.dom.DOMSource) TransformerException(javax.xml.transform.TransformerException) ENewLineMode(com.helger.commons.system.ENewLineMode) CommonsTestHelper(com.helger.commons.mock.CommonsTestHelper) Assert.assertNotNull(org.junit.Assert.assertNotNull) MapBasedNamespaceContext(com.helger.xml.namespace.MapBasedNamespaceContext) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) OutputKeys(javax.xml.transform.OutputKeys) XMLTransformerFactory(com.helger.xml.transform.XMLTransformerFactory) StringStreamResult(com.helger.xml.transform.StringStreamResult) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) StringSAXInputSource(com.helger.xml.sax.StringSAXInputSource) DOMReaderSettings(com.helger.xml.serialize.read.DOMReaderSettings) NonBlockingByteArrayOutputStream(com.helger.commons.io.stream.NonBlockingByteArrayOutputStream) DOMReader(com.helger.xml.serialize.read.DOMReader) Element(org.w3c.dom.Element) XMLFactory(com.helger.xml.XMLFactory) Document(org.w3c.dom.Document) Assert.assertEquals(org.junit.Assert.assertEquals) MapBasedNamespaceContext(com.helger.xml.namespace.MapBasedNamespaceContext) StringSAXInputSource(com.helger.xml.sax.StringSAXInputSource) DOMReaderSettings(com.helger.xml.serialize.read.DOMReaderSettings) Document(org.w3c.dom.Document) File(java.io.File)

Example 4 with StringSAXInputSource

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

the class DOMReaderTest method testReadWithSchema.

@Test
public void testReadWithSchema() {
    final Schema aSchema = XMLSchemaCache.getInstance().getSchema(new ClassPathResource("xml/schema1.xsd"));
    assertNotNull(aSchema);
    // read valid
    final String sValid = "<?xml version='1.0'?><root xmlns='http://www.example.org/schema1'><a>1</a><b>2</b></root>";
    Document doc = DOMReader.readXMLDOM(sValid, new DOMReaderSettings().setSchema(aSchema));
    assertNotNull(doc);
    doc = DOMReader.readXMLDOM(sValid, new DOMReaderSettings().setErrorHandler(new LoggingSAXErrorHandler()));
    assertNotNull(doc);
    doc = DOMReader.readXMLDOM(sValid, new DOMReaderSettings().setSchema(aSchema).setErrorHandler(new LoggingSAXErrorHandler()));
    assertNotNull(doc);
    DOMReader.readXMLDOM(new NonBlockingStringReader(sValid), new DOMReaderSettings().setSchema(aSchema));
    assertNotNull(doc);
    doc = DOMReader.readXMLDOM(new NonBlockingStringReader(sValid), new DOMReaderSettings().setErrorHandler(new CollectingSAXErrorHandler()));
    assertNotNull(doc);
    doc = DOMReader.readXMLDOM(new NonBlockingStringReader(sValid), new DOMReaderSettings().setSchema(aSchema).setErrorHandler(new LoggingSAXErrorHandler()));
    assertNotNull(doc);
    doc = DOMReader.readXMLDOM(new StringInputStream(sValid, StandardCharsets.ISO_8859_1), new DOMReaderSettings().setSchema(aSchema));
    assertNotNull(doc);
    doc = DOMReader.readXMLDOM(new StringInputStream(sValid, StandardCharsets.ISO_8859_1), new DOMReaderSettings().setErrorHandler(new LoggingSAXErrorHandler()));
    assertNotNull(doc);
    doc = DOMReader.readXMLDOM(new StringInputStream(sValid, StandardCharsets.ISO_8859_1), new DOMReaderSettings().setSchema(aSchema).setErrorHandler(new LoggingSAXErrorHandler()));
    assertNotNull(doc);
    doc = DOMReader.readXMLDOM(new StringSAXInputSource(sValid), new DOMReaderSettings().setSchema(aSchema));
    assertNotNull(doc);
    doc = DOMReader.readXMLDOM(new StringSAXInputSource(sValid), new DOMReaderSettings().setErrorHandler(new LoggingSAXErrorHandler()));
    assertNotNull(doc);
    doc = DOMReader.readXMLDOM(new StringSAXInputSource(sValid), new DOMReaderSettings().setSchema(aSchema).setErrorHandler(new LoggingSAXErrorHandler()));
    assertNotNull(doc);
    doc = DOMReader.readXMLDOM(new ClassPathResource("xml/schema1-valid.xml"), new DOMReaderSettings().setSchema(aSchema));
    assertNotNull(doc);
    doc = DOMReader.readXMLDOM(new ClassPathResource("xml/schema1-valid.xml"), new DOMReaderSettings().setErrorHandler(new LoggingSAXErrorHandler()));
    assertNotNull(doc);
    doc = DOMReader.readXMLDOM(new ClassPathResource("xml/schema1-valid.xml"), new DOMReaderSettings().setSchema(aSchema).setErrorHandler(new LoggingSAXErrorHandler()));
    assertNotNull(doc);
    // Read invalid (<c> tag is unknown)
    final String sInvalid = "<?xml version='1.0'?><root xmlns='http://www.example.org/schema1'><a>1</a><b>2</b><c>3</c></root>";
    doc = DOMReader.readXMLDOM(sInvalid, new DOMReaderSettings().setSchema(aSchema));
    assertNull(doc);
    doc = DOMReader.readXMLDOM(sInvalid, new DOMReaderSettings().setSchema(aSchema).setErrorHandler(new LoggingSAXErrorHandler()));
    assertNull(doc);
    doc = DOMReader.readXMLDOM(new NonBlockingStringReader(sInvalid), new DOMReaderSettings().setSchema(aSchema));
    assertNull(doc);
    doc = DOMReader.readXMLDOM(new NonBlockingStringReader(sInvalid), new DOMReaderSettings().setSchema(aSchema).setErrorHandler(new LoggingSAXErrorHandler()));
    assertNull(doc);
    doc = DOMReader.readXMLDOM(new StringInputStream(sInvalid, StandardCharsets.ISO_8859_1), new DOMReaderSettings().setSchema(aSchema));
    assertNull(doc);
    doc = DOMReader.readXMLDOM(new StringInputStream(sInvalid, StandardCharsets.ISO_8859_1), new DOMReaderSettings().setSchema(aSchema).setErrorHandler(new LoggingSAXErrorHandler()));
    assertNull(doc);
    doc = DOMReader.readXMLDOM(new StringSAXInputSource(sInvalid), new DOMReaderSettings().setSchema(aSchema));
    assertNull(doc);
    doc = DOMReader.readXMLDOM(new StringSAXInputSource(sInvalid), new DOMReaderSettings().setSchema(aSchema).setErrorHandler(new LoggingSAXErrorHandler()));
    assertNull(doc);
    doc = DOMReader.readXMLDOM(new ClassPathResource("xml/schema1-invalid.xml"), new DOMReaderSettings().setSchema(aSchema));
    assertNull(doc);
    doc = DOMReader.readXMLDOM(new ClassPathResource("xml/schema1-invalid.xml"), new DOMReaderSettings().setSchema(aSchema).setErrorHandler(new LoggingSAXErrorHandler()));
    assertNull(doc);
    try {
        DOMReader.readXMLDOM((Reader) null, new DOMReaderSettings());
        fail();
    } catch (final NullPointerException ex) {
    }
    try {
        DOMReader.readXMLDOM((InputStream) null, new DOMReaderSettings());
        fail();
    } catch (final NullPointerException ex) {
    }
    try {
        DOMReader.readXMLDOM((InputSource) null, new DOMReaderSettings());
        fail();
    } catch (final NullPointerException ex) {
    }
    try {
        DOMReader.readXMLDOM((IReadableResource) null, new DOMReaderSettings());
        fail();
    } catch (final NullPointerException ex) {
    }
}
Also used : StringInputStream(com.helger.commons.io.stream.StringInputStream) NonBlockingStringReader(com.helger.commons.io.stream.NonBlockingStringReader) CollectingSAXErrorHandler(com.helger.xml.sax.CollectingSAXErrorHandler) StringSAXInputSource(com.helger.xml.sax.StringSAXInputSource) Schema(javax.xml.validation.Schema) LoggingSAXErrorHandler(com.helger.xml.sax.LoggingSAXErrorHandler) Document(org.w3c.dom.Document) ClassPathResource(com.helger.commons.io.resource.ClassPathResource) Test(org.junit.Test)

Example 5 with StringSAXInputSource

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

the class DOMReaderTest method testReadXMLDOMInputSource.

/**
 * Test method readXMLDOM @ never
 */
@Test
public void testReadXMLDOMInputSource() {
    Document doc = DOMReader.readXMLDOM("<root/>");
    assertNotNull(doc);
    doc = DOMReader.readXMLDOM(new NonBlockingStringReader("<root/>"));
    assertNotNull(doc);
    doc = DOMReader.readXMLDOM(new StringSAXInputSource("<root/>"));
    assertNotNull(doc);
    doc = DOMReader.readXMLDOM(new StringSAXInputSource("<?xml version=\"1.0\"?>\n" + "<root/>"));
    assertNotNull(doc);
    doc = DOMReader.readXMLDOM(new StringInputStream("<?xml version=\"1.0\"?>\n<root/>", StandardCharsets.ISO_8859_1));
    assertNotNull(doc);
    try {
        // null reader not allowed
        DOMReader.readXMLDOM((InputSource) null);
        fail();
    } catch (final NullPointerException ex) {
    }
    // non-XML
    assertNull(DOMReader.readXMLDOM(new StringSAXInputSource("")));
    // non-XML
    assertNull(DOMReader.readXMLDOM(new StringSAXInputSource("<bla>")));
}
Also used : StringInputStream(com.helger.commons.io.stream.StringInputStream) NonBlockingStringReader(com.helger.commons.io.stream.NonBlockingStringReader) StringSAXInputSource(com.helger.xml.sax.StringSAXInputSource) Document(org.w3c.dom.Document) Test(org.junit.Test)

Aggregations

StringSAXInputSource (com.helger.xml.sax.StringSAXInputSource)5 Test (org.junit.Test)5 NonBlockingByteArrayOutputStream (com.helger.commons.io.stream.NonBlockingByteArrayOutputStream)3 NonBlockingStringReader (com.helger.commons.io.stream.NonBlockingStringReader)3 StringInputStream (com.helger.commons.io.stream.StringInputStream)2 EXMLVersion (com.helger.xml.EXMLVersion)2 IMicroDocument (com.helger.xml.microdom.IMicroDocument)2 MapBasedNamespaceContext (com.helger.xml.namespace.MapBasedNamespaceContext)2 LoggingSAXErrorHandler (com.helger.xml.sax.LoggingSAXErrorHandler)2 SAXReaderSettings (com.helger.xml.serialize.read.SAXReaderSettings)2 XMLWriterSettings (com.helger.xml.serialize.write.XMLWriterSettings)2 StandardCharsets (java.nio.charset.StandardCharsets)2 Assert.assertEquals (org.junit.Assert.assertEquals)2 Assert.assertNotNull (org.junit.Assert.assertNotNull)2 Assert.assertTrue (org.junit.Assert.assertTrue)2 Document (org.w3c.dom.Document)2 ClassPathResource (com.helger.commons.io.resource.ClassPathResource)1 NonBlockingByteArrayInputStream (com.helger.commons.io.stream.NonBlockingByteArrayInputStream)1 StringInputStreamProvider (com.helger.commons.io.streamprovider.StringInputStreamProvider)1 CommonsTestHelper (com.helger.commons.mock.CommonsTestHelper)1