use of net.sf.saxon.om.Item 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.om.Item in project camel by apache.
the class XQueryBuilder method evaluateAsString.
public String evaluateAsString(Exchange exchange) throws Exception {
LOG.debug("evaluateAsString: {} for exchange: {}", expression, exchange);
initialize(exchange);
StringWriter buffer = new StringWriter();
SequenceIterator iter = getExpression().iterator(createDynamicContext(exchange));
for (Item item = iter.next(); item != null; item = iter.next()) {
buffer.append(item.getStringValueCS());
}
String answer = buffer.toString();
buffer.close();
return answer;
}
use of net.sf.saxon.om.Item in project camel by apache.
the class MyExtensionFunction1 method makeCallExpression.
@Override
public ExtensionFunctionCall makeCallExpression() {
return new ExtensionFunctionCall() {
private static final long serialVersionUID = 1L;
@Override
public Sequence call(XPathContext xPathContext, Sequence[] arguments) throws XPathException {
// 1st argument (mandatory, index 0)
Int64Value arg1 = (Int64Value) arguments[0].iterate().next();
int arg1Int = arg1.getDecimalValue().toBigInteger().intValue();
// 2nd argument (mandatory, index 1)
Int64Value arg2 = (Int64Value) arguments[1].iterate().next();
int arg2Int = arg2.getDecimalValue().toBigInteger().intValue();
// Functionality goes here
int resultInt = arg1Int + arg2Int;
Item result = new Int64Value(resultInt);
return SequenceTool.toLazySequence(SingletonIterator.makeIterator(result));
}
};
}
use of net.sf.saxon.om.Item in project pentaho-platform by pentaho.
the class XQResultSet method evaluate.
protected List evaluate() throws XPathException {
SequenceIterator sequenceiterator = exp.iterator(dynamicContext);
List rtn = new ArrayList(100);
int rowCount = 0;
int maxRows = (this.connection != null) ? this.connection.getMaxRows() : -1;
Item item = null;
while ((item = sequenceiterator.next()) != null) {
if ((item == null)) {
break;
}
rowCount++;
if ((maxRows >= 0) && (rowCount > maxRows)) {
break;
}
rtn.add(Value.convertToJava(item));
}
return rtn;
}
use of net.sf.saxon.om.Item in project teiid by teiid.
the class XMLSystemFunctions method xpathValue.
public static String xpathValue(Object doc, String xpath) throws XPathException, TeiidProcessingException {
Source s = null;
try {
s = convertToSource(doc);
XPathEvaluator eval = new XPathEvaluator();
// Wrap the string() function to force a string return
XPathExpression expr = eval.createExpression(xpath);
XPathDynamicContext context = expr.createDynamicContext(eval.getConfiguration().buildDocumentTree(s).getRootNode());
Object o = expr.evaluateSingle(context);
if (o == null) {
return null;
}
// Return string value of node type
if (o instanceof Item) {
Item i = (Item) o;
if (isNull(i)) {
return null;
}
return i.getStringValue();
}
// Return string representation of non-node value
return o.toString();
} finally {
Util.closeSource(s);
}
}
Aggregations