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);
}
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);
}
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);
}
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) {
}
}
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>")));
}
Aggregations