KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xpath > axes > HasPositionalPredChecker


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: HasPositionalPredChecker.java,v 1.4 2004/02/17 04:32:08 minchau Exp $
18  */

19 package org.apache.xpath.axes;
20
21 import org.apache.xpath.Expression;
22 import org.apache.xpath.ExpressionOwner;
23 import org.apache.xpath.XPathVisitor;
24 import org.apache.xpath.functions.FuncLast;
25 import org.apache.xpath.functions.FuncPosition;
26 import org.apache.xpath.functions.Function;
27 import org.apache.xpath.objects.XNumber;
28 import org.apache.xpath.operations.Div;
29 import org.apache.xpath.operations.Minus;
30 import org.apache.xpath.operations.Mod;
31 import org.apache.xpath.operations.Mult;
32 import org.apache.xpath.operations.Plus;
33 import org.apache.xpath.operations.Quo;
34 import org.apache.xpath.operations.Variable;
35
36 public class HasPositionalPredChecker extends XPathVisitor
37 {
38     private boolean m_hasPositionalPred = false;
39     private int m_predDepth = 0;
40     
41     /**
42      * Process the LocPathIterator to see if it contains variables
43      * or functions that may make it context dependent.
44      * @param path LocPathIterator that is assumed to be absolute, but needs checking.
45      * @return true if the path is confirmed to be absolute, false if it
46      * may contain context dependencies.
47      */

48     public static boolean check(LocPathIterator path)
49     {
50         HasPositionalPredChecker hppc = new HasPositionalPredChecker();
51         path.callVisitors(null, hppc);
52         return hppc.m_hasPositionalPred;
53     }
54     
55     /**
56      * Visit a function.
57      * @param owner The owner of the expression, to which the expression can
58      * be reset if rewriting takes place.
59      * @param func The function reference object.
60      * @return true if the sub expressions should be traversed.
61      */

62     public boolean visitFunction(ExpressionOwner owner, Function func)
63     {
64         if((func instanceof FuncPosition) ||
65            (func instanceof FuncLast))
66             m_hasPositionalPred = true;
67         return true;
68     }
69     
70 // /**
71
// * Visit a variable reference.
72
// * @param owner The owner of the expression, to which the expression can
73
// * be reset if rewriting takes place.
74
// * @param var The variable reference object.
75
// * @return true if the sub expressions should be traversed.
76
// */
77
// public boolean visitVariableRef(ExpressionOwner owner, Variable var)
78
// {
79
// m_hasPositionalPred = true;
80
// return true;
81
// }
82

83   /**
84    * Visit a predicate within a location path. Note that there isn't a
85    * proper unique component for predicates, and that the expression will
86    * be called also for whatever type Expression is.
87    *
88    * @param owner The owner of the expression, to which the expression can
89    * be reset if rewriting takes place.
90    * @param pred The predicate object.
91    * @return true if the sub expressions should be traversed.
92    */

93   public boolean visitPredicate(ExpressionOwner owner, Expression pred)
94   {
95     m_predDepth++;
96
97     if(m_predDepth == 1)
98     {
99       if((pred instanceof Variable) ||
100          (pred instanceof XNumber) ||
101          (pred instanceof Div) ||
102          (pred instanceof Plus) ||
103          (pred instanceof Minus) ||
104          (pred instanceof Mod) ||
105          (pred instanceof Quo) ||
106          (pred instanceof Mult) ||
107          (pred instanceof org.apache.xpath.operations.Number) ||
108          (pred instanceof Function))
109           m_hasPositionalPred = true;
110       else
111         pred.callVisitors(owner, this);
112     }
113
114     m_predDepth--;
115
116     // Don't go have the caller go any further down the subtree.
117
return false;
118   }
119
120
121 }
122
123
Popular Tags