if (node instanceof SingletonNode) {
node = ((SingletonNode)node).getNode();
}
if (node instanceof NodeInfo) {
if (!((NodeInfo)node).getConfiguration().isCompatible(config)) {
throw new XPathExpressionException(
"Supplied node must be built using the same or a compatible Configuration");
}
contextNode = ((NodeInfo)node);
} else {
JPConverter converter = JPConverter.allocate(node.getClass(), config);
ValueRepresentation val;
try {
val = converter.convert(node, new EarlyEvaluationContext(config, null));
} catch (XPathException e) {
throw new XPathExpressionException(
"Failure converting a node of class " + node.getClass().getName() +
": " + e.getMessage());
}
if (val instanceof NodeInfo) {
contextNode = (NodeInfo)val;
} else {
throw new XPathExpressionException(
"Cannot locate an object model implementation for nodes of class "
+ node.getClass().getName());
}
}
}
XPathContextMajor context = new XPathContextMajor(contextNode, executable);
context.openStackFrame(stackFrameMap);
try {
if (qName.equals(XPathConstants.BOOLEAN)) {
return Boolean.valueOf(expression.effectiveBooleanValue(context));
} else if (qName.equals(XPathConstants.STRING)) {
SequenceIterator iter = expression.iterate(context);
Item first = iter.next();
if (first == null) {
return "";
}
return first.getStringValue();
} else if (qName.equals(XPathConstants.NUMBER)) {
if (atomizer == null) {
atomizer = new Atomizer(expression, config);
}
SequenceIterator iter = atomizer.iterate(context);
Item first = iter.next();
if (first == null) {
return new Double(Double.NaN);
}
if (first instanceof NumericValue) {
return new Double(((NumericValue)first).getDoubleValue());
} else {
DoubleValue v = NumberFn.convert((AtomicValue)first);
return new Double(v.getDoubleValue());
}
} else if (qName.equals(XPathConstants.NODE)) {
SequenceIterator iter = expression.iterate(context);
Item first = iter.next();
if (first instanceof VirtualNode) {
return ((VirtualNode)first).getUnderlyingNode();
}
if (first == null || first instanceof NodeInfo) {
return first;
}
throw new XPathExpressionException("Expression result is not a node");
} else if (qName.equals(XPathConstants.NODESET)) {
//SequenceIterator iter = expression.iterate(context);
SequenceIterator iter = rawIterator(context);
SequenceExtent extent = new SequenceExtent(iter);
PJConverter converter = PJConverter.allocateNodeListCreator(config, node);
return converter.convert(extent, Object.class, context);
} else {
throw new IllegalArgumentException("qName: Unknown type for expected result");
}
} catch (XPathException e) {
throw new XPathExpressionException(e);
}
}