KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > xquery > ModuleManager


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2004 XQuark Group.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 package org.xquark.xquery;
24
25 import java.net.MalformedURLException JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Iterator JavaDoc;
30
31 import org.xml.sax.InputSource JavaDoc;
32 import org.xquark.schema.SchemaManager;
33 import org.xquark.xquery.metadata.StaticContext;
34 import org.xquark.xquery.metadata.VarCounter;
35 import org.xquark.xquery.metadata.resolver.MetadataAccess;
36 import org.xquark.xquery.parser.*;
37 import org.xquark.xquery.typing.TypeVisitor;
38
39 public class ModuleManager extends DefaultModuleLocator {
40     private static final String JavaDoc RCSRevision = "$Revision: 1.4 $";
41     private static final String JavaDoc RCSName = "$Name: $";
42
43
44     // XQueryModule cache based on namespaces
45
private HashMap JavaDoc modules = new HashMap JavaDoc();
46     private boolean useCache = true;
47
48     private StaticContext staticContext = null;
49
50     public ModuleManager() {
51         this(null, true);
52     }
53
54 // public ModuleManager(boolean useCache) {
55
// this(null, useCache);
56
// }
57

58     public ModuleManager(ModuleManager parent) {
59         this(parent, true);
60     }
61
62     public ModuleManager(ModuleManager parent, boolean useCache) {
63         this.useCache = useCache;
64         if (parent != null) {
65             Iterator JavaDoc it = parent.getModules().iterator();
66             while (it.hasNext()) {
67                 XQueryModule s = (XQueryModule) it.next();
68                 // start add temporary
69
if (s != null)
70                     // end add temporary
71
modules.put(s.getNamespace(), s);
72             }
73         }
74     }
75
76     public void setStaticContext(StaticContext staticContext) {
77         this.staticContext = staticContext;
78     }
79
80     public boolean isRegistered(String JavaDoc namespace) {
81         return modules.containsKey(namespace);
82     }
83
84     public XQueryModule getModule(String JavaDoc namespace) {
85         return (XQueryModule) modules.get(namespace);
86     }
87
88     public Collection JavaDoc getModules() {
89         return modules.values();
90     }
91
92     public XQueryModule removeModule(String JavaDoc namespace) {
93         return (XQueryModule) modules.remove(namespace);
94     }
95
96     private void checkNamespace(String JavaDoc ns, XQueryModule module) throws XQueryException {
97         if ((ns != null && !ns.equals(module.getNamespace())) || (ns == null && module.getNamespace() != null))
98             throw new XQueryException("Loaded module target namespace " + module.getNamespace() + " does not match expected URI: " + ns);
99     }
100
101     public XQueryModule loadModule(String JavaDoc namespace, SchemaManager sc, TypeVisitor typeVisitor) throws XQueryException {
102         return loadModule(namespace, sc, typeVisitor, new VarCounter());
103     }
104     public XQueryModule loadModule(String JavaDoc namespace, SchemaManager sc, TypeVisitor typeVisitor, VarCounter varCounter) throws XQueryException {
105         if (namespace.startsWith("java:")) {
106             return new JavaModule(namespace.substring(5), typeVisitor);
107         } else if (namespace.startsWith("wsdl:")) {
108             return new WSDLModule(namespace.substring(5));
109         }
110         return loadModule(this, namespace, sc, varCounter);
111     }
112
113     public XQueryModule loadModule(String JavaDoc namespace, SchemaManager sc) throws XQueryException {
114         return loadModule(namespace, sc, new VarCounter());
115     }
116     public XQueryModule loadModule(String JavaDoc namespace, SchemaManager sc, VarCounter varCounter) throws XQueryException {
117         return loadModule(this, namespace, sc, varCounter);
118     }
119
120     public XQueryModule loadModule(ModuleLocator locator, String JavaDoc namespace, SchemaManager sc) throws XQueryException {
121         return loadModule(locator, namespace, sc, new VarCounter());
122     }
123     public XQueryModule loadModule(ModuleLocator locator, String JavaDoc namespace, SchemaManager sc, VarCounter varCounter) throws XQueryException {
124         // Look into module cache
125
if (isRegistered(namespace))
126             return getModule(namespace);
127
128         XQueryModule result = null;
129         InputSource JavaDoc source = null;
130         // Load module from its locations as strings
131
Iterator JavaDoc it1 = locator.getModuleLocations(namespace);
132         if (it1 != null) {
133             while (it1.hasNext()) {
134                 source = locator.resolveLocation(namespace, (String JavaDoc) it1.next());
135                 if (source != null)
136                     result = loadModule(locator, namespace, source, sc, varCounter);
137                 if (result != null)
138                     return result;
139             }
140         }
141
142         // Load module from its URI as string
143
source = locator.resolveLocation(namespace, namespace);
144         if (source != null)
145             result = loadModule(locator, namespace, source, sc, varCounter);
146         if (result != null)
147             return result;
148
149         // Load module from its locations as URLs
150
Iterator JavaDoc it2 = locator.getModuleLocations(namespace);
151         if (it2 != null) {
152             while (it2.hasNext()) {
153                 try {
154                     URL JavaDoc url = new URL JavaDoc((String JavaDoc) it2.next());
155                     source = locator.resolveLocation(namespace, url);
156                     if (source != null)
157                         result = loadModule(locator, namespace, source, sc, varCounter);
158                     if (result != null)
159                         return result;
160                 } catch (MalformedURLException JavaDoc ex) {
161                     // continue
162
}
163             }
164         }
165
166         // result is null
167
throw new XQueryException("Could not find module "+namespace);
168     }
169
170     private XQueryModule loadModule(ModuleLocator locator, String JavaDoc namespace, InputSource JavaDoc source, SchemaManager sc, VarCounter varCounter) throws XQueryException {
171         // Look into module cache
172
if (isRegistered(namespace))
173             return getModule(namespace);
174
175         XQueryModule result = null;
176
177         try {
178             XQueryParser parser = new XQueryParser(source.getByteStream());
179             // parsing request
180
parser.setBaseURI(source.getSystemId());
181             result = parser.Start(staticContext.getMetaData(), sc, this, varCounter);
182             checkNamespace(namespace, result);
183         } catch (ParseException ex) {
184             throw new XQueryException(ex.getMessage(), ex);
185         }
186         // Add module to namespace cache
187
modules.put(namespace, result);
188         return result;
189     }
190     private XQueryModule loadModule(ModuleLocator locator, InputSource JavaDoc source, MetadataAccess metadata, SchemaManager sc, VarCounter varCounter) throws XQueryException {
191         XQueryModule result = null;
192
193         try {
194             XQueryParser parser = new XQueryParser(source.getByteStream());
195             // parsing request
196
parser.setBaseURI(source.getSystemId());
197             // test staticContext
198
result = parser.Start(metadata, sc, this, varCounter);
199         } catch (ParseException ex) {
200             throw new XQueryException(ex.getMessage(), ex);
201         }
202         // Add module to namespace cache
203
String JavaDoc namespace = result.getNamespace();
204         modules.put(namespace, result);
205         return result;
206     }
207
208     public XQueryModule loadModule(InputSource JavaDoc source, MetadataAccess metadata, SchemaManager sc) throws XQueryException {
209         return loadModule(this, source, metadata, sc, new VarCounter());
210     }
211     public XQueryModule loadModule(String JavaDoc namespace, InputSource JavaDoc source, SchemaManager sc) throws XQueryException {
212         return loadModule(this, namespace, source, sc, new VarCounter());
213     }
214     public XQueryModule loadModule(String JavaDoc namespace, InputSource JavaDoc source, SchemaManager sc, VarCounter varCounter) throws XQueryException {
215         return loadModule(this, namespace, source, sc, varCounter);
216     }
217
218     public XQueryModule loadModule(ModuleLocator locator, String JavaDoc namespace, URL JavaDoc location, SchemaManager sc) throws XQueryException {
219         return loadModule(locator, namespace, locator.resolveLocation(namespace, location), sc, new VarCounter());
220     }
221     public XQueryModule loadModule(ModuleLocator locator, String JavaDoc namespace, URL JavaDoc location, SchemaManager sc, VarCounter varCounter) throws XQueryException {
222         return loadModule(locator, namespace, locator.resolveLocation(namespace, location), sc, varCounter);
223     }
224
225     public XQueryModule loadModule(String JavaDoc namespace, URL JavaDoc location, SchemaManager sc) throws XQueryException {
226         return loadModule(this, namespace, location, sc);
227     }
228     public XQueryModule loadModule(String JavaDoc namespace, URL JavaDoc location, SchemaManager sc, VarCounter varCounter) throws XQueryException {
229         return loadModule(this, namespace, location, sc, varCounter);
230     }
231
232     public XQueryModule loadModule(ModuleLocator locator, String JavaDoc namespace, String JavaDoc location, SchemaManager sc) throws XQueryException {
233         return loadModule(locator, namespace, locator.resolveLocation(namespace, location), sc, new VarCounter());
234     }
235     public XQueryModule loadModule(ModuleLocator locator, String JavaDoc namespace, String JavaDoc location, SchemaManager sc, VarCounter varCounter) throws XQueryException {
236         return loadModule(locator, namespace, locator.resolveLocation(namespace, location), sc, varCounter);
237     }
238
239     public XQueryModule loadModule(String JavaDoc namespace, String JavaDoc location, SchemaManager sc) throws XQueryException {
240         return loadModule(this, namespace, location, sc);
241     }
242     public XQueryModule loadModule(String JavaDoc namespace, String JavaDoc location, SchemaManager sc, VarCounter varCounter) throws XQueryException {
243         return loadModule(this, namespace, location, sc, varCounter);
244     }
245
246 // public XQueryModule loadModule(ModuleLocator locator, InputStream stream, SchemaManager sc) throws XQueryException {
247
// return loadModule(locator, stream, sc, new VarCounter());
248
// }
249
// public XQueryModule loadModule(ModuleLocator locator, InputStream stream, SchemaManager sc, VarCounter varCounter) throws XQueryException {
250
// XQueryModule result = null;
251
// try {
252
// XQueryParser parser = new XQueryParser(stream);
253
// // parsing request
254
// // setBaseURI
255
// result = parser.Start(staticContext.getMetaData(),sc,this,varCounter);
256
// } catch (ParseException ex) {
257
// throw new XQueryException(ex.getMessage(), ex);
258
// }
259
// String namespace = result.getNamespace();
260
// // Add module to namespace cache
261
// modules.put(namespace, result);
262
// return result;
263
// }
264
// public XQueryModule loadModule(InputStream stream, SchemaManager sc) throws XQueryException {
265
// return loadModule(this, stream, sc);
266
// }
267
// public XQueryModule loadModule(InputStream stream, SchemaManager sc, VarCounter varCounter) throws XQueryException {
268
// return loadModule(this, stream, sc, varCounter);
269
// }
270

271     public boolean putModule(XQueryModule module) throws XQueryException {
272         if (isRegistered(module.getNamespace()))
273             return false;
274         modules.put(module.getNamespace(), module);
275         return true;
276     }
277
278 }
279
Popular Tags