KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > xquery > normalize > SubExpressionVisitor


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.xquery.normalize;
25
26 // import org.xquark.xml.xqueryparser.common.primitivefunctions.*;
27
import java.util.ArrayList JavaDoc;
28 import java.util.HashSet JavaDoc;
29 import java.util.Set JavaDoc;
30
31 import org.xquark.schema.AttributeDeclaration;
32 import org.xquark.schema.ElementDeclaration;
33 import org.xquark.schema.Type;
34 import org.xquark.xpath.Axis;
35 import org.xquark.xquery.parser.*;
36 import org.xquark.xquery.typing.*;
37
38 public class SubExpressionVisitor extends DefaultParserVisitor {
39
40     private static final String JavaDoc RCSRevision = "$Revision: 1.2 $";
41     private static final String JavaDoc RCSName = "$Name: $";
42
43     private ArrayList JavaDoc resultExprs = new ArrayList JavaDoc(1);
44     private XQueryExpression subExpression = null;
45     private byte axis = Axis.NONE;
46     private int depth = 1;
47     private boolean last = false;
48     private String JavaDoc subNameSpace = null;
49     private String JavaDoc subLocalName = null;
50     private TypeVisitor typeVisitor = null;
51
52     public ArrayList JavaDoc getResultExprs() {
53         return resultExprs;
54     }
55
56     public SubExpressionVisitor(TypeVisitor typeVisitor) {
57         this.typeVisitor = typeVisitor;
58     }
59
60     public void setSubExpression(byte axis) {
61         setSubExpression(null, axis, 1, false);
62     }
63     public void setSubExpression(byte axis, int depth) {
64         setSubExpression(null, axis, depth, false);
65     }
66     public void setSubExpression(QName qname) {
67         setSubExpression(qname, Axis.NONE, 1, false);
68     }
69     public void setSubExpression(QName qname, boolean last) {
70         setSubExpression(qname, Axis.CHILD, 1, last);
71     }
72     public void setSubExpression(QName qname, byte axis) {
73         setSubExpression(qname, axis, 1, false);
74     }
75     public void setSubExpression(QName qname, byte axis, int depth, boolean last) {
76         subExpression = qname;
77         this.axis = axis;
78         this.depth = depth;
79         this.last = last;
80         if (subExpression != null) {
81             subNameSpace = ((QName) subExpression).getNameSpace();
82             subLocalName = ((QName) subExpression).getLocalName();
83         } else {
84             subNameSpace = null;
85             subLocalName = null;
86         }
87         resultExprs.clear();
88     }
89
90     /*
91     public void visit(AggregateFunctionCall arg) throws XQueryException;
92      */

93
94     public void visit(AttributeValuePair arg) throws XQueryException {
95         resultExprs.clear();
96         // if (arg.getQType().getOccurence() != QType.OCC_1_1) return;
97
if (depth == 0 && axis == Axis.ATTRIBUTE) {
98             XQueryExpression attValPairName = arg.getAttributeName();
99             if (!(attValPairName instanceof QName))
100                 return;
101             String JavaDoc nameSpace = ((QName) attValPairName).getNameSpace();
102             String JavaDoc localName = ((QName) attValPairName).getLocalName();
103             if (compareQNames(nameSpace, localName, subNameSpace, subLocalName)) {
104                 XQueryExpression attValPairValue = arg.getAttributeValue();
105                 if (attValPairValue instanceof XQueryExpressionSequence)
106                     resultExprs.add((XQueryExpression) ((XQueryExpressionSequence) attValPairValue).getSubExpressions().get(0));
107                 else
108                     resultExprs.add(attValPairValue);
109             }
110         }
111     }
112
113     /*
114     public void visit(BinOpANDExpression arg) throws XQueryException;
115     public void visit(BinOpORExpression arg) throws XQueryException;
116     public void visit(CastTreatExpression arg) throws XQueryException;
117     public void visit(ContextDeclaration arg) throws XQueryException;
118     public void visit(Dereference arg) throws XQueryException;
119      */

120
121     public void visit(Element arg) throws XQueryException {
122         resultExprs.clear();
123         // if (arg.getQType().getOccurence() != QType.OCC_1_1) return;
124
ArrayList JavaDoc subExpressions = arg.getSubExpressions();
125         ArrayList JavaDoc attributes = arg.getAttributes();
126         QType qtype = arg.getQType();
127         Type type = qtype.getType();
128         if (subExpression == null) {
129             if (subExpressions != null && subExpressions.size() == 1) {
130                 XQueryExpression tmpExpr = (XQueryExpression) subExpressions.get(0);
131                 // verify that QType is QTypeElement with simple type
132
switch (axis) {
133                     case Axis.SELF :
134                         if (type != null && type.getValueType() != null) {
135                             if (tmpExpr instanceof XQueryExpressionSequence)
136                                 subExpressions = ((XQueryExpressionSequence) tmpExpr).getSubExpressions();
137                             resultExprs.add(subExpressions.get(0));
138                         }
139                         break;
140                 }
141             }
142             return;
143         }
144         XQueryExpression startTag = arg.getStartTag();
145         if (!(startTag instanceof QName)) {
146             return;
147         }
148         if (depth == 0) {
149             String JavaDoc nameSpace = null;
150             String JavaDoc localName = null;
151             switch (axis) {
152                 case Axis.NONE :
153                 case Axis.CHILD :
154                     QName startTagName = (QName) startTag;
155                     nameSpace = startTagName.getNameSpace();
156                     localName = startTagName.getLocalName();
157                     if (compareQNames(nameSpace, localName, subNameSpace, subLocalName)) {
158                         if (last) {
159                             if (qtype.getType().isSimpleType()) {
160                                 if (subExpressions != null && subExpressions.size() == 1) {
161                                     XQueryExpression tmpExpr = (XQueryExpression) subExpressions.get(0);
162                                     if (tmpExpr instanceof XQueryExpressionSequence)
163                                         subExpressions = ((XQueryExpressionSequence) tmpExpr).getSubExpressions();
164                                     resultExprs.add(subExpressions.get(0));
165                                 }
166                             }
167                         } else {
168                             if (subExpressions != null)
169                                 for (int i = 0; i < subExpressions.size(); i++)
170                                     resultExprs.add(subExpressions.get(i));
171                             if (attributes != null)
172                                 for (int i = 0; i < attributes.size(); i++)
173                                     resultExprs.add(attributes.get(i));
174                         }
175                         return;
176                     }
177                     break;
178                     /*
179                     case Constants.ATTRIBUTE :
180                         for (int i=0;i<attributes.size();i++) {
181                             AttributeValuePair attValPair = (AttributeValuePair)attributes.get(i);
182                             XQueryExpression attValPairName = attValPair.getAttributeName();
183                             if (!(attValPairName instanceof QName)) continue;
184                             nameSpace = ((QName)attValPairName).getNameSpace();
185                             localName = ((QName)attValPairName).getLocalName();
186                             if (compareQNames(nameSpace,localName,subNameSpace,subLocalName)) {
187                                 XQueryExpression attValPairValue = attValPair.getAttributeValue();
188                                 if (attValPairValue instanceof XQueryExpressionSequence) resultExprs.add((XQueryExpression)((XQueryExpressionSequence)attValPairValue).getSubExpressions().get(0));
189                                 else resultExprs.add(attValPairValue);
190                                 break;
191                             }
192                         }
193                      */

194             }
195         } else {
196             if (depth == 1 && axis == Axis.ATTRIBUTE) {
197                 if (attributes != null) {
198                     for (int i = 0; i < attributes.size(); i++) {
199                         AttributeValuePair attValPair = (AttributeValuePair) attributes.get(i);
200                         XQueryExpression attValPairName = attValPair.getAttributeName();
201                         if (!(attValPairName instanceof QName))
202                             continue;
203                         String JavaDoc nameSpace = ((QName) attValPairName).getNameSpace();
204                         String JavaDoc localName = ((QName) attValPairName).getLocalName();
205                         if (compareQNames(nameSpace, localName, subNameSpace, subLocalName)) {
206                             XQueryExpression attValPairValue = attValPair.getAttributeValue();
207                             if (attValPairValue instanceof XQueryExpressionSequence)
208                                 resultExprs.add((XQueryExpression) ((XQueryExpressionSequence) attValPairValue).getSubExpressions().get(0));
209                             else
210                                 resultExprs.add(attValPairValue);
211                             break;
212                         }
213                     }
214                 }
215             } else {
216                 if (depth == -1) {
217                     Set JavaDoc tmpResults = new HashSet JavaDoc();
218                     if (attributes != null) {
219                         for (int i = 0; i < attributes.size(); i++) {
220                             AttributeValuePair attValPair = (AttributeValuePair) attributes.get(i);
221                             depth = 0;
222                             attValPair.accept(this);
223                             if (resultExprs != null)
224                                 tmpResults.addAll(resultExprs);
225                             depth = -1;
226                         }
227                     }
228                     if (subExpressions != null) {
229                         for (int i = 0; i < subExpressions.size(); i++) {
230                             XQueryExpression subExpr = (XQueryExpression) subExpressions.get(i);
231                             depth = 0;
232                             subExpr.accept(this);
233                             if (resultExprs != null)
234                                 tmpResults.addAll(resultExprs);
235                             depth = -1;
236                             subExpr.accept(this);
237                             if (resultExprs != null)
238                                 tmpResults.addAll(resultExprs);
239                         }
240                     }
241                     resultExprs = new ArrayList JavaDoc(tmpResults);
242                 } else {
243                     if (subExpressions != null) {
244                         ArrayList JavaDoc tmpResults = new ArrayList JavaDoc();
245                         for (int i = 0; i < subExpressions.size(); i++) {
246                             XQueryExpression subExpr = (XQueryExpression) subExpressions.get(i);
247
248                             depth--;
249                             resultExprs.clear();
250                             subExpr.accept(this);
251                             if (resultExprs != null)
252                                 tmpResults.addAll(resultExprs);
253                             depth++;
254                         }
255                         resultExprs = new ArrayList JavaDoc(tmpResults);
256                     }
257                 }
258             }
259         }
260
261     }
262
263     // utility
264
boolean compareQNames(String JavaDoc nameSpace1, String JavaDoc localName1, String JavaDoc nameSpace2, String JavaDoc localName2) {
265         if ((((nameSpace1 == null || nameSpace1.equals("")) && (nameSpace2 == null || nameSpace2.equals(""))) || (nameSpace1 != null && nameSpace1.equals("*")) || (nameSpace2 != null) && (nameSpace2.equals("*") || nameSpace2.equals(nameSpace1))) && localName1 != null && localName2 != null && localName1.equals(localName2))
266             return true;
267         return false;
268     }
269
270     /*
271     public void visit(FLWRExpression arg) throws XQueryException
272     public void visit(FunctionCall arg) throws XQueryException;
273     public void visit(FunctionDefinition arg) throws XQueryException;
274     public void visit(InstanceOfExpression arg) throws XQueryException;
275     public void visit(InternalFunctionCall arg) throws XQueryException;
276     public void visit(ITEExpression arg) throws XQueryException;
277     public void visit(LibraryFunctionCall arg) throws XQueryException;
278     public void visit(ListOpAFTERExpression arg) throws XQueryException;
279     public void visit(ListOpArithExpression arg) throws XQueryException;
280     public void visit(ListOpBEFOREExpression arg) throws XQueryException;
281     public void visit(ListOpCompExpression arg) throws XQueryException;
282     public void visit(ListOpEXCEPTExpression arg) throws XQueryException;
283     public void visit(ListOpINTERSECTExpression arg) throws XQueryException;
284     public void visit(ListOpUNIONExpression arg) throws XQueryException;
285      */

286
287     public void visit(LocatedExpression arg) throws XQueryException {
288         if (depth != -1)
289             resultExprs.clear();
290         // if (arg.getQType().getOccurence() != QType.OCC_1_1) return;
291
// get elements to work with and verify some stuff
292
QType qtype = arg.getQType();
293         if (!(qtype instanceof QTypeElement)) {
294             return;
295         }
296         QTypeElement qtypeelt = (QTypeElement) qtype;
297         XQueryExpression name = qtypeelt.getName();
298         if (!(name instanceof QName)) {
299             return;
300         }
301         QName qname = (QName) name;
302         Type type = qtypeelt.getType();
303         // special case .
304
if (subExpression == null) {
305             if (axis == Axis.SELF) {
306                 if (!type.isSimpleType()) {
307                     return;
308                 }
309                 resultExprs.add(arg);
310             }
311             return;
312         }
313         if (depth == 0) {
314             if (!type.isSimpleType() && !type.getName().equals(Type.ANY_TYPE)) {
315                 return;
316             }
317             if (!compareQNames(qname.getNameSpace(), qname.getLocalName(), subNameSpace, subLocalName)) {
318                 return;
319             }
320             resultExprs.add(arg);
321         } else if (depth == 1) {
322             if (type.isSimpleType()) {
323                 return;
324             }
325             if (axis == Axis.ATTRIBUTE) {
326                 AttributeDeclaration attDecl = type.getAttributeDeclaration(subNameSpace, subLocalName);
327                 if (attDecl != null) {
328                     ArrayList JavaDoc steps = (ArrayList JavaDoc) arg.getSteps().clone();
329                     steps.add(new Step(true, Axis.ATTRIBUTE, subExpression, null, arg.getParentModule()));
330                     resultExprs.add(new LocatedExpression(steps, false, arg.getParentModule()));
331                     return;
332                 }
333             } else {
334                 ElementDeclaration eltDecl = type.getElementDeclaration(subNameSpace, subLocalName);
335                 if (eltDecl != null) {
336                     ArrayList JavaDoc steps = (ArrayList JavaDoc) arg.getSteps().clone();
337                     steps.add(new Step(true, Axis.CHILD, subExpression, null, arg.getParentModule()));
338                     resultExprs.add(new LocatedExpression(steps, false, arg.getParentModule()));
339                     return;
340                 }
341             }
342
343         } else if (depth == -1) {
344         }
345     }
346
347     // public void visit(NodeType arg) throws XQueryException;
348
// public void visit(Parameter arg) throws XQueryException;
349
// public void visit(PrimitiveFunctionCall arg) throws XQueryException;
350
// public void visit(QName arg) throws XQueryException;
351
// public void visit(QuantifiedExpression arg) throws XQueryException;
352
// public void visit(RangeExpression arg) throws XQueryException;
353
// public void visit(RootNodeFunctionCall arg) throws XQueryException;
354
// public void visit(SortedExpression arg) throws XQueryException;
355
// public void visit(Step arg) throws XQueryException;
356
// public void visit(TypeSwitchExpression arg) throws XQueryException;
357
// public void visit(UnOpMinusExpression arg) throws XQueryException;
358
// public void visit(Value arg) throws XQueryException;
359
// public void visit(ValueBoolean arg) throws XQueryException;
360
// public void visit(ValueFloat arg) throws XQueryException;
361
// public void visit(ValueInteger arg) throws XQueryException;
362
// public void visit(ValueString arg) throws XQueryException;
363
// public void visit(ValueText arg) throws XQueryException;
364

365     public void visit(Variable arg) throws XQueryException {
366         resultExprs.clear();
367         // if (arg.getQType().getOccurence() != QType.OCC_1_1) return;
368
QType qtype = arg.getQType();
369         Type type = qtype.getType();
370         // special case .
371
if (subExpression == null) {
372             if (axis == Axis.SELF) {
373                 if (type != null && !type.isSimpleType()) {
374                     return;
375                 }
376                 resultExprs.add(arg);
377             }
378             return;
379         }
380         if (!(qtype instanceof QTypeElement || (qtype instanceof QTypeUnion && qtype.isElement()))) {
381             return;
382         }
383         if (qtype instanceof QTypeElement) {
384             QTypeElement qtypeelt = (QTypeElement) qtype;
385             XQueryExpression name = qtypeelt.getName();
386             if (!(name instanceof QName)) {
387                 return;
388             }
389             QName qname = (QName) name;
390             if (depth == 0) {
391                 if (/* qtype != null && qtype instanceof QTypeElement && qtype.getName() instanceof QName && */
392                     subExpression instanceof QName) {
393                     String JavaDoc nameSpace = ((QName) qtype.getName()).getNameSpace();
394                     String JavaDoc localName = ((QName) qtype.getName()).getLocalName();
395                     if (compareQNames(nameSpace, localName, subNameSpace, subLocalName)) {
396                         resultExprs.add(arg);
397                         return;
398                     }
399                 }
400             }
401             //else if (depth == 1) {
402
// this has been done for cases where variable contains several steps of the sort expression ....
403
if (depth != -1) {
404                 if (subExpression instanceof QName) {
405                     QType qt = Utils.getSubType(QTypeDocument.ANY_ORIGIN, qtype, new Step(false, axis, subExpression, null, null), this.typeVisitor.getSchemaManager(), false);
406                     if (qt != null) {
407                         ArrayList JavaDoc steps = new ArrayList JavaDoc();
408                         steps.add(new Step(false, Axis.NONE, arg, null, arg.getParentModule()));
409                         steps.add(new Step(true, axis, subExpression, null, arg.getParentModule()));
410                         resultExprs.add(new LocatedExpression(steps, false, arg.getParentModule()));
411                         return;
412                     }
413                 }
414             }
415             /*
416             else if (depth == -1) {
417             }
418             */

419         } else {
420             QTypeUnion qtypeunion = (QTypeUnion) qtype;
421             boolean add1 = true;
422             boolean add2 = true;
423             for (int i = 0; i < qtypeunion.getList().size() && (add1 || add2); i++) {
424                 QTypeElement qtypeelt = (QTypeElement) qtypeunion.getList().get(i);
425                 XQueryExpression name = qtypeelt.getName();
426                 if (!(name instanceof QName)) {
427                     add1 = false;
428                     add2 = false;
429                     break;
430                 }
431                 QName qname = (QName) name;
432                 if (add1 && depth == 0) {
433                     if (/* qtype != null && qtype instanceof QTypeElement && qtype.getName() instanceof QName && */
434                         subExpression instanceof QName) {
435                         String JavaDoc nameSpace = ((QName) qtypeelt.getName()).getNameSpace();
436                         String JavaDoc localName = ((QName) qtypeelt.getName()).getLocalName();
437                         if (!compareQNames(nameSpace, localName, subNameSpace, subLocalName))
438                             add1 = false;
439                     }
440                 }
441                 //else if (depth == 1) {
442
// this has been done for cases where variable contains several steps of the sort expression ....
443
if (!add1 && add2 && depth != -1) {
444                     if (subExpression instanceof QName) {
445                         Object JavaDoc obj = Utils.getSubType(QTypeDocument.ANY_ORIGIN, qtypeelt, new Step(false, axis, subExpression, null, null), this.typeVisitor.getSchemaManager(), false);
446                         if (obj == null)
447                             add2 = false;
448                     }
449                 }
450                 /*
451                 else if (depth == -1) {
452                 }
453                 */

454             }
455             if (add1 || add2) {
456                 if (add1 && depth == 0) {
457                     resultExprs.add(arg);
458                     return;
459                 }
460                 //else if (depth == 1) {
461
// this has benn done for cases wehere variable contains several steps of the sort expression ....
462
if (add2 && depth != -1) {
463                     ArrayList JavaDoc steps = new ArrayList JavaDoc();
464                     steps.add(new Step(false, Axis.NONE, arg, null, arg.getParentModule()));
465                     steps.add(new Step(true, axis, subExpression, null, arg.getParentModule()));
466                     resultExprs.add(new LocatedExpression(steps, false, arg.getParentModule()));
467                     return;
468                 }
469             }
470         }
471     }
472
473     // public void visit(XQueryBinaryOperatorExpression arg) throws XQueryException;
474
// public void visit(XQueryBooleanOperatorExpression arg) throws XQueryException;
475

476     public void visit(XQueryExpression arg) throws XQueryException {
477         resultExprs.clear();
478     }
479
480     public void visit(XQueryExpressionSequence arg) throws XQueryException {
481         resultExprs.clear();
482         // if (arg.getQType().getOccurence() != QType.OCC_1_1) return;
483
if (subExpression == null) {
484             return;
485         }
486         ArrayList JavaDoc subExpressions = arg.getSubExpressions();
487         for (int i = 0; i < subExpressions.size(); i++) {
488             resultExprs.clear();
489             XQueryExpression subExpr = (XQueryExpression) subExpressions.get(i);
490             subExpr.accept(this);
491             // if (depth != -1 && !resultExprs.isEmpty()) break;
492
}
493     }
494     /*
495     public void visit(XQueryFile arg) throws XQueryException;
496     public void visit(XQueryListBinaryOperatorExpression arg) throws XQueryException;
497     public void visit(XQueryListUnaryOperatorExpression arg) throws XQueryException;
498     public void visit(XQueryUnaryOperatorExpression arg) throws XQueryException {
499     public void visit(XQueryUnit arg) throws XQueryException;
500     public void visit(XQueryVoid arg) throws XQueryException;
501      */

502     // primitive functions
503
/*
504     public void visit(FunctionATTRIBUTE arg) throws XQueryException ;
505     public void visit(FunctionAVG arg) throws XQueryException ;
506     public void visit(FunctionCOLLECTION arg) throws XQueryException;
507     public void visit(FunctionCONCAT arg) throws XQueryException;
508     public void visit(FunctionCONTAINS arg) throws XQueryException;
509     public void visit(FunctionCOUNT arg) throws XQueryException;
510     public void visit(FunctionCURRENT_DATETIME arg) throws XQueryException;
511     public void visit(FunctionDATA arg) throws XQueryException;
512     public void visit(FunctionDATE arg) throws XQueryException;
513     public void visit(FunctionDATETIME arg) throws XQueryException;
514     public void visit(FunctionDISTINCT_VALUES arg) throws XQueryException;
515     public void visit(FunctionDOCUMENT arg) throws XQueryException;
516     public void visit(FunctionEMPTY arg) throws XQueryException;
517     public void visit(FunctionENDS_WITH arg) throws XQueryException;
518     public void visit(FunctionFILTER arg) throws XQueryException;
519     public void visit(FunctionGET_GMONTH_FROM_DATE arg) throws XQueryException;
520     public void visit(FunctionGET_GYEAR_FROM_DATE arg) throws XQueryException;
521     public void visit(FunctionLOWER_CASE arg) throws XQueryException;
522     public void visit(FunctionMATCH arg) throws XQueryException;
523     public void visit(FunctionMAX arg) throws XQueryException;
524     public void visit(FunctionMIN arg) throws XQueryException;
525     public void visit(FunctionNAME arg) throws XQueryException;
526     public void visit(FunctionNOT arg) throws XQueryException;
527     public void visit(FunctionNUMBER arg) throws XQueryException;
528     public void visit(FunctionPART arg) throws XQueryException;
529     public void visit(FunctionPOSITION arg) throws XQueryException;
530     public void visit(FunctionSHALLOW arg) throws XQueryException;
531     public void visit(FunctionSTARTS_WITH arg) throws XQueryException;
532     public void visit(FunctionSTRING arg) throws XQueryException;
533     public void visit(FunctionSTRING_LENGTH arg) throws XQueryException;
534     public void visit(FunctionSTRING_VALUE arg) throws XQueryException;
535     public void visit(FunctionSUBSTRING arg) throws XQueryException;
536     public void visit(FunctionSUM arg) throws XQueryException;
537     // public void visit(FunctionTEXT arg) throws XQueryException;
538     public void visit(FunctionTIME arg) throws XQueryException;
539     public void visit(FunctionUPPER_CASE arg) throws XQueryException;
540      */

541 }
542
Popular Tags