Search in sources :

Example 1 with LoggingSAXErrorHandler

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);
}
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 2 with LoggingSAXErrorHandler

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) {
    }
}
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 3 with LoggingSAXErrorHandler

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) {
    }
}
Also used : CollectingLSResourceResolver(com.helger.xml.ls.CollectingLSResourceResolver) LSResourceData(com.helger.xml.ls.LSResourceData) LoggingLSResourceResolver(com.helger.xml.ls.LoggingLSResourceResolver) Schema(javax.xml.validation.Schema) LoggingSAXErrorHandler(com.helger.xml.sax.LoggingSAXErrorHandler) IReadableResource(com.helger.commons.io.resource.IReadableResource) ClassPathResource(com.helger.commons.io.resource.ClassPathResource) PresentForCodeCoverage(com.helger.commons.annotation.PresentForCodeCoverage) Test(org.junit.Test) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Aggregations

LoggingSAXErrorHandler (com.helger.xml.sax.LoggingSAXErrorHandler)3 Test (org.junit.Test)3 ClassPathResource (com.helger.commons.io.resource.ClassPathResource)2 NonBlockingStringReader (com.helger.commons.io.stream.NonBlockingStringReader)2 StringSAXInputSource (com.helger.xml.sax.StringSAXInputSource)2 Schema (javax.xml.validation.Schema)2 PresentForCodeCoverage (com.helger.commons.annotation.PresentForCodeCoverage)1 IReadableResource (com.helger.commons.io.resource.IReadableResource)1 NonBlockingByteArrayInputStream (com.helger.commons.io.stream.NonBlockingByteArrayInputStream)1 NonBlockingByteArrayOutputStream (com.helger.commons.io.stream.NonBlockingByteArrayOutputStream)1 StringInputStream (com.helger.commons.io.stream.StringInputStream)1 StringInputStreamProvider (com.helger.commons.io.streamprovider.StringInputStreamProvider)1 CollectingLSResourceResolver (com.helger.xml.ls.CollectingLSResourceResolver)1 LSResourceData (com.helger.xml.ls.LSResourceData)1 LoggingLSResourceResolver (com.helger.xml.ls.LoggingLSResourceResolver)1 IMicroDocument (com.helger.xml.microdom.IMicroDocument)1 CollectingSAXErrorHandler (com.helger.xml.sax.CollectingSAXErrorHandler)1 ISAXReaderSettings (com.helger.xml.serialize.read.ISAXReaderSettings)1 SAXReaderSettings (com.helger.xml.serialize.read.SAXReaderSettings)1 XMLWriterSettings (com.helger.xml.serialize.write.XMLWriterSettings)1