01: package net.sf.saxon.functions;
02:
03: import net.sf.saxon.CollectionURIResolver;
04: import net.sf.saxon.expr.Expression;
05: import net.sf.saxon.expr.StaticContext;
06: import net.sf.saxon.expr.XPathContext;
07: import net.sf.saxon.om.EmptyIterator;
08: import net.sf.saxon.om.SequenceIterator;
09: import net.sf.saxon.trans.XPathException;
10:
11: /**
12: * Implement the fn:collection() function. The Saxon implementation loads an XML
13: * document called the collection catalogue, which acts as an index of the collection.
14: *
15: * <p>The structure of this index is:
16: * <pre>
17: * <collection>
18: * <doc href="doc1.xml">
19: * <doc href="doc2.xml">
20: * <doc href="doc3.xml">
21: * </collection>
22: * </pre></p>
23: *
24: * <p>The document URIs are resolved relative to the base URI of the doc element
25: * in the catalogue document.</p>
26: */
27:
28: public class Collection extends SystemFunction {
29:
30: private String expressionBaseURI = null;
31:
32: public void checkArguments(StaticContext env) throws XPathException {
33: if (expressionBaseURI == null) {
34: super .checkArguments(env);
35: expressionBaseURI = env.getBaseURI();
36: }
37: }
38:
39: /**
40: * preEvaluate: this method suppresses compile-time evaluation by doing nothing
41: */
42:
43: public Expression preEvaluate(StaticContext env) {
44: return this ;
45: }
46:
47: public SequenceIterator iterate(XPathContext context)
48: throws XPathException {
49:
50: String href;
51:
52: // Zero-argument function:
53:
54: if (getNumberOfArguments() == 0) {
55: href = null;
56: } else {
57: href = argument[0].evaluateItem(context).getStringValue();
58: }
59:
60: CollectionURIResolver resolver = context.getConfiguration()
61: .getCollectionURIResolver();
62: SequenceIterator iter = resolver.resolve(href,
63: expressionBaseURI, context);
64:
65: if (iter == null) {
66: return EmptyIterator.getInstance();
67: } else {
68: return iter;
69: }
70: }
71:
72: // TODO: provide control over error recovery (etc) through options in the catalog file.
73:
74: }
75:
76: //
77: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
78: // you may not use this file except in compliance with the License. You may obtain a copy of the
79: // License at http://www.mozilla.org/MPL/
80: //
81: // Software distributed under the License is distributed on an "AS IS" basis,
82: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
83: // See the License for the specific language governing rights and limitations under the License.
84: //
85: // The Original Code is: all this file.
86: //
87: // The Initial Developer of the Original Code is Michael H. Kay.
88: //
89: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
90: //
91: // Contributor(s): none.
92: //
|