protected int streamThroughFailing(XMLInputFactory f, String contents, String msg) throws XMLStreamException { int result = 0; try { XMLStreamReader sr = constructStreamReader(f, contents); result = streamThrough(sr); } catch (XMLStreamException ex) { // good if (PRINT_EXP_EXCEPTION) { System.out.println( "Expected failure: '" + ex.getMessage() + "' " + "(matching message: '" + msg + "')"); } return 0; } catch (RuntimeException ex2) { // ok if (PRINT_EXP_EXCEPTION) { System.out.println( "Expected failure: '" + ex2.getMessage() + "' " + "(matching message: '" + msg + "')"); } return 0; } catch (Throwable t) { // not so good fail("Expected an XMLStreamException or RuntimeException for " + msg + ", got: " + t); } fail("Expected an exception for " + msg); return result; // never gets here }
@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; }