net.sf.saxon.query
Class StaticQueryContext

java.lang.Object
  extended by net.sf.saxon.query.StaticQueryContext

public class StaticQueryContext
extends Object

StaticQueryContext contains information used to build a StaticContext for use when processing XQuery expressions.

Despite its name, StaticQueryContext no longer implements the StaticContext interface, which means it cannot be used directly by Saxon when parsing a query. Instead it is first copied to create a QueryModule object, which does implement the StaticContext interface.

The application constructs a StaticQueryContext and initializes it with information about the context, for example, default namespaces, base URI, and so on. When a query is compiled using this StaticQueryContext, the query parser makes a copy of the StaticQueryContext and uses this internally, modifying it with information obtained from the query prolog, as well as information such as namespace and variable declarations that can occur at any point in the query. The query parser does not modify the original StaticQueryContext supplied by the calling application, which may therefore be used for compiling multiple queries, serially or even in multiple threads.

This class forms part of Saxon's published XQuery API. Methods that are considered stable are labelled with the JavaDoc "since" tag. The value 8.4 indicates a method introduced at or before Saxon 8.4; other values indicate the version at which the method was introduced.

In the longer term, this entire API may at some stage be superseded by a proposed standard Java API for XQuery.

Since:
8.4

Constructor Summary
StaticQueryContext(Configuration config)
          Create a StaticQueryContext using a given Configuration.
 
Method Summary
 DocumentInfo buildDocument(Source source)
          Convenience method for building Saxon's internal representation of a source XML document.
 void clearNamespaces()
          Clear all the user-declared namespaces
 void clearPassiveNamespaces()
          Deprecated. since 9.0 - use clearNamespaces()
 XQueryExpression compileQuery(InputStream source, String encoding)
          Prepare an XQuery query for subsequent evaluation.
 XQueryExpression compileQuery(Reader source)
          Prepare an XQuery query for subsequent evaluation.
 XQueryExpression compileQuery(String query)
          Prepare an XQuery query for subsequent evaluation.
 void declareCollation(String name, Comparator comparator)
          Declare a named collation.
 void declareCollation(String name, StringCollator comparator)
          Declare a named collation.
 void declareDefaultCollation(String name)
          Set the default collation.
 void declareNamespace(String prefix, String uri)
          Declare a namespace whose prefix can be used in expressions.
 void declarePassiveNamespace(String prefix, String uri, boolean explicit)
          Deprecated. since 9.0. Use declareNamespace(java.lang.String, java.lang.String)
 CollationMap getAllCollations()
          Get a HashMap that maps all registered collations to Comparators.
 String getBaseURI()
          Get the Base URI of the query, for resolving any relative URI's used in the expression.
 StringCollator getCollation(String name)
          Get a named collation.
 CollationMap getCollationMap()
          Get the collation map
 Configuration getConfiguration()
          Get the Configuration options
 int getConstructionMode()
          Get the current construction mode
 String getDefaultCollationName()
          Get the name of the default collation.
 String getDefaultElementNamespace()
          Get the default namespace for elements and types
 String getDefaultFunctionNamespace()
          Get the default function namespace
 ErrorListener getErrorListener()
          Get the ErrorListener in use for this static context
 Executable getExecutable()
          Get the executable containing this query
 NamespaceResolver getExternalNamespaceResolver()
          Get the external namespace resolver that has been registered using setExternalNamespaceResolver(), if any.
 ModuleURIResolver getModuleURIResolver()
          Get the user-defined ModuleURIResolver for resolving URIs used in "import module" declarations in the XQuery prolog; returns null if none has been explicitly set either on the StaticQueryContext or on the Configuration.
 NamePool getNamePool()
          Get the NamePool used for compiling expressions
 String getNamespaceForPrefix(String prefix)
          Get the namespace URI for a given prefix, which must have been declared using the method declareNamespace(java.lang.String, java.lang.String).
 ItemType getRequiredContextItemType()
          Get the required type of the context item.
 String getSystemId()
          Get the system ID of the container of the expression.
 boolean isCompileWithTracing()
          Ask whether compile-time generation of trace code was requested
 boolean isEmptyLeast()
          Ask what is the option for where an empty sequence appears in the collation order, if not otherwise specified in the "order by" clause
 boolean isGeneratingJavaCode()
          Ask whether this query is to be optimized with a view to generating Java code.
 boolean isInheritNamespaces()
          Get the namespace inheritance mode
 boolean isPreserveBoundarySpace()
          Ask whether the policy for boundary space is "preserve" or "strip"
 boolean isPreserveNamespaces()
          Get the namespace copy mode
 Iterator iterateDeclaredPrefixes()
          Get the namespace prefixes that have been declared using the method declareNamespace(java.lang.String, java.lang.String)
 void reset()
          Reset the state of this StaticQueryContext to an uninitialized state
 void setBaseURI(String baseURI)
          Set the Base URI of the query
 void setCompileWithTracing(boolean trace)
          Request compile-time generation of trace code (or not)
 void setConfiguration(Configuration config)
          Set the Configuration options
 void setConstructionMode(int mode)
          Set the construction mode for this module
 void setDefaultElementNamespace(String uri)
          Set the default element namespace
 void setDefaultFunctionNamespace(String defaultFunctionNamespace)
          Set the default function namespace
 void setEmptyLeast(boolean least)
          Set the option for where an empty sequence appears in the collation order, if not otherwise specified in the "order by" clause
 void setErrorListener(ErrorListener listener)
          Set the ErrorListener to be used to report compile-time errors in a query.
 void setExecutable(Executable executable)
          Set the Executable to contain this query.
 void setExternalNamespaceResolver(NamespaceResolver resolver)
          Set an external namespace resolver.
 void setGeneratingJavaCode(boolean generateCode)
          Indicate that the query should be optimized with a view to generating Java code.
 void setInheritNamespaces(boolean inherit)
          Set the namespace inheritance mode
 void setModuleURIResolver(ModuleURIResolver resolver)
          Set a user-defined ModuleURIResolver for resolving URIs used in "import module" declarations in the XQuery prolog.
 void setPreserveBoundarySpace(boolean preserve)
          Set the policy for preserving boundary space
 void setPreserveNamespaces(boolean inherit)
          Set the namespace copy mode
 void setRequiredContextItemType(ItemType type)
          Declare the static type of the context item.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StaticQueryContext

public StaticQueryContext(Configuration config)
Create a StaticQueryContext using a given Configuration. This creates a StaticQueryContext for a main module (that is, a module that is not a library module).

Parameters:
config - the Saxon Configuration
Since:
8.4
Method Detail

reset

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

Since:
8.4

setConfiguration

public void setConfiguration(Configuration config)
Set the Configuration options

Parameters:
config - the Saxon Configuration
Throws:
IllegalArgumentException - if the configuration supplied is different from the existing configuration
Since:
8.4

getConfiguration

public Configuration getConfiguration()
Get the Configuration options

Returns:
the Saxon configuration
Since:
8.4

setExecutable

public void setExecutable(Executable executable)
Set the Executable to contain this query. Normally a query constitutes its own Executable, and the executable will then be created automatically. This method is used when the query is to share the same executable as a host program, specifically, an XSLT stylesheet that imports the query.

Parameters:
executable - the executable

getExecutable

public Executable getExecutable()
Get the executable containing this query

Returns:
the executable (or null if not set)

setBaseURI

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

Parameters:
baseURI - the base URI of the query
Since:
8.4

buildDocument

public DocumentInfo buildDocument(Source source)
                           throws XPathException
Convenience method for building Saxon's internal representation of a source XML document. The document will be built using Configuration (and NamePool) associated with this StaticQueryContext.

This method is retained for backwards compatibility; however, it is merely a wrapper around the method Configuration.buildDocument(javax.xml.transform.Source), which should be called in preference.

Parameters:
source - Any javax.xml.transform.Source object representing the document against which queries will be executed. Note that a Saxon DocumentInfo (indeed any NodeInfo) can be used as a Source. To use a third-party DOM Document as a source, create an instance of DOMSource to wrap it.

For additional control over the way in which the source document is processed, supply an AugmentedSource object and set appropriate options on the object.

Returns:
the DocumentInfo representing the root node of the resulting document object.
Throws:
XPathException
Since:
8.4

isCompileWithTracing

public boolean isCompileWithTracing()
Ask whether compile-time generation of trace code was requested

Returns:
true if compile-time generation of code was requested
Since:
9.0

setCompileWithTracing

public void setCompileWithTracing(boolean trace)
Request compile-time generation of trace code (or not)

Parameters:
trace - true if compile-time generation of trace code is required
Since:
9.0

setGeneratingJavaCode

public void setGeneratingJavaCode(boolean generateCode)
Indicate that the query should be optimized with a view to generating Java code. This inhibits some rewrites to constructs for which code generation is not possible.

Parameters:
generateCode - true if Java code is to be generated as the final output

isGeneratingJavaCode

public boolean isGeneratingJavaCode()
Ask whether this query is to be optimized with a view to generating Java code. This inhibits some rewrites to constructs for which code generation is not possible.

Returns:
true if Java code is to be generated as the final output

setInheritNamespaces

public void setInheritNamespaces(boolean inherit)
Set the namespace inheritance mode

Parameters:
inherit - true if namespaces are inherited, false if not
Since:
8.4

isInheritNamespaces

public boolean isInheritNamespaces()
Get the namespace inheritance mode

Returns:
true if namespaces are inherited, false if not
Since:
8.4

setPreserveNamespaces

public void setPreserveNamespaces(boolean inherit)
Set the namespace copy mode

Parameters:
inherit - true if namespaces are preserved, false if not
Since:
8.4

isPreserveNamespaces

public boolean isPreserveNamespaces()
Get the namespace copy mode

Returns:
true if namespaces are preserved, false if not
Since:
8.4

setConstructionMode

public void setConstructionMode(int mode)
Set the construction mode for this module

Parameters:
mode - one of Validation.STRIP, Validation.PRESERVE
Since:
8.4

getConstructionMode

public int getConstructionMode()
Get the current construction mode

Returns:
one of Validation.STRIP, Validation.PRESERVE
Since:
8.4

compileQuery

public XQueryExpression compileQuery(String query)
                              throws XPathException
Prepare an XQuery query for subsequent evaluation. The source text of the query is supplied as a String. The base URI of the query is taken from the static context, and defaults to the current working directory.

Note that this interface makes caller responsible for decoding the query and presenting it as a string of characters. This means it is likely that any encoding specified in the query prolog will be ignored.

Parameters:
query - The XQuery query to be evaluated, supplied as a string.
Returns:
an XQueryExpression object representing the prepared expression
Throws:
XPathException - if the syntax of the expression is wrong, or if it references namespaces, variables, or functions that have not been declared, or contains other static errors.
Since:
8.4

compileQuery

public XQueryExpression compileQuery(Reader source)
                              throws XPathException,
                                     IOException
Prepare an XQuery query for subsequent evaluation. The Query is supplied in the form of a Reader. The base URI of the query is taken from the static context, and defaults to the current working directory.

Note that this interface makes the Reader responsible for decoding the query and presenting it as a stream of characters. This means it is likely that any encoding specified in the query prolog will be ignored. Also, some implementations of Reader cannot handle a byte order mark.

Parameters:
source - A Reader giving access to the text of the XQuery query to be compiled.
Returns:
an XPathExpression object representing the prepared expression.
Throws:
XPathException - if the syntax of the expression is wrong, or if it references namespaces, variables, or functions that have not been declared, or any other static error is reported.
IOException - if a failure occurs reading the supplied input.
Since:
8.4

compileQuery

public XQueryExpression compileQuery(InputStream source,
                                     String encoding)
                              throws XPathException,
                                     IOException
Prepare an XQuery query for subsequent evaluation. The Query is supplied in the form of a InputStream, with an optional encoding. If the encoding is not specified, the query parser attempts to obtain the encoding by inspecting the input stream: it looks specifically for a byte order mark, and for the encoding option in the version declaration of an XQuery prolog. The encoding defaults to UTF-8. The base URI of the query is taken from the static context, and defaults to the current working directory.

Parameters:
source - An InputStream giving access to the text of the XQuery query to be compiled, as a stream of octets
encoding - The encoding used to translate characters to octets in the query source. The parameter may be null: in this case the query parser attempts to infer the encoding by inspecting the source, and if that fails, it assumes UTF-8 encoding
Returns:
an XPathExpression object representing the prepared expression.
Throws:
XPathException - if the syntax of the expression is wrong, or if it references namespaces, variables, or functions that have not been declared, or any other static error is reported.
IOException - if a failure occurs reading the supplied input.
Since:
8.5

declareNamespace

public void declareNamespace(String prefix,
                             String uri)
Declare a namespace whose prefix can be used in expressions. This is equivalent to declaring a prefix in the Query prolog. Any namespace declared in the Query prolog overrides a namespace declared using this API.

Parameters:
prefix - The namespace prefix. Must not be null. Setting this to "" means that the namespace will be used as the default namespace for elements and types.
uri - The namespace URI. Must not be null. The value "" (zero-length string) is used to undeclare a namespace; it is not an error if there is no existing binding for the namespace prefix.
Throws:
NullPointerException - if either the prefix or URI is null
IllegalArgumentException - if the prefix is "xml" and the namespace is not the XML namespace, or vice versa.
Since:
9.0

declarePassiveNamespace

public void declarePassiveNamespace(String prefix,
                                    String uri,
                                    boolean explicit)
                             throws XPathException
Deprecated. since 9.0. Use declareNamespace(java.lang.String, java.lang.String)

Declare a namespace whose prefix can be used in expressions. This is equivalent to declaring a prefix in the Query prolog. The term "passive" was a term from a draft XQuery proposal indicating a namespace that won't be copied into the result tree. Passive namespaces are never undeclared. Any namespace declared in the Query prolog overrides a namespace declared using this API.

Parameters:
prefix - The namespace prefix. Must not be null.
uri - The namespace URI. Must not be null. The value "" (zero-length string) is used to undeclare a namespace; it is not an error if there is no existing binding for the namespace prefix.
explicit - Must be false (the value true was previously reserved for internal use, but is no longer permitted)
Throws:
XPathException
Since:
8.4

clearNamespaces

public void clearNamespaces()
Clear all the user-declared namespaces

Since:
9.0

clearPassiveNamespaces

public void clearPassiveNamespaces()
Deprecated. since 9.0 - use clearNamespaces()

Clear all the declared passive namespaces, except for the standard ones (xml, saxon, etc)

Since:
8.4

iterateDeclaredPrefixes

public Iterator iterateDeclaredPrefixes()
Get the namespace prefixes that have been declared using the method declareNamespace(java.lang.String, java.lang.String)

Returns:
an iterator that returns the namespace prefixes that have been explicitly declared, as strings. The default namespace for elements and types will be included, using the prefix "".
Since:
9.0

getNamespaceForPrefix

public String getNamespaceForPrefix(String prefix)
Get the namespace URI for a given prefix, which must have been declared using the method declareNamespace(java.lang.String, java.lang.String). Note that this method will not call the external namespace resolver to resolve the prefix.

Parameters:
prefix - the namespace prefix, or "" to represent the null prefix
Returns:
the namespace URI. Returns "" to represent the non-namespace, null to indicate that the prefix has not been declared

setExternalNamespaceResolver

public void setExternalNamespaceResolver(NamespaceResolver resolver)
Set an external namespace resolver. If a namespace prefix cannot be resolved using any other mechanism, then as a last resort the external namespace resolver is called to obtain a URI for the given prefix.

Changed in Saxon 9.0 so that the namespaces resolved by the external namespace resolver are available at run-time, just like namespaces declared in the query prolog. In consequence, the supplied NamespaceResolver must now implement the NamespaceResolver.iteratePrefixes() method.

Parameters:
resolver - the external namespace resolver

getExternalNamespaceResolver

public NamespaceResolver getExternalNamespaceResolver()
Get the external namespace resolver that has been registered using setExternalNamespaceResolver(), if any.

Returns:
the external namespace resolver

getDefaultFunctionNamespace

public String getDefaultFunctionNamespace()
Get the default function namespace

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

setDefaultFunctionNamespace

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

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

setDefaultElementNamespace

public void setDefaultElementNamespace(String uri)
                                throws XPathException
Set the default element namespace

Parameters:
uri - the namespace URI to be used as the default namespace for elements and types
Throws:
XPathException
Since:
8.4

getDefaultElementNamespace

public String getDefaultElementNamespace()
Get the default namespace for elements and types

Returns:
the namespace URI to be used as the default namespace for elements and types
Since:
8.9 Modified in 8.9 to return the namespace URI as a string rather than an integer code

setModuleURIResolver

public void setModuleURIResolver(ModuleURIResolver resolver)
Set a user-defined ModuleURIResolver for resolving URIs used in "import module" declarations in the XQuery prolog. This will be used for resolving URIs in XQuery "import module" declarations, overriding any ModuleURIResolver that was specified as part of the configuration.

Parameters:
resolver - the ModuleURIResolver to be used

getModuleURIResolver

public ModuleURIResolver getModuleURIResolver()
Get the user-defined ModuleURIResolver for resolving URIs used in "import module" declarations in the XQuery prolog; returns null if none has been explicitly set either on the StaticQueryContext or on the Configuration.

Returns:
the registered ModuleURIResolver

declareCollation

public void declareCollation(String name,
                             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
Since:
8.4.

declareCollation

public void declareCollation(String name,
                             StringCollator 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 StringCollator. 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
Since:
8.9.

declareDefaultCollation

public void declareDefaultCollation(String name)
Set the default collation.

Parameters:
name - The collation name, as specified in the query prolog. The name is not validated until it is used.
Since:
8.4. Changed in 8.6 so it no longer validates the collation name: this is because the base URI is not necessarily known at the point where the default collation is declared.

getCollation

public StringCollator getCollation(String name)
Get a named collation.

Parameters:
name - the name of the collation, as an absolute URI
Returns:
the collation identified by the given name, as set previously using declareCollation. If no collation with this name has been declared, the method calls the CollationURIResolver to locate a collation with this name. Return null if no collation with this name is found.
Since:
8.4

getCollationMap

public CollationMap getCollationMap()
Get the collation map

Returns:
the collation map, which identifies all the known collations

getDefaultCollationName

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

Returns:
the name of the default collation; or the name of the codepoint collation if no default collation has been defined. The name is returned in the form it was specified; that is, it is not yet resolved against the base URI. (This is because the base URI declaration can follow the default collation declaration in the query prolog.) If no default collation has been specified, the "default default" (that is, the Unicode codepoint collation) is returned.
Since:
8.4

getAllCollations

public CollationMap 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.

This method is intended for internal use.

Returns:
the CollationMap containing all the collations defined in this 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

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.

Returns:
the required type of the context item

getNamePool

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

Returns:
the name pool
Since:
8.4

getSystemId

public String getSystemId()
Get the system ID of the container of the expression. Used to construct error messages. Note that the systemID and the Base URI are currently identical, but they might be distinguished in the future.

Returns:
the Base URI
Since:
8.4

getBaseURI

public String getBaseURI()
Get the Base URI of the query, for resolving any relative URI's used in the expression. Note that the systemID and the Base URI are currently identical, but they might be distinguished in the future. Used by the document() function.

Returns:
the base URI of the query
Since:
8.4

setPreserveBoundarySpace

public void setPreserveBoundarySpace(boolean preserve)
Set the policy for preserving boundary space

Parameters:
preserve - true if boundary space is to be preserved, false if it is to be stripped
Since:
9.0

isPreserveBoundarySpace

public boolean isPreserveBoundarySpace()
Ask whether the policy for boundary space is "preserve" or "strip"

Returns:
true if the policy is to preserve boundary space, false if it is to strip it
Since:
9.0

setEmptyLeast

public void setEmptyLeast(boolean least)
Set the option for where an empty sequence appears in the collation order, if not otherwise specified in the "order by" clause

Parameters:
least - true if the empty sequence is considered less than any other value (the default), false if it is considered greater than any other value
Since:
9.0

isEmptyLeast

public boolean isEmptyLeast()
Ask what is the option for where an empty sequence appears in the collation order, if not otherwise specified in the "order by" clause

Returns:
true if the empty sequence is considered less than any other value (the default), false if it is considered greater than any other value
Since:
9.0

setErrorListener

public void setErrorListener(ErrorListener listener)
Set the ErrorListener to be used to report compile-time errors in a query. This will also be the default for the run-time error listener used to report dynamic errors

Parameters:
listener - the ErrorListener to be used

getErrorListener

public ErrorListener getErrorListener()
Get the ErrorListener in use for this static context

Returns:
the registered ErrorListener


Copyright (C) Michael H. Kay. All rights reserved.