KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > xquery > parser > XQueryModule


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
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.parser;
24
25 import java.util.*;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.xquark.schema.SchemaManager;
30 import org.xquark.util.NamespaceContextStack;
31 import org.xquark.xquery.metadata.StaticContext;
32 import org.xquark.xquery.normalize.*;
33 import org.xquark.xquery.parser.util.Constants;
34 import org.xquark.xquery.typing.TypeVisitor;
35
36 /*
37 [30] Module ::= VersionDecl? (MainModule | LibraryModule)
38 [31] MainModule ::= Prolog QueryBody
39 [32] LibraryModule ::= ModuleDecl Prolog
40 [33] ModuleDecl ::= <"module" "namespace"> NCName "=" StringLiteral Separator
41 [34] Prolog ::= ((NamespaceDecl
42 | XMLSpaceDecl
43 | DefaultNamespaceDecl
44 | DefaultCollationDecl
45 | BaseURIDecl
46 | SchemaImport
47 | ModuleImport
48 | VarDecl
49 | ValidationDecl
50 | FunctionDecl) Separator)*
51 [35] Separator ::= ";"
52 */

53
54 public class XQueryModule {
55     private static final String JavaDoc RCSRevision = "$Revision: 1.36 $";
56     private static final String JavaDoc RCSName = "$Name: $";
57
58 // private static Log log = LogFactory.getLog("internal." + XQueryModule.class.getName());
59

60     protected String JavaDoc version = null;
61     protected String JavaDoc prefix = null;
62     protected String JavaDoc namespace = null;
63     protected NamespaceContextStack declarations = null;
64     protected String JavaDoc defaultFunctionNamespace = null;
65     protected int xmlspace = Constants.NOTHING;
66     protected String JavaDoc defaultCollation = null;
67     protected String JavaDoc baseURI = null;
68
69     protected HashMap hashVarList = null;
70     protected ArrayList declList = null;
71     protected HashMap hashExtVarList = null;
72     protected HashMap hashFuncList = null;
73     protected HashMap importSchemas = null;
74     protected ArrayList schemaList = null;
75     protected HashMap importModules = null;
76     protected ArrayList moduleList = null;
77
78     protected int validation = Constants.NOTHING;
79     protected ArrayList expressions = null;
80
81     protected SchemaManager schemaManager = null;
82     protected StaticContext staticContext = null;
83
84     // #############################################################################
85
// VISITOR STUFF
86
// #############################################################################
87

88     public void accept(ParserVisitor visitor) throws XQueryException {
89         visitor.visit(this);
90     }
91
92     // #############################################################################
93
// CONSTRUCTOR STUFF
94
// #############################################################################
95

96     public XQueryModule() {
97     }
98
99     // #############################################################################
100
// ATTRIBUTE ACCESS STUFF
101
// #############################################################################
102

103     // expressions
104
public ArrayList getExpressions() {
105         return expressions;
106     }
107     public void setExpressions(ArrayList expressions) throws XQueryException {
108         this.expressions = expressions;
109         if (expressions != null)
110             for (int i = 0; i < this.expressions.size(); i++) {
111                 ((XQueryExpression) this.expressions.get(i)).setParentModule(this);
112                 ((XQueryExpression) this.expressions.get(i)).setParentExpressions(null);
113             }
114     }
115
116     public void setElementAt(XQueryExpression expr, int index) {
117         expr.setParentModule(this);
118         expressions.set(index, expr);
119     }
120
121     // declarations
122
public NamespaceContextStack getDeclarations() {
123         return declarations;
124     }
125     public void setDeclarations(NamespaceContextStack declarations) {
126         this.declarations = declarations;
127     }
128
129     public String JavaDoc getNameSpace(String JavaDoc prefixname) {
130         if (prefixname == null)
131             return null;
132         if (declarations == null)
133             return null;
134         String JavaDoc str = declarations.getNamespaceURI(prefixname);
135         if (str == null)
136             return null;
137         int len = str.length();
138         if (str.indexOf("\"") == 0)
139             return str.substring(1, len - 2);
140         else
141             return str;
142     }
143
144     // variables
145
public HashMap getVariables() {
146         return hashVarList;
147     }
148     public void setDeclList(ArrayList declList) {
149         this.declList = declList;
150     }
151     public ArrayList getDeclList() {
152         return declList;
153     }
154     public void setVariables(HashMap hashVarList) {
155         this.hashVarList = hashVarList;
156         if (hashVarList != null) {
157             Collection varValues = hashVarList.values();
158             Iterator itValues = varValues.iterator();
159             while (itValues.hasNext()) {
160                 HashMap map = (HashMap) itValues.next();
161                 Collection mapValues = map.values();
162                 Iterator itMapValues = mapValues.iterator();
163                 while (itMapValues.hasNext()) {
164                     ((Variable) itMapValues.next()).setParentModule(this);
165                 }
166             }
167         }
168     }
169
170     public Variable getVariableDefinition(String JavaDoc name) {
171         if (name == null || hashVarList == null)
172             return null;
173         return (Variable) hashVarList.get(name);
174     }
175
176     // external variables
177
public HashMap getExternalVariables() {
178         return hashExtVarList;
179     }
180     public void setExternalVariables(HashMap hashExtVarList) {
181         this.hashExtVarList = hashExtVarList;
182     }
183
184     // functions
185
public HashMap getFunctions() {
186         return hashFuncList;
187     }
188     public void setFunctions(HashMap hashFuncList) {
189         this.hashFuncList = hashFuncList;
190         if (hashFuncList != null) {
191             Collection funcValues = hashFuncList.values();
192             Iterator itValues = funcValues.iterator();
193             while (itValues.hasNext()) {
194                 HashMap map = (HashMap) itValues.next();
195                 Collection mapValues = map.values();
196                 Iterator itMapValues = mapValues.iterator();
197                 while (itMapValues.hasNext()) {
198                     ((FunctionDeclaration) itMapValues.next()).setParentModule(this);
199                 }
200             }
201         }
202     }
203
204     // default collation
205
public void setDefaultCollation(String JavaDoc defaultCollation) {
206         this.defaultCollation = defaultCollation;
207     }
208
209     public String JavaDoc getDefaultCollation() {
210         return defaultCollation;
211     }
212
213     // xquery version
214
public void setVersion(String JavaDoc version) {
215         this.version = version;
216     }
217
218     public String JavaDoc getVersion() {
219         return version;
220     }
221
222     // module prefix
223
public void setPrefix(String JavaDoc prefix) {
224         this.prefix = prefix;
225     }
226
227     public String JavaDoc getPrefix() {
228         return prefix;
229     }
230
231     // module location
232
public void setNamespace(String JavaDoc location) {
233         this.namespace = location;
234     }
235
236     public String JavaDoc getNamespace() {
237         return namespace;
238     }
239
240     // default function namespace
241
public void setDefaultFunctionNamespace(String JavaDoc namespace) {
242         this.defaultFunctionNamespace = namespace;
243     }
244
245     public String JavaDoc getDefaultFunctionNamespace() {
246         return defaultFunctionNamespace;
247     }
248
249     // base uri
250
public void setBaseURI(String JavaDoc uri) {
251         this.baseURI = uri;
252     }
253
254     public String JavaDoc getBaseURI() {
255         return baseURI;
256     }
257
258     // xmlspace
259
public void setXmlSpace(int xmlspace) {
260         this.xmlspace = xmlspace;
261     }
262
263     public int getXmlSpace() {
264         return xmlspace;
265     }
266
267     // validation
268
public void setValidation(int validation) {
269         this.validation = validation;
270     }
271
272     public int getValidation() {
273         return validation;
274     }
275
276     // import schemas
277
public HashMap getImportSchemas() {
278         return importSchemas;
279     }
280     public ArrayList getSchemaList() {
281         return schemaList;
282     }
283     public void setSchemaList(ArrayList schemaList) {
284         this.schemaList = schemaList;
285     }
286     public void setImportSchemas(HashMap importSchemas) {
287         this.importSchemas = importSchemas;
288         if (importSchemas != null) {
289             Collection importSchemaValues = importSchemas.values();
290             Iterator itValues = importSchemaValues.iterator();
291             while (itValues.hasNext())
292                  ((ImportSchema) itValues.next()).setParentModule(this);
293         }
294     }
295
296     public ImportSchema getImportSchema(String JavaDoc name) {
297         if (name == null || importSchemas == null)
298             return null;
299         return (ImportSchema) importSchemas.get(name);
300     }
301
302     // import modules
303
public HashMap getImportModules() {
304         return importModules;
305     }
306     public void setModuleList(ArrayList moduleList) {
307         this.moduleList = moduleList;
308     }
309     public ArrayList getModuleList() {
310         return moduleList;
311     }
312     public void setImportModules(HashMap importModules) {
313         this.importModules = importModules;
314         if (importModules != null) {
315             Collection importModuleValues = importModules.values();
316             Iterator itValues = importModuleValues.iterator();
317             while (itValues.hasNext())
318                  ((ImportModule) itValues.next()).setParentModule(this);
319         }
320     }
321
322     public ImportModule getImportModule(String JavaDoc name) {
323         if (name == null || importModules == null)
324             return null;
325         return (ImportModule) importModules.get(name);
326     }
327
328     // schemaManager
329
public void setSchemaManager(SchemaManager schemaManager) {
330         this.schemaManager = schemaManager;
331     }
332     public SchemaManager getSchemaManager() {
333         return schemaManager;
334     }
335
336     // staticContext
337
public void setStaticContext(StaticContext staticContext) {
338         this.staticContext = staticContext;
339     }
340     public StaticContext getStaticContext() {
341         return staticContext;
342     }
343
344     // #############################################################################
345
// NORMALIZE STUFF
346
// #############################################################################
347

348     public void normalize(TypeVisitor typeVisitor) throws XQueryException {
349         normalize(typeVisitor, null);
350     }
351
352     public void normalize(TypeVisitor typeVisitor, String JavaDoc sourceName) throws XQueryException {
353 // if (log.isTraceEnabled())
354
// log.trace("START NORMALIZE ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤");
355
try {
356             ExpressionNormalizationVisitor normalizeVisitor = new ExpressionNormalizationVisitor(typeVisitor, sourceName);
357             accept(normalizeVisitor);
358         } catch (XQueryException ne) {
359             if (namespace != null)
360                 throw new XQueryException(ne.getMessage() + " in module " + namespace);
361             else
362                 throw new XQueryException(ne.getMessage() + " in main module");
363         }
364 // if (log.isTraceEnabled()) {
365
// log.trace("STOP NORMALIZE ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤");
366
// log.trace(toString(true, true));
367
// }
368

369 // if (log.isTraceEnabled())
370
// log.trace("START RESTRUCTURE ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤");
371
try {
372             if (expressions != null) {
373                 RestructureVisitor restructureVisitor = new RestructureVisitor(typeVisitor, (sourceName != null));
374                 accept(restructureVisitor);
375             }
376         } catch (XQueryException xqe) {
377             if (namespace != null)
378                 throw new XQueryException(xqe.getMessage() + " in module " + namespace);
379             else
380                 throw new XQueryException(xqe.getMessage() + " in main module");
381         }
382 // if (log.isTraceEnabled()) {
383
// log.trace("STOP RESTRUCTURE ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤");
384
// log.trace(toString(true, true));
385
// }
386
}
387
388     public TransformXQueryExpression canonize(XQueryExpression expression, TypeVisitor typeVisitor) throws ParseException {
389 // if (log.isTraceEnabled())
390
// log.trace("START CANONIZE ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤");
391
ArrayList QDB = null;
392         XQueryExpression QMEM = null;
393         try {
394             CanonizeVisitor canonizeVisitor = new CanonizeVisitor(typeVisitor);
395             expression.accept(canonizeVisitor);
396 // if (log.isTraceEnabled())
397
// log.trace("STOP CANONIZE ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤");
398
QDB = canonizeVisitor.getQDB();
399             QMEM = canonizeVisitor.getQMEM();
400         } catch (XQueryException e) {
401             throw new ParseException("Invalid expression : " + this +" -> " + e.getMessage());
402         }
403         if (QDB == null)
404             throw new ParseException("Invalid expression : " + this);
405 // if (log.isTraceEnabled()) {
406
// for (int si = 0; si < QDB.size(); si++) {
407
// log.trace("QDB[" + si + "]: " + ((Variable) QDB.get(si)).getExpression());
408
// }
409
// log.trace("QMEM: " + QMEM);
410
// }
411
return new TransformXQueryExpression(QDB, QMEM);
412     }
413
414     // #############################################################################
415
// SOURCES STUFF
416
// #############################################################################
417

418     public Set getSourceNames() {
419         Set sources = null;
420         if (expressions != null) {
421             for (int i = 0; i < expressions.size(); i++) {
422                 Set addSet = ((XQueryExpression) expressions.get(i)).getSourceNames();
423                 if (addSet != null) {
424                     if (sources == null)
425                         sources = new HashSet();
426                     sources.addAll(addSet);
427                 }
428             }
429         }
430         return sources;
431     }
432
433     public Set getUrls() {
434         Set urls = null;
435         if (expressions != null) {
436             for (int i = 0; i < expressions.size(); i++) {
437                 Set addSet = ((XQueryExpression) expressions.get(i)).getUrls();
438                 if (addSet != null) {
439                     if (urls == null)
440                         urls = new HashSet();
441                     urls.addAll(addSet);
442                 }
443             }
444         }
445         return urls;
446     }
447
448     // #############################################################################
449
// PRINT STUFF
450
// #############################################################################
451

452     public String JavaDoc toString(boolean doLineFeeds, boolean doImports) {
453         PrintVisitor pv = new PrintVisitor();
454         pv.reset(doLineFeeds, doImports, false, true);
455         pv.setNamespaceContextStack(declarations);
456         try {
457             this.accept(pv);
458         } catch (XQueryException qe) {
459         }
460         return pv.getString();
461     }
462
463     public String JavaDoc toString() {
464         PrintVisitor pv = new PrintVisitor();
465         pv.reset(false, true, false, true);
466         try {
467             this.accept(pv);
468         } catch (XQueryException qe) {
469         }
470         return pv.getString();
471     }
472 }
473
Popular Tags