KickJava   Java API By Example, From Geeks To Geeks.

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


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: WalkingIteratorSorted.java,v 1.11 2004/02/17 04:32:08 minchau Exp $
18  */

19 package org.apache.xpath.axes;
20
21 import org.apache.xml.dtm.Axis;
22 import org.apache.xml.utils.PrefixResolver;
23 import org.apache.xpath.compiler.Compiler;
24
25 /**
26  * This class iterates over set of nodes that needs to be sorted.
27  * @xsl.usage internal
28  */

29 public class WalkingIteratorSorted extends WalkingIterator
30 {
31
32 // /** True if the nodes will be found in document order */
33
// protected boolean m_inNaturalOrder = false;
34

35   /** True if the nodes will be found in document order, and this can
36    * be determined statically. */

37   protected boolean m_inNaturalOrderStatic = false;
38
39   /**
40    * Create a WalkingIteratorSorted object.
41    *
42    * @param nscontext The namespace context for this iterator,
43    * should be OK if null.
44    */

45   public WalkingIteratorSorted(PrefixResolver nscontext)
46   {
47     super(nscontext);
48   }
49
50   /**
51    * Create a WalkingIterator iterator, including creation
52    * of step walkers from the opcode list, and call back
53    * into the Compiler to create predicate expressions.
54    *
55    * @param compiler The Compiler which is creating
56    * this expression.
57    * @param opPos The position of this iterator in the
58    * opcode list from the compiler.
59    * @param shouldLoadWalkers True if walkers should be
60    * loaded, or false if this is a derived iterator and
61    * it doesn't wish to load child walkers.
62    *
63    * @throws javax.xml.transform.TransformerException
64    */

65   WalkingIteratorSorted(
66           Compiler JavaDoc compiler, int opPos, int analysis, boolean shouldLoadWalkers)
67             throws javax.xml.transform.TransformerException JavaDoc
68   {
69     super(compiler, opPos, analysis, shouldLoadWalkers);
70   }
71   
72   /**
73    * Returns true if all the nodes in the iteration well be returned in document
74    * order.
75    *
76    * @return true as a default.
77    */

78   public boolean isDocOrdered()
79   {
80     return m_inNaturalOrderStatic;
81   }
82
83     
84   /**
85    * Tell if the nodeset can be walked in doc order, via static analysis.
86    *
87    *
88    * @return true if the nodeset can be walked in doc order, without sorting.
89    */

90   boolean canBeWalkedInNaturalDocOrderStatic()
91   {
92
93     if (null != m_firstWalker)
94     {
95       AxesWalker walker = m_firstWalker;
96       int prevAxis = -1;
97       boolean prevIsSimpleDownAxis = true;
98
99       for(int i = 0; null != walker; i++)
100       {
101         int axis = walker.getAxis();
102         
103         if(walker.isDocOrdered())
104         {
105           boolean isSimpleDownAxis = ((axis == Axis.CHILD)
106                                    || (axis == Axis.SELF)
107                                    || (axis == Axis.ROOT));
108           // Catching the filtered list here is only OK because
109
// FilterExprWalker#isDocOrdered() did the right thing.
110
if(isSimpleDownAxis || (axis == -1))
111             walker = walker.getNextWalker();
112           else
113           {
114             boolean isLastWalker = (null == walker.getNextWalker());
115             if(isLastWalker)
116             {
117               if(walker.isDocOrdered() && (axis == Axis.DESCENDANT ||
118                  axis == Axis.DESCENDANTORSELF || axis == Axis.DESCENDANTSFROMROOT
119                  || axis == Axis.DESCENDANTSORSELFFROMROOT) || (axis == Axis.ATTRIBUTE))
120                 return true;
121             }
122             return false;
123           }
124         }
125         else
126           return false;
127       }
128       return true;
129     }
130     return false;
131   }
132
133
134 // /**
135
// * NEEDSDOC Method canBeWalkedInNaturalDocOrder
136
// *
137
// *
138
// * NEEDSDOC (canBeWalkedInNaturalDocOrder) @return
139
// */
140
// boolean canBeWalkedInNaturalDocOrder()
141
// {
142
//
143
// if (null != m_firstWalker)
144
// {
145
// AxesWalker walker = m_firstWalker;
146
// int prevAxis = -1;
147
// boolean prevIsSimpleDownAxis = true;
148
//
149
// for(int i = 0; null != walker; i++)
150
// {
151
// int axis = walker.getAxis();
152
//
153
// if(walker.isDocOrdered())
154
// {
155
// boolean isSimpleDownAxis = ((axis == Axis.CHILD)
156
// || (axis == Axis.SELF)
157
// || (axis == Axis.ROOT));
158
// // Catching the filtered list here is only OK because
159
// // FilterExprWalker#isDocOrdered() did the right thing.
160
// if(isSimpleDownAxis || (axis == -1))
161
// walker = walker.getNextWalker();
162
// else
163
// {
164
// boolean isLastWalker = (null == walker.getNextWalker());
165
// if(isLastWalker)
166
// {
167
// if(walker.isDocOrdered() && (axis == Axis.DESCENDANT ||
168
// axis == Axis.DESCENDANTORSELF || axis == Axis.DESCENDANTSFROMROOT
169
// || axis == Axis.DESCENDANTSORSELFFROMROOT) || (axis == Axis.ATTRIBUTE))
170
// return true;
171
// }
172
// return false;
173
// }
174
// }
175
// else
176
// return false;
177
// }
178
// return true;
179
// }
180
// return false;
181
// }
182

183   /**
184    * This function is used to perform some extra analysis of the iterator.
185    *
186    * @param vars List of QNames that correspond to variables. This list
187    * should be searched backwards for the first qualified name that
188    * corresponds to the variable reference qname. The position of the
189    * QName in the vector from the start of the vector will be its position
190    * in the stack frame (but variables above the globalsTop value will need
191    * to be offset to the current stack frame).
192    */

193   public void fixupVariables(java.util.Vector JavaDoc vars, int globalsSize)
194   {
195     super.fixupVariables(vars, globalsSize);
196
197     int analysis = getAnalysisBits();
198     if(WalkerFactory.isNaturalDocOrder(analysis))
199     {
200         m_inNaturalOrderStatic = true;
201     }
202     else
203     {
204         m_inNaturalOrderStatic = false;
205         // System.out.println("Setting natural doc order to false: "+
206
// WalkerFactory.getAnalysisString(analysis));
207
}
208     
209   }
210
211 }
212
Popular Tags