Network Deployment (Distributed operating systems), v8.0 > Develop and deploying applications > XML applications > Use the XML API to perform operations > Use resolvers


Use a module resolver

The XModuleResolver interface can be implemented and the implementation registered with the XStaticContext to override the default XQuery module resolution behavior. Modules are resolved whenever an XQuery module import is encountered.

The default module resolution behavior is to attempt to locate one module for each location hint specified in the module import. The default resolution behavior for each location hint is to resolve relative URIs against the base URI from the static context, if the base URI is available, or to interpret them as file paths relative to the current working directory, if the base URI is not available. Absolute URIs are used unchanged. If a module cannot be located for a location hint, the processor ignores it unless no modules can be loaded for the namespace, in which case the processor will emit an error message.


Procedure

Use the setModuleResolver method on the XStaticContext interface to register a module resolver.

The getModule method returns an instance of the java.util.List interface. This is because there might be more than one module document for a particular namespace and set of location hints.


Example

The following is a basic example of using a module resolver.

XFactory factory = XFactory.newInstance();

// Create the static context XStaticContext staticContext = factory.newStaticContext();

// Create the module resolver and register it with the static context.
staticContext.setModuleResolver(new AModuleResolver(replacementBase));

// Prepare the query.
XQueryExecutable executable = factory.prepareXQuery(new StreamSource(queryFile), staticContext);

// Execute the transformation.
Source source = new StreamSource(inputFile);
Result result = new StreamResult();
executable.execute(source, result);

The following is a basic example of a module resolver implementation.

class AModuleResolver implements XModuleResolver
{
    String _replacementBase;

    public AModuleResolver(String replacementBase)
    {
        _replacementBase=replacementBase;
    }

    // Resolve URI, returning the Source that URI represents.
    // Implements the "rebase:" pseudo-scheme.
    public List
<? extends Source> getModule(String namespace, List
<String> locations, String baseURI) {
        String rebasePrefix="rebase:";

        List
<StreamSource> list = new ArrayList
<StreamSource>();
        for (int i = 0; i
< locations.size(); i++) {
            String href = locations.get(i);
            String base = baseURI;
            if(href.startsWith(rebasePrefix)) {
                href=href.substring(rebasePrefix.length());
                base=_replacementBase;
            }

            java.net.URI uri;
            StreamSource source=null;
            try {

       // Get base URI object
        uri = new java.net.URI(base);
                // Resolved relative reference against base URI
                URI resolvedURI = uri.resolve(href);
                // Try to read...
                source = new StreamSource(resolvedURI.toString());
            } catch (java.net.URISyntaxException use) {
                throw new RuntimeException(use);
            }

            list.add(source);
        }
        return list;
    }
}

+

Search Tips   |   Advanced Search