Registering a collection resolver
You can register implementations of the XCollectionResolver interface with the XDynamicContext.
Procedure
Register a collection resolver with the dynamic context.
The XCollectionResolver implementation registered with the XDynamicContext is used at execution time to retrieve the collection of nodes associated with the URI provided in calls to the fn:collection method. If no collection resolver is registered with the XDynamicContext then calls to fn:collection will result in a recoverable error and the empty sequence is used for the collection.
Note that the collection resolver and the fn:collection function are not meant to resolve document URIs. The source resolver and fn:doc function should be used for this purpose.
Example
The following is a basic example of using a collection
resolver.
XFactory factory = XFactory.newInstance();
// Prepare the XPath expression
XPathExecutable executable = factory.prepareXPath("count(collection('typeA-typeB'))"
// Register the collection resolver with the dynamic context
XCollectionResolver collectionResolver = new ACollectionResolver(factory);
XDynamicContext dynamicContext = factory.newDynamicContext();
dynamicContext.setCollectionResolver(collectionResolver);
// Execute the XPath expression
XSequenceCursor cursor = executable.execute(dynamicContext);
The
following is a basic example of an XCollectionResolver implementation.
public class ACollectionResolver implements XCollectionResolver {
private XFactory m_factory;
public ACollectionResolver(XFactory factory) {
m_factory = factory;
}
public XSequenceCursor getCollection(String uri, String base) {
// Get the default collection
if (uri.equals("")) {
return getCollection("default", base);
}
// Get the requested collection
ArrayList<XItemView> list = new ArrayList<XItemView>();
StringTokenizer tokenizer = new StringTokenizer(uri, "-");
XSequenceCursor cursor = null;
while (tokenizer.hasMoreTokens()) {
String token = tokenizer.nextToken();
XSequenceCursor temp = getNodes(new StreamSource("collections.xml"), "/doc/" + token);
if (cursor == null) {
cursor = temp;
} else {
cursor = cursor.append(temp);
}
}
return cursor;
}
private XSequenceCursor getNodes(Source source, String expression) {
XPathExecutable executable = m_factory.prepareXPath(expression);
XSequenceCursor cursor = executable.execute(source);
return cursor;
}
}