Search in sources :

Example 1 with GrammarCache

use of org.openexi.proc.grammars.GrammarCache 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)

Example 2 with GrammarCache

use of org.openexi.proc.grammars.GrammarCache in project ddf by codice.

the class EXIEncoder method encode.

/**
     * Takes the incoming xmlStream and performs EXI-encoding on it into the exiStream.
     *
     * @param xmlStream Input of xml data
     * @param exiStream Output of exi-encoded data
     * @throws EXIOptionsException
     * @throws TransmogrifierException
     * @throws java.io.IOException
     */
public static void encode(InputStream xmlStream, OutputStream exiStream) throws EXIOptionsException, TransmogrifierException, IOException {
    Transmogrifier trans = new Transmogrifier();
    trans.setAlignmentType(AlignmentType.bitPacked);
    GrammarCache grammarCache = new GrammarCache(null, GrammarOptions.DEFAULT_OPTIONS);
    trans.setGrammarCache(grammarCache);
    trans.setOutputStream(exiStream);
    LOGGER.debug("Starting EXI encoding process.");
    trans.encode(new InputSource(xmlStream));
    LOGGER.debug("EXI encoding complete.");
}
Also used : InputSource(org.xml.sax.InputSource) Transmogrifier(org.openexi.sax.Transmogrifier) GrammarCache(org.openexi.proc.grammars.GrammarCache)

Aggregations

GrammarCache (org.openexi.proc.grammars.GrammarCache)2 InputSource (org.xml.sax.InputSource)2 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 EXIReader (org.openexi.sax.EXIReader)1 Transmogrifier (org.openexi.sax.Transmogrifier)1