KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > style > SaxonImportQuery


1 package net.sf.saxon.style;
2 import net.sf.saxon.expr.Expression;
3 import net.sf.saxon.functions.ExecutableFunctionLibrary;
4 import net.sf.saxon.instruct.Executable;
5 import net.sf.saxon.om.AttributeCollection;
6 import net.sf.saxon.om.NamePool;
7 import net.sf.saxon.query.StaticQueryContext;
8 import net.sf.saxon.query.XQueryFunction;
9 import net.sf.saxon.query.ModuleURIResolver;
10 import net.sf.saxon.query.QueryReader;
11 import net.sf.saxon.trans.XPathException;
12 import net.sf.saxon.trans.StaticError;
13
14 import javax.xml.transform.stream.StreamSource JavaDoc;
15 import java.util.Iterator JavaDoc;
16
17
18 /**
19 * The class implements a saxon:import-query declaration in a stylesheet. This
20  * declaration imports an XQuery library module and adds the functions defined
21  * in that module to the static context, making them available for calling from
22  * XPath expressions in the stylesheet.
23 */

24
25 public class SaxonImportQuery extends StyleElement {
26
27     private String JavaDoc href;
28     private String JavaDoc moduleURI;
29
30     /**
31      * The importModule() method is called very early, before preparing the attributes,
32      * to make sure that all functions in the imported modules are available in the static
33      * context.
34      * @throws XPathException
35      */

36
37     public void importModule() throws XPathException {
38         prepareAttributes();
39         loadLibraryModule();
40     }
41
42     public void prepareAttributes() throws XPathException {
43
44         // Avoid reporting errors twice
45
if (href!=null || moduleURI!=null) {
46             return;
47         }
48
49         AttributeCollection atts = getAttributeList();
50
51         for (int a=0; a<atts.getLength(); a++) {
52             int nc = atts.getNameCode(a);
53             String JavaDoc f = getNamePool().getClarkName(nc);
54             if (f==StandardNames.HREF) {
55                 href = atts.getValue(a).trim();
56             } else if (f==StandardNames.NAMESPACE) {
57                 moduleURI = atts.getValue(a).trim();
58             } else {
59                 checkUnknownAttribute(nc);
60                 moduleURI=""; // for error recovery path
61
}
62         }
63
64         if (href==null && moduleURI==null) {
65             compileError("At least one of href or namespace must be specified");
66             moduleURI=""; // for error recovery path
67
}
68     }
69
70     public void validate() throws XPathException {
71         checkEmpty();
72         checkTopLevel(null);
73     }
74
75     private void loadLibraryModule() throws XPathException {
76
77         if (href==null && moduleURI==null) {
78             // error already reported
79
return;
80         }
81
82         try {
83             XSLStylesheet top = getPrincipalStylesheet();
84             getExecutable().setFunctionLibrary(new ExecutableFunctionLibrary(getConfiguration()));
85                         // this is not actually used, but is needed to keep the XQuery processor happy
86
StaticQueryContext importedModule = loadModule();
87
88             // Do the importing
89

90             short ns = importedModule.getModuleNamespaceCode();
91             NamePool pool = getTargetNamePool();
92             Iterator JavaDoc it = importedModule.getFunctionDefinitions();
93             while (it.hasNext()) {
94                 XQueryFunction def = (XQueryFunction)it.next();
95                 // don't import functions transitively
96
if (pool.getURICode(def.getFunctionFingerprint()) == ns) {
97                     top.declareXQueryFunction(def);
98                 }
99                 // Note, we are not importing global variables at present
100
}
101         } catch (XPathException err) {
102             compileError(err);
103         }
104     }
105
106     /**
107      * Load a query module
108      */

109
110     private StaticQueryContext loadModule() throws XPathException {
111         // Call the module URI resolver to find the module or modules
112

113         ModuleURIResolver resolver = getConfiguration().getModuleURIResolver();
114         if (resolver == null) {
115             resolver = getConfiguration().getStandardModuleURIResolver();
116         }
117
118         String JavaDoc[] hints = {href};
119         StreamSource JavaDoc[] sources;
120         try {
121             sources = resolver.resolve(moduleURI, getBaseURI(), hints);
122             if (sources == null) {
123                 resolver = getConfiguration().getStandardModuleURIResolver();
124                 sources = resolver.resolve(moduleURI, getBaseURI(), hints);
125             }
126         } catch (XPathException e) {
127             throw StaticError.makeStaticError(e);
128         }
129
130         if (sources.length != 1) {
131             StaticError err = new StaticError("Query module resolver must return a single module");
132             throw err;
133         }
134
135
136         StreamSource JavaDoc ss = sources[0];
137         String JavaDoc baseURI = ss.getSystemId();
138         if (baseURI == null) {
139             ss.setSystemId(hints[0]);
140         }
141         String JavaDoc queryText = QueryReader.readSourceQuery(ss);
142         StaticQueryContext sqc = StaticQueryContext.makeStaticQueryContext(
143                 baseURI, getExecutable(), null, queryText, moduleURI);
144         getExecutable().fixupQueryModules(sqc);
145         return sqc;
146
147
148     }
149
150
151     public Expression compile(Executable exec) throws XPathException {
152         exec.setReasonUnableToCompile("Cannot compile a stylesheet that imports an XQuery library module");
153         return null;
154     }
155 }
156
157 //
158
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
159
// you may not use this file except in compliance with the License. You may obtain a copy of the
160
// License at http://www.mozilla.org/MPL/
161
//
162
// Software distributed under the License is distributed on an "AS IS" basis,
163
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
164
// See the License for the specific language governing rights and limitations under the License.
165
//
166
// The Original Code is: all this file.
167
//
168
// The Initial Developer of the Original Code is Michael H. Kay.
169
//
170
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
171
//
172
// Contributor(s): none.
173
//
174
Popular Tags