KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > mediator > decomposer > StepLists


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

23
24 package org.xquark.mediator.decomposer;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Set JavaDoc;
29
30 import org.xquark.xquery.metadata.MetaDataImpl;
31 import org.xquark.xquery.metadata.resolver.PathResolver;
32 import org.xquark.xquery.parser.*;
33
34 public class StepLists {
35     // **********************************************************************
36
// * VERSIONING
37
// **********************************************************************
38
private static final String JavaDoc RCSRevision = "$Revision: 1.6 $";
39     private static final String JavaDoc RCSName = "$Name: $";
40     // **********************************************************************
41
// * CLASS VARIABLES
42
// **********************************************************************
43
private ArrayList JavaDoc stepslists = null;
44     private ArrayList JavaDoc cur_stepslists = null;
45
46     private HashMap JavaDoc mapvar = null;
47     private MetaDataImpl metadata = null;
48     
49     private PathResolver pathresolver = null;
50     private MakeStepsListVisitor stepslistvisitor = null;
51     
52     // **********************************************************************
53
// * INITIALIZATION
54
// **********************************************************************
55
/**
56      *
57      */

58     public StepLists(MetaDataImpl metadata) {
59         stepslists = new ArrayList JavaDoc();
60         mapvar = new HashMap JavaDoc();
61         this.metadata = metadata;
62         pathresolver = new PathResolver(metadata);
63         stepslistvisitor = new MakeStepsListVisitor();
64     }
65     
66     public StepLists(HashMap JavaDoc mapvar, MetaDataImpl metadata) {
67         stepslists = new ArrayList JavaDoc();
68         this.mapvar = mapvar;
69         this.metadata = metadata;
70         pathresolver = new PathResolver(metadata);
71         stepslistvisitor = new MakeStepsListVisitor();
72     }
73
74     // **********************************************************************
75
// * METHODS
76
// **********************************************************************
77
public void newStepslist() throws XQueryException {
78         if ((cur_stepslists != null) && (cur_stepslists.isEmpty()))
79             return;
80         cur_stepslists = new ArrayList JavaDoc(1);
81         cur_stepslists.add(new ArrayList JavaDoc());
82         stepslists.addAll(cur_stepslists);
83     }
84
85     public ArrayList JavaDoc getStepsLists() {
86         return stepslists;
87     }
88
89     public Set JavaDoc getRegisteredVariables() {
90         return mapvar.keySet();
91     }
92
93     public void registerVars(ArrayList JavaDoc vars) throws XQueryException {
94         if (vars == null)
95             return;
96         for (int i = 0; i < vars.size(); i++)
97             this.registerVar((Variable) vars.get(i));
98     }
99     public void registerVar(Variable var) throws XQueryException {
100
101         // ADD 11/04/2003
102
if ((var.getSourceNames() != null && !var.getSourceNames().isEmpty()) || (var.getUrls() != null && !var.getUrls().isEmpty())) {
103             if (mapvar.get(var) != null)
104                 return;
105             mapvar.put(var, null);
106             return;
107         }
108         // END ADD
109

110         XQueryExpression exp = var.getExpression();
111
112         // ADD 11/04/2003
113
if ((exp.getSourceNames() != null && !exp.getSourceNames().isEmpty()) || (exp.getUrls() != null && !exp.getUrls().isEmpty())) {
114             var.setSourceNames(exp.getSourceNames());
115             var.setUrls(exp.getUrls());
116             if (mapvar.get(var) != null)
117                 return;
118             mapvar.put(var, null);
119             return;
120         }
121         // END ADD
122

123         StepLists tmplist = new StepLists(this.mapvar, this.metadata);
124         stepslistvisitor.setStepsLists(tmplist);
125         exp.accept(stepslistvisitor);
126
127         // ADD 11/04/2003
128
if ((exp.getSourceNames() != null && !exp.getSourceNames().isEmpty()) || (exp.getUrls() != null && !exp.getUrls().isEmpty())) {
129             var.setSourceNames(exp.getSourceNames());
130             var.setUrls(exp.getUrls());
131             if (mapvar.get(var) != null)
132                 return;
133             mapvar.put(var, null);
134             return;
135         }
136         // END ADD
137

138         // if (exp.getSourceNames() != null && !exp.getSourceNames().isEmpty())
139
// var.setSourceNames(exp.getSourceNames());
140
// else {
141
ArrayList JavaDoc al = tmplist.getStepsLists();
142
143         if (al != null && !al.isEmpty()) {
144             // there should be only one path for each variable
145
/*
146             if (al.size() != 1)
147                 throw new XQueryException("More than one path associated to variable : " + var);
148             */

149             ArrayList JavaDoc endlist = (ArrayList JavaDoc) al.get(al.size() - 1);
150             mapvar.put(var, endlist);
151             
152             if (!endlist.isEmpty()) {
153                 if (((Step)endlist.get(0)).getExpression() instanceof Variable) {
154                     Variable frontvar = (Variable)((Step)endlist.get(0)).getExpression();
155                     var.setSourceNames(frontvar.getSourceNames());
156                     var.setUrls(frontvar.getUrls());
157                     exp.setSourceNames(frontvar.getSourceNames());
158                     exp.setUrls(frontvar.getUrls());
159                 } else if (metadata != null) {
160                     pathresolver.resolveSteps(endlist);
161                     var.setSourceNames(pathresolver.getSourceNames());
162                     var.setUrls(pathresolver.getUrls());
163                     exp.setSourceNames(pathresolver.getSourceNames());
164                     exp.setUrls(pathresolver.getUrls());
165                 }
166             }
167         }
168         // }
169
//return;
170
// ArrayList al = visitor.getStepsLists().getStepsLists();
171
// if (al != null && !al.isEmpty()) {
172
// // there should be only one path for each variable
173
// /*
174
// if (al.size() != 1)
175
// throw new XQueryException("More than one path associated to variable : " + var);
176
// */
177
// if (al.size() == 1)
178
// mapvar.put(var, al.get(0)) ;
179
//
180
// if (metadata != null) {
181
// PathResolver pathresolver = new PathResolver(metadata);
182
// pathresolver.resolveStepsList(al);
183
// var.setSources(pathresolver.getArraySources());
184
// }
185
// }
186

187     }
188
189     public void addReplaceVar(Variable var) throws XQueryException {
190         ArrayList JavaDoc prefix = (ArrayList JavaDoc) mapvar.get(var);
191         if (prefix == null)
192             return; // throw new Exception(var + " was not registered before. Something went wrong") ;
193
for (int i = 0; i < prefix.size(); i++) {
194             Step stepi = (Step) prefix.get(i);
195             addStep(stepi);
196         }
197     }
198
199     public void addStep(XQueryExpression step) throws XQueryException {
200         if (step == null)
201             return;
202
203         if (cur_stepslists == null)
204             newStepslist();
205
206         if (step instanceof Variable) {
207             newStepslist();
208             addReplaceVar((Variable) step);
209             return;
210         }
211
212         if (!(step instanceof Step))
213             throw new XQueryException("Error: step is not type Step or Variable");
214
215         Step stepp = (Step) step;
216         /*
217         if (stepp.getExpression() instanceof NodeType) {
218             // do nothing
219         } else if (stepp.getExpression() instanceof Variable)
220             addReplaceVar((Variable) stepp.getExpression());
221         else if (stepp.getExpression() instanceof QName)
222             for (int i = 0; i < cur_stepslists.size(); i++)
223                  ((ArrayList) cur_stepslists.get(i)).add(stepp);
224         else if (stepp.getExpression() instanceof FunctionCOLLECTION)
225             for (int i = 0; i < cur_stepslists.size(); i++)
226                  ((ArrayList) cur_stepslists.get(i)).add(stepp);
227         else if (stepp.getExpression() instanceof FunctionDOC)
228             for (int i = 0; i < cur_stepslists.size(); i++)
229                  ((ArrayList) cur_stepslists.get(i)).add(stepp);
230         else if (stepp.getExpression() instanceof Value)
231             for (int i = 0; i < cur_stepslists.size(); i++)
232                  ((ArrayList) cur_stepslists.get(i)).add(stepp);
233         else
234         */

235         if (stepp.getExpression() instanceof ListOpUNIONExpression) {
236             // special case union operators are issued from normalization (located expressions)
237
// handle first located expression
238
ArrayList JavaDoc steps = ((LocatedExpression)((ListOpUNIONExpression)stepp.getExpression()).getExpression1()).getSteps();
239             // clone current list (depth 2)
240
ArrayList JavaDoc tmplist = new ArrayList JavaDoc(cur_stepslists.size());
241             for (int i=0;i<cur_stepslists.size();i++)
242                 tmplist.add(((ArrayList JavaDoc)cur_stepslists.get(i)).clone());
243             for (int i=0;i<steps.size();i++)
244                 addStep((Step)steps.get(i));
245             // create new current list and remember previous
246
ArrayList JavaDoc oldlist = cur_stepslists;
247             stepslists.addAll(tmplist);
248             cur_stepslists = tmplist;
249             // handle second located expression
250
steps = ((LocatedExpression)((ListOpUNIONExpression)stepp.getExpression()).getExpression2()).getSteps();
251             for (int i=0;i<steps.size();i++)
252                 addStep((Step)steps.get(i));
253             // set new current list (addition fof both lists
254
cur_stepslists.addAll(oldlist);
255         }
256         else {
257             for (int i = 0; i < cur_stepslists.size(); i++)
258                  ((ArrayList JavaDoc) cur_stepslists.get(i)).add(stepp);
259         }
260 // else
261
// throw new XQueryException("Error: step.getExpression : " + ((Step)step).getExpression() + " is not type QName or Variable or a FunctionCOLLECTION or a FunctionDOCUMENT");
262
}
263
264 // public String toString() {
265
// StringBuffer sb = new StringBuffer();
266
//
267
// sb.append("{");
268
// for (int i = 0; i < stepslists.size(); i++) {
269
// sb.append(" [");
270
// ArrayList stepslist = (ArrayList) stepslists.get(i);
271
// for (int j = 0; j < stepslist.size(); j++) {
272
// Step step = (Step) stepslist.get(j);
273
// if (j != 0)
274
// sb.append(", ");
275
// sb.append(step.toString());
276
// }
277
// sb.append("] ");
278
// }
279
// sb.append("}");
280
// return sb.toString();
281
// }
282
}
283
Popular Tags