KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
26
27 import org.xquark.schema.Type;
28 import org.xquark.xpath.*;
29 import org.xquark.xquery.typing.TypeException;
30
31 public class Step extends XQueryUnaryOperatorExpression implements StepExpr {
32     private static final String JavaDoc RCSRevision = "$Revision: 1.11 $";
33     private static final String JavaDoc RCSName = "$Name: $";
34
35     public static final int NONE = 0;
36     
37     public static final int SELF_QNAME = 0x10;
38     public static final int CHILD_QNAME = 0x20;
39     public static final int PARENT_QNAME = 0x30;
40     public static final int ANCESTOR_QNAME = 0x40;
41     public static final int ANCESTOR_OR_SELF_QNAME = 0x50;
42     public static final int DESCENDANT_QNAME = 0x60;
43     public static final int DESCENDANT_OR_SELF_QNAME = 0x70;
44     public static final int PRECEDING_QNAME = 0x80;
45     public static final int PRECEDING_SIBLING_QNAME = 0x90;
46     public static final int FOLLOWING_QNAME = 0xa0;
47     public static final int FOLLOWING_SIBLING_QNAME = 0xb0;
48     
49     // attention order is important ... see ReconstructionVisitor
50

51     public static final int SELF_NODE = 0x11;
52     public static final int CHILD_NODE = 0x21;
53     public static final int PARENT_NODE = 0x31;
54     public static final int ANCESTOR_NODE = 0x41;
55     public static final int ANCESTOR_OR_SELF_NODE = 0x51;
56     public static final int DESCENDANT_NODE = 0x61;
57     public static final int DESCENDANT_OR_SELF_NODE = 0x71;
58     public static final int PRECEDING_NODE = 0x81;
59     public static final int PRECEDING_SIBLING_NODE = 0x91;
60     public static final int FOLLOWING_NODE = 0xa1;
61     public static final int FOLLOWING_SIBLING_NODE = 0xb1;
62
63     public static final int SELF_DOCUMENT = 0x12;
64     public static final int CHILD_DOCUMENT = 0x22;
65     public static final int PARENT_DOCUMENT = 0x32;
66     public static final int ANCESTOR_DOCUMENT = 0x42;
67     public static final int ANCESTOR_OR_SELF_DOCUMENT = 0x52;
68     public static final int DESCENDANT_DOCUMENT = 0x62;
69     public static final int DESCENDANT_OR_SELF_DOCUMENT = 0x72;
70     public static final int PRECEDING_DOCUMENT = 0x82;
71     public static final int PRECEDING_SIBLING_DOCUMENT = 0x92;
72     public static final int FOLLOWING_DOCUMENT = 0xa2;
73     public static final int FOLLOWING_SIBLING_DOCUMENT = 0xb2;
74     
75     public static final int SELF_ATTRIBUTE = 0x13;
76     public static final int CHILD_ATTRIBUTE = 0x23;
77     public static final int PARENT_ATTRIBUTE = 0x33;
78     public static final int ANCESTOR_ATTRIBUTE = 0x43;
79     public static final int ANCESTOR_OR_SELF_ATTRIBUTE = 0x53;
80     public static final int DESCENDANT_ATTRIBUTE = 0x63;
81     public static final int DESCENDANT_OR_SELF_ATTRIBUTE = 0x73;
82     public static final int PRECEDING_ATTRIBUTE = 0x83;
83     public static final int PRECEDING_SIBLING_ATTRIBUTE = 0x93;
84     public static final int FOLLOWING_ATTRIBUTE = 0xa3;
85     public static final int FOLLOWING_SIBLING_ATTRIBUTE = 0xb3;
86
87     public static final int SELF_PI = 0x14;
88     public static final int CHILD_PI = 0x24;
89     public static final int PARENT_PI = 0x34;
90     public static final int ANCESTOR_PI = 0x44;
91     public static final int ANCESTOR_OR_SELF_PI = 0x54;
92     public static final int DESCENDANT_PI = 0x64;
93     public static final int DESCENDANT_OR_SELF_PI = 0x74;
94     public static final int PRECEDING_PI = 0x84;
95     public static final int PRECEDING_SIBLING_PI = 0x94;
96     public static final int FOLLOWING_PI = 0xa4;
97     public static final int FOLLOWING_SIBLING_PI = 0xb4;
98
99     public static final int SELF_COMMENT = 0x15;
100     public static final int CHILD_COMMENT = 0x25;
101     public static final int PARENT_COMMENT = 0x35;
102     public static final int ANCESTOR_COMMENT = 0x45;
103     public static final int ANCESTOR_OR_SELF_COMMENT = 0x55;
104     public static final int DESCENDANT_COMMENT = 0x65;
105     public static final int DESCENDANT_OR_SELF_COMMENT = 0x75;
106     public static final int PRECEDING_COMMENT = 0x85;
107     public static final int PRECEDING_SIBLING_COMMENT = 0x95;
108     public static final int FOLLOWING_COMMENT = 0xa5;
109     public static final int FOLLOWING_SIBLING_COMMENT = 0xb5;
110
111     public static final int SELF_TEXT = 0x16;
112     public static final int CHILD_TEXT = 0x26;
113     public static final int PARENT_TEXT = 0x36;
114     public static final int ANCESTOR_TEXT = 0x46;
115     public static final int ANCESTOR_OR_SELF_TEXT = 0x56;
116     public static final int DESCENDANT_TEXT = 0x66;
117     public static final int DESCENDANT_OR_SELF_TEXT = 0x76;
118     public static final int PRECEDING_TEXT = 0x86;
119     public static final int PRECEDING_SIBLING_TEXT = 0x96;
120     public static final int FOLLOWING_TEXT = 0xa6;
121     public static final int FOLLOWING_SIBLING_TEXT = 0xb6;
122
123     public static final int SELF_NAMESPACE = 0x17;
124     public static final int CHILD_NAMESPACE = 0x27;
125     public static final int PARENT_NAMESPACE = 0x37;
126     public static final int ANCESTOR_NAMESPACE = 0x47;
127     public static final int ANCESTOR_OR_SELF_NAMESPACE = 0x57;
128     public static final int DESCENDANT_NAMESPACE = 0x67;
129     public static final int DESCENDANT_OR_SELF_NAMESPACE = 0x77;
130     public static final int PRECEDING_NAMESPACE = 0x87;
131     public static final int PRECEDING_SIBLING_NAMESPACE = 0x97;
132     public static final int FOLLOWING_NAMESPACE = 0xa7;
133     public static final int FOLLOWING_SIBLING_NAMESPACE = 0xb7;
134
135     public static final int SELF_ELEMENT = 0x18;
136     public static final int CHILD_ELEMENT = 0x28;
137     public static final int PARENT_ELEMENT = 0x38;
138     public static final int ANCESTOR_ELEMENT = 0x48;
139     public static final int ANCESTOR_OR_SELF_ELEMENT = 0x58;
140     public static final int DESCENDANT_ELEMENT = 0x68;
141     public static final int DESCENDANT_OR_SELF_ELEMENT = 0x78;
142     public static final int PRECEDING_ELEMENT = 0x88;
143     public static final int PRECEDING_SIBLING_ELEMENT = 0x98;
144     public static final int FOLLOWING_ELEMENT = 0xa8;
145     public static final int FOLLOWING_SIBLING_ELEMENT = 0xb8;
146
147     protected ArrayList JavaDoc predicates = null;
148     /**
149      *
150      */

151     private boolean hasSeparator = false;
152
153     /**
154      *
155      */

156     private byte axis = Axis.NONE;
157
158     /**
159      *
160      */

161     private String JavaDoc localName = null;
162
163     /**
164      *
165      */

166     private String JavaDoc nameSpace = null;
167
168     /**
169      *
170      */

171     private byte wild = StepExpr.NO_WILDCARD;
172
173     /**
174      *
175      */

176     private byte kindtest = NodeKind.NONE;
177
178     /**
179      *
180      */

181     private Type type = null;
182
183     /**
184      *
185      */

186     private boolean isTypeNillable = false;
187
188     /**
189      *
190      */

191     private int stepKind = NONE;
192
193     // #############################################################################
194
// VISITOR STUFF
195
// #############################################################################
196

197     public void accept(ParserVisitor visitor) throws XQueryException {
198         visitor.visit(this);
199     }
200
201     // #############################################################################
202
// CONSTRUCTOR STUFF
203
// #############################################################################
204

205     public Step(boolean hasSeparator, byte axis, XQueryExpression expression, ArrayList JavaDoc predicates, XQueryModule parentModule) throws TypeException, XQueryException {
206         super(expression);
207         setHasSeparator(hasSeparator);
208         setAxis(axis);
209         setPredicates(predicates);
210         setInterfaceProperties();
211         setParentModule(parentModule);
212 // if (parentModule != null && parentModule.getStaticContext().getTypeVisitor() != null)
213
// accept(parentModule.getStaticContext().getTypeVisitor());
214
}
215
216     // #############################################################################
217
// ATTRIBUTE ACCESS STUFF
218
// #############################################################################
219

220     private void setInterfaceProperties() {
221         // depending on expression
222
// add LARS 07/04/04
223
stepKind = NONE;
224         if (expression instanceof QName) {
225             QName qname = (QName) expression;
226             localName = qname.getLocalName();
227             nameSpace = qname.getNameSpace();
228             kindtest = NodeKind.ELEMENT;
229             byte nodeKind = NodeKind.ELEMENT;
230             switch (axis) {
231                 case Axis.ATTRIBUTE :
232                     nodeKind = NodeKind.ATTRIBUTE;
233                     break;
234                 case Axis.NAMESPACE :
235                     nodeKind = NodeKind.NAMESPACE;
236                     break;
237             }
238             xnode = new XNode(nameSpace, localName, nodeKind);
239             // set wild
240
if (NAME_WILDCARD.equals(qname.getName()))
241                 wild = StepExpr.WILDCARD;
242             else if (NAME_WILDCARD.equals(localName))
243                 wild = StepExpr.LOCALNAME_WILDCARD;
244             else if (NAME_WILDCARD.equals(nameSpace))
245                 wild = StepExpr.NAMESPACE_WILDCARD;
246             // add LARS 07/04/04
247
stepKind = axis*16;
248         } else if (expression instanceof NodeTest) {
249             // set kindtest
250
kindtest = ((NodeTest) expression).getKind();
251             xnode = new XNode(kindtest);
252             // add LARS 07/04/04
253
stepKind = axis*16+kindtest;
254         }
255     }
256
257     public void setParentModule(XQueryModule parentUnit) {
258         if (parentUnit == null)
259             return;
260         this.parentModule = parentUnit;
261         super.setParentModule(parentUnit);
262         if (predicates != null)
263             for (int i = 0; i < predicates.size(); i++)
264                  ((XQueryExpression) predicates.get(i)).setParentModule(parentUnit);
265     }
266
267     public boolean hasSeparator() {
268         return hasSeparator;
269     }
270     public void setHasSeparator(boolean hasSeparator) {
271         this.hasSeparator = hasSeparator;
272     }
273
274     public byte getAxis() {
275         return axis;
276     }
277     public void setAxis(byte axis) {
278         this.axis = axis;
279     }
280
281     public ArrayList JavaDoc getPredicates() {
282         return predicates;
283     }
284     public void setPredicates(ArrayList JavaDoc predicates) {
285         this.predicates = predicates;
286         if (predicates != null)
287             for (int i = 0; i < this.predicates.size(); i++) {
288                 XQueryExpression expr = (XQueryExpression) this.predicates.get(i);
289                 expr.setParentModule(parentModule);
290                 expr.setParentExpression(this);
291             }
292     }
293
294     public void addPredicates(ArrayList JavaDoc predicates) {
295         if (predicates == null || predicates.isEmpty())
296             return;
297         if (this.predicates == null || this.predicates.isEmpty()) {
298             setPredicates(predicates);
299             return;
300         }
301         this.predicates.addAll(predicates);
302         setPredicates(this.predicates);
303     }
304
305     public void addPredicate(XQueryExpression predicate) {
306         if (predicate == null)
307             return;
308         if (predicates == null)
309             predicates = new ArrayList JavaDoc(1);
310         predicates.add(predicate);
311         predicate.setParentModule(parentModule);
312         predicate.setParentExpression(this);
313     }
314
315     public void addPredicate(int index, XQueryExpression predicate) {
316         if (predicates == null)
317             predicates = new ArrayList JavaDoc(1);
318         predicates.add(index, predicate);
319         predicate.setParentModule(parentModule);
320         predicate.setParentExpression(this);
321     }
322
323     // public String getName() {
324
// return expression.getName();
325
// }
326

327     public String JavaDoc toHashCodeString() {
328         return toString();
329 // StringBuffer buf = new StringBuffer();
330
// if (axis != Axis.NONE)
331
// buf.append('/');
332
// buf.append(Axis.AXISSTRINGS[axis]);
333
// switch (kindtest) {
334
// case NodeKind.ATTRIBUTE :
335
//// buf.append(NodeKind.NODEKINDSTRINGS[kindtest]);
336
//// buf.append('(');
337
// if (isWildcard())
338
// buf.append('*');
339
// else if (localName != null) {
340
//// buf.append('@');
341
// if (nameSpace != null) {
342
// buf.append('{');
343
// buf.append(nameSpace);
344
// buf.append('}');
345
// }
346
// buf.append(localName);
347
//// if (type != null) {
348
//// buf.append(" ,");
349
//// buf.append(type.toString());
350
//// }
351
//// } else if (type != null) {
352
//// buf.append(type.toString());
353
// }
354
//// buf.append(')');
355
// break;
356
// case NodeKind.COMMENT :
357
// buf.append(NodeKind.NODEKINDSTRINGS[kindtest]);
358
// buf.append('(');
359
// buf.append(')');
360
// break;
361
// case NodeKind.DOCUMENT :
362
// buf.append(NodeKind.NODEKINDSTRINGS[kindtest]);
363
// buf.append('(');
364
// if (localName != null || type != null) {
365
//// buf.append(NodeKind.NODEKINDSTRINGS[NodeKind.ELEMENT]);
366
//// buf.append('(');
367
// if (isWildcard())
368
// buf.append('*');
369
// else if (localName != null) {
370
// if (nameSpace != null) {
371
// buf.append('{');
372
// buf.append(nameSpace);
373
// buf.append('}');
374
// }
375
// buf.append(localName);
376
//// if (type != null) {
377
//// buf.append(" ,");
378
//// buf.append(type.toString());
379
//// if (isTypeNillable) {
380
//// buf.append(" nillable");
381
//// }
382
//// }
383
//// } else if (type != null) {
384
//// buf.append(type.toString());
385
// }
386
//// buf.append(')');
387
// }
388
// buf.append(')');
389
// break;
390
// case NodeKind.ELEMENT :
391
//// buf.append(NodeKind.NODEKINDSTRINGS[kindtest]);
392
//// buf.append('(');
393
// if (isWildcard())
394
// buf.append('*');
395
// else if (localName != null) {
396
// if (nameSpace != null) {
397
// buf.append('{');
398
// buf.append(nameSpace);
399
// buf.append('}');
400
// }
401
// buf.append(localName);
402
//// if (type != null) {
403
//// buf.append(" ,");
404
//// buf.append(type.toString());
405
//// if (isTypeNillable) {
406
//// buf.append(" nillable");
407
//// }
408
//// }
409
//// } else if (type != null) {
410
//// buf.append(type.toString());
411
// }
412
//// buf.append(')');
413
// break;
414
// //case NodeKind.NAMESPACE :
415
// case NodeKind.NODE :
416
// buf.append(NodeKind.NODEKINDSTRINGS[kindtest]);
417
// buf.append('(');
418
// buf.append(')');
419
// break;
420
// case NodeKind.PI :
421
// buf.append(NodeKind.NODEKINDSTRINGS[kindtest]);
422
// buf.append('(');
423
// if (localName != null)
424
// buf.append(localName);
425
// buf.append(')');
426
// break;
427
// case NodeKind.TEXT :
428
// buf.append(NodeKind.NODEKINDSTRINGS[kindtest]);
429
// buf.append('(');
430
// buf.append(')');
431
// break;
432
// }
433
// return buf.toString();
434
}
435
436     // #############################################################################
437
// CLONE STUFF
438
// #############################################################################
439
public void addParentExpression(XQueryExpression parentExpression) {
440         addParentExpression(parentExpression);
441         expression.addParentExpression(parentExpression);
442         if (predicates != null)
443             for (int i = 0; i < predicates.size(); i++)
444                  ((XQueryExpression) predicates.get(i)).addParentExpression(parentExpression);
445     }
446
447     // #############################################################################
448
// RESTRUCTURATION STUFF
449
// #############################################################################
450

451     public boolean hasPredicates() {
452         if (predicates != null && !predicates.isEmpty())
453             return true;
454         return false;
455     }
456
457     public boolean hasPositionPredicates() {
458         if (predicates != null)
459             for (int i = 0; i < predicates.size(); i++) {
460                 XQueryExpression predExpr = (XQueryExpression) predicates.get(i);
461                 // test the type of the predicates
462
if (predExpr instanceof ValueInteger)
463                     return true;
464             }
465         return false;
466     }
467
468     public int getStepKind() {
469         return stepKind;
470     }
471     
472     // #############################################################################
473
// INTERFACE STUFF
474
// #############################################################################
475

476     /**
477      *
478      * @return the namespace if any null otherwise
479      */

480     public String JavaDoc getNameSpace() {
481         return nameSpace;
482     }
483
484     /**
485      *
486      * @return the localname if any null otherwise
487      */

488     public String JavaDoc getLocalName() {
489         return localName;
490     }
491
492     /**
493      *
494      * @return the constant representing the wilcard
495      */

496     public byte getWildCardType() {
497         return wild;
498     }
499
500     /**
501      *
502      * @return false if no wildcard is present
503      */

504     public boolean hasWildcard() {
505         return wild != StepExpr.NO_WILDCARD;
506     }
507
508     /**
509      *
510      * @return true if step if *
511      */

512     public boolean isWildcard() {
513         return wild == StepExpr.WILDCARD;
514     }
515
516     /**
517      *
518      * @return true if step is qname with * as namespace
519      */

520     public boolean isNameSpaceWildcard() {
521         return isWildcard() || wild == StepExpr.NAMESPACE_WILDCARD;
522     }
523
524     /**
525      *
526      * @return true if step is qname with * as localname
527      */

528     public boolean isLocalNameWildcard() {
529         return isWildcard() || wild == StepExpr.LOCALNAME_WILDCARD;
530     }
531
532     /**
533      *
534      * @return the constant representing the kind of the step
535      */

536     public byte getKindTest() {
537         return kindtest;
538     }
539
540     /**
541      *
542      * @return the (schema) type used in element or attribute test
543      */

544     public org.xquark.schema.Type getType() {
545         return type;
546     }
547
548     /**
549      *
550      * @return true if the type used in element test is nillable
551      */

552     public boolean isTypeNillable() {
553         return isTypeNillable;
554     }
555
556     // methods for path resolving
557

558     public static final String JavaDoc NAME_WILDCARD = "*";
559     public XNode xnode = null;
560
561     // public StepExprImpl(byte axis, XNode xnode) {
562
// this.axis = axis;
563
// this.xnode = xnode;
564
// }
565
//
566
// public StepExprImpl(XNode xnode) {
567
// this.axis = Axis.CHILD;
568
// this.xnode = xnode;
569
// }
570

571     public XNode getXNode() {
572         return xnode;
573     }
574
575     public boolean match(XNode node) {
576         if (node == null)
577             return false;
578         if (isTypeTest()) { // true if local name == null
579
return typeMatch(node);
580         } else // namespace matching
581
{
582             if (typeMatch(node) == false) {
583                 return false;
584             }
585             if (getNamespace() == null) {
586                 if (node.getNamespace() == null) {
587                     // namespace match
588
} else {
589                     return false;
590                 }
591             } else if (isNamespaceWildcard()) {
592                 // namespace match
593
} else if (node.getNamespace() == null) {
594                 return false;
595             } else if (getNamespace().equals(node.getNamespace())) {
596                 // namespace match
597
} else {
598                 return false;
599             }
600
601             // matching local name
602
if (this.getLocalName() == null) {
603                 if (node.getLocalName() == null) {
604                     // local name match
605
} else {
606                     return false;
607                 }
608             } else if (this.isLocalNameWildcard()) {
609                 // local name match
610
} else if (node.getLocalName() == null) {
611                 return false;
612             } else if (this.getLocalName().equals(node.getLocalName())) {
613                 // local name match
614
} else {
615                 return false;
616             }
617             return true;
618         }
619     }
620     public boolean isTypeTest() {
621         return xnode.getLocalName() == null;
622     }
623     private boolean typeMatch(XNode node) {
624         return ((getTypeTest() == node.getType()) || (getTypeTest() == NodeKind.NODE));
625     }
626     public byte getTypeTest() {
627         return xnode.getType();
628     }
629     public String JavaDoc getNamespace() {
630         return xnode.getNamespace();
631     }
632     public boolean isNamespaceWildcard() {
633         return isNameTest() && (xnode.getNamespace() != null) && xnode.getNamespace().equals(NAME_WILDCARD);
634     }
635     public boolean isNameTest() {
636         return xnode.getLocalName() != null;
637     }
638
639 }
640
Popular Tags