use of com.helger.xml.sax.LoggingSAXErrorHandler 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.LoggingSAXErrorHandler 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.LoggingSAXErrorHandler in project ph-commons by phax.
the class XMLSchemaCacheTest method testCustom.
@Test
@SuppressFBWarnings(value = "NP_NONNULL_PARAM_VIOLATION")
public void testCustom() {
@PresentForCodeCoverage XMLSchemaCache sc = new XMLSchemaCache(new LoggingSAXErrorHandler());
assertNotNull(sc);
final CollectingLSResourceResolver crr = new CollectingLSResourceResolver();
sc = new XMLSchemaCache(new LoggingLSResourceResolver().setWrappedResourceResolver(crr));
assertNotNull(sc);
// Valid schema
Schema aSchema = sc.getSchema(new ClassPathResource("xml/schema1.xsd"));
assertNotNull(aSchema);
assertTrue(crr.getAllRequestedResources().isEmpty());
// Valid schema (with includes)
aSchema = sc.getSchema(new ClassPathResource("xml/schema2.xsd"));
assertNotNull(aSchema);
assertEquals(1, crr.getAllRequestedResources().size());
final LSResourceData rd = crr.getAllRequestedResources().get(0);
assertEquals("http://www.w3.org/2001/XMLSchema", rd.getType());
assertEquals("http://www.example.org/schema1", rd.getNamespaceURI());
assertNull(rd.getPublicID());
assertEquals("schema1.xsd", rd.getSystemID());
assertNotNull(rd.getBaseURI());
assertTrue(rd.getBaseURI().endsWith("xml/schema2.xsd"));
// Not a schema
try {
sc.getSchema(new ClassPathResource("test1.txt"));
fail();
} catch (final IllegalArgumentException ex) {
}
// empty
try {
sc.getSchema((IReadableResource[]) null);
fail();
} catch (final NullPointerException ex) {
}
try {
sc.getSchema(new IReadableResource[0]);
fail();
} catch (final IllegalArgumentException ex) {
}
}
Aggregations