KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > xquery > metadata > StaticContext


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 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.metadata;
24
25 import java.util.*;
26
27 import org.xquark.schema.SchemaManager;
28 import org.xquark.schema.datatypes.URI;
29 import org.xquark.util.NamespaceContextStack;
30 import org.xquark.xpath.StepExpr;
31 import org.xquark.xpath.XNode;
32 import org.xquark.xpath.XTreeNode;
33 import org.xquark.xquery.ModuleManager;
34 import org.xquark.xquery.metadata.resolver.CollectionMetadata;
35 import org.xquark.xquery.metadata.resolver.MetadataAccess;
36 import org.xquark.xquery.metadata.resolver.SourceMetadata;
37 import org.xquark.xquery.parser.*;
38 import org.xquark.xquery.typing.*;
39
40 public class StaticContext {
41
42     public static final String JavaDoc XQUERY_FUNCTIONS_URI = "http://www.w3.org/2002/11/xquery-functions";
43     public static final String JavaDoc XML_SCHEMA_URI = "http://www.w3.org/2001/XMLSchema";
44     public static final String JavaDoc XML_SCHEMA_INSTANCE_URI = "http://www.w3.org/2001/XMLSchema-instance";
45
46     private NamespaceContextStack prefixes = new NamespaceContextStack();
47     private String JavaDoc defaultFunctionNamespace = XQUERY_FUNCTIONS_URI;
48     private SchemaManager schemaManager;
49     private ModuleManager moduleManager;
50     private MetadataAccess resolver = null;
51     private HashMap functions = new HashMap();
52     private HashMap collations = new HashMap();
53     private String JavaDoc defaultCollation = null;
54     private String JavaDoc baseURI = null;
55     private boolean stripWhitespace = true;
56     private TypeVisitor typeVisitor = null;
57     
58     private VarCounter varCounter = null;
59     
60     public StaticContext(MetadataAccess resolver, SchemaManager schemamanager, ModuleManager modulemanager, VarCounter varCounter) {
61         this.resolver = resolver;
62         this.varCounter = varCounter;
63         schemaManager = new SchemaManager(schemamanager);
64         moduleManager = new ModuleManager(modulemanager);
65         moduleManager.setStaticContext(this);
66     }
67     
68     public VarCounter getVarCounter() {
69         return varCounter;
70     }
71
72     /**
73      * Returns the baseURI.
74      * @return String
75      */

76     public String JavaDoc getBaseURI() {
77         return baseURI;
78     }
79
80     /**
81      * Returns the defaultCollation.
82      * @return String
83      */

84     public String JavaDoc getDefaultCollation() {
85         return defaultCollation;
86     }
87
88     /**
89      * Returns the defaultFunctionNamespace.
90      * @return String
91      */

92     public String JavaDoc getDefaultFunctionNamespace() {
93         return defaultFunctionNamespace;
94     }
95
96     /**
97      * Returns the schemaManager.
98      * @return SchemaManager
99      */

100     public SchemaManager getSchemaManager() {
101         return schemaManager;
102     }
103     
104     public void setTypeVisitor(TypeVisitor typeVisitor) {
105         this.typeVisitor = typeVisitor;
106     }
107     public TypeVisitor getTypeVisitor() {
108         return typeVisitor;
109     }
110
111     /**
112      * Sets the baseURI.
113      * @param baseURI The baseURI to set
114      */

115     public void setBaseURI(String JavaDoc baseURI) {
116         this.baseURI = baseURI;
117     }
118
119     /**
120      * Sets the defaultCollation.
121      * @param defaultCollation The defaultCollation to set
122      */

123     public void setDefaultCollation(String JavaDoc defaultCollation) {
124         this.defaultCollation = defaultCollation;
125     }
126
127     /**
128      * Sets the defaultFunctionNamespace.
129      * @param defaultFunctionNamespace The defaultFunctionNamespace to set
130      */

131     public void setDefaultFunctionNamespace(String JavaDoc defaultFunctionNamespace) {
132         this.defaultFunctionNamespace = defaultFunctionNamespace;
133     }
134
135     /**
136      * Returns the collations.
137      * @return HashMap
138      */

139     public HashMap getCollations() {
140         return collations;
141     }
142
143     /**
144      * Returns the functions.
145      * @return HashMap
146      */

147     public HashMap getFunctions() {
148         return functions;
149     }
150
151     /**
152      * Returns the prefixes.
153      * @return NamespaceContextStack
154      */

155     public NamespaceContextStack getPrefixes() {
156         return prefixes;
157     }
158
159     /**
160      * Returns the module manager.
161      * @return ModuleManager
162      */

163     public ModuleManager getModuleManager() {
164         return moduleManager;
165     }
166 // public void setModuleManager(ModuleManager modulemanager) {
167
// this.modulemanager = modulemanager;
168
// }
169

170     /**
171      * Returns the variables.
172      * @return HashMap
173      */

174     /*
175     public ScopeManager getVariables() {
176         return variables;
177     }
178     */

179
180     /**
181      * Returns the stripWhitespace.
182      * @return boolean
183      */

184     public boolean isStripWhitespace() {
185         return stripWhitespace;
186     }
187
188     /**
189      * Sets the stripWhitespace.
190      * @param stripWhitespace The stripWhitespace to set
191      */

192     public void setStripWhitespace(boolean stripWhitespace) {
193         this.stripWhitespace = stripWhitespace;
194     }
195
196     // XPathResolver interface
197

198     public ArrayList resolveXPath(ArrayList xnodes, ArrayList steps) {
199         if (steps == null || steps.isEmpty())
200             return xnodes;
201         for (int i = 0; i < steps.size(); i++) {
202             xnodes = resolveXPath(xnodes, (StepExpr) steps.get(i));
203         }
204         return xnodes;
205     }
206
207     public ArrayList resolveXPath(ArrayList xnodes, StepExpr step) {
208         if (xnodes == null || xnodes.isEmpty())
209             return null;
210         ArrayList reslist = new ArrayList();
211         for (int i = 0; i < xnodes.size(); i++) {
212             Collection col = ((XTreeNode) xnodes.get(i)).navigate(step);
213             reslist.addAll(col);
214         }
215         if (reslist.isEmpty())
216             reslist = null;
217         return reslist;
218     }
219
220     // this method assumes that steps are simple
221
public QType resolveQType(byte context, QType qtype, StepExpr step, boolean inPredicate) throws TypeException, XQueryException {
222         //return resolver.resolveQType(qtype,step);
223
return Utils.getSubType(context, qtype, step, schemaManager, inPredicate);
224     }
225
226     private final static String JavaDoc COLON = ":";
227     private final static String JavaDoc STAR = "*";
228     private final static String JavaDoc DOT = ".";
229
230     public ArrayList getCollectionRootNodes(String JavaDoc sourceName, String JavaDoc collectionName) {
231
232         ArrayList al = new ArrayList();
233         SourceMetadata mw = null;
234         mw = resolver.getSourceMetadata(sourceName);
235         if (mw != null) {
236             CollectionMetadata metacol = mw.getCollectionMetadata(collectionName);
237             if (metacol != null)
238                 al.add(metacol.getXTree().getRoot());
239             else { // treat case where collection name ends with .collectionName
240
Collection coll = mw.getCollectionsMetadata();
241                 for (Iterator it = coll.iterator(); it.hasNext();) {
242                     metacol = (CollectionMetadata) it.next();
243                     String JavaDoc colName = metacol.getCollectionName();
244                     if (STAR.equals(collectionName) || colName.endsWith(DOT + collectionName)) {
245                         //metacol = mw.getCollectionMetadata(colName);
246
al.add(metacol.getXTree().getRoot());
247                         //break;
248
}
249                 }
250             }
251         } else {
252             Collection sources = resolver.getSources();
253             for (Iterator e = sources.iterator(); e.hasNext();) {
254                 mw = (SourceMetadata) e.next();
255                 CollectionMetadata metacol = mw.getCollectionMetadata(collectionName);
256                 if (metacol != null)
257                     al.add(metacol.getXTree().getRoot());
258                 else { // treat case where collection name ends with .collectionName
259
Collection coll = mw.getCollectionsMetadata();
260                     for (Iterator it = coll.iterator(); it.hasNext();) {
261                         metacol = (CollectionMetadata) it.next();
262                         String JavaDoc colName = metacol.getCollectionName();
263                         if (STAR.equals(collectionName) || colName.endsWith(DOT + collectionName)) {
264                             //metacol = mw.getCollectionMetadata(colName);
265
al.add(metacol.getXTree().getRoot());
266                             //break;
267
}
268                     }
269                 }
270             }
271         }
272         if (al.isEmpty())
273             al = null;
274         return al;
275     }
276
277     public MetadataAccess getMetaData() {
278         return resolver;
279     }
280
281     public XNode getDocumentRootNode(URI documentURI) {
282         return null;
283     }
284
285     public ArrayList getInputRootNodes() {
286         return null;
287     }
288     
289     private final String JavaDoc VAR_NAME = "var";
290
291     public Variable createVariable(int bindingType, XQueryExpression expr, byte creator) throws XQueryException {
292         if (typeVisitor != null && expr != null && expr.getQType() == null)
293             expr.accept(typeVisitor);
294         Variable var = new Variable(getVarName(expr.getParentModule()), expr.getParentModule());
295         var.setBindingType(bindingType);
296         var.setExpression(expr,true);
297         var.setCreator(creator);
298         return var;
299     }
300     public QName getVarName(XQueryModule parentModule) throws XQueryException {
301         return new QName(parentModule.getNamespace(), parentModule.getPrefix(), VAR_NAME + (++varCounter.lastVarIndex), parentModule, parentModule.getStaticContext().getPrefixes());
302     }
303
304 }
305
Popular Tags