docNode = dBuilder.parse(
new InputSource(
new StringReader(xmlContext.getString())));
// Evaluate the expresion using Xalan.
XPathResult result = (XPathResult)
query.evaluate(docNode, XPathResult.ANY_TYPE, null);
if (!returnResults)
{
// We don't want to return the actual results, we just
// want to know if there was at least one item in the
// result sequence.
switch (result.getResultType()) {
case XPathResult.UNORDERED_NODE_ITERATOR_TYPE:
case XPathResult.ORDERED_NODE_ITERATOR_TYPE:
if (result.iterateNext() == null) {
// We have an empty sequence, so return null.
return null;
} else {
// We have a non-empty sequence, so return a non-null
// list to indicate that we found at least one item.
return Collections.EMPTY_LIST;
}
default:
// We have a single atomic value, which means the result is
// non-empty. So return a non-null list.
return Collections.EMPTY_LIST;
}
}
// Else process the results.
List itemRefs;
switch (result.getResultType()) {
case XPathResult.NUMBER_TYPE:
// Single atomic number. Get its string value.
String val = numberToString(result.getNumberValue());
itemRefs = Collections.singletonList(val);
break;
case XPathResult.STRING_TYPE:
// Single atomic string value.
itemRefs = Collections.singletonList(result.getStringValue());
break;
case XPathResult.BOOLEAN_TYPE:
// Single atomic boolean. Get its string value.
itemRefs = Collections.singletonList(
String.valueOf(result.getBooleanValue()));
break;
case XPathResult.UNORDERED_NODE_ITERATOR_TYPE:
case XPathResult.ORDERED_NODE_ITERATOR_TYPE:
// We have a sequence. Get all nodes.
itemRefs = new ArrayList();
Node node;
while ((node = result.iterateNext()) != null) {
itemRefs.add(node);
}
break;
default:
if (SanityManager.DEBUG) {
SanityManager.THROWASSERT(
"Don't know how to handle XPath result type " +
result.getResultType());
}
itemRefs = null;
}
/* Indicate what kind of XML result value we have. If