| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.sxpath; |
| 9 |
|
| 10 |
import net.sf.saxon.Configuration; |
| 11 |
import net.sf.saxon.expr.Expression; |
| 12 |
import net.sf.saxon.expr.PackageData; |
| 13 |
import net.sf.saxon.expr.instruct.Executable; |
| 14 |
import net.sf.saxon.expr.instruct.SlotManager; |
| 15 |
import net.sf.saxon.expr.parser.*; |
| 16 |
import net.sf.saxon.functions.FunctionLibrary; |
| 17 |
import net.sf.saxon.functions.FunctionLibraryList; |
| 18 |
import net.sf.saxon.functions.registry.ConstructorFunctionLibrary; |
| 19 |
import net.sf.saxon.pattern.Pattern; |
| 20 |
import net.sf.saxon.trans.XPathException; |
| 21 |
import net.sf.saxon.type.ItemType; |
| 22 |
import net.sf.saxon.type.Type; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public class XPathEvaluator { |
| 36 |
|
| 37 |
private XPathStaticContext staticContext; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public XPathEvaluator() { |
| 47 |
this(new Configuration()); |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public XPathEvaluator(Configuration config) { |
| 58 |
staticContext = new IndependentContext(config); |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public Configuration getConfiguration() { |
| 68 |
return staticContext.getConfiguration(); |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
public void setStaticContext(XPathStaticContext context) { |
| 85 |
staticContext = context; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public XPathStaticContext getStaticContext() { |
| 97 |
return staticContext; |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
public XPathExpression createExpression(String expression) throws XPathException { |
| 110 |
Configuration config = getConfiguration(); |
| 111 |
Executable exec = new Executable(config); |
| 112 |
|
| 113 |
boolean allowXPath30 = staticContext.getXPathVersion() >= 30; |
| 114 |
exec.setTopLevelPackage(staticContext.getPackageData()); |
| 115 |
exec.setSchemaAware(staticContext.getPackageData().isSchemaAware()); |
| 116 |
exec.setHostLanguage(Configuration.XPATH); |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
FunctionLibrary userlib = exec.getFunctionLibrary(); |
| 122 |
FunctionLibraryList lib = new FunctionLibraryList(); |
| 123 |
lib.addFunctionLibrary(config.getXPath31FunctionSet()); |
| 124 |
lib.addFunctionLibrary(config.getBuiltInExtensionLibraryList()); |
| 125 |
lib.addFunctionLibrary(new ConstructorFunctionLibrary(config)); |
| 126 |
lib.addFunctionLibrary(config.getIntegratedFunctionLibrary()); |
| 127 |
config.addExtensionBinders(lib); |
| 128 |
if (userlib != null) { |
| 129 |
lib.addFunctionLibrary(userlib); |
| 130 |
} |
| 131 |
exec.setFunctionLibrary(lib); |
| 132 |
|
| 133 |
Optimizer opt = config.obtainOptimizer(); |
| 134 |
Expression exp = ExpressionTool.make(expression, staticContext, 0, -1, null); |
| 135 |
RetainedStaticContext rsc = staticContext.makeRetainedStaticContext(); |
| 136 |
exp.setRetainedStaticContext(rsc); |
| 137 |
ExpressionVisitor visitor = ExpressionVisitor.make(staticContext); |
| 138 |
ItemType contextItemType = staticContext.getRequiredContextItemType(); |
| 139 |
ContextItemStaticInfo cit = config.makeContextItemStaticInfo(contextItemType, true); |
| 140 |
exp = exp.typeCheck(visitor, cit); |
| 141 |
if (opt.isOptionSet(OptimizerOptions.MISCELLANEOUS)) { |
| 142 |
exp = exp.optimize(visitor, cit); |
| 143 |
} |
| 144 |
if (opt.isOptionSet(OptimizerOptions.LOOP_LIFTING)) { |
| 145 |
exp.setParentExpression(null); |
| 146 |
exp = LoopLifter.process(exp, visitor, cit); |
| 147 |
} |
| 148 |
exp = postProcess(exp, visitor, cit); |
| 149 |
exp.setRetainedStaticContext(rsc); |
| 150 |
SlotManager map = staticContext.getStackFrameMap(); |
| 151 |
int numberOfExternalVariables = map.getNumberOfVariables(); |
| 152 |
ExpressionTool.allocateSlots(exp, numberOfExternalVariables, map); |
| 153 |
XPathExpression xpe = new XPathExpression(staticContext, exp, exec); |
| 154 |
xpe.setStackFrameMap(map, numberOfExternalVariables); |
| 155 |
return xpe; |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
protected Expression postProcess(Expression exp, ExpressionVisitor visitor, ContextItemStaticInfo cit) throws XPathException { |
| 167 |
return exp; |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
public XPathExpression createPattern(String pattern) throws XPathException { |
| 184 |
Configuration config = getConfiguration(); |
| 185 |
Executable exec = new Executable(config); |
| 186 |
Pattern pat = Pattern.make(pattern, staticContext, new PackageData(config)); |
| 187 |
ExpressionVisitor visitor = ExpressionVisitor.make(staticContext); |
| 188 |
pat.typeCheck(visitor, config.makeContextItemStaticInfo(Type.NODE_TYPE, true)); |
| 189 |
SlotManager map = staticContext.getStackFrameMap(); |
| 190 |
int slots = map.getNumberOfVariables(); |
| 191 |
slots = pat.allocateSlots(map, slots); |
| 192 |
|
| 193 |
XPathExpression xpe = new XPathExpression(staticContext, pat, exec); |
| 194 |
xpe.setStackFrameMap(map, slots); |
| 195 |
return xpe; |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
} |
| 200 |
|