use of org.apache.xpath.XPath in project nokogiri by sparklemotion.
the class XmlXpathContext method tryGetNodeSet.
private IRubyObject tryGetNodeSet(ThreadContext context, String expr, NokogiriXPathFunctionResolver fnResolver) throws TransformerException {
final Node contextNode = this.context.node;
final JAXPPrefixResolver prefixResolver = new JAXPPrefixResolver(nsContext);
XPath xpathInternal = new XPath(expr, null, prefixResolver, XPath.SELECT);
// We always need to have a ContextNode with Xalan XPath implementation
// To allow simple expression evaluation like 1+1 we are setting
// dummy Document as Context Node
final XObject xobj;
if (contextNode == null)
xobj = xpathInternal.execute(getXPathContext(fnResolver), DTM.NULL, prefixResolver);
else
xobj = xpathInternal.execute(getXPathContext(fnResolver), contextNode, prefixResolver);
switch(xobj.getType()) {
case XObject.CLASS_BOOLEAN:
return context.getRuntime().newBoolean(xobj.bool());
case XObject.CLASS_NUMBER:
return context.getRuntime().newFloat(xobj.num());
case XObject.CLASS_NODESET:
XmlNodeSet xmlNodeSet = XmlNodeSet.create(context.getRuntime());
xmlNodeSet.setNodeList(xobj.nodelist());
xmlNodeSet.initialize(context.getRuntime(), this.context);
return xmlNodeSet;
default:
return context.getRuntime().newString(xobj.str());
}
}
use of org.apache.xpath.XPath in project intellij-community by JetBrains.
the class XalanStyleFrame method eval.
public Value eval(String expr) throws Debugger.EvaluationException {
assert isValid();
try {
final DTMIterator context = myTransformer.getContextNodeList();
final int ctx;
final DTM dtm = context.getDTM(myCurrentNode);
if (dtm.getDocumentRoot(myCurrentNode) == myCurrentNode) {
ctx = dtm.getFirstChild(myCurrentNode);
} else {
ctx = myCurrentNode;
}
final DTMNodeProxy c = new DTMNodeProxy(dtm, ctx);
final PrefixResolver prefixResolver = new PrefixResolverDefault(c) {
public String getNamespaceForPrefix(String prefix, Node context) {
if (context instanceof DTMNodeProxy) {
final DTMNodeProxy proxy = (DTMNodeProxy) context;
final DTM dtm = proxy.getDTM();
int p = proxy.getDTMNodeNumber();
while (p != DTM.NULL) {
int nsNode = dtm.getFirstNamespaceNode(p, true);
while (nsNode != DTM.NULL) {
final String s = dtm.getLocalName(nsNode);
if (s.equals(prefix)) {
return dtm.getNodeValue(nsNode);
}
nsNode = dtm.getNextNamespaceNode(p, nsNode, true);
}
p = dtm.getParent(p);
}
}
return super.getNamespaceForPrefix(prefix, context);
}
};
final XPath xPath = new XPath(expr, myCurrentElement, prefixResolver, XPath.SELECT, myTransformer.getErrorListener());
return new XObjectValue(xPath.execute(myContext, myCurrentNode, myCurrentElement));
} catch (Exception e) {
debug(e);
final String message = e.getMessage();
throw new Debugger.EvaluationException(message != null ? message : e.getClass().getSimpleName());
}
}
Aggregations