@Override public Scenario parse(InputStream inputStream) throws IOException, SAXException, ParserConfigurationException { OutputStream output = new ByteArrayOutputStream(); JsonXMLConfig config = new JsonXMLConfigBuilder().autoArray(true).prettyPrint(false).build(); try { /* Create source (XML). */ XMLEventReader reader = XMLInputFactory.newInstance().createXMLEventReader(inputStream); // XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(inputStream); // Source source = new StAXSource(reader); /* Create result (JSON). */ XMLEventWriter writer = new JsonXMLOutputFactory(config).createXMLEventWriter(output); // XMLStreamWriter writer = new JsonXMLOutputFactory(config).createXMLStreamWriter(output); // Result result = new StAXResult(writer); /* * Copy events from reader to writer. */ writer.add(reader); /* Copy source to result via "identity transform". */ // TransformerFactory.newInstance().newTransformer().transform(source, result); } catch (XMLStreamException e) { e.printStackTrace(); } finally { /* As per StAX specification, XMLStreamReader/Writer.close() doesn't close the underlying stream. */ output.close(); inputStream.close(); } /* try { json = xmlToJson(inputStream); } catch (JSONException e) { e.printStackTrace(); } String jsonString = json.toString(); System.out.println(jsonString); GenericDocument genericDocument = new GenericDocument(); genericDocument.setJson(jsonString); */ GenericDocument genericDocument = new GenericDocument(); String json = output.toString(); genericDocument.setJson(json); return genericDocument; }
public void write(SecureItemTable tbl, char[] password) throws IOException { OutputStream os = new FileOutputStream(file); OutputStream xmlout; if (password.length == 0) { xmlout = os; os = null; } else { PBEKeySpec keyspec = new PBEKeySpec(password); Cipher c; try { SecretKeyFactory fac = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); SecretKey key = fac.generateSecret(keyspec); c = Cipher.getInstance("PBEWithMD5AndDES"); c.init(Cipher.ENCRYPT_MODE, key, pbeSpec); } catch (java.security.GeneralSecurityException exc) { os.close(); IOException ioe = new IOException("Security exception during write"); ioe.initCause(exc); throw ioe; } CipherOutputStream out = new CipherOutputStream(os, c); xmlout = out; } try { TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = tf.newTransformer(); DOMSource src = new DOMSource(tbl.getDocument()); StringWriter writer = new StringWriter(); StreamResult sr = new StreamResult(writer); t.transform(src, sr); OutputStreamWriter osw = new OutputStreamWriter(xmlout, StandardCharsets.UTF_8); osw.write(writer.toString()); osw.close(); } catch (Exception exc) { IOException ioe = new IOException("Unable to serialize XML"); ioe.initCause(exc); throw ioe; } finally { xmlout.close(); if (os != null) os.close(); } tbl.setDirty(false); return; }
public static void main(String args[]) throws Exception { final InputStream xmlInputFile = new BufferedInputStream(new FileInputStream(args[0])); final InputStream xslFile = new BufferedInputStream(new FileInputStream(args[1])); final OutputStream xmlOutputFile = new BufferedOutputStream(new FileOutputStream(args[2])); final Transformer xslTransformer = TransformerFactory.newInstance().newTransformer(new StreamSource(xslFile)); final long startTime = System.currentTimeMillis(); xslTransformer.transform(new StreamSource(xmlInputFile), new StreamResult(xmlOutputFile)); xmlOutputFile.close(); final long endTime = System.currentTimeMillis(); System.out.println("Done in " + ((endTime - startTime) / 1000.0) + "s"); }
public void transform(Path inputFile, Path outputFile) throws TransformerException, IOException { OutputStream outputStream = Files.newOutputStream(outputFile); transform(inputFile, outputStream); outputStream.close(); }