1 package com.icl.saxon.style; 2 import com.icl.saxon.Loader; 3 import com.icl.saxon.FeatureKeys; 4 import com.icl.saxon.expr.StaticContext; 5 import com.icl.saxon.expr.Function; 6 import com.icl.saxon.expr.ExpressionParser; 7 import com.icl.saxon.expr.FunctionProxy; 8 import com.icl.saxon.expr.XPathException; 9 import com.icl.saxon.Binding; 10 import com.icl.saxon.om.Name; 11 import com.icl.saxon.om.Namespace; 12 import com.icl.saxon.om.NamePool; 13 import com.icl.saxon.om.NamespaceException; 14 import com.icl.saxon.pattern.NameTest; 15 import com.icl.saxon.pattern.NamespaceTest; 16 import javax.xml.transform.TransformerException ; 17 18 19 23 24 public class ExpressionContext implements StaticContext { 25 26 private StyleElement element; 27 private NamePool namePool; 28 29 public ExpressionContext(StyleElement styleElement) { 30 element = styleElement; 31 namePool = styleElement.getNamePool(); } 33 34 38 39 public StaticContext makeRuntimeContext(NamePool pool) { 40 ExpressionContext ec = new ExpressionContext(element); 41 ec.namePool = pool; 42 return ec; 43 } 44 45 48 49 public String getSystemId() { 50 return element.getSystemId(); 51 } 52 53 57 58 public int getLineNumber() { 59 return element.getLineNumber(); 60 } 61 62 67 68 public String getBaseURI() { 69 return element.getBaseURI(); 70 } 71 72 77 78 public String getURIForPrefix(String prefix) throws XPathException { 79 try { 80 short uriCode = element.getURICodeForPrefix(prefix); 81 return namePool.getURIFromURICode(uriCode); 82 } catch (NamespaceException err) { 83 throw new XPathException(err.getMessage()); 84 } 85 } 86 87 94 95 public final int makeNameCode(String qname, boolean useDefault) throws XPathException { 96 97 String prefix = Name.getPrefix(qname); 98 try { 99 if (prefix.equals("")) { 100 short uriCode = 0; 101 102 if (!Name.isNCName(qname)) { 103 throw new XPathException("Name " + qname + " contains invalid characters"); 104 } 105 106 if (useDefault) { 107 uriCode = element.getURICodeForPrefix(prefix); 108 } 109 110 return namePool.allocate(prefix, uriCode, qname); 111 112 } else { 113 String localName = Name.getLocalName(qname); 114 short uriCode = element.getURICodeForPrefix(prefix); 115 return namePool.allocate(prefix, uriCode, localName); 116 } 117 } catch (NamespaceException err) { 118 throw new XPathException("Namespace prefix " + prefix + " has not been declared"); 119 } 120 121 } 122 123 131 132 public int getFingerprint(String qname, boolean useDefault) throws XPathException { 133 134 String prefix = Name.getPrefix(qname); 135 if (prefix.equals("")) { 136 String uri = ""; 137 138 if (useDefault) { 139 uri = getURIForPrefix(prefix); 140 } 141 142 return namePool.getFingerprint(uri, qname); 143 144 } else { 145 String localName = Name.getLocalName(qname); 146 String uri = getURIForPrefix(prefix); 147 return namePool.getFingerprint(uri, localName); 148 } 149 } 150 151 154 155 public NameTest makeNameTest(short nodeType, String qname, boolean useDefault) 156 throws XPathException { 157 int nameCode = makeNameCode(qname, useDefault); 158 NameTest nt = new NameTest(nodeType, nameCode); 159 nt.setOriginalText(qname); 160 return nt; 161 } 162 163 166 167 public NamespaceTest makeNamespaceTest(short nodeType, String prefix) 168 throws XPathException { 169 try { 170 short uriCode = element.getURICodeForPrefix(prefix); 171 NamespaceTest nt = new NamespaceTest(namePool, nodeType, uriCode); 172 nt.setOriginalText(prefix + ":*"); 173 return nt; 174 } catch (NamespaceException err) { 175 throw new XPathException(err.getMessage()); 176 } 177 } 178 179 185 186 public Binding bindVariable(int fingerprint) throws XPathException { 187 return element.bindVariable(fingerprint); 188 } 189 190 193 194 public boolean isExtensionNamespace(short uriCode) throws XPathException { 195 return element.isExtensionNamespace(uriCode); 196 } 197 198 201 202 public boolean forwardsCompatibleModeIsEnabled() throws XPathException { 203 return element.forwardsCompatibleModeIsEnabled(); 204 } 205 206 211 212 public Function getStyleSheetFunction(int fingerprint) throws XPathException { 213 return element.getStyleSheetFunction(fingerprint); 214 } 215 216 222 223 public Class getExternalJavaClass(String uri) throws TransformerException { 224 225 227 XSLStyleSheet sse = element.getPrincipalStyleSheet(); 228 Class c = sse.getExternalJavaClass(uri); 229 if (c != null) { 230 return c; 231 } 232 233 235 if (uri.equals(Namespace.SAXON)) { 236 return com.icl.saxon.functions.Extensions.class; 237 238 } else if (uri.equals(Namespace.EXSLT_COMMON)) { 239 return com.icl.saxon.exslt.Common.class; 240 } else if (uri.equals(Namespace.EXSLT_SETS)) { 241 return com.icl.saxon.exslt.Sets.class; 242 } else if (uri.equals(Namespace.EXSLT_MATH)) { 243 return com.icl.saxon.exslt.Math.class; 244 } else if (uri.equals(Namespace.EXSLT_DATES_AND_TIMES)) { 245 return com.icl.saxon.exslt.Date.class; 246 247 249 } else { 250 if (!((Boolean )sse.getPreparedStyleSheet().getTransformerFactory(). 251 getAttribute(FeatureKeys.ALLOW_EXTERNAL_FUNCTIONS)).booleanValue()) { 252 throw new TransformerException ("Calls to external functions have been disabled"); 253 } 254 try { 255 256 258 if (uri.startsWith("java:")) { 259 return Loader.getClass(uri.substring(5)); 260 } 261 262 265 int slash = uri.lastIndexOf('/'); 266 if (slash<0) { 267 return Loader.getClass(uri); 268 } else if (slash==uri.length()-1) { 269 return null; 270 } else { 271 return Loader.getClass(uri.substring(slash+1)); 272 } 273 } catch (TransformerException err) { 274 return null; 275 } 276 } 277 } 278 279 283 284 public boolean isElementAvailable(String qname) throws XPathException { 285 if (!Name.isQName(qname)) { 286 throw new XPathException("Invalid QName: " + qname); 287 } 288 289 String prefix = Name.getPrefix(qname); 290 String localName = Name.getLocalName(qname); 291 String uri = getURIForPrefix(prefix); 292 293 return element.getPreparedStyleSheet(). 294 getStyleNodeFactory().isElementAvailable(uri, localName); 295 } 296 297 300 301 public boolean isFunctionAvailable(String qname) throws XPathException { 302 if (!Name.isQName(qname)) { 303 throw new XPathException("Invalid QName: " + qname); 304 } 305 String prefix = Name.getPrefix(qname); 306 String uri = getURIForPrefix(prefix); 307 try { 308 if (prefix.equals("")) { 309 return ExpressionParser.makeSystemFunction(qname)!=null; 310 } 311 312 int fingerprint = getFingerprint(qname, false); 313 if (fingerprint>=0) { 314 Function f = getStyleSheetFunction(fingerprint); 315 if (f!=null) return true; 316 } 317 318 Class theClass = getExternalJavaClass(uri); 319 if (theClass==null) { 320 return false; 321 } 322 323 String localName = Name.getLocalName(qname); 324 325 FunctionProxy fp = new FunctionProxy(); 326 return fp.setFunctionName(theClass, localName); 327 } catch (Exception err) { 328 return false; 329 } 330 } 331 332 335 336 public boolean allowsKeyFunction() { 337 return !(element instanceof XSLKey); 338 } 339 340 343 344 public String getVersion() { 345 return element.getVersion(); 346 } 347 348 351 352 public String toString() { 353 return "Expression Context at " + element.toString(); 354 } 355 356 } 357 358 | Popular Tags |