@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; }
/** * Open and parse the specified file expressed as a file: URL.. * * @param url the URL of the file to open, expressed as a URL with a scheme of "file". * @param linkBase the original address of the document if the file is a retrieved and cached * file. * @return A {@code ColladaRoot} representing the file's COLLADA contents. * @throws IOException if an I/O error occurs during opening and parsing. * @throws XMLStreamException if a server parsing error is encountered. */ protected ColladaRoot parseCachedColladaFile(URL url, String linkBase) throws IOException, XMLStreamException { ColladaDoc colladaDoc; InputStream refStream = url.openStream(); colladaDoc = new ColladaInputStream(refStream, WWIO.makeURI(linkBase)); try { ColladaRoot refRoot = new ColladaRoot(colladaDoc); refRoot.parse(); // also closes the URL's stream return refRoot; } catch (XMLStreamException e) { refStream.close(); // parsing failed, so explicitly close the stream throw e; } }