| 1 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 2 | // Copyright (c) 2018 Saxonica Limited. |
| 3 | // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. |
| 4 | // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 5 | // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0. |
| 6 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 7 | |
| 8 | package net.sf.saxon.xpath; |
| 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.SlotManager; |
| 14 | import net.sf.saxon.lib.NamespaceConstant; |
| 15 | import net.sf.saxon.om.NamespaceResolver; |
| 16 | import net.sf.saxon.om.StructuredQName; |
| 17 | import net.sf.saxon.sxpath.AbstractStaticContext; |
| 18 | import net.sf.saxon.trans.XPathException; |
| 19 | import net.sf.saxon.type.SchemaException; |
| 20 | |
| 21 | import javax.xml.XMLConstants; |
| 22 | import javax.xml.namespace.NamespaceContext; |
| 23 | import javax.xml.transform.Source; |
| 24 | import javax.xml.xpath.XPathFunctionResolver; |
| 25 | import javax.xml.xpath.XPathVariableResolver; |
| 26 | import java.util.Arrays; |
| 27 | import java.util.Iterator; |
| 28 | import java.util.Set; |
| 29 | |
| 30 | /** |
| 31 | * A JAXPXPathStaticContext provides a context for parsing an XPath expression |
| 32 | * in a context other than a stylesheet. In particular, it is used to support |
| 33 | * the JAXP 1.3 XPath API. The JAXP API does not actually expose the StaticContext |
| 34 | * object directly; rather, the static context (namespaces, variables, and functions) |
| 35 | * is manipulated through the XPath object, implemented in Saxon by the {@link XPathEvaluator} |
| 36 | */ |
| 37 | |
| 38 | public class JAXPXPathStaticContext extends AbstractStaticContext |
| 39 | implements NamespaceResolver { |
| 40 | |
| 41 | private SlotManager stackFrameMap; |
| 42 | private XPathFunctionLibrary xpathFunctionLibrary; |
| 43 | |
| 44 | private NamespaceContext namespaceContext = new MinimalNamespaceContext(); |
| 45 | private XPathVariableResolver variableResolver; |
| 46 | |
| 47 | /** |
| 48 | * Create a JAXPXPathStaticContext using a specific Configuration. |
| 49 | * |
| 50 | * @param config the Configuration. For schema-aware XPath expressions, this must be an EnterpriseConfiguration. |
| 51 | */ |
| 52 | |
| 53 | public JAXPXPathStaticContext(/*@NotNull*/ Configuration config) { |
| 54 | setConfiguration(config); |
| 55 | stackFrameMap = config.makeSlotManager(); |
| 56 | setDefaultFunctionLibrary(31); |
| 57 | xpathFunctionLibrary = new XPathFunctionLibrary(); |
| 58 | addFunctionLibrary(xpathFunctionLibrary); |
| 59 | setPackageData(new PackageData(getConfiguration())); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Supply the NamespaceContext used to resolve namespaces. |
| 64 | */ |
| 65 | |
| 66 | public void setNamespaceContext(NamespaceContext context) { |
| 67 | this.namespaceContext = context; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Get the NamespaceContext that was set using {@link #setNamespaceContext} |
| 72 | */ |
| 73 | |
| 74 | public NamespaceContext getNamespaceContext() { |
| 75 | return namespaceContext; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Get the stack frame map containing the slot number allocations for the variables declared |
| 80 | * in this static context |
| 81 | */ |
| 82 | |
| 83 | public SlotManager getStackFrameMap() { |
| 84 | return stackFrameMap; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Set an XPathVariableResolver. This is used to resolve variable references |
| 89 | * if no variable has been explicitly declared. |
| 90 | * |
| 91 | * @param resolver A JAXP 1.3 XPathVariableResolver |
| 92 | */ |
| 93 | |
| 94 | public void setXPathVariableResolver(XPathVariableResolver resolver) { |
| 95 | this.variableResolver = resolver; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Get the XPathVariableResolver |
| 100 | */ |
| 101 | |
| 102 | public XPathVariableResolver getXPathVariableResolver() { |
| 103 | return variableResolver; |
| 104 | } |
| 105 | |
| 106 | public void setXPathFunctionResolver(XPathFunctionResolver xPathFunctionResolver) { |
| 107 | if (xpathFunctionLibrary != null) { |
| 108 | xpathFunctionLibrary.setXPathFunctionResolver(xPathFunctionResolver); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | /*@Nullable*/ |
| 113 | public XPathFunctionResolver getXPathFunctionResolver() { |
| 114 | if (xpathFunctionLibrary != null) { |
| 115 | return xpathFunctionLibrary.getXPathFunctionResolver(); |
| 116 | } else { |
| 117 | return null; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | /*@NotNull*/ |
| 122 | public NamespaceResolver getNamespaceResolver() { |
| 123 | return this; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Get the namespace URI corresponding to a given prefix. Return null |
| 128 | * if the prefix is not in scope. This method searches |
| 129 | * any namespace context supplied using {@link #setNamespaceContext(javax.xml.namespace.NamespaceContext)}. |
| 130 | * |
| 131 | * @param prefix the namespace prefix |
| 132 | * @param useDefault true if the default namespace for elements and types is to be used when the |
| 133 | * prefix is "" |
| 134 | * @return the uri for the namespace, or null if the prefix is not in scope. |
| 135 | * Return "" if the prefix maps to the null namespace. |
| 136 | */ |
| 137 | |
| 138 | public String getURIForPrefix(/*@NotNull*/ String prefix, boolean useDefault) { |
| 139 | if (prefix.isEmpty()) { |
| 140 | if (useDefault) { |
| 141 | return getDefaultElementNamespace(); |
| 142 | } else { |
| 143 | return NamespaceConstant.NULL; |
| 144 | } |
| 145 | } else { |
| 146 | return namespaceContext.getNamespaceURI(prefix); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Get an iterator over all the prefixes declared in this namespace context. This method is implemented |
| 152 | * only in the case where the NamespaceContext supplied using {@link #setNamespaceContext} is an |
| 153 | * instance of Saxon's {@link NamespaceResolver} class. In other cases the method throws an |
| 154 | * UnsupportedOperationException |
| 155 | * |
| 156 | * @return an iterator over all the inscope namespace prefixes, if available |
| 157 | * @throws UnsupportedOperationException if the NamespaceContext object is not a NamespaceResolver. |
| 158 | */ |
| 159 | |
| 160 | public Iterator<String> iteratePrefixes() { |
| 161 | if (namespaceContext instanceof NamespaceResolver) { |
| 162 | return ((NamespaceResolver) namespaceContext).iteratePrefixes(); |
| 163 | } else { |
| 164 | throw new UnsupportedOperationException(); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Bind a variable used in an XPath Expression to the XSLVariable element in which it is declared. |
| 170 | * This method is provided for use by the XPath parser, and it should not be called by the user of |
| 171 | * the API. |
| 172 | * |
| 173 | * @param qName the name of the variable to be bound |
| 174 | * @throws XPathException if no VariableResolver has been supplied. |
| 175 | */ |
| 176 | |
| 177 | /*@NotNull*/ |
| 178 | public final Expression bindVariable(StructuredQName qName) throws XPathException { |
| 179 | // bindVariable is called at compile time, but the JAXP variable resolver |
| 180 | // is designed to be called at run time. So we need to create a variable now, |
| 181 | // which will call the variableResolver when called upon to return the run-time value |
| 182 | if (variableResolver != null) { |
| 183 | // Note that despite its name, JAXPVariableReference is not actually a VariableReference |
| 184 | return new JAXPVariableReference(qName, variableResolver); |
| 185 | } else { |
| 186 | throw new XPathException( |
| 187 | "Variable is used in XPath expression, but no JAXP VariableResolver is available"); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Import a schema. This is possible only if Saxon-EE is being used, |
| 193 | * and if the Configuration is a EnterpriseConfiguration. Having imported a schema, the types |
| 194 | * defined in that schema become part of the static context. |
| 195 | * |
| 196 | * @param source A Source object identifying the schema document to be loaded |
| 197 | * @throws net.sf.saxon.type.SchemaException |
| 198 | * if the schema contained in this document is invalid |
| 199 | * @throws UnsupportedOperationException if the configuration is not schema-aware |
| 200 | */ |
| 201 | |
| 202 | public void importSchema(Source source) throws SchemaException { |
| 203 | getConfiguration().addSchemaSource(source, getConfiguration().getErrorListener()); |
| 204 | setSchemaAware(true); |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Determine whether a Schema for a given target namespace has been imported. Note that the |
| 209 | * in-scope element declarations, attribute declarations and schema types are the types registered |
| 210 | * with the (schema-aware) configuration, provided that their namespace URI is registered |
| 211 | * in the static context as being an imported schema namespace. (A consequence of this is that |
| 212 | * within a Configuration, there can only be one schema for any given namespace, including the |
| 213 | * null namespace). |
| 214 | * |
| 215 | * @return true if schema components for the given namespace have been imported into the |
| 216 | * schema-aware configuration |
| 217 | */ |
| 218 | |
| 219 | public boolean isImportedSchema(String namespace) { |
| 220 | return getConfiguration().isSchemaAvailable(namespace); |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Get the set of imported schemas |
| 225 | * |
| 226 | * @return a Set, the set of URIs representing the names of imported schemas |
| 227 | */ |
| 228 | |
| 229 | public Set<String> getImportedSchemaNamespaces() { |
| 230 | return getConfiguration().getImportedNamespaces(); |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Define a minimal namespace context for use when no user-defined namespace context has been |
| 235 | * registered |
| 236 | */ |
| 237 | |
| 238 | private static class MinimalNamespaceContext implements NamespaceContext, NamespaceResolver { |
| 239 | |
| 240 | /** |
| 241 | * Get the namespace URI bound to a prefix in the current scope.</p> |
| 242 | * |
| 243 | * @param prefix the prefix to look up |
| 244 | * @return Namespace URI bound to prefix in the current scope |
| 245 | */ |
| 246 | /*@Nullable*/ |
| 247 | public String getNamespaceURI(/*@Nullable*/ String prefix) { |
| 248 | if (prefix == null) { |
| 249 | throw new IllegalArgumentException("prefix"); |
| 250 | } else if (prefix.equals(XMLConstants.DEFAULT_NS_PREFIX)) { |
| 251 | return ""; //XMLConstants.NULL_NS_URI; |
| 252 | } else if (prefix.equals("xml")) { |
| 253 | return NamespaceConstant.XML; |
| 254 | } else if (prefix.equals("xs")) { |
| 255 | return NamespaceConstant.SCHEMA; |
| 256 | } else if (prefix.equals("xsi")) { |
| 257 | return NamespaceConstant.SCHEMA_INSTANCE; |
| 258 | } else if (prefix.equals("saxon")) { |
| 259 | return NamespaceConstant.SAXON; |
| 260 | } else { |
| 261 | return null; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * <p>Get prefix bound to Namespace URI in the current scope.</p> |
| 267 | * |
| 268 | * @throws UnsupportedOperationException (always) |
| 269 | */ |
| 270 | /*@NotNull*/ |
| 271 | public String getPrefix(String namespaceURI) { |
| 272 | throw new UnsupportedOperationException(); |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * <p>Get all prefixes bound to a Namespace URI in the current |
| 277 | * |
| 278 | * @throws UnsupportedOperationException (always) |
| 279 | */ |
| 280 | /*@NotNull*/ |
| 281 | public Iterator getPrefixes(String namespaceURI) { |
| 282 | throw new UnsupportedOperationException(); |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * Get an iterator over all the prefixes declared in this namespace context. This will include |
| 287 | * the default namespace (prefix="") and the XML namespace where appropriate |
| 288 | */ |
| 289 | |
| 290 | public Iterator<String> iteratePrefixes() { |
| 291 | String[] prefixes = {"", "xml", "xs", "xsi", "saxon"}; |
| 292 | return Arrays.asList(prefixes).iterator(); |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * Get the namespace URI corresponding to a given prefix. Return null |
| 297 | * if the prefix is not in scope. |
| 298 | * |
| 299 | * @param prefix the namespace prefix. May be the zero-length string, indicating |
| 300 | * that there is no prefix. This indicates either the default namespace or the |
| 301 | * null namespace, depending on the value of useDefault. |
| 302 | * @param useDefault true if the default namespace is to be used when the |
| 303 | * prefix is "". If false, the method returns "" when the prefix is "". |
| 304 | * @return the uri for the namespace, or null if the prefix is not in scope. |
| 305 | * The "null namespace" is represented by the pseudo-URI "". |
| 306 | */ |
| 307 | |
| 308 | /*@Nullable*/ |
| 309 | public String getURIForPrefix(String prefix, boolean useDefault) { |
| 310 | return getNamespaceURI(prefix); |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | } |
| 315 |