net.sf.saxon.query
Class StaticQueryContext

java.lang.Object
  |
  +--net.sf.saxon.query.StaticQueryContext
All Implemented Interfaces:
StaticContext

public class StaticQueryContext
extends java.lang.Object
implements StaticContext

StaticQueryContext is the implementation of StaticContext used when processing XQuery expressions. Note that some of the methods are intended for use internally by the query processor itself.


Nested Class Summary
static class StaticQueryContext.ActiveNamespace
          Inner class containing information about an active namespace entry
 
Constructor Summary
StaticQueryContext(Configuration config)
          Create a StaticQueryContext using the default NamePool
 
Method Summary
 void addImportedSchema(java.lang.String targetNamespace)
          Add an imported schema to this static context.
 void allocateLocalSlots(int n)
          Ensure that enough slots are available in each stack frame for local variables.
 Expression bindFunction(java.lang.String qname, Expression[] arguments)
          Identify a (namespace-prefixed) function appearing in the expression.
protected  void bindUnboundFunctionCalls()
          Bind function calls that could not be bound when first encountered.
 VariableDeclaration bindVariable(int fingerprint)
          Bind a variable used in a query to the expression in which it is declared.
 java.lang.String checkURIForPrefix(java.lang.String prefix)
          Get the URI for a prefix if there is one, return null if not.
 void clearNamespaces()
          Clear all the declared namespaces, except for the standard ones (xml, saxon, etc)
protected  void declareActiveNamespace(java.lang.String prefix, java.lang.String uri)
          Declare an active namespace
 void declareCollation(java.lang.String name, java.util.Comparator comparator)
          Declare a named collation.
 void declareDefaultCollation(java.lang.String name)
          Set the default collation.
 void declareFunction(XQueryFunction function)
          Register a user-defined XQuery function
protected  void declarePassiveNamespace(java.lang.String prefix, java.lang.String uri)
          Declare a namespace whose prefix can be used in expressions.
 void declareVariable(VariableDeclaration var)
          Declare a global variable.
 void explainGlobalFunctions()
          Output "explain" information about each declared function
protected  void fixupGlobalFunctions()
          Fixup all references to global functions.
 void fixupGlobalVariables()
          Fixup all references to global variables.
 int[] getActiveNamespaceCodes()
          Get an array containing the namespace codes of all active namespaces.
 java.util.HashMap getAllCollations()
          Get a HashMap that maps all registered collations to Comparators.
 java.lang.String getBaseURI()
          Get the Base URI of the query, for resolving any relative URI's used in the expression.
 java.util.Comparator getCollation(java.lang.String name)
          Get a named collation.
 Configuration getConfiguration()
          Get the system configuration
 java.lang.String getDefaultCollationName()
          Get the name of the default collation.
 short getDefaultElementNamespace()
          Get the default XPath namespace, as a namespace URI code that can be looked up in the NamePool
 java.lang.String getDefaultFunctionNamespace()
          Get the default function namespace
 java.util.Iterator getFunctionDefinitions()
          Get an iterator over the Functions defined in this module
 int getLineNumber()
          Get the line number of the expression within that container.
 java.lang.String getModuleNamespace()
          Get the namespace of the current library module.
 short getModuleNamespaceCode()
          Get the namesapce code of the current library module.
 NamePool getNamePool()
          Get the NamePool used for compiling expressions
 NamespaceResolver getNamespaceContext()
          Get a copy of the Namespace Context.
 int getNumberOfGlobalVariables()
          Get the number of global variables.
 int getNumberOfLocalVariables()
          Get the maximum number of local variables in any stack frame.
 java.lang.String getSystemId()
          Get the system ID of the container of the expression.
 java.lang.String getURIForPrefix(java.lang.String prefix)
          Get the URI for a prefix.
 ValidationContext getValidationContext()
          Get the current validation context
 int getValidationMode()
          Get the current validation mode
 java.util.Iterator getVariableDeclarations()
          Get an iterator over the variables defined in this module
 boolean isImportedSchema(java.lang.String namespace)
          Get the schema for a given namespace, if it has been imported
 boolean isInBackwardsCompatibleMode()
          Determine whether Backwards Compatible Mode is used
 void issueWarning(java.lang.String s)
          Issue a compile-time warning.
protected  StaticQueryContext loadModule(java.lang.String namespaceURI, java.lang.String locationURI)
          Load another query module
 void popValidationContext()
          Remove the validation context currently at the top of the stack
 void popValidationMode()
          Remove the validation mode currently at the top of the stack
 void pushValidationContext(ValidationContext context)
          Add a new validation context to the stack of validation contexts
 void pushValidationMode(int mode)
          Add a new validation mode to the stack of validation modes
 void reset()
          Reset the state of this StaticQueryContext to an uninitialized state
 void setBaseURI(java.lang.String baseURI)
          Set the Base URI of the query
 void setConfiguration(Configuration config)
          Set the Configuration options
 void setContainedValidationContext(int fingerprint)
          Set the validation context to a local element declaration within the current validation context
protected  void setDefaultElementNamespace(java.lang.String uri)
          Set the default element namespace
 void setDefaultFunctionNamespace(java.lang.String defaultFunctionNamespace)
          Set the default function namespace
 void setModuleNamespace(java.lang.String uri)
          Set the namespace for a library module
 void undeclareNamespace()
          Undeclare the most recently-declared active namespace
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StaticQueryContext

public StaticQueryContext(Configuration config)
Create a StaticQueryContext using the default NamePool

Method Detail

reset

public void reset()
Reset the state of this StaticQueryContext to an uninitialized state


setConfiguration

public void setConfiguration(Configuration config)
Set the Configuration options


getConfiguration

public Configuration getConfiguration()
Description copied from interface: StaticContext
Get the system configuration

Specified by:
getConfiguration in interface StaticContext

declarePassiveNamespace

protected void declarePassiveNamespace(java.lang.String prefix,
                                       java.lang.String uri)
Declare a namespace whose prefix can be used in expressions. This is a passive namespace, it won't be copied into the result tree. Passive namespaces are never undeclared, and active namespaces override them.

Parameters:
prefix - The namespace prefix. Must not be null.
uri - The namespace URI. Must not be null.

declareActiveNamespace

protected void declareActiveNamespace(java.lang.String prefix,
                                      java.lang.String uri)
Declare an active namespace


undeclareNamespace

public void undeclareNamespace()
Undeclare the most recently-declared active namespace


clearNamespaces

public void clearNamespaces()
Clear all the declared namespaces, except for the standard ones (xml, saxon, etc)


getURIForPrefix

public java.lang.String getURIForPrefix(java.lang.String prefix)
                                 throws XPathException
Get the URI for a prefix. This method is used by the XQuery parser to resolve namespace prefixes.

Specified by:
getURIForPrefix in interface StaticContext
Parameters:
prefix - The prefix
Returns:
the corresponding namespace URI
Throws:
XPathException - if the prefix has not been declared

checkURIForPrefix

public java.lang.String checkURIForPrefix(java.lang.String prefix)
Get the URI for a prefix if there is one, return null if not. This method is used by the XQuery parser to resolve namespace prefixes.

Parameters:
prefix - The prefix
Returns:
the corresponding namespace URI, or null if the prefix has not been declared.

getActiveNamespaceCodes

public int[] getActiveNamespaceCodes()
Get an array containing the namespace codes of all active namespaces.


getNamespaceContext

public NamespaceResolver getNamespaceContext()
Get a copy of the Namespace Context. This method is used internally by the query parser when a construct is encountered that needs to save the namespace context for use at run-time.


getDefaultFunctionNamespace

public java.lang.String getDefaultFunctionNamespace()
Get the default function namespace

Returns:
the default function namespace (defaults to the fn: namespace)

setDefaultFunctionNamespace

public void setDefaultFunctionNamespace(java.lang.String defaultFunctionNamespace)
Set the default function namespace

Parameters:
defaultFunctionNamespace - The namespace to be used for unprefixed function calls

setDefaultElementNamespace

protected void setDefaultElementNamespace(java.lang.String uri)
Set the default element namespace


getDefaultElementNamespace

public short getDefaultElementNamespace()
Get the default XPath namespace, as a namespace URI code that can be looked up in the NamePool

Specified by:
getDefaultElementNamespace in interface StaticContext

setModuleNamespace

public void setModuleNamespace(java.lang.String uri)
Set the namespace for a library module


getModuleNamespace

public java.lang.String getModuleNamespace()
Get the namespace of the current library module.

Returns:
the module namespace, or null if this is a main module

getModuleNamespaceCode

public short getModuleNamespaceCode()
Get the namesapce code of the current library module.

Returns:
the module namespace, or null if this is a main module

declareCollation

public void declareCollation(java.lang.String name,
                             java.util.Comparator comparator)
Declare a named collation. Collations are only available in a query if this method has been called externally to declare the collation and associate it with an implementation, in the form of a Java Comparator. The default collation is the Unicode codepoint collation, unless otherwise specified.

Parameters:
name - The name of the collation (technically, a URI)
comparator - The Java Comparator used to implement the collating sequence

declareDefaultCollation

public void declareDefaultCollation(java.lang.String name)
                             throws XPathException
Set the default collation. The collation that is specified must be one that has been previously registered using the declareCollation() method.

Parameters:
name - The collation name
Throws:
XPathException - if the collation name has not been registered

getCollation

public java.util.Comparator getCollation(java.lang.String name)
Get a named collation.

Specified by:
getCollation in interface StaticContext
Parameters:
name - The name of the required collation. Supply null to get the default collation.
Returns:
the collation identified by the given name, as set previously using declareCollation. Return null if no collation with this name is found.

getDefaultCollationName

public java.lang.String getDefaultCollationName()
Get the name of the default collation.

Specified by:
getDefaultCollationName in interface StaticContext
Returns:
the name of the default collation; or the name of the codepoint collation if no default collation has been defined

getAllCollations

public java.util.HashMap getAllCollations()
Get a HashMap that maps all registered collations to Comparators. Note that this returns a snapshot copy of the data held by the static context. This method is provided for internal use by the query processor.


declareVariable

public void declareVariable(VariableDeclaration var)
                     throws XPathException
Declare a global variable. A variable must be declared before an expression referring to it is compiled. Global variables are normally declared in the Query Prolog, but they can also be predeclared using this API.

XPathException

fixupGlobalVariables

public void fixupGlobalVariables()
                          throws XPathException
Fixup all references to global variables. This method is for internal use by the Query Parser only.

XPathException

getVariableDeclarations

public java.util.Iterator getVariableDeclarations()
Get an iterator over the variables defined in this module

Returns:
an Iterator, whose items are VariableDeclaration objects. It returns all variables known to this module including those imported from elsewhere; they can be distinguished by their namespace.

getNumberOfGlobalVariables

public int getNumberOfGlobalVariables()
Get the number of global variables. This method is for internal use.


allocateLocalSlots

public void allocateLocalSlots(int n)
Ensure that enough slots are available in each stack frame for local variables. This method is for internal use.


getNumberOfLocalVariables

public int getNumberOfLocalVariables()
Get the maximum number of local variables in any stack frame. This method is for internal use.


getNamePool

public NamePool getNamePool()
Get the NamePool used for compiling expressions

Specified by:
getNamePool in interface StaticContext

issueWarning

public void issueWarning(java.lang.String s)
Issue a compile-time warning. This method is used during XPath expression compilation to output warning conditions. The default implementation writes the message to System.err. To change the destination of messages, create a subclass of StandaloneContext that overrides this method.

Specified by:
issueWarning in interface StaticContext

setBaseURI

public void setBaseURI(java.lang.String baseURI)
Set the Base URI of the query


getSystemId

public java.lang.String getSystemId()
Get the system ID of the container of the expression. Used to construct error messages.

Specified by:
getSystemId in interface StaticContext
Returns:
the Base URI

getBaseURI

public java.lang.String getBaseURI()
Get the Base URI of the query, for resolving any relative URI's used in the expression. Used by the document() function.

Specified by:
getBaseURI in interface StaticContext
Returns:
"" always

getLineNumber

public int getLineNumber()
Get the line number of the expression within that container. Used to construct error messages.

Specified by:
getLineNumber in interface StaticContext
Returns:
-1 always

bindVariable

public VariableDeclaration bindVariable(int fingerprint)
                                 throws XPathException
Bind a variable used in a query to the expression in which it is declared. This method is provided for use by the XQuery 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
XPathException

declareFunction

public void declareFunction(XQueryFunction function)
                     throws XPathException
Register a user-defined XQuery function

XPathException

bindFunction

public Expression bindFunction(java.lang.String qname,
                               Expression[] arguments)
                        throws XPathException
Identify a (namespace-prefixed) function appearing in the expression. This method is called by the XQuery parser to resolve function calls found within the query.

Note that a function call may appear earlier in the query than the definition of the function to which it is bound. Unlike XSLT, we cannot search forwards to find the function definition. Binding of function calls is therefore a two-stage process; at the time the function call is parsed, we simply register it as pending; subsequently at the end of query parsing all the pending function calls are resolved. Another consequence of this is that we cannot tell at the time a function call is parsed whether it is a call to an internal (XSLT or XQuery) function or to an extension function written in Java.

Specified by:
bindFunction in interface StaticContext
Returns:
an Expression representing the function call. This will normally be a FunctionCall, but it may be rewritten as some other expression.
Throws:
XPathException - if the function call is invalid, either because it is an unprefixed call to a non-system function, or because it is calling a system function that is available in XSLT only. A prefixed function call that cannot be recognized at this stage is assumed to be a forwards reference, and is bound later when bindUnboundFunctionCalls() is called.

bindUnboundFunctionCalls

protected void bindUnboundFunctionCalls()
                                 throws XPathException
Bind function calls that could not be bound when first encountered. These will either be forwards references to functions declared later in the query, or errors. This method is for internal use.

Throws:
XPathException - if a function call refers to a function that has not been declared

getFunctionDefinitions

public java.util.Iterator getFunctionDefinitions()
Get an iterator over the Functions defined in this module

Returns:
an Iterator, whose items are XQueryFunction objects. It returns all function known to this module including those imported from elsewhere; they can be distinguished by their namespace.

fixupGlobalFunctions

protected void fixupGlobalFunctions()
                             throws XPathException
Fixup all references to global functions. This method is called on completion of query parsing. Each XQueryFunction is required to bind all references to that function to the object representing the run-time executable code of the function.

This method is for internal use.

XPathException

explainGlobalFunctions

public void explainGlobalFunctions()
                            throws XPathException
Output "explain" information about each declared function

XPathException

isInBackwardsCompatibleMode

public boolean isInBackwardsCompatibleMode()
Determine whether Backwards Compatible Mode is used

Specified by:
isInBackwardsCompatibleMode in interface StaticContext
Returns:
false; XPath 1.0 compatibility mode is not supported in XQuery

addImportedSchema

public void addImportedSchema(java.lang.String targetNamespace)
Add an imported schema to this static context.

Parameters:
targetNamespace - The target namespace of the schema to be added

isImportedSchema

public boolean isImportedSchema(java.lang.String namespace)
Get the schema for a given namespace, if it has been imported

Specified by:
isImportedSchema in interface StaticContext
Parameters:
namespace - The namespace of the required schema. Supply "" for a no-namespace schema.
Returns:
The schema if found, or null if not found.

pushValidationMode

public void pushValidationMode(int mode)
Add a new validation mode to the stack of validation modes


popValidationMode

public void popValidationMode()
Remove the validation mode currently at the top of the stack


getValidationMode

public int getValidationMode()
Get the current validation mode


pushValidationContext

public void pushValidationContext(ValidationContext context)
Add a new validation context to the stack of validation contexts


popValidationContext

public void popValidationContext()
Remove the validation context currently at the top of the stack


getValidationContext

public ValidationContext getValidationContext()
Get the current validation context


setContainedValidationContext

public void setContainedValidationContext(int fingerprint)
                                   throws XPathException
Set the validation context to a local element declaration within the current validation context

XPathException

loadModule

protected StaticQueryContext loadModule(java.lang.String namespaceURI,
                                        java.lang.String locationURI)
                                 throws XPathException
Load another query module

XPathException