/** * Retrieves a new Result for setting the XML value designated by this * SQLXML instance. * * @param resultClass The class of the result, or null. * @throws java.sql.SQLException if there is an error processing the XML * value or the state is not writable * @return for setting the XML value designated by this SQLXML instance. */ protected <T extends Result>T createResult( Class<T> resultClass) throws SQLException { checkWritable(); setWritable(false); setReadable(true); if (JAXBResult.class.isAssignableFrom(resultClass)) { // Must go first presently, since JAXBResult extends SAXResult // (purely as an implmentation detail) and it's not possible // to instantiate a valid JAXBResult with a Zero-Args // constructor(or any subclass thereof, due to the finality of // its private UnmarshallerHandler) // FALL THROUGH... will throw an exception } else if ((resultClass == null) || StreamResult.class.isAssignableFrom(resultClass)) { return createStreamResult(resultClass); } else if (DOMResult.class.isAssignableFrom(resultClass)) { return createDOMResult(resultClass); } else if (SAXResult.class.isAssignableFrom(resultClass)) { return createSAXResult(resultClass); } else if (StAXResult.class.isAssignableFrom(resultClass)) { return createStAXResult(resultClass); } throw JDBCUtil.invalidArgument("resultClass: " + resultClass); }
public javax.xml.stream.XMLStreamWriter createXMLStreamWriter(javax.xml.transform.Result result) throws javax.xml.stream.XMLStreamException { if (result instanceof StreamResult) { return createXMLStreamWriter((StreamResult) result, null); } else if (result instanceof DOMResult) { return new XMLDOMWriterImpl((DOMResult) result); } else if (result instanceof StAXResult) { if (((StAXResult) result).getXMLStreamWriter() != null) { return ((StAXResult) result).getXMLStreamWriter(); } else { throw new java.lang.UnsupportedOperationException("Result of type " + result + " is not supported"); } } else { if (result.getSystemId() !=null) { //this is not correct impl of SAXResult. Keep it for now for compatibility return createXMLStreamWriter(new StreamResult(result.getSystemId())); } else { throw new java.lang.UnsupportedOperationException("Result of type " + result + " is not supported. " + "Supported result types are: DOMResult, StAXResult and StreamResult."); } } }
@Test public void testStreamWriterWithStAXResultNStreamWriter() { final String EXPECTED_OUTPUT = "<?xml version=\"1.0\"?><root></root>"; try { XMLOutputFactory ofac = XMLOutputFactory.newInstance(); ByteArrayOutputStream buffer = new ByteArrayOutputStream(); XMLStreamWriter writer = ofac.createXMLStreamWriter(buffer); StAXResult res = new StAXResult(writer); writer = ofac.createXMLStreamWriter(res); writer.writeStartDocument("1.0"); writer.writeStartElement("root"); writer.writeEndElement(); writer.writeEndDocument(); writer.close(); Assert.assertEquals(buffer.toString(), EXPECTED_OUTPUT); } catch (Exception e) { e.printStackTrace(); Assert.fail(e.toString()); } }
@Test public void testValidateStAX() { File resultFile = null; try { resultFile = new File("stax.result"); if (resultFile.exists()) { resultFile.delete(); } Result xmlResult = new javax.xml.transform.stax.StAXResult(XMLOutputFactory.newInstance().createXMLStreamWriter(new FileWriter(resultFile))); Source xmlSource = new javax.xml.transform.stax.StAXSource(getXMLEventReader("toys.xml")); validate("toys.xsd", xmlSource, xmlResult); ((StAXResult) xmlResult).getXMLStreamWriter().close(); Assert.assertTrue(resultFile.exists(), "result file is not created"); } catch (Exception ex) { ex.printStackTrace(); Assert.fail("Exception : " + ex.getMessage()); } finally { if (resultFile != null && resultFile.exists()) { resultFile.delete(); } } }
@Override public StringValue call(XPathContext context, Sequence[] arguments) throws XPathException { StringWriter sw = new StringWriter(); JsonXMLConfig config = new JsonXMLConfigBuilder(). prettyPrint(true). build(); XMLOutputFactory jFactory = new JsonXMLOutputFactory(config); TransformerFactory tFactory = new TransformerFactoryImpl(); try { SequenceIterator iter = arguments[0].iterate(); Item item; while ((item = iter.next()) != null) { if (item instanceof NodeInfo) { Transformer transformer = tFactory.newTransformer(); XMLStreamWriter xsw = jFactory.createXMLStreamWriter(sw); transformer.transform((NodeInfo) item, new StAXResult(xsw)); } else { sw.append(item.getStringValue()); } } } catch (Exception e) { throw new XPathException("Error serializing sequence to JSON", e); } return StringValue.makeStringValue(sw.toString()); }
/** * Sets up handler for <code>StAXResult</code>. */ private void setupStAXResultHandler(StAXResult result) { // If there's no StAXResult, unset the validator handler if (result == null) { fStAXValidatorHandler = null; fSchemaValidator.setDocumentHandler(null); return; } XMLStreamWriter writer = result.getXMLStreamWriter(); if (writer != null) { if (fStAXStreamResultBuilder == null) { fStAXStreamResultBuilder = new StAXStreamResultBuilder(fNamespaceContext); } fStAXValidatorHandler = fStAXStreamResultBuilder; fStAXStreamResultBuilder.setStAXResult(result); } else { if (fStAXEventResultBuilder == null) { fStAXEventResultBuilder = new StAXEventResultBuilder(this, fNamespaceContext); } fStAXValidatorHandler = fStAXEventResultBuilder; fStAXEventResultBuilder.setStAXResult(result); } fSchemaValidator.setDocumentHandler(fStAXValidatorHandler); }
void anyxmlNode(final QName qname, final Object value) throws IOException { if (value != null) { checkArgument(value instanceof DOMSource, "AnyXML value must be DOMSource, not %s", value); final DOMSource domSource = (DOMSource) value; requireNonNull(domSource.getNode()); checkArgument(domSource.getNode().getNodeName().equals(qname.getLocalName())); checkArgument(domSource.getNode().getNamespaceURI().equals(qname.getNamespace().toString())); try { // TODO can the transformer be a constant ? is it thread safe ? final Transformer transformer = TRANSFORMER_FACTORY.newTransformer(); // Writer has to be wrapped in a wrapper that ignores endDocument event // EndDocument event forbids any other modification to the writer so a nested anyXml breaks // serialization transformer.transform(domSource, new StAXResult(new DelegateWriterNoEndDoc(writer))); } catch (final TransformerException e) { throw new IOException("Unable to transform anyXml(" + qname + ") value: " + value, e); } } }
/** * Retrieves a new Result for setting the XML value designated by this * SQLXML instance. * * @param resultClass The class of the result, or null. * @throws java.sql.SQLException if there is an error processing the XML * value or the state is not writable * @return for setting the XML value designated by this SQLXML instance. */ protected <T extends Result>T createResult( Class<T> resultClass) throws SQLException { checkWritable(); setWritable(false); setReadable(true); if (JAXBResult.class.isAssignableFrom(resultClass)) { // Must go first presently, since JAXBResult extends SAXResult // (purely as an implementation detail) and it's not possible // to instantiate a valid JAXBResult with a Zero-Args // constructor(or any subclass thereof, due to the finality of // its private UnmarshallerHandler) // FALL THROUGH... will throw an exception } else if ((resultClass == null) || StreamResult.class.isAssignableFrom(resultClass)) { return createStreamResult(resultClass); } else if (DOMResult.class.isAssignableFrom(resultClass)) { return createDOMResult(resultClass); } else if (SAXResult.class.isAssignableFrom(resultClass)) { return createSAXResult(resultClass); } else if (StAXResult.class.isAssignableFrom(resultClass)) { return createStAXResult(resultClass); } throw JDBCUtil.invalidArgument("resultClass: " + resultClass); }
/** * Retrieves a new Result for setting the XML value designated by this * SQLXML instance. * * @param resultClass The class of the result, or null. * @throws java.sql.SQLException if there is an error processing the XML * value or the state is not writable * @return for setting the XML value designated by this SQLXML instance. */ protected <T extends Result>T createResult( Class<T> resultClass) throws SQLException { checkWritable(); setWritable(false); setReadable(true); if (JAXBResult.class.isAssignableFrom(resultClass)) { // Must go first presently, since JAXBResult extends SAXResult // (purely as an implmentation detail) and it's not possible // to instantiate a valid JAXBResult with a Zero-Args // constructor(or any subclass thereof, due to the finality of // its private UnmarshallerHandler) // FALL THROUGH... will throw an exception } else if ((resultClass == null) || StreamResult.class.isAssignableFrom(resultClass)) { return createStreamResult(resultClass); } else if (DOMResult.class.isAssignableFrom(resultClass)) { return createDOMResult(resultClass); } else if (SAXResult.class.isAssignableFrom(resultClass)) { return createSAXResult(resultClass); } else if (StAXResult.class.isAssignableFrom(resultClass)) { return createStAXResult(resultClass); } throw Util.invalidArgument("resultClass: " + resultClass); }
@Test public void adaptsToStAXResult() throws SAXException, XMLStreamException { filter.setResult(new StAXResult(this.xmlStreamWriter)); filter.endDocument(); verify(this.xmlStreamWriter).writeEndDocument(); }
@Test public void testEventWriterWithStAXResultNStreamWriter() { String encoding = ""; if (getSystemProperty("file.encoding").equals("UTF-8")) { encoding = " encoding=\"UTF-8\""; } final String EXPECTED_OUTPUT = "<?xml version=\"1.0\"" + encoding + "?><root></root>"; try { XMLOutputFactory ofac = XMLOutputFactory.newInstance(); ByteArrayOutputStream buffer = new ByteArrayOutputStream(); XMLStreamWriter swriter = ofac.createXMLStreamWriter(buffer); StAXResult res = new StAXResult(swriter); XMLEventWriter writer = ofac.createXMLEventWriter(res); XMLEventFactory efac = XMLEventFactory.newInstance(); writer.add(efac.createStartDocument(null, "1.0")); writer.add(efac.createStartElement("", "", "root")); writer.add(efac.createEndElement("", "", "root")); writer.add(efac.createEndDocument()); writer.close(); Assert.assertEquals(buffer.toString(), EXPECTED_OUTPUT); } catch (Exception e) { e.printStackTrace(); Assert.fail(e.toString()); } }
@Test public void testEventWriterWithStAXResultNEventWriter() { String encoding = ""; if (getSystemProperty("file.encoding").equals("UTF-8")) { encoding = " encoding=\"UTF-8\""; } final String EXPECTED_OUTPUT = "<?xml version=\"1.0\"" + encoding + "?><root></root>"; try { XMLOutputFactory ofac = XMLOutputFactory.newInstance(); ByteArrayOutputStream buffer = new ByteArrayOutputStream(); XMLEventWriter writer = ofac.createXMLEventWriter(buffer); StAXResult res = new StAXResult(writer); writer = ofac.createXMLEventWriter(res); XMLEventFactory efac = XMLEventFactory.newInstance(); writer.add(efac.createStartDocument(null, "1.0")); writer.add(efac.createStartElement("", "", "root")); writer.add(efac.createEndElement("", "", "root")); writer.add(efac.createEndDocument()); writer.close(); Assert.assertEquals(buffer.toString(), EXPECTED_OUTPUT); } catch (Exception e) { e.printStackTrace(); Assert.fail(e.toString()); } }
@Test public void testStreamWriterWithStAXResultNEventWriter() throws Exception { try { XMLOutputFactory ofac = XMLOutputFactory.newInstance(); ByteArrayOutputStream buffer = new ByteArrayOutputStream(); XMLEventWriter writer = ofac.createXMLEventWriter(buffer); StAXResult res = new StAXResult(writer); XMLStreamWriter swriter = ofac.createXMLStreamWriter(res); Assert.fail("Expected an Exception as XMLStreamWriter can't be created " + "with a StAXResult which has EventWriter."); } catch (Exception e) { System.out.println(e.toString()); } }
/** * Refer to 6631168 : StAXSource & StAXResult support in JavaSE6 */ @Test public final void testFeatures() { TransformerFactory tff = TransformerFactory.newInstance(); Assert.assertTrue(tff.getFeature(StAXSource.FEATURE)); Assert.assertTrue(tff.getFeature(StAXResult.FEATURE)); }
@Test public void testDOM2StAX() { try { Source input = domUtil.prepareSource(this.getClass().getResourceAsStream(XML_FILE)); StAXResult staxResult = (StAXResult) staxUtil.prepareResult(); idTransform.transform(input, staxResult); staxUtil.checkResult(staxResult, EXPECTED_VERSION, "UTF-8"); } catch (Exception e) { e.printStackTrace(); Assert.fail("Exception occured: " + e.getMessage()); } }
@Test public void testDOM2StAXStream() { try { Source input = domUtil.prepareSource(this.getClass().getResourceAsStream(XML_FILE)); StAXResult staxResult = (StAXResult) staxUtil.prepareStreamResult(); idTransform.transform(input, staxResult); staxUtil.checkStreamResult(staxResult, EXPECTED_VERSION); } catch (Exception e) { e.printStackTrace(); Assert.fail("Exception occured: " + e.getMessage()); } }
@Test public void testSAX2StAX() { try { Source input = saxUtil.prepareSource(this.getClass().getResourceAsStream(XML_FILE)); StAXResult staxResult = (StAXResult) staxUtil.prepareResult(); idTransform.transform(input, staxResult); staxUtil.checkResult(staxResult, EXPECTED_VERSION, "UTF-8"); } catch (Exception e) { e.printStackTrace(); Assert.fail("Exception occured: " + e.getMessage()); } }
@Test public void testSAX2StAXStream() { try { Source input = saxUtil.prepareSource(this.getClass().getResourceAsStream(XML_FILE)); StAXResult staxResult = (StAXResult) staxUtil.prepareStreamResult(); idTransform.transform(input, staxResult); staxUtil.checkStreamResult(staxResult, EXPECTED_VERSION); } catch (Exception e) { e.printStackTrace(); Assert.fail("Exception occured: " + e.getMessage()); } }
@Test public void testStream2Stax() { try { Source input = streamUtil.prepareSource(this.getClass().getResourceAsStream(XML_FILE)); StAXResult staxResult = (StAXResult) staxUtil.prepareResult(); idTransform.transform(input, staxResult); staxUtil.checkResult(staxResult, EXPECTED_VERSION, "UTF-8"); } catch (Exception e) { e.printStackTrace(); Assert.fail("Exception occured: " + e.getMessage()); } }
@Test public void testStream2StaxStream() { try { Source input = streamUtil.prepareSource(this.getClass().getResourceAsStream(XML_FILE)); StAXResult staxResult = (StAXResult) staxUtil.prepareStreamResult(); idTransform.transform(input, staxResult); staxUtil.checkStreamResult(staxResult, EXPECTED_VERSION); } catch (Exception e) { e.printStackTrace(); Assert.fail("Exception occured: " + e.getMessage()); } }
@Test public void testStAX2StAX() { try { Source input = staxUtil.prepareStreamSource(this.getClass().getResourceAsStream(XML10_FILE)); StAXResult staxResult = (StAXResult) staxUtil.prepareResult(); idTransform.transform(input, staxResult); staxUtil.checkResult(staxResult, "1.0", "UTF-8"); } catch (Exception e) { e.printStackTrace(); Assert.fail("Exception occured: " + e.getMessage()); } }
@Test public void testEventWriterWithStAXResultNStreamWriter() { String encoding = ""; if (System.getProperty("file.encoding").equals("UTF-8")) { encoding = " encoding=\"UTF-8\""; } final String EXPECTED_OUTPUT = "<?xml version=\"1.0\"" + encoding + "?><root></root>"; try { XMLOutputFactory ofac = XMLOutputFactory.newInstance(); ByteArrayOutputStream buffer = new ByteArrayOutputStream(); XMLStreamWriter swriter = ofac.createXMLStreamWriter(buffer); StAXResult res = new StAXResult(swriter); XMLEventWriter writer = ofac.createXMLEventWriter(res); XMLEventFactory efac = XMLEventFactory.newInstance(); writer.add(efac.createStartDocument(null, "1.0")); writer.add(efac.createStartElement("", "", "root")); writer.add(efac.createEndElement("", "", "root")); writer.add(efac.createEndDocument()); writer.close(); Assert.assertEquals(buffer.toString(), EXPECTED_OUTPUT); } catch (Exception e) { e.printStackTrace(); Assert.fail(e.toString()); } }
@Test public void testEventWriterWithStAXResultNEventWriter() { String encoding = ""; if (System.getProperty("file.encoding").equals("UTF-8")) { encoding = " encoding=\"UTF-8\""; } final String EXPECTED_OUTPUT = "<?xml version=\"1.0\"" + encoding + "?><root></root>"; try { XMLOutputFactory ofac = XMLOutputFactory.newInstance(); ByteArrayOutputStream buffer = new ByteArrayOutputStream(); XMLEventWriter writer = ofac.createXMLEventWriter(buffer); StAXResult res = new StAXResult(writer); writer = ofac.createXMLEventWriter(res); XMLEventFactory efac = XMLEventFactory.newInstance(); writer.add(efac.createStartDocument(null, "1.0")); writer.add(efac.createStartElement("", "", "root")); writer.add(efac.createEndElement("", "", "root")); writer.add(efac.createEndDocument()); writer.close(); Assert.assertEquals(buffer.toString(), EXPECTED_OUTPUT); } catch (Exception e) { e.printStackTrace(); Assert.fail(e.toString()); } }