You can use the XCompilationFactory interface and its various
load methods to load a precompiled expression, query, or stylesheet.
These load methods load the Java classes
and return an XPathExecutable, XQueryExecutable, or XSLTExecutable
object respectively.
Procedure
- Retrieve the XCompilationFactory by calling the getCompilationFactory
method on the XFactory class.
An XCompilationFactory
instance is associated with a particular XFactory instance, so they
share registered schemas. If a new schema is registered with the XFactory
instance, therefore, it is visible to the associated XCompilationFactory
instance.
- Create a new XCompilationParameters instance by calling
the XCompilationFactory newCompilationParameters method, passing in
the base class name of the classes to be loaded.
Configure
the parameters further by using the set methods described in this
table.
Table 1. Valid set methods. These
are set methods that are defined in the XCompilationParameters interface
and are valid for loading a precompiled executable.
Set Methods Valid for Loading a Precompiled
Executable |
Description |
Default |
setPackageName |
Specify the package name of the classes to load. |
Java default
package |
setClassLoader |
Specify the class loader to use. |
Class loader that was used to load the processor |
The setDirectoryName method is not valid when loading a precompiled
executable because the classpath is used to search for the classes.
If the directory name is set, it is ignored. The setDirectoryName
method can be used when generating precompiled executables to specify
the directory to which you want to write the classes.
- Use one of the load methods on the XCompilationFactory,
passing in the XCompilationParameters, to load the precompiled executable.
Example
The following is a basic example of loading a precompiled
XPath expression.
// Create the factory
XFactory factory = XFactory.newInstance();
// Get the compilation factory
XCompilationFactory compileFactory = factory.getCompilationFactory();
// Create the compilation parameters
XCompilationParameters params = compileFactory.newCompilationParameters("MyXPath");
params.setPackageName( "org.example.myxpath");
// Load the executable
XPathExecutable executable = compileFactory.loadXPath(params);
// Create the input source
StreamSource input = new StreamSource("simple.xml");
// Execute the XPath expression
XSequenceCursor cursor = executable.execute(input);
Appropriate
load methods are available for XQuery and XSLT as well.