use of net.sf.saxon.Configuration in project camel by apache.
the class XQueryBuilder method initialize.
/**
* Initializes this builder - <b>Must be invoked before evaluation</b>.
*/
protected synchronized void initialize(Exchange exchange) throws XPathException, IOException {
// must use synchronized for concurrency issues and only let it initialize once
if (!initialized.get()) {
LOG.debug("Initializing XQueryBuilder {}", this);
if (configuration == null) {
configuration = new Configuration();
configuration.setStripsWhiteSpace(isStripsAllWhiteSpace() ? Whitespace.ALL : Whitespace.IGNORABLE);
LOG.debug("Created new Configuration {}", configuration);
} else {
LOG.debug("Using existing Configuration {}", configuration);
}
if (configurationProperties != null && !configurationProperties.isEmpty()) {
for (Map.Entry<String, Object> entry : configurationProperties.entrySet()) {
configuration.setConfigurationProperty(entry.getKey(), entry.getValue());
}
}
staticQueryContext = getConfiguration().newStaticQueryContext();
if (moduleURIResolver != null) {
staticQueryContext.setModuleURIResolver(moduleURIResolver);
}
Set<Map.Entry<String, String>> entries = namespacePrefixes.entrySet();
for (Map.Entry<String, String> entry : entries) {
String prefix = entry.getKey();
String uri = entry.getValue();
staticQueryContext.declareNamespace(prefix, uri);
staticQueryContext.setInheritNamespaces(true);
}
expression = createQueryExpression(staticQueryContext);
initialized.set(true);
}
// let the configuration be accessible on the exchange as its shared for this evaluation
// and can be needed by 3rd party type converters or other situations (camel-artixds)
exchange.setProperty("CamelSaxonConfiguration", configuration);
}
use of net.sf.saxon.Configuration 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 net.sf.saxon.Configuration in project camel by apache.
the class SaxonConverter method toDOMDocument.
@Converter
public static Document toDOMDocument(NodeInfo node) throws XPathException {
switch(node.getNodeKind()) {
case Type.DOCUMENT:
// DOCUMENT type nodes can be wrapped directly
return (Document) NodeOverNodeInfo.wrap(node);
case Type.ELEMENT:
// ELEMENT nodes need to build a new DocumentInfo before wrapping
Configuration config = node.getConfiguration();
DocumentInfo documentInfo = config.buildDocument(node);
return (Document) NodeOverNodeInfo.wrap(documentInfo);
default:
return null;
}
}
use of net.sf.saxon.Configuration in project camel by apache.
the class SaxonXsltEndpointConfigurationTest method testConfiguration.
@Test
public void testConfiguration() throws Exception {
Configuration configuration = context.getRegistry().lookupByNameAndType("saxon-configuration", Configuration.class);
Map<String, Object> properties = context.getRegistry().lookupByNameAndType("saxon-properties", Map.class);
XsltComponent component = context.getComponent("xslt", XsltComponent.class);
XsltEndpoint endpoint = null;
assertNotNull(configuration);
assertNotNull(properties);
for (Endpoint ep : context.getEndpoints()) {
if (ep instanceof XsltEndpoint) {
endpoint = (XsltEndpoint) ep;
break;
}
}
assertNotNull(component);
assertNotNull(endpoint);
assertNull(component.getSaxonConfiguration());
assertTrue(component.getSaxonConfigurationProperties().isEmpty());
assertNotNull(endpoint.getSaxonConfiguration());
assertNotNull(endpoint.getSaxonConfigurationProperties());
assertEquals(configuration, endpoint.getSaxonConfiguration());
assertEquals(properties, endpoint.getSaxonConfigurationProperties());
}
use of net.sf.saxon.Configuration in project sirix by sirixdb.
the class XPathEvaluator method call.
@Override
public XPathSelector call() throws Exception {
final Processor proc = new Processor(false);
final Configuration config = proc.getUnderlyingConfiguration();
final NodeInfo doc = new DocumentWrapper(mSession, mRevision, config);
final XPathCompiler xpath = proc.newXPathCompiler();
final DocumentBuilder builder = proc.newDocumentBuilder();
final XdmItem item = builder.wrap(doc);
final XPathSelector selector = xpath.compile(mExpression).load();
selector.setContextItem(item);
return selector;
}
Aggregations