1 package net.sf.saxon.xpath; 2 import net.sf.saxon.Configuration; 3 import net.sf.saxon.expr.StaticContext; 4 import net.sf.saxon.expr.VariableDeclaration; 5 import net.sf.saxon.functions.ConstructorFunctionLibrary; 6 import net.sf.saxon.functions.FunctionLibrary; 7 import net.sf.saxon.functions.FunctionLibraryList; 8 import net.sf.saxon.functions.SystemFunctionLibrary; 9 import net.sf.saxon.instruct.LocationMap; 10 import net.sf.saxon.instruct.SlotManager; 11 import net.sf.saxon.om.*; 12 import net.sf.saxon.trans.StaticError; 13 import net.sf.saxon.trans.Variable; 14 import net.sf.saxon.trans.XPathException; 15 import net.sf.saxon.type.AtomicType; 16 import net.sf.saxon.value.QNameValue; 17 18 import javax.xml.namespace.NamespaceContext ; 19 import javax.xml.transform.SourceLocator ; 20 import javax.xml.xpath.XPathFunctionResolver ; 21 import javax.xml.xpath.XPathVariableResolver ; 22 import java.util.Comparator ; 23 import java.util.HashMap ; 24 import java.util.Iterator ; 25 26 33 34 public class StandaloneContext implements StaticContext, NamespaceResolver { 35 36 private NamePool namePool; 37 private HashMap namespaces = new HashMap (10); 38 private HashMap collations = new HashMap (10); 39 private HashMap variables = new HashMap (20); 40 private SlotManager stackFrameMap; 41 private String defaultCollationName = null; 42 private String baseURI = null; 43 private Configuration config; 44 private LocationMap locationMap = new LocationMap(); 45 private FunctionLibrary functionLibrary; 46 private XPathFunctionLibrary xpathFunctionLibrary; 47 private String defaultFunctionNamespace = NamespaceConstant.FN; 48 private short defaultElementNamespace = NamespaceConstant.NULL_CODE; 49 private boolean backwardsCompatible = false; 50 51 private NamespaceContext namespaceContext; 52 private XPathVariableResolver variableResolver; 53 54 57 58 public StandaloneContext() { 59 this(new Configuration()); 60 } 61 62 65 66 public StandaloneContext(Configuration config) { 67 this.config = config; 68 namePool = config.getNamePool(); 69 stackFrameMap = config.makeSlotManager(); 70 clearNamespaces(); 71 72 74 FunctionLibraryList lib = new FunctionLibraryList(); 75 lib.addFunctionLibrary(new SystemFunctionLibrary(SystemFunctionLibrary.XPATH_ONLY)); 76 lib.addFunctionLibrary(getConfiguration().getVendorFunctionLibrary()); 77 lib.addFunctionLibrary(new ConstructorFunctionLibrary(getConfiguration())); 78 if (config.isAllowExternalFunctions()) { 79 xpathFunctionLibrary = new XPathFunctionLibrary(); 80 lib.addFunctionLibrary(xpathFunctionLibrary); 81 lib.addFunctionLibrary(config.getExtensionBinder()); 82 } 83 functionLibrary = lib; 84 } 85 86 90 91 public StandaloneContext(NodeInfo node) { 92 DocumentInfo doc = node.getDocumentRoot(); 93 if (doc==null) { 94 throw new IllegalArgumentException ( 95 "The node used to establish a standalone context must be in a tree whose root is a document node"); 96 } 97 namePool = doc.getNamePool(); 98 setNamespaces(node); 99 } 100 101 104 105 public Configuration getConfiguration() { 106 return config; 107 } 108 109 public LocationMap getLocationMap() { 110 return locationMap; 111 } 112 113 public void setLocationMap(LocationMap locationMap) { 114 this.locationMap = locationMap; 115 } 116 117 126 127 public void declareNamespace(String prefix, String uri) { 128 if (prefix==null) { 129 throw new NullPointerException ("Null prefix supplied to declareNamespace()"); 130 } 131 if (uri==null) { 132 throw new NullPointerException ("Null namespace URI supplied to declareNamespace()"); 133 } 134 namespaces.put(prefix, uri); 135 namePool.allocateNamespaceCode(prefix, uri); 136 } 137 138 143 144 public void setNamespaceContext(NamespaceContext context) { 145 this.namespaceContext = context; 146 } 147 148 151 152 public NamespaceContext getNamespaceContext() { 153 return namespaceContext; 154 } 155 156 160 161 public void clearNamespaces() { 162 namespaces.clear(); 163 declareNamespace("xml", NamespaceConstant.XML); 164 declareNamespace("xsl", NamespaceConstant.XSLT); 165 declareNamespace("saxon", NamespaceConstant.SAXON); 166 declareNamespace("xs", NamespaceConstant.SCHEMA); 167 declareNamespace("xdt", NamespaceConstant.XDT); 168 declareNamespace("", ""); 169 } 170 171 175 176 public void clearAllNamespaces() { 177 namespaces.clear(); 178 declareNamespace("xml", NamespaceConstant.XML); 179 declareNamespace("", ""); 180 } 181 182 188 189 public void setNamespaces(NodeInfo node) { 190 namespaces.clear(); 191 AxisIterator iter = node.iterateAxis(Axis.NAMESPACE); 192 while (true) { 193 NodeInfo ns = (NodeInfo)iter.next(); 194 if (ns == null) { 195 return; 196 } 197 declareNamespace(ns.getLocalPart(), ns.getStringValue()); 198 } 199 } 200 201 204 205 public void setBaseURI(String baseURI) { 206 this.baseURI = baseURI; 207 } 208 209 215 216 public void declareCollation(String name, Comparator comparator, boolean isDefault) { 217 collations.put(name, comparator); 218 if (isDefault) { 219 defaultCollationName = name; 220 } 221 } 222 223 227 228 public SlotManager getStackFrameMap() { 229 return stackFrameMap; 230 } 231 232 243 244 public Variable declareVariable(String qname, Object initialValue) throws XPathException { 245 String prefix; 246 String localName; 247 try { 248 String [] parts = Name.getQNameParts(qname); 249 prefix = parts[0]; 250 localName = parts[1]; 251 } catch (QNameException err) { 252 throw new StaticError("Invalid QName for variable: " + qname); 253 } 254 String uri = ""; 255 if (!("".equals(prefix))) { 256 uri = getURIForPrefix(prefix); 257 } 258 QNameValue q = new QNameValue(prefix, uri, localName); 259 Variable var = Variable.make(q, getConfiguration()); 260 if (initialValue instanceof ValueRepresentation) { 261 var.setXPathValue((ValueRepresentation)initialValue); 262 } else { 263 var.setValue(initialValue); 264 } 265 int fingerprint = namePool.allocate(prefix, uri, localName) & 0xfffff; 266 variables.put(new Integer (fingerprint), var); 267 stackFrameMap.allocateSlotNumber(fingerprint); 268 return var; 269 } 270 271 276 277 public void setXPathVariableResolver(XPathVariableResolver resolver) { 278 this.variableResolver = resolver; 279 } 280 281 284 285 public XPathVariableResolver getXPathVariableResolver() { 286 return variableResolver; 287 } 288 289 public void setXPathFunctionResolver(XPathFunctionResolver xPathFunctionResolver) { 290 if (xpathFunctionLibrary != null) { 291 xpathFunctionLibrary.setXPathFunctionResolver(xPathFunctionResolver); 292 } 293 } 295 296 public XPathFunctionResolver getXPathFunctionResolver() { 297 if (xpathFunctionLibrary != null) { 298 return xpathFunctionLibrary.getXPathFunctionResolver(); 299 } else { 300 return null; 301 } 302 } 303 304 307 308 public NamePool getNamePool() { 309 return namePool; 310 } 311 312 318 319 public void issueWarning(String s, SourceLocator locator) { 320 System.err.println(s); 321 } 322 323 327 328 public String getSystemId() { 329 return ""; 330 } 331 332 338 339 public String getBaseURI() { 340 return baseURI==null ? "" : baseURI; 341 } 342 343 348 349 public int getLineNumber() { 350 return -1; 351 } 352 353 361 362 public String getURIForPrefix(String prefix) throws XPathException { 363 String uri = getURIForPrefix(prefix, false); 364 if (uri==null) { 365 throw new StaticError("Prefix " + prefix + " has not been declared"); 366 } 367 return uri; 368 } 369 370 public NamespaceResolver getNamespaceResolver() { 371 return this; 372 } 373 374 385 386 public String getURIForPrefix(String prefix, boolean useDefault) { 387 if (prefix.equals("") && !useDefault) { 388 return ""; 389 } else { 390 String uri = (String )namespaces.get(prefix); 391 if (uri == null && namespaceContext != null) { 392 return namespaceContext.getNamespaceURI(prefix); 393 } else { 394 return uri; 395 } 396 } 397 } 398 399 407 408 public Iterator iteratePrefixes() { 409 return namespaces.keySet().iterator(); 410 } 411 412 424 425 public VariableDeclaration bindVariable(int fingerprint) throws StaticError { 426 Variable var = (Variable)variables.get(new Integer (fingerprint)); 427 if (var!=null) { 428 return var; 429 } 430 if (variableResolver != null) { 434 QNameValue qname = new QNameValue(namePool, fingerprint); 435 436 return new JAXPVariable(qname, variableResolver); 437 } 438 throw new StaticError("Undeclared variable in a standalone expression"); 439 440 } 441 442 446 447 public FunctionLibrary getFunctionLibrary() { 448 return functionLibrary; 449 } 450 451 454 455 public void setFunctionLibrary(FunctionLibrary lib) { 456 functionLibrary = lib; 457 } 458 459 464 465 public Comparator getCollation(String name) { 466 Configuration config = getConfiguration(); 467 return config.getCollationURIResolver().resolve(name, getBaseURI(), config); 468 } 469 470 475 476 public String getDefaultCollationName() { 477 if (defaultCollationName != null) { 478 return defaultCollationName; 479 } else { 480 return NamespaceConstant.CODEPOINT_COLLATION_URI; 481 } 482 } 483 484 487 488 public void setDefaultElementNamespace(String uri) { 489 defaultElementNamespace = namePool.allocateCodeForURI(uri); 490 } 491 492 495 496 public short getDefaultElementNamespace() { 497 return defaultElementNamespace; 498 } 499 500 503 504 public void setDefaultFunctionNamespace(String uri) { 505 defaultFunctionNamespace = uri; 506 } 507 508 511 512 public String getDefaultFunctionNamespace() { 513 return defaultFunctionNamespace; 514 } 515 516 521 522 public void setBackwardsCompatibilityMode(boolean backwardsCompatible) { 523 this.backwardsCompatible = true; 524 } 525 530 531 public boolean isInBackwardsCompatibleMode() { 532 return backwardsCompatible; 533 } 534 535 545 546 public boolean isImportedSchema(String namespace) { 547 return false; 548 } 549 550 558 559 public boolean isAllowedBuiltInType(AtomicType type) { 560 return true; 561 } 562 } 563 564 | Popular Tags |