Search in sources :

Example 1 with EXIReader

use of org.openexi.sax.EXIReader in project ddf by codice.

the class EXIEncoderTest method testEncode.

/**
     * Tests that the encode method converts xml into exi-compressed xml.
     *
     * @throws Exception
     */
@Test
public void testEncode() throws Exception {
    ByteArrayOutputStream exiStream = new ByteArrayOutputStream();
    InputStream xmlStream = getClass().getResourceAsStream(TEST_FILE);
    EXIEncoder.encode(xmlStream, exiStream);
    StringWriter stringWriter = new StringWriter();
    GrammarCache grammarCache;
    SAXTransformerFactory saxTransformerFactory = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
    SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
    saxParserFactory.setNamespaceAware(true);
    TransformerHandler transformerHandler = saxTransformerFactory.newTransformerHandler();
    EXIReader reader = new EXIReader();
    grammarCache = new GrammarCache(null, GrammarOptions.DEFAULT_OPTIONS);
    reader.setGrammarCache(grammarCache);
    transformerHandler.setResult(new StreamResult(stringWriter));
    reader.setContentHandler(transformerHandler);
    reader.parse(new InputSource(new ByteArrayInputStream(exiStream.toByteArray())));
    XMLUnit.setNormalize(true);
    XMLUnit.setNormalizeWhitespace(true);
    InputStream stream = getClass().getResourceAsStream(TEST_FILE);
    Diff diff = XMLUnit.compareXML(IOUtils.toString(stream), stringWriter.getBuffer().toString());
    IOUtils.closeQuietly(stream);
    assertTrue("The XML input file (" + TEST_FILE + ") did not match the EXI-decoded output", diff.similar());
}
Also used : TransformerHandler(javax.xml.transform.sax.TransformerHandler) InputSource(org.xml.sax.InputSource) EXIReader(org.openexi.sax.EXIReader) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) ByteArrayInputStream(java.io.ByteArrayInputStream) Diff(org.custommonkey.xmlunit.Diff) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) SAXTransformerFactory(javax.xml.transform.sax.SAXTransformerFactory) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GrammarCache(org.openexi.proc.grammars.GrammarCache) SAXParserFactory(javax.xml.parsers.SAXParserFactory) Test(org.junit.Test)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStream (java.io.InputStream)1 StringWriter (java.io.StringWriter)1 SAXParserFactory (javax.xml.parsers.SAXParserFactory)1 SAXTransformerFactory (javax.xml.transform.sax.SAXTransformerFactory)1 TransformerHandler (javax.xml.transform.sax.TransformerHandler)1 StreamResult (javax.xml.transform.stream.StreamResult)1 Diff (org.custommonkey.xmlunit.Diff)1 Test (org.junit.Test)1 GrammarCache (org.openexi.proc.grammars.GrammarCache)1 EXIReader (org.openexi.sax.EXIReader)1 InputSource (org.xml.sax.InputSource)1