net.sf.saxon.sxpath
Class IndependentContext

java.lang.Object
  extended by net.sf.saxon.sxpath.AbstractStaticContext
      extended by net.sf.saxon.sxpath.IndependentContext
All Implemented Interfaces:
Serializable, StaticContext, NamespaceResolver, XPathStaticContext
Direct Known Subclasses:
DedicatedStaticContext

public class IndependentContext
extends AbstractStaticContext
implements XPathStaticContext, NamespaceResolver, Serializable

An IndependentContext provides a context for parsing an XPath expression appearing in a context other than a stylesheet.

This class is used in a number of places where freestanding XPath expressions occur. These include the native Saxon XPath API, the .NET XPath API, XPath expressions used in XML Schema identity constraints, and XPath expressions supplied to saxon:evaluate(). It is not used by the JAXP XPath API (though it shares code with that API through the common superclass AbstractStaticContext).

This class currently provides no mechanism for binding user-defined functions.

See Also:
Serialized Form

Field Summary
protected  boolean autoDeclare
           
protected  NamespaceResolver externalResolver
           
protected  Set importedSchemaNamespaces
           
protected  HashMap<String,String> namespaces
           
protected  ItemType requiredContextItemType
           
protected  HashMap<StructuredQName,XPathVariable> variables
           
 
Fields inherited from class net.sf.saxon.sxpath.AbstractStaticContext
usingDefaultFunctionLibrary
 
Constructor Summary
IndependentContext()
          Create an IndependentContext along with a new (non-schema-aware) Saxon Configuration
IndependentContext(Configuration config)
          Create an IndependentContext using a specific Configuration
 
Method Summary
 Expression bindVariable(StructuredQName qName)
          Bind a variable used in an XPath Expression to the XSLVariable element in which it is declared.
 void clearAllNamespaces()
          Clear all the declared namespaces, including the standard ones (xml, xslt, saxon).
 void clearNamespaces()
          Clear all the declared namespaces, except for the standard ones (xml, xslt, saxon, xdt).
 void declareNamespace(String prefix, String uri)
          Declare a namespace whose prefix can be used in expressions
 XPathVariable declareVariable(QNameValue qname)
          Declare a variable.
 XPathVariable declareVariable(String namespaceURI, String localName)
          Declare a variable.
 XPathVariable declareVariable(StructuredQName qName)
          Declare a variable.
 int getContainerGranularity()
          Get the granularity of the container.
 XPathVariable getExternalVariable(StructuredQName qName)
          Get the declared variable with a given name, if there is one
 Set<String> getImportedSchemaNamespaces()
          Get the set of imported schemas
 NamespaceResolver getNamespaceResolver()
          Get a namespace resolver to resolve the namespaces declared in this static context.
 ItemType getRequiredContextItemType()
          Get the required type of the context item.
 int getSlotNumber(QNameValue qname)
          Get the slot number allocated to a particular variable
 SlotManager getStackFrameMap()
          Get a Stack Frame Map containing definitions of all the declared variables.
 String getURIForPrefix(String prefix)
          Get the URI for a prefix, using the declared namespaces as the context for namespace resolution.
 String getURIForPrefix(String prefix, boolean useDefault)
          Get the namespace URI corresponding to a given prefix.
 boolean isAllowUndeclaredVariables()
          Ask whether undeclared variables are allowed.
 boolean isImportedSchema(String namespace)
          Ask whether a Schema for a given target namespace has been imported.
 Iterator<XPathVariable> iterateExternalVariables()
          Get an iterator over all the variables that have been declared, either explicitly by an application call on declareVariable(), or implicitly if the option allowUndeclaredVariables is set.
 Iterator iteratePrefixes()
          Get an iterator over all the prefixes declared in this namespace context.
 void setAllowUndeclaredVariables(boolean allow)
          Say whether undeclared variables are allowed.
 void setDefaultElementNamespace(String uri)
          Set the default namespace for elements and types
 void setImportedSchemaNamespaces(Set namespaces)
          Register the set of imported schema namespaces
 void setNamespaceResolver(NamespaceResolver resolver)
          Set an external namespace resolver.
 void setNamespaces(NodeInfo node)
          Declares all the namespaces that are in-scope for a given node, removing all previous namespace declarations.
 void setRequiredContextItemType(ItemType type)
          Declare the static type of the context item.
 
Methods inherited from class net.sf.saxon.sxpath.AbstractStaticContext
addFunctionLibrary, declareCollation, getBaseURI, getCollation, getCollationMap, getColumnNumber, getConfiguration, getDecimalFormatManager, getDefaultCollationName, getDefaultElementNamespace, getDefaultFunctionNamespace, getFunctionLibrary, getHostLanguage, getLineNumber, getLocationMap, getLocationProvider, getNamePool, getPublicId, getSystemId, getXPathLanguageLevel, isAllowedBuiltInType, isInBackwardsCompatibleMode, isSchemaAware, issueWarning, makeEarlyEvaluationContext, setBackwardsCompatibilityMode, setBaseURI, setCollationMap, setConfiguration, setDecimalFormatManager, setDefaultFunctionLibrary, setDefaultFunctionNamespace, setFunctionLibrary, setLocationMap, setSchemaAware, setXPathLanguageLevel
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface net.sf.saxon.expr.StaticContext
getBaseURI, getCollation, getConfiguration, getDecimalFormatManager, getDefaultCollationName, getDefaultElementNamespace, getDefaultFunctionNamespace, getFunctionLibrary, getLineNumber, getLocationMap, getNamePool, getSystemId, getXPathLanguageLevel, isAllowedBuiltInType, isInBackwardsCompatibleMode, isSchemaAware, issueWarning, makeEarlyEvaluationContext
 

Field Detail

namespaces

protected HashMap<String,String> namespaces

variables

protected HashMap<StructuredQName,XPathVariable> variables

externalResolver

protected NamespaceResolver externalResolver

requiredContextItemType

protected ItemType requiredContextItemType

importedSchemaNamespaces

protected Set importedSchemaNamespaces

autoDeclare

protected boolean autoDeclare
Constructor Detail

IndependentContext

public IndependentContext()
Create an IndependentContext along with a new (non-schema-aware) Saxon Configuration


IndependentContext

public IndependentContext(Configuration config)
Create an IndependentContext using a specific Configuration

Parameters:
config - the Saxon configuration to be used
Method Detail

getContainerGranularity

public int getContainerGranularity()
Get the granularity of the container.

Returns:
0 for a temporary container created during parsing; 1 for a container that operates at the level of an XPath expression; 2 for a container at the level of a global function or template

declareNamespace

public void declareNamespace(String prefix,
                             String uri)
Declare a namespace whose prefix can be used in expressions

Parameters:
prefix - The namespace prefix. Must not be null. Supplying "" sets the default element namespace.
uri - The namespace URI. Must not be null.

setDefaultElementNamespace

public void setDefaultElementNamespace(String uri)
Set the default namespace for elements and types

Specified by:
setDefaultElementNamespace in interface XPathStaticContext
Overrides:
setDefaultElementNamespace in class AbstractStaticContext
Parameters:
uri - the namespace to be used for unprefixed element and type names. The value "" (or NamespaceConstant.NULL) represents the non-namespace

clearNamespaces

public void clearNamespaces()
Clear all the declared namespaces, except for the standard ones (xml, xslt, saxon, xdt). This also resets the default element namespace to the "null" namespace


clearAllNamespaces

public void clearAllNamespaces()
Clear all the declared namespaces, including the standard ones (xml, xslt, saxon). Leave only the XML namespace and the default namespace (xmlns=""). This also resets the default element namespace to the "null" namespace.


setNamespaces

public void setNamespaces(NodeInfo node)
Declares all the namespaces that are in-scope for a given node, removing all previous namespace declarations. In addition, the standard namespaces (xml, xslt, saxon) are declared. This method also sets the default element namespace to be the same as the default namespace for this node.

Parameters:
node - The node whose in-scope namespaces are to be used as the context namespaces. If the node is an attribute, text node, etc, then the namespaces of its parent element are used.

setNamespaceResolver

public void setNamespaceResolver(NamespaceResolver resolver)
Set an external namespace resolver. If this is set, then all resolution of namespace prefixes is delegated to the external namespace resolver, and namespaces declared individually on this IndependentContext object are ignored.

Specified by:
setNamespaceResolver in interface XPathStaticContext
Parameters:
resolver - the external NamespaceResolver

setAllowUndeclaredVariables

public void setAllowUndeclaredVariables(boolean allow)
Say whether undeclared variables are allowed. By default, they are not allowed. When undeclared variables are allowed, it is not necessary to predeclare the variables that may be used in the XPath expression; instead, a variable is automatically declared when a reference to the variable is encountered within the expression.

Parameters:
allow - true if undeclared variables are allowed, false if they are not allowed.
Since:
9.2

isAllowUndeclaredVariables

public boolean isAllowUndeclaredVariables()
Ask whether undeclared variables are allowed. By default, they are not allowed. When undeclared variables are allowed, it is not necessary to predeclare the variables that may be used in the XPath expression; instead, a variable is automatically declared when a reference to the variable is encountered within the expression.

Returns:
true if undeclared variables are allowed, false if they are not allowed.
Since:
9.2

declareVariable

public XPathVariable declareVariable(QNameValue qname)
Declare a variable. A variable must be declared before an expression referring to it is compiled. The initial value of the variable will be the empty sequence

Specified by:
declareVariable in interface XPathStaticContext
Parameters:
qname - The name of the variable
Returns:
an XPathVariable object representing information about the variable that has been declared.

declareVariable

public XPathVariable declareVariable(String namespaceURI,
                                     String localName)
Declare a variable. A variable must be declared before an expression referring to it is compiled. The initial value of the variable will be the empty sequence

Specified by:
declareVariable in interface XPathStaticContext
Parameters:
namespaceURI - The namespace URI of the name of the variable. Supply "" to represent names in no namespace (null is also accepted)
localName - The local part of the name of the variable (an NCName)
Returns:
an XPathVariable object representing information about the variable that has been declared.

declareVariable

public XPathVariable declareVariable(StructuredQName qName)
Declare a variable. A variable must be declared before an expression referring to it is compiled. The initial value of the variable will be the empty sequence

Parameters:
qName - the name of the variable.
Returns:
an XPathVariable object representing information about the variable that has been declared.
Since:
9.2

iterateExternalVariables

public Iterator<XPathVariable> iterateExternalVariables()
Get an iterator over all the variables that have been declared, either explicitly by an application call on declareVariable(), or implicitly if the option allowUndeclaredVariables is set.

Returns:
an iterator; the objects returned by this iterator will be instances of XPathVariable
Since:
9.2

getExternalVariable

public XPathVariable getExternalVariable(StructuredQName qName)
Get the declared variable with a given name, if there is one

Parameters:
qName - the name of the required variable
Returns:
the explicitly or implicitly declared variable with this name if it exists, or null otherwise
Since:
9.2

getSlotNumber

public int getSlotNumber(QNameValue qname)
Get the slot number allocated to a particular variable

Parameters:
qname - the name of the variable
Returns:
the slot number, or -1 if the variable has not been declared

getURIForPrefix

public String getURIForPrefix(String prefix)
                       throws XPathException
Get the URI for a prefix, using the declared namespaces as the context for namespace resolution. The default namespace is NOT used when the prefix is empty. This method is provided for use by the XPath parser.

Specified by:
getURIForPrefix in interface StaticContext
Parameters:
prefix - The prefix
Returns:
the corresponding namespace URI
Throws:
XPathException - if the prefix is not declared; the associated error code should be XPST0081

getNamespaceResolver

public NamespaceResolver getNamespaceResolver()
Description copied from interface: StaticContext
Get a namespace resolver to resolve the namespaces declared in this static context.

Specified by:
getNamespaceResolver in interface StaticContext
Returns:
a namespace resolver.

getURIForPrefix

public String getURIForPrefix(String prefix,
                              boolean useDefault)
Get the namespace URI corresponding to a given prefix. Return null if the prefix is not in scope.

Specified by:
getURIForPrefix in interface NamespaceResolver
Parameters:
prefix - the namespace prefix
useDefault - true if the default namespace is to be used when the prefix is ""
Returns:
the uri for the namespace, or null if the prefix is not in scope. Return "" if the prefix maps to the null namespace.

iteratePrefixes

public Iterator iteratePrefixes()
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

Specified by:
iteratePrefixes in interface NamespaceResolver
Returns:
an iterator over all the prefixes for which a namespace binding exists, including the zero-length string to represent the null/absent prefix if it is bound

bindVariable

public Expression bindVariable(StructuredQName qName)
                        throws XPathException
Bind a variable used in an XPath Expression to the XSLVariable element in which it is declared. This method is provided for use by the XPath parser, and it should not be called by the user of the API, or overridden, unless variables are to be declared using a mechanism other than the declareVariable method of this class.

Specified by:
bindVariable in interface StaticContext
Parameters:
qName - the name of the variable
Returns:
the resulting variable reference
Throws:
XPathException

getStackFrameMap

public SlotManager getStackFrameMap()
Get a Stack Frame Map containing definitions of all the declared variables. This will return a newly created object that the caller is free to modify by adding additional variables, without affecting the static context itself.

Specified by:
getStackFrameMap in interface XPathStaticContext
Returns:
a SlotManager object holding details of the allocation of variables on the stack frame.

isImportedSchema

public boolean isImportedSchema(String namespace)
Description copied from interface: StaticContext
Ask whether a Schema for a given target namespace has been imported. Note that the in-scope element declarations, attribute declarations and schema types are the types registered with the (schema-aware) configuration, provided that their namespace URI is registered in the static context as being an imported schema namespace. (A consequence of this is that within a Configuration, there can only be one schema for any given namespace, including the null namespace).

Specified by:
isImportedSchema in interface StaticContext
Parameters:
namespace - the target namespace in question
Returns:
true if the given namespace has been imported

getImportedSchemaNamespaces

public Set<String> getImportedSchemaNamespaces()
Get the set of imported schemas

Specified by:
getImportedSchemaNamespaces in interface StaticContext
Returns:
a Set, the set of URIs representing the names of imported schemas

setImportedSchemaNamespaces

public void setImportedSchemaNamespaces(Set namespaces)
Register the set of imported schema namespaces

Parameters:
namespaces - the set of namespaces for which schema components are available in the static context

setRequiredContextItemType

public void setRequiredContextItemType(ItemType type)
Declare the static type of the context item. If this type is declared, and if a context item is supplied when the query is invoked, then the context item must conform to this type (no type conversion will take place to force it into this type).

Parameters:
type - the required type of the context item
Since:
9.3

getRequiredContextItemType

public ItemType getRequiredContextItemType()
Get the required type of the context item. If no type has been explicitly declared for the context item, an instance of AnyItemType (representing the type item()) is returned.

Specified by:
getRequiredContextItemType in interface StaticContext
Overrides:
getRequiredContextItemType in class AbstractStaticContext
Returns:
the required type of the context item
Since:
9.3


Copyright (c) 2004-2011 Saxonica Limited. All rights reserved.