1 package net.sf.saxon.trans; 2 import net.sf.saxon.Configuration; 3 import net.sf.saxon.value.QNameValue; 4 import net.sf.saxon.expr.StaticContext; 5 import net.sf.saxon.expr.VariableDeclaration; 6 import net.sf.saxon.functions.ConstructorFunctionLibrary; 7 import net.sf.saxon.functions.FunctionLibrary; 8 import net.sf.saxon.functions.FunctionLibraryList; 9 import net.sf.saxon.functions.SystemFunctionLibrary; 10 import net.sf.saxon.instruct.LocationMap; 11 import net.sf.saxon.instruct.SlotManager; 12 import net.sf.saxon.om.*; 13 import net.sf.saxon.type.AtomicType; 14 import net.sf.saxon.type.Type; 15 16 import javax.xml.transform.SourceLocator ; 17 import java.io.Serializable ; 18 import java.util.Comparator ; 19 import java.util.HashMap ; 20 import java.util.Iterator ; 21 22 29 30 public class IndependentContext implements StaticContext, NamespaceResolver, Serializable { 31 32 private NamePool namePool; 33 private HashMap namespaces = new HashMap (10); 34 private HashMap collations = new HashMap (10); 35 private HashMap variables = new HashMap (20); 36 private SlotManager stackFrameMap; 37 private String defaultCollationName = null; 38 private String baseURI = null; 39 private Configuration config; 40 private LocationMap locationMap = new LocationMap(); 41 private FunctionLibrary functionLibrary; 42 private String defaultFunctionNamespace = NamespaceConstant.FN; 43 private StaticContext schemaImporter = null; 44 private NamespaceResolver externalResolver = null; 45 46 49 50 public IndependentContext(Configuration config) { 51 this.config = config; 52 namePool = config.getNamePool(); 53 stackFrameMap = config.makeSlotManager(); 54 clearNamespaces(); 55 56 58 FunctionLibraryList lib = new FunctionLibraryList(); 59 lib.addFunctionLibrary(new SystemFunctionLibrary(SystemFunctionLibrary.XPATH_ONLY)); 60 lib.addFunctionLibrary(getConfiguration().getVendorFunctionLibrary()); 61 lib.addFunctionLibrary(new ConstructorFunctionLibrary(getConfiguration())); 62 if (config.isAllowExternalFunctions()) { 63 lib.addFunctionLibrary(config.getExtensionBinder()); 64 } 65 functionLibrary = lib; 66 } 67 68 72 73 public IndependentContext copy() { 74 IndependentContext ic = new IndependentContext(config); 75 ic.namespaces = new HashMap (namespaces); 76 ic.collations = new HashMap (collations); 77 ic.variables = new HashMap (); 78 ic.defaultCollationName = defaultCollationName; 79 ic.baseURI = baseURI; 80 ic.locationMap = locationMap; 81 ic.functionLibrary = functionLibrary; 82 ic.defaultFunctionNamespace = defaultFunctionNamespace; 83 ic.schemaImporter = schemaImporter; 84 ic.externalResolver = externalResolver; 85 return ic; 86 } 87 88 91 92 public Configuration getConfiguration() { 93 return config; 94 } 95 96 public LocationMap getLocationMap() { 97 return locationMap; 98 } 99 100 public void setLocationMap(LocationMap locationMap) { 101 this.locationMap = locationMap; 102 } 103 104 110 111 public void declareNamespace(String prefix, String uri) { 112 if (prefix==null) { 113 throw new NullPointerException ("Null prefix supplied to declareNamespace()"); 114 } 115 if (uri==null) { 116 throw new NullPointerException ("Null namespace URI supplied to declareNamespace()"); 117 } 118 namespaces.put(prefix, uri); 119 namePool.allocateNamespaceCode(prefix, uri); 120 } 121 122 125 126 public void clearNamespaces() { 127 namespaces.clear(); 128 declareNamespace("xml", NamespaceConstant.XML); 129 declareNamespace("xsl", NamespaceConstant.XSLT); 130 declareNamespace("saxon", NamespaceConstant.SAXON); 131 declareNamespace("xs", NamespaceConstant.SCHEMA); 132 declareNamespace("xdt", NamespaceConstant.XDT); 133 declareNamespace("", ""); 134 } 135 136 140 141 public void clearAllNamespaces() { 142 namespaces.clear(); 143 declareNamespace("xml", NamespaceConstant.XML); 144 declareNamespace("", ""); 145 } 146 147 153 154 public void setNamespaces(NodeInfo node) { 155 namespaces.clear(); 156 int kind = node.getNodeKind(); 157 if (kind == Type.ATTRIBUTE || kind == Type.TEXT || 158 kind == Type.COMMENT || kind == Type.PROCESSING_INSTRUCTION || 159 kind == Type.NAMESPACE) { 160 node = node.getParent(); 161 } 162 if (node == null) { 163 return; 164 } 165 166 AxisIterator iter = node.iterateAxis(Axis.NAMESPACE); 167 while (true) { 168 NodeInfo ns = (NodeInfo)iter.next(); 169 if (ns == null) { 170 return; 171 } 172 declareNamespace(ns.getLocalPart(), ns.getStringValue()); 173 } 174 } 175 176 181 182 public void setNamespaceResolver(NamespaceResolver resolver) { 183 this.externalResolver = resolver; 184 } 185 186 189 190 public void setBaseURI(String baseURI) { 191 this.baseURI = baseURI; 192 } 193 194 200 201 public void declareCollation(String name, Comparator comparator, boolean isDefault) { 202 collations.put(name, comparator); 203 if (isDefault) { 204 defaultCollationName = name; 205 } 206 } 207 208 212 213 public SlotManager getStackFrameMap() { 214 return stackFrameMap; 215 } 216 217 222 223 public Variable declareVariable(QNameValue qname) { 224 Variable var = Variable.make(qname, getConfiguration()); 225 var.setXPathValue(null); 226 int fingerprint = qname.allocateNameCode(getNamePool()) & 0xfffff; 227 variables.put(new Integer (fingerprint), var); 228 stackFrameMap.allocateSlotNumber(fingerprint); 229 return var; 230 } 231 232 241 242 public Variable declareVariable(String qname) throws XPathException { 243 String prefix; 244 String localName; 245 try { 246 String [] parts = Name.getQNameParts(qname); 247 prefix = parts[0]; 248 localName = parts[1]; 249 } catch (QNameException err) { 250 throw new StaticError("Invalid QName for variable: " + qname); 251 } 252 String uri = ""; 253 if (!("".equals(prefix))) { 254 uri = getURIForPrefix(prefix); 255 } 256 QNameValue q = new QNameValue(prefix, uri, localName); 257 Variable var = Variable.make(q, getConfiguration()); 258 int fingerprint = namePool.allocate(prefix, uri, localName) & 0xfffff; 259 variables.put(new Integer (fingerprint), var); 260 stackFrameMap.allocateSlotNumber(fingerprint); 261 return var; 262 } 263 264 265 268 269 public NamePool getNamePool() { 270 return namePool; 271 } 272 273 279 280 public void issueWarning(String s, SourceLocator locator) { 281 System.err.println(s); 282 } 283 284 288 289 public String getSystemId() { 290 return ""; 291 } 292 293 299 300 public String getBaseURI() { 301 return baseURI==null ? "" : baseURI; 302 } 303 304 309 310 public int getLineNumber() { 311 return -1; 312 } 313 314 322 323 public String getURIForPrefix(String prefix) throws XPathException { 324 String uri = getURIForPrefix(prefix, false); 325 if (uri==null) { 326 throw new StaticError("Prefix " + prefix + " has not been declared"); 327 } 328 return uri; 329 } 330 331 public NamespaceResolver getNamespaceResolver() { 332 if (externalResolver != null) { 333 return externalResolver; 334 } else { 335 return this; 336 } 337 } 338 339 348 349 public String getURIForPrefix(String prefix, boolean useDefault) { 350 if (externalResolver != null) { 351 return externalResolver.getURIForPrefix(prefix, useDefault); 352 } 353 if (prefix.equals("") && !useDefault) { 354 return ""; 355 } else { 356 return (String )namespaces.get(prefix); 357 } 358 } 359 360 364 365 public Iterator iteratePrefixes() { 366 if (externalResolver != null) { 367 return externalResolver.iteratePrefixes(); 368 } else { 369 return namespaces.keySet().iterator(); 370 } 371 } 372 373 379 380 public VariableDeclaration bindVariable(int fingerprint) throws StaticError { 381 Variable var = (Variable)variables.get(new Integer (fingerprint)); 382 if (var==null) { 383 throw new StaticError("Undeclared variable in a standalone expression"); 384 } else { 385 return var; 386 } 387 } 388 389 393 394 public FunctionLibrary getFunctionLibrary() { 395 return functionLibrary; 396 } 397 398 401 402 public void setFunctionLibrary(FunctionLibrary lib) { 403 functionLibrary = lib; 404 } 405 406 411 412 public Comparator getCollation(String name) { 413 Configuration config = getConfiguration(); 414 return config.getCollationURIResolver().resolve(name, getBaseURI(), config); 415 } 416 417 422 423 public String getDefaultCollationName() { 424 if (defaultCollationName != null) { 425 return defaultCollationName; 426 } else { 427 return NamespaceConstant.CODEPOINT_COLLATION_URI; 428 } 429 } 430 431 434 435 public short getDefaultElementNamespace() { 436 return NamespaceConstant.NULL_CODE; 437 } 438 439 442 443 public void setDefaultFunctionNamespace(String uri) { 444 defaultFunctionNamespace = uri; 445 } 446 447 450 451 public String getDefaultFunctionNamespace() { 452 return defaultFunctionNamespace; 453 } 454 455 460 461 public boolean isInBackwardsCompatibleMode() { 462 return false; 463 } 464 465 public boolean isImportedSchema(String namespace) { 466 if (schemaImporter == null) { 467 return false; 468 } 469 return schemaImporter.isImportedSchema(namespace); 470 } 471 472 480 481 public boolean isAllowedBuiltInType(AtomicType type) { 482 if (schemaImporter == null) { 483 return true; 484 } 485 return schemaImporter.isAllowedBuiltInType(type); 486 487 } 488 489 public void setSchemaImporter(StaticContext importer) { 490 schemaImporter = importer; 491 } 492 493 } 494 495 | Popular Tags |