1 package net.sf.saxon.xpath; 2 import net.sf.saxon.Configuration; 3 import net.sf.saxon.event.Builder; 4 import net.sf.saxon.event.Stripper; 5 import net.sf.saxon.expr.Expression; 6 import net.sf.saxon.expr.ExpressionTool; 7 import net.sf.saxon.expr.XPathContextMajor; 8 import net.sf.saxon.instruct.SlotManager; 9 import net.sf.saxon.om.*; 10 import net.sf.saxon.type.Type; 11 import net.sf.saxon.value.Value; 12 import org.xml.sax.InputSource ; 13 14 import javax.xml.namespace.NamespaceContext ; 15 import javax.xml.namespace.QName ; 16 import javax.xml.transform.Source ; 17 import javax.xml.transform.stream.StreamSource ; 18 import javax.xml.xpath.*; 19 import java.io.File ; 20 import java.util.ArrayList ; 21 import java.util.List ; 22 23 32 33 35 public class XPathEvaluator implements XPath { 36 37 private Configuration config; 38 private NodeInfo contextNode = null; 39 private StandaloneContext staticContext; 40 private boolean stripSpace = false; 41 42 45 46 public XPathEvaluator() { 47 this(new Configuration()); 48 } 49 50 54 public XPathEvaluator(Configuration config) { 55 this.config = config; 56 staticContext = new StandaloneContext(config); 57 } 58 59 64 65 public XPathEvaluator(Source source) throws net.sf.saxon.trans.XPathException { 66 super(); 67 if (source instanceof NodeInfo) { 68 config = ((NodeInfo)source).getDocumentRoot().getConfiguration(); 69 } else { 70 config = new Configuration(); 71 } 72 setSource(source); 73 } 74 75 82 83 public void setStripSpace(boolean strip) { 84 stripSpace = strip; 85 } 86 87 101 102 public NodeInfo setSource(Source source) throws net.sf.saxon.trans.XPathException { 103 NamePool pool; 104 if (source instanceof NodeInfo) { 105 pool = ((NodeInfo)source).getNamePool(); 106 } else { 107 pool = NamePool.getDefaultNamePool(); 108 } 109 Stripper stripper = null; 110 if (stripSpace) { 111 stripper = AllElementStripper.getInstance(); 112 } 113 Configuration config = new Configuration(); 114 config.setNamePool(pool); 115 contextNode = Builder.build(source, stripper, config); 116 staticContext = new StandaloneContext(config); 117 return contextNode; 118 } 119 120 129 130 public void setStaticContext(StandaloneContext context) { 131 staticContext = context; 132 } 133 134 137 138 public StandaloneContext getStaticContext() { 139 return staticContext; 140 } 141 142 149 150 public XPathExpressionImpl createExpression(String expression) throws net.sf.saxon.trans.XPathException { 151 Expression exp = ExpressionTool.make(expression, staticContext,0,-1,1); 152 exp = exp.typeCheck(staticContext, Type.ITEM_TYPE); 153 SlotManager map = staticContext.getConfiguration().makeSlotManager(); 154 ExpressionTool.allocateSlots(exp, 0, map); 155 XPathExpressionImpl xpe = new XPathExpressionImpl(exp, config); 156 xpe.setStackFrameMap(map); 157 if (contextNode != null) { 158 xpe.setContextNode(contextNode); 159 } 160 return xpe; 161 } 162 163 171 172 public void setContextNode(NodeInfo node) { 173 if (node==null) { 174 throw new NullPointerException ("Context node cannot be null"); 175 } 176 contextNode = node; 177 } 178 179 198 199 public List evaluate(String expression) throws net.sf.saxon.trans.XPathException { 200 Expression exp = ExpressionTool.make(expression, staticContext,0,-1,1); 201 exp = exp.typeCheck(staticContext, Type.ITEM_TYPE); 202 SlotManager map = staticContext.getConfiguration().makeSlotManager(); 203 ExpressionTool.allocateSlots(exp, 0, map); 204 XPathContextMajor context = new XPathContextMajor(contextNode, staticContext.getConfiguration()); 205 context.openStackFrame(map); 206 SequenceIterator iterator = exp.iterate(context); 207 ArrayList list = new ArrayList (20); 208 while (true) { 209 Item item = iterator.next(); 210 if (item == null) { 211 return list; 212 } 213 list.add(Value.convert(item)); 214 } 215 } 216 217 public void reset() { 218 config = null; 219 contextNode = null; 220 staticContext = null; 221 stripSpace = false; 222 } 223 224 229 230 public void setBackwardsCompatible(boolean compatible) { 231 staticContext.setBackwardsCompatibilityMode(true); 232 } 233 234 238 239 public boolean isBackwardsCompatible() { 240 return staticContext.isInBackwardsCompatibleMode(); 241 } 242 243 247 248 public void setXPathVariableResolver(XPathVariableResolver xPathVariableResolver) { 249 staticContext.setXPathVariableResolver(xPathVariableResolver); 250 } 251 252 256 public XPathVariableResolver getXPathVariableResolver() { 257 return staticContext.getXPathVariableResolver(); 258 } 259 260 264 265 public void setXPathFunctionResolver(XPathFunctionResolver xPathFunctionResolver) { 266 staticContext.setXPathFunctionResolver(xPathFunctionResolver); 267 } 268 269 273 274 public XPathFunctionResolver getXPathFunctionResolver() { 275 return staticContext.getXPathFunctionResolver(); 276 } 277 278 283 284 public void setNamespaceContext(NamespaceContext namespaceContext) { 285 staticContext.setNamespaceContext(namespaceContext); 286 } 287 288 292 293 public NamespaceContext getNamespaceContext() { 294 return staticContext.getNamespaceContext(); 295 } 296 297 305 public XPathExpression compile(String expr) throws XPathExpressionException { 306 try { 307 return createExpression(expr); 308 } catch (net.sf.saxon.trans.XPathException e) { 309 throw new XPathExpressionException(e); 310 } 311 } 312 313 324 325 public Object evaluate(String expr, Object node, QName qName) throws XPathExpressionException { 326 XPathExpression exp = compile(expr); 327 return exp.evaluate(node, qName); 328 } 329 330 340 341 public String evaluate(String expr, Object node) throws XPathExpressionException { 342 XPathExpression exp = compile(expr); 343 return exp.evaluate(node); 344 } 345 346 360 361 public Object evaluate(String expr, InputSource inputSource, QName qName) throws XPathExpressionException { 362 XPathExpression exp = compile(expr); 363 return exp.evaluate(inputSource, qName); 364 } 365 366 379 380 public String evaluate(String expr, InputSource inputSource) throws XPathExpressionException { 381 XPathExpression exp = compile(expr); 382 return exp.evaluate(inputSource); 383 } 384 385 395 396 public Object evaluateSingle(String expression) throws net.sf.saxon.trans.XPathException { 397 Expression exp = ExpressionTool.make(expression, staticContext,0,-1,1); 398 exp = exp.typeCheck(staticContext, Type.ITEM_TYPE); 399 SlotManager map = staticContext.getConfiguration().makeSlotManager(); 400 ExpressionTool.allocateSlots(exp, 0, map); 401 XPathContextMajor context = new XPathContextMajor(contextNode, staticContext.getConfiguration()); 402 context.openStackFrame(map); 403 SequenceIterator iterator = exp.iterate(context); 404 Item item = iterator.next(); 405 if (item == null) { 406 return null; 407 } else { 408 return Value.convert(item); 409 } 410 } 411 412 417 418 public static void main(String [] args) throws Exception { 419 if (args.length != 2) { 420 System.err.println("format: java XPathEvaluator source.xml \"expression\""); 421 return; 422 } 423 XPathEvaluator xpe = new XPathEvaluator(new StreamSource (new File (args[0]))); 424 List results = xpe.evaluate(args[1]); 425 for (int i = 0; i < results.size(); i++) { 426 Object o = results.get(i); 427 System.err.println(o); 428 } 429 } 430 431 } 432 433 | Popular Tags |