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