use of javax.xml.transform.Source in project camel by apache.
the class XQueryBuilder method createDynamicContext.
/**
* Creates a dynamic context for the given exchange
*/
protected DynamicQueryContext createDynamicContext(Exchange exchange) throws Exception {
Configuration config = getConfiguration();
DynamicQueryContext dynamicQueryContext = new DynamicQueryContext(config);
Message in = exchange.getIn();
Item item = null;
if (ObjectHelper.isNotEmpty(getHeaderName())) {
item = in.getHeader(getHeaderName(), Item.class);
} else {
item = in.getBody(Item.class);
}
if (item != null) {
dynamicQueryContext.setContextItem(item);
} else {
Object body = null;
if (ObjectHelper.isNotEmpty(getHeaderName())) {
body = in.getHeader(getHeaderName());
} else {
body = in.getBody();
}
// the underlying input stream, which we need to close to avoid locking files or other resources
InputStream is = null;
try {
Source source;
// only convert to input stream if really needed
if (isInputStreamNeeded(exchange)) {
if (ObjectHelper.isNotEmpty(getHeaderName())) {
is = exchange.getIn().getHeader(getHeaderName(), InputStream.class);
} else {
is = exchange.getIn().getBody(InputStream.class);
}
source = getSource(exchange, is);
} else {
source = getSource(exchange, body);
}
// special for bean invocation
if (source == null) {
if (body instanceof BeanInvocation) {
// if its a null bean invocation then handle that
BeanInvocation bi = exchange.getContext().getTypeConverter().convertTo(BeanInvocation.class, body);
if (bi.getArgs() != null && bi.getArgs().length == 1 && bi.getArgs()[0] == null) {
// its a null argument from the bean invocation so use null as answer
source = null;
}
}
}
if (source == null) {
// indicate it was not possible to convert to a Source type
throw new NoTypeConversionAvailableException(body, Source.class);
}
DocumentInfo doc = config.buildDocument(source);
dynamicQueryContext.setContextItem(doc);
} finally {
// can deal if is is null
IOHelper.close(is);
}
}
configureQuery(dynamicQueryContext, exchange);
// call the reset if the in message body is StreamCache
MessageHelper.resetStreamCache(exchange.getIn());
return dynamicQueryContext;
}
use of javax.xml.transform.Source in project camel by apache.
the class SaxonUriResolverTest method test.
@Test
public void test() throws Exception {
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
Source xsl = fromClasspath(XSL_PATH);
xsl.setSystemId("classpath:/" + XSL_PATH);
Source xml = fromString(XML_DATA);
TransformerFactory factory = new TransformerFactoryImpl();
Transformer transformer = factory.newTransformer(xsl);
transformer.setURIResolver(new XsltUriResolver(context(), XSL_PATH));
transformer.transform(xml, result);
Assert.assertEquals(XML_RESP, writer.toString());
}
use of javax.xml.transform.Source in project camel by apache.
the class XAdESSignaturePropertiesTest method validateAgainstSchema.
private void validateAgainstSchema(Document doc) throws Exception {
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Source schema1 = new StreamSource(new File("target/test-classes/org/apache/camel/component/xmlsecurity/xades/XAdES.xsd"));
Source schema2 = new StreamSource(new File("target/test-classes/org/apache/camel/component/xmlsecurity/xades/xmldsig-core-schema.xsd"));
Schema schema = factory.newSchema(new Source[] { schema2, schema1 });
Validator validator = schema.newValidator();
validator.validate(new DOMSource(doc));
}
use of javax.xml.transform.Source in project camel by apache.
the class ProducerLocalRouteTest method consumeStockQuoteWebserviceWithCamelStringSourceInput.
@Test
public void consumeStockQuoteWebserviceWithCamelStringSourceInput() throws Exception {
Object result = template.requestBody("direct:stockQuoteWebservice", new StringSource(xmlRequestForGoogleStockQuote));
assertNotNull(result);
assertTrue(result instanceof Source);
}
use of javax.xml.transform.Source in project asciidoctor-fopub by asciidoctor.
the class InputHandler method transformTo.
/**
* Transforms the input document to the input format expected by FOP using XSLT.
* @param result the Result object where the result of the XSL transformation is sent to
* @throws FOPException in case of an error during processing
*/
protected void transformTo(Result result) throws FOPException {
try {
// Setup XSLT
TransformerFactory factory = TransformerFactory.newInstance();
if (uriResolver != null) {
factory.setURIResolver(uriResolver);
}
factory.setErrorListener(this);
Transformer transformer;
Source xsltSource = createXSLTSource();
if (xsltSource == null) {
// FO Input
transformer = factory.newTransformer();
} else {
// XML/XSLT input
transformer = factory.newTransformer(xsltSource);
// Set the value of parameters, if any, defined for stylesheet
if (xsltParams != null) {
for (int i = 0; i < xsltParams.size(); i += 2) {
transformer.setParameter((String) xsltParams.elementAt(i), (String) xsltParams.elementAt(i + 1));
}
}
}
transformer.setErrorListener(this);
// Create a SAXSource from the input Source file
Source src = createMainSource();
// Start XSLT transformation and FOP processing
transformer.transform(src, result);
} catch (Exception e) {
throw new FOPException(e);
}
}
Aggregations