1 package net.sf.saxon.sxpath; 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.instruct.SlotManager; 8 import net.sf.saxon.om.AllElementStripper; 9 import net.sf.saxon.om.NamePool; 10 import net.sf.saxon.om.NamespaceResolver; 11 import net.sf.saxon.om.NodeInfo; 12 import net.sf.saxon.trans.IndependentContext; 13 import net.sf.saxon.trans.XPathException; 14 import net.sf.saxon.type.Type; 15 16 import javax.xml.transform.Source ; 17 import javax.xml.transform.stream.StreamSource ; 18 import java.io.File ; 19 import java.util.List ; 20 21 29 30 public class XPathEvaluator { 31 32 private IndependentContext staticContext; 33 private boolean stripSpace = false; 34 35 38 39 public XPathEvaluator() { 40 this(new Configuration()); 41 } 42 43 47 public XPathEvaluator(Configuration config) { 48 staticContext = new IndependentContext(config); 49 } 50 51 54 55 public Configuration getConfiguration() { 56 return staticContext.getConfiguration(); 57 } 58 59 65 66 public void setStripSpace(boolean strip) { 67 stripSpace = strip; 68 } 69 70 78 79 public NodeInfo build(Source source) throws XPathException { 80 NamePool pool; 81 if (source instanceof NodeInfo) { 82 pool = ((NodeInfo)source).getNamePool(); 83 } else { 84 pool = NamePool.getDefaultNamePool(); 85 } 86 Stripper stripper = null; 87 if (stripSpace) { 88 stripper = AllElementStripper.getInstance(); 89 } 90 Configuration config = new Configuration(); 91 config.setNamePool(pool); 92 return Builder.build(source, stripper, config); 93 } 94 95 104 105 public void setStaticContext(IndependentContext context) { 106 staticContext = context; 107 } 108 109 113 114 public IndependentContext getStaticContext() { 115 return staticContext; 116 } 117 118 125 126 public XPathExpression createExpression(String expression) throws XPathException { 127 Expression exp = ExpressionTool.make(expression, staticContext,0,-1,1); 128 exp = exp.typeCheck(staticContext, Type.ITEM_TYPE); 129 SlotManager map = staticContext.getConfiguration().makeSlotManager(); 130 ExpressionTool.allocateSlots(exp, 0, map); 131 XPathExpression xpe = new XPathExpression(this, exp); 132 xpe.setStackFrameMap(map); 133 return xpe; 134 } 135 136 141 142 public void setNamespaceResolver(NamespaceResolver namespaceContext) { 143 staticContext.setNamespaceResolver(namespaceContext); 144 } 145 146 150 151 public NamespaceResolver getNamespaceResolver() { 152 return staticContext.getNamespaceResolver(); 153 } 154 155 160 161 public static void main(String [] args) throws Exception { 162 if (args.length != 2) { 163 System.err.println("format: java XPathEvaluator source.xml \"expression\""); 164 return; 165 } 166 XPathEvaluator xpe = new XPathEvaluator(); 167 XPathExpression exp = xpe.createExpression(args[1]); 168 List results = exp.evaluate(new StreamSource (new File (args[0]))); 169 for (int i = 0; i < results.size(); i++) { 170 Object o = results.get(i); 171 System.err.println(o); 172 } 173 } 174 175 } 176 177 | Popular Tags |