1 package net.sf.saxon.sxpath; 2 3 import net.sf.saxon.expr.Expression; 4 import net.sf.saxon.expr.XPathContextMajor; 5 import net.sf.saxon.instruct.SlotManager; 6 import net.sf.saxon.trans.XPathException; 7 import net.sf.saxon.value.SequenceExtent; 8 import net.sf.saxon.value.Value; 9 import net.sf.saxon.om.NodeInfo; 10 import net.sf.saxon.om.SequenceIterator; 11 import net.sf.saxon.om.Item; 12 13 import javax.xml.transform.Source ; 14 import java.util.Collections ; 15 import java.util.List ; 16 17 24 25 26 public class XPathExpression { 27 28 private XPathEvaluator evaluator; 29 private Expression expression; 30 private SlotManager stackFrameMap; 31 32 36 37 protected XPathExpression(XPathEvaluator evaluator, Expression exp) { 38 expression = exp; 39 this.evaluator = evaluator; 40 } 41 42 45 46 protected void setStackFrameMap(SlotManager map) { 47 stackFrameMap = map; 48 } 49 50 61 62 public List evaluate(Source source) throws XPathException { 63 NodeInfo origin; 64 if (source instanceof NodeInfo) { 65 origin = (NodeInfo)source; 66 } else { 67 origin = evaluator.build(source); 68 } 69 XPathContextMajor context = new XPathContextMajor(origin, evaluator.getConfiguration()); 70 context.openStackFrame(stackFrameMap); 71 SequenceIterator iter = expression.iterate(context); 72 SequenceExtent extent = new SequenceExtent(iter); 73 List result = (List )extent.convertToJava(List .class, context); 74 if (result == null) { 75 result = Collections.EMPTY_LIST; 76 } 77 return result; 78 } 79 80 94 95 public Object evaluateSingle(Source source) throws XPathException { 96 NodeInfo origin; 97 if (source instanceof NodeInfo) { 98 origin = (NodeInfo)source; 99 } else { 100 origin = evaluator.build(source); 101 } 102 XPathContextMajor context = new XPathContextMajor(origin, evaluator.getConfiguration()); 103 context.openStackFrame(stackFrameMap); 104 SequenceIterator iterator = expression.iterate(context); 105 Item item = iterator.next(); 106 if (item == null) { 107 return null; 108 } else { 109 return Value.convert(item); 110 } 111 } 112 113 123 124 public SequenceIterator rawIterator(Source source) throws XPathException { 125 NodeInfo origin; 126 if (source instanceof NodeInfo) { 127 origin = (NodeInfo)source; 128 } else { 129 origin = evaluator.build(source); 130 } 131 XPathContextMajor context = new XPathContextMajor(origin, evaluator.getConfiguration()); 132 context.openStackFrame(stackFrameMap); 133 SequenceIterator iterator = expression.iterate(context); 134 return iterator; 135 } 136 137 144 145 public Expression getInternalExpression() { 146 return expression; 147 } 148 149 } 150 151 162 | Popular Tags |