use of javax.xml.transform.stax.StAXSource in project voltdb by VoltDB.
the class JDBCSQLXML method createStAXSource.
/**
* Retrieves a new StAXSource for reading the XML value designated by this
* SQLXML instance. <p>
*
* @param sourceClass The class of the source
* @throws java.sql.SQLException if there is an error processing the XML
* value or if the given <tt>sourceClass</tt> is not supported.
* @return a new StAXSource for reading the XML value designated by this
* SQLXML instance
*/
@SuppressWarnings("unchecked")
protected <T extends Source> T createStAXSource(Class<T> sourceClass) throws SQLException {
StAXSource source = null;
Constructor sourceCtor = null;
Reader reader = null;
XMLInputFactory factory = null;
XMLEventReader eventReader = null;
try {
factory = XMLInputFactory.newInstance();
} catch (FactoryConfigurationError ex) {
throw Exceptions.sourceInstantiation(ex);
}
try {
sourceCtor = (sourceClass == null) ? StAXSource.class.getConstructor(XMLEventReader.class) : sourceClass.getConstructor(XMLEventReader.class);
} catch (SecurityException ex) {
throw Exceptions.sourceInstantiation(ex);
} catch (NoSuchMethodException ex) {
throw Exceptions.sourceInstantiation(ex);
}
reader = getCharacterStreamImpl();
try {
eventReader = factory.createXMLEventReader(reader);
} catch (XMLStreamException ex) {
throw Exceptions.sourceInstantiation(ex);
}
try {
source = (StAXSource) sourceCtor.newInstance(eventReader);
} catch (SecurityException ex) {
throw Exceptions.sourceInstantiation(ex);
} catch (IllegalArgumentException ex) {
throw Exceptions.sourceInstantiation(ex);
} catch (IllegalAccessException ex) {
throw Exceptions.sourceInstantiation(ex);
} catch (InstantiationException ex) {
throw Exceptions.sourceInstantiation(ex);
} catch (InvocationTargetException ex) {
throw Exceptions.sourceInstantiation(ex.getTargetException());
} catch (ClassCastException ex) {
throw Exceptions.sourceInstantiation(ex);
}
return (T) source;
}
use of javax.xml.transform.stax.StAXSource in project voltdb by VoltDB.
the class JDBCSQLXML method getSource.
/**
* Returns a Source for reading the XML value designated by this SQLXML instance.
* Sources are used as inputs to XML parsers and XSLT transformers.
* <p>
* Sources for XML parsers will have namespace processing on by default.
* The systemID of the Source is implementation dependent.
* <p>
* The SQL XML object becomes not readable when this method is called and
* may also become not writable depending on implementation.
* <p>
* Note that SAX is a callback architecture, so a returned
* SAXSource should then be set with a content handler that will
* receive the SAX events from parsing. The content handler
* will receive callbacks based on the contents of the XML.
* <pre>
* SAXSource saxSource = sqlxml.getSource(SAXSource.class);
* XMLReader xmlReader = saxSource.getXMLReader();
* xmlReader.setContentHandler(myHandler);
* xmlReader.parse(saxSource.getInputSource());
* </pre>
*
* @param sourceClass The class of the source, or null.
* If the class is null, a vendor specifc Source implementation will be returned.
* The following classes are supported at a minimum:
* <pre>
* javax.xml.transform.dom.DOMSource - returns a DOMSource
* javax.xml.transform.sax.SAXSource - returns a SAXSource
* javax.xml.transform.stax.StAXSource - returns a StAXSource
* javax.xml.transform.stream.StreamSource - returns a StreamSource
* </pre>
* @return a Source for reading the XML value.
* @throws SQLException if there is an error processing the XML value
* or if this feature is not supported.
* The getCause() method of the exception may provide a more detailed exception, for example,
* if an XML parser exception occurs.
* An exception is thrown if the state is not readable.
* @exception SQLFeatureNotSupportedException if the JDBC driver does not support
* this method
* @since JDK 1.6 Build 79
*/
@SuppressWarnings("unchecked")
public synchronized <T extends Source> T getSource(Class<T> sourceClass) throws SQLException {
checkClosed();
checkReadable();
final Source source = getSourceImpl(sourceClass);
setReadable(false);
setWritable(false);
return (T) source;
}
use of javax.xml.transform.stax.StAXSource in project camel by apache.
the class ValidatingProcessor method doProcess.
protected void doProcess(Exchange exchange) throws Exception {
Schema schema;
if (isUseSharedSchema()) {
schema = getSchema();
} else {
schema = createSchema();
}
Validator validator = schema.newValidator();
// the underlying input stream, which we need to close to avoid locking files or other resources
Source source = null;
InputStream is = null;
try {
Result result = null;
// only convert to input stream if really needed
if (isInputStreamNeeded(exchange)) {
is = getContentToValidate(exchange, InputStream.class);
if (is != null) {
source = getSource(exchange, is);
}
} else {
Object content = getContentToValidate(exchange);
if (content != null) {
source = getSource(exchange, content);
}
}
if (shouldUseHeader()) {
if (source == null && isFailOnNullHeader()) {
throw new NoXmlHeaderValidationException(exchange, headerName);
}
} else {
if (source == null && isFailOnNullBody()) {
throw new NoXmlBodyValidationException(exchange);
}
}
//CAMEL-7036 We don't need to set the result if the source is an instance of StreamSource
if (source instanceof DOMSource) {
result = new DOMResult();
} else if (source instanceof SAXSource) {
result = new SAXResult();
} else if (source instanceof StAXSource || source instanceof StreamSource) {
result = null;
}
if (source != null) {
// create a new errorHandler and set it on the validator
// must be a local instance to avoid problems with concurrency (to be
// thread safe)
ValidatorErrorHandler handler = errorHandler.getClass().newInstance();
validator.setErrorHandler(handler);
try {
LOG.trace("Validating {}", source);
validator.validate(source, result);
handler.handleErrors(exchange, schema, result);
} catch (SAXParseException e) {
// can be thrown for non well formed XML
throw new SchemaValidationException(exchange, schema, Collections.singletonList(e), Collections.<SAXParseException>emptyList(), Collections.<SAXParseException>emptyList());
}
}
} finally {
IOHelper.close(is);
}
}
use of javax.xml.transform.stax.StAXSource in project camel by apache.
the class XmlConverter method toStAXSource.
/**
* Converts the source instance to a {@link StAXSource} or returns null if the conversion is not
* supported (making it easy to derive from this class to add new kinds of conversion).
* @throws FileNotFoundException
* @throws XMLStreamException
*/
@Converter
public StAXSource toStAXSource(File file, Exchange exchange) throws FileNotFoundException, XMLStreamException {
InputStream is = IOHelper.buffered(new FileInputStream(file));
XMLStreamReader r = new StaxConverter().createXMLStreamReader(is, exchange);
return new StAXSource(r);
}
use of javax.xml.transform.stax.StAXSource in project camel by apache.
the class XmlConverterTest method testToStAXSourceFromFile.
public void testToStAXSourceFromFile() throws Exception {
XmlConverter conv = new XmlConverter();
template.sendBodyAndHeader("file:target/xml", "<foo>bar</foo>", Exchange.FILE_NAME, "myxml.xml");
File file = new File("target/xml/myxml.xml");
StAXSource out = conv.toStAXSource(file, null);
assertNotNull(out);
assertEquals("<foo>bar</foo>", context.getTypeConverter().convertTo(String.class, out));
}
Aggregations