public void testParseCacheonixXSD() throws ParserConfigurationException, IOException, SAXException { final InputStream xsdStream = getClass().getClassLoader().getResourceAsStream("META-INF/cacheonix-config-2.0.xsd"); try { // Create parser factory final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setNamespaceAware(true); documentBuilderFactory.setValidating(true); documentBuilderFactory.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA); documentBuilderFactory.setAttribute( JAXP_SCHEMA_SOURCE, "http://www.w3.org/2001/XMLSchema.xsd"); // Create parser final DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); documentBuilder.setErrorHandler(new ErrorHandler()); final Document document = documentBuilder.parse(xsdStream); // Parse assertNotNull(document); } finally { IOUtils.closeHard(xsdStream); } }
/** * De-compresses given byte array with a zip input stream with entry name {@link * #OBJECT_ZIP_ENTRY}. * * @param compressedBytes bytes to un-compress * @return byte array with de-compressed source. * @throws IOException if there is an unrecoverable problem while de-compressing bytes. */ public byte[] decompress(final byte[] compressedBytes) throws IOException { ZipInputStream zis = null; ByteArrayOutputStream baos = null; ByteArrayInputStream bais = null; try { bais = new ByteArrayInputStream(compressedBytes); zis = new ZipInputStream(bais); // Get entry so that we can start reading. zis.getNextEntry(); baos = new ByteArrayOutputStream(compressedBytes.length); IOUtils.copyInputToOuputStream(zis, baos); baos.flush(); return baos.toByteArray(); } finally { IOUtils.closeHard(bais); IOUtils.closeHard(zis); IOUtils.closeHard(baos); } }
/** * Compresses given byte array by creating a zip output stream with entry name {@link * #OBJECT_ZIP_ENTRY}. * * @param bytes bytes to compress * @return byte array with compressed source. * @throws IOException if there is an unrecoverable problem while compressing bytes. * @see #decompress(byte[]) */ public byte[] compress(final byte[] bytes) throws IOException { ByteArrayOutputStream baos = null; ZipOutputStream zos = null; ByteArrayInputStream bais = null; try { baos = new ByteArrayOutputStream(); bais = new ByteArrayInputStream(bytes); zos = new ZipOutputStream(baos); // Create entry as required by ZipOutputStream final ZipEntry entry = new ZipEntry(OBJECT_ZIP_ENTRY); zos.putNextEntry(entry); IOUtils.copyInputToOuputStream(bais, zos); zos.flush(); baos.flush(); zos.closeEntry(); return baos.toByteArray(); } finally { IOUtils.closeHard(bais); IOUtils.closeHard(baos); IOUtils.closeHard(zos); } }
public void testParseCacheonixLocal() throws ParserConfigurationException, IOException, SAXException { // Parse final Document document; final InputStream testFileInputStream = TestUtils.getTestFileInputStream("new-config-example-local.xml"); try { document = parse(testFileInputStream); } finally { IOUtils.closeHard(testFileInputStream); } assertNotNull(document); }
private Document parse(final InputStream testFileInputStream) throws ParserConfigurationException, SAXException, IOException { final Document document; final InputStream xsdStream = getClass().getClassLoader().getResourceAsStream("META-INF/cacheonix-config-2.0.xsd"); try { // Create parser factory final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setNamespaceAware(true); documentBuilderFactory.setValidating(true); documentBuilderFactory.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA); documentBuilderFactory.setAttribute(JAXP_SCHEMA_SOURCE, xsdStream); // Create parser final DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); documentBuilder.setErrorHandler(new ErrorHandler()); document = documentBuilder.parse(testFileInputStream); } finally { IOUtils.closeHard(xsdStream); } return document; }