////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2018 Saxonica Limited.
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
// If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
// This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package net.sf.saxon.xpath;
import net.sf.saxon.Configuration;
import net.sf.saxon.expr.Expression;
import net.sf.saxon.expr.PackageData;
import net.sf.saxon.expr.instruct.SlotManager;
import net.sf.saxon.lib.NamespaceConstant;
import net.sf.saxon.om.NamespaceResolver;
import net.sf.saxon.om.StructuredQName;
import net.sf.saxon.sxpath.AbstractStaticContext;
import net.sf.saxon.trans.XPathException;
import net.sf.saxon.type.SchemaException;
import javax.xml.XMLConstants;
import javax.xml.namespace.NamespaceContext;
import javax.xml.transform.Source;
import javax.xml.xpath.XPathFunctionResolver;
import javax.xml.xpath.XPathVariableResolver;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Set;
/**
* A JAXPXPathStaticContext provides a context for parsing an XPath expression
* in a context other than a stylesheet. In particular, it is used to support
* the JAXP 1.3 XPath API. The JAXP API does not actually expose the StaticContext
* object directly; rather, the static context (namespaces, variables, and functions)
* is manipulated through the XPath object, implemented in Saxon by the {@link XPathEvaluator}
*/
public class JAXPXPathStaticContext extends AbstractStaticContext
implements NamespaceResolver {
private SlotManager stackFrameMap;
private XPathFunctionLibrary xpathFunctionLibrary;
private NamespaceContext namespaceContext = new MinimalNamespaceContext();
private XPathVariableResolver variableResolver;
/**
* Create a JAXPXPathStaticContext using a specific Configuration.
*
* @param config the Configuration. For schema-aware XPath expressions, this must be an EnterpriseConfiguration.
*/
public JAXPXPathStaticContext(/*@NotNull*/ Configuration config) {
setConfiguration(config);
stackFrameMap = config.makeSlotManager();
setDefaultFunctionLibrary(31);
xpathFunctionLibrary = new XPathFunctionLibrary();
addFunctionLibrary(xpathFunctionLibrary);
setPackageData(new PackageData(getConfiguration()));
}
/**
* Supply the NamespaceContext used to resolve namespaces.
*/
public void setNamespaceContext(NamespaceContext context) {
this.namespaceContext = context;
}
/**
* Get the NamespaceContext that was set using {@link #setNamespaceContext}
*/
public NamespaceContext getNamespaceContext() {
return namespaceContext;
}
/**
* Get the stack frame map containing the slot number allocations for the variables declared
* in this static context
*/
public SlotManager getStackFrameMap() {
return stackFrameMap;
}
/**
* Set an XPathVariableResolver. This is used to resolve variable references
* if no variable has been explicitly declared.
*
* @param resolver A JAXP 1.3 XPathVariableResolver
*/
public void setXPathVariableResolver(XPathVariableResolver resolver) {
this.variableResolver = resolver;
}
/**
* Get the XPathVariableResolver
*/
public XPathVariableResolver getXPathVariableResolver() {
return variableResolver;
}
public void setXPathFunctionResolver(XPathFunctionResolver xPathFunctionResolver) {
if (xpathFunctionLibrary != null) {
xpathFunctionLibrary.setXPathFunctionResolver(xPathFunctionResolver);
}
}
/*@Nullable*/
public XPathFunctionResolver getXPathFunctionResolver() {
if (xpathFunctionLibrary != null) {
return xpathFunctionLibrary.getXPathFunctionResolver();
} else {
return null;
}
}
/*@NotNull*/
public NamespaceResolver getNamespaceResolver() {
return this;
}
/**
* Get the namespace URI corresponding to a given prefix. Return null
* if the prefix is not in scope. This method searches
* any namespace context supplied using {@link #setNamespaceContext(javax.xml.namespace.NamespaceContext)}.
*
* @param prefix the namespace prefix
* @param useDefault true if the default namespace for elements and types is to be used when the
* prefix is ""
* @return the uri for the namespace, or null if the prefix is not in scope.
* Return "" if the prefix maps to the null namespace.
*/
public String getURIForPrefix(/*@NotNull*/ String prefix, boolean useDefault) {
if (prefix.isEmpty()) {
if (useDefault) {
return getDefaultElementNamespace();
} else {
return NamespaceConstant.NULL;
}
} else {
return namespaceContext.getNamespaceURI(prefix);
}
}
/**
* Get an iterator over all the prefixes declared in this namespace context. This method is implemented
* only in the case where the NamespaceContext supplied using {@link #setNamespaceContext} is an
* instance of Saxon's {@link NamespaceResolver} class. In other cases the method throws an
* UnsupportedOperationException
*
* @return an iterator over all the inscope namespace prefixes, if available
* @throws UnsupportedOperationException if the NamespaceContext object is not a NamespaceResolver.
*/
public Iterator
Get prefix bound to Namespace URI in the current scope.
* * @throws UnsupportedOperationException (always) */ /*@NotNull*/ public String getPrefix(String namespaceURI) { throw new UnsupportedOperationException(); } /** *Get all prefixes bound to a Namespace URI in the current
*
* @throws UnsupportedOperationException (always)
*/
/*@NotNull*/
public Iterator getPrefixes(String namespaceURI) {
throw new UnsupportedOperationException();
}
/**
* Get an iterator over all the prefixes declared in this namespace context. This will include
* the default namespace (prefix="") and the XML namespace where appropriate
*/
public Iterator