6
|
1 |
/*
|
|
2 |
* reserved comment block
|
|
3 |
* DO NOT REMOVE OR ALTER!
|
|
4 |
*/
|
|
5 |
/*
|
|
6 |
* Copyright 1999-2004 The Apache Software Foundation.
|
|
7 |
*
|
|
8 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
9 |
* you may not use this file except in compliance with the License.
|
|
10 |
* You may obtain a copy of the License at
|
|
11 |
*
|
|
12 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
13 |
*
|
|
14 |
* Unless required by applicable law or agreed to in writing, software
|
|
15 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
16 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
17 |
* See the License for the specific language governing permissions and
|
|
18 |
* limitations under the License.
|
|
19 |
*/
|
|
20 |
/*
|
|
21 |
* $Id: DescendantIterator.java,v 1.2.4.2 2005/09/14 19:45:21 jeffsuttor Exp $
|
|
22 |
*/
|
|
23 |
package com.sun.org.apache.xpath.internal.axes;
|
|
24 |
|
|
25 |
import com.sun.org.apache.xml.internal.dtm.Axis;
|
|
26 |
import com.sun.org.apache.xml.internal.dtm.DTM;
|
|
27 |
import com.sun.org.apache.xml.internal.dtm.DTMAxisTraverser;
|
|
28 |
import com.sun.org.apache.xml.internal.dtm.DTMFilter;
|
|
29 |
import com.sun.org.apache.xml.internal.dtm.DTMIterator;
|
|
30 |
import com.sun.org.apache.xpath.internal.Expression;
|
|
31 |
import com.sun.org.apache.xpath.internal.XPathContext;
|
|
32 |
import com.sun.org.apache.xpath.internal.compiler.Compiler;
|
|
33 |
import com.sun.org.apache.xpath.internal.compiler.OpCodes;
|
12458
|
34 |
import com.sun.org.apache.xpath.internal.compiler.OpMap;
|
6
|
35 |
import com.sun.org.apache.xpath.internal.patterns.NodeTest;
|
12458
|
36 |
import org.w3c.dom.DOMException;
|
6
|
37 |
|
|
38 |
/**
|
|
39 |
* This class implements an optimized iterator for
|
|
40 |
* descendant, descendant-or-self, or "//foo" patterns.
|
|
41 |
* @see com.sun.org.apache.xpath.internal.axes.LocPathIterator
|
|
42 |
* @xsl.usage advanced
|
|
43 |
*/
|
|
44 |
public class DescendantIterator extends LocPathIterator
|
|
45 |
{
|
|
46 |
static final long serialVersionUID = -1190338607743976938L;
|
|
47 |
/**
|
|
48 |
* Create a DescendantIterator object.
|
|
49 |
*
|
|
50 |
* @param compiler A reference to the Compiler that contains the op map.
|
|
51 |
* @param opPos The position within the op map, which contains the
|
|
52 |
* location path expression for this itterator.
|
|
53 |
*
|
|
54 |
* @throws javax.xml.transform.TransformerException
|
|
55 |
*/
|
|
56 |
DescendantIterator(Compiler compiler, int opPos, int analysis)
|
|
57 |
throws javax.xml.transform.TransformerException
|
|
58 |
{
|
|
59 |
|
|
60 |
super(compiler, opPos, analysis, false);
|
|
61 |
|
12458
|
62 |
int firstStepPos = OpMap.getFirstChildPos(opPos);
|
6
|
63 |
int stepType = compiler.getOp(firstStepPos);
|
|
64 |
|
|
65 |
boolean orSelf = (OpCodes.FROM_DESCENDANTS_OR_SELF == stepType);
|
|
66 |
boolean fromRoot = false;
|
|
67 |
if (OpCodes.FROM_SELF == stepType)
|
|
68 |
{
|
|
69 |
orSelf = true;
|
|
70 |
// firstStepPos += 8;
|
|
71 |
}
|
|
72 |
else if(OpCodes.FROM_ROOT == stepType)
|
|
73 |
{
|
|
74 |
fromRoot = true;
|
|
75 |
// Ugly code... will go away when AST work is done.
|
|
76 |
int nextStepPos = compiler.getNextStepPos(firstStepPos);
|
|
77 |
if(compiler.getOp(nextStepPos) == OpCodes.FROM_DESCENDANTS_OR_SELF)
|
|
78 |
orSelf = true;
|
|
79 |
// firstStepPos += 8;
|
|
80 |
}
|
|
81 |
|
|
82 |
// Find the position of the last step.
|
|
83 |
int nextStepPos = firstStepPos;
|
|
84 |
while(true)
|
|
85 |
{
|
|
86 |
nextStepPos = compiler.getNextStepPos(nextStepPos);
|
|
87 |
if(nextStepPos > 0)
|
|
88 |
{
|
|
89 |
int stepOp = compiler.getOp(nextStepPos);
|
|
90 |
if(OpCodes.ENDOP != stepOp)
|
|
91 |
firstStepPos = nextStepPos;
|
|
92 |
else
|
|
93 |
break;
|
|
94 |
}
|
|
95 |
else
|
|
96 |
break;
|
|
97 |
|
|
98 |
}
|
|
99 |
|
|
100 |
// Fix for http://nagoya.apache.org/bugzilla/show_bug.cgi?id=1336
|
|
101 |
if((analysis & WalkerFactory.BIT_CHILD) != 0)
|
|
102 |
orSelf = false;
|
|
103 |
|
|
104 |
if(fromRoot)
|
|
105 |
{
|
|
106 |
if(orSelf)
|
|
107 |
m_axis = Axis.DESCENDANTSORSELFFROMROOT;
|
|
108 |
else
|
|
109 |
m_axis = Axis.DESCENDANTSFROMROOT;
|
|
110 |
}
|
|
111 |
else if(orSelf)
|
|
112 |
m_axis = Axis.DESCENDANTORSELF;
|
|
113 |
else
|
|
114 |
m_axis = Axis.DESCENDANT;
|
|
115 |
|
|
116 |
int whatToShow = compiler.getWhatToShow(firstStepPos);
|
|
117 |
|
|
118 |
if ((0 == (whatToShow
|
|
119 |
& (DTMFilter.SHOW_ATTRIBUTE | DTMFilter.SHOW_ELEMENT
|
|
120 |
| DTMFilter.SHOW_PROCESSING_INSTRUCTION))) ||
|
|
121 |
(whatToShow == DTMFilter.SHOW_ALL))
|
|
122 |
initNodeTest(whatToShow);
|
|
123 |
else
|
|
124 |
{
|
|
125 |
initNodeTest(whatToShow, compiler.getStepNS(firstStepPos),
|
|
126 |
compiler.getStepLocalName(firstStepPos));
|
|
127 |
}
|
|
128 |
initPredicateInfo(compiler, firstStepPos);
|
|
129 |
}
|
|
130 |
|
|
131 |
/**
|
|
132 |
* Create a DescendantIterator object.
|
|
133 |
*
|
|
134 |
*/
|
|
135 |
public DescendantIterator()
|
|
136 |
{
|
|
137 |
super(null);
|
|
138 |
m_axis = Axis.DESCENDANTSORSELFFROMROOT;
|
|
139 |
int whatToShow = DTMFilter.SHOW_ALL;
|
|
140 |
initNodeTest(whatToShow);
|
|
141 |
}
|
|
142 |
|
|
143 |
|
|
144 |
/**
|
|
145 |
* Get a cloned Iterator that is reset to the beginning
|
|
146 |
* of the query.
|
|
147 |
*
|
|
148 |
* @return A cloned NodeIterator set of the start of the query.
|
|
149 |
*
|
|
150 |
* @throws CloneNotSupportedException
|
|
151 |
*/
|
|
152 |
public DTMIterator cloneWithReset() throws CloneNotSupportedException
|
|
153 |
{
|
|
154 |
|
|
155 |
DescendantIterator clone = (DescendantIterator) super.cloneWithReset();
|
|
156 |
clone.m_traverser = m_traverser;
|
|
157 |
|
|
158 |
clone.resetProximityPositions();
|
|
159 |
|
|
160 |
return clone;
|
|
161 |
}
|
|
162 |
|
|
163 |
/**
|
|
164 |
* Returns the next node in the set and advances the position of the
|
|
165 |
* iterator in the set. After a NodeIterator is created, the first call
|
|
166 |
* to nextNode() returns the first node in the set.
|
|
167 |
*
|
|
168 |
* @return The next <code>Node</code> in the set being iterated over, or
|
|
169 |
* <code>null</code> if there are no more members in that set.
|
|
170 |
*
|
|
171 |
* @throws DOMException
|
|
172 |
* INVALID_STATE_ERR: Raised if this method is called after the
|
|
173 |
* <code>detach</code> method was invoked.
|
|
174 |
*/
|
|
175 |
public int nextNode()
|
|
176 |
{
|
|
177 |
if(m_foundLast)
|
|
178 |
return DTM.NULL;
|
|
179 |
|
|
180 |
if(DTM.NULL == m_lastFetched)
|
|
181 |
{
|
|
182 |
resetProximityPositions();
|
|
183 |
}
|
|
184 |
|
|
185 |
int next;
|
|
186 |
|
|
187 |
com.sun.org.apache.xpath.internal.VariableStack vars;
|
|
188 |
int savedStart;
|
|
189 |
if (-1 != m_stackFrame)
|
|
190 |
{
|
|
191 |
vars = m_execContext.getVarStack();
|
|
192 |
|
|
193 |
// These three statements need to be combined into one operation.
|
|
194 |
savedStart = vars.getStackFrame();
|
|
195 |
|
|
196 |
vars.setStackFrame(m_stackFrame);
|
|
197 |
}
|
|
198 |
else
|
|
199 |
{
|
|
200 |
// Yuck. Just to shut up the compiler!
|
|
201 |
vars = null;
|
|
202 |
savedStart = 0;
|
|
203 |
}
|
|
204 |
|
|
205 |
try
|
|
206 |
{
|
|
207 |
do
|
|
208 |
{
|
|
209 |
if(0 == m_extendedTypeID)
|
|
210 |
{
|
|
211 |
next = m_lastFetched = (DTM.NULL == m_lastFetched)
|
|
212 |
? m_traverser.first(m_context)
|
|
213 |
: m_traverser.next(m_context, m_lastFetched);
|
|
214 |
}
|
|
215 |
else
|
|
216 |
{
|
|
217 |
next = m_lastFetched = (DTM.NULL == m_lastFetched)
|
|
218 |
? m_traverser.first(m_context, m_extendedTypeID)
|
|
219 |
: m_traverser.next(m_context, m_lastFetched,
|
|
220 |
m_extendedTypeID);
|
|
221 |
}
|
|
222 |
|
|
223 |
if (DTM.NULL != next)
|
|
224 |
{
|
|
225 |
if(DTMIterator.FILTER_ACCEPT == acceptNode(next))
|
|
226 |
break;
|
|
227 |
else
|
|
228 |
continue;
|
|
229 |
}
|
|
230 |
else
|
|
231 |
break;
|
|
232 |
}
|
|
233 |
while (next != DTM.NULL);
|
|
234 |
|
|
235 |
if (DTM.NULL != next)
|
|
236 |
{
|
|
237 |
m_pos++;
|
|
238 |
return next;
|
|
239 |
}
|
|
240 |
else
|
|
241 |
{
|
|
242 |
m_foundLast = true;
|
|
243 |
|
|
244 |
return DTM.NULL;
|
|
245 |
}
|
|
246 |
}
|
|
247 |
finally
|
|
248 |
{
|
|
249 |
if (-1 != m_stackFrame)
|
|
250 |
{
|
|
251 |
// These two statements need to be combined into one operation.
|
|
252 |
vars.setStackFrame(savedStart);
|
|
253 |
}
|
|
254 |
}
|
|
255 |
}
|
|
256 |
|
|
257 |
/**
|
|
258 |
* Initialize the context values for this expression
|
|
259 |
* after it is cloned.
|
|
260 |
*
|
|
261 |
* @param context The XPath runtime context for this
|
|
262 |
* transformation.
|
|
263 |
*/
|
|
264 |
public void setRoot(int context, Object environment)
|
|
265 |
{
|
|
266 |
super.setRoot(context, environment);
|
|
267 |
m_traverser = m_cdtm.getAxisTraverser(m_axis);
|
|
268 |
|
|
269 |
String localName = getLocalName();
|
|
270 |
String namespace = getNamespace();
|
|
271 |
int what = m_whatToShow;
|
|
272 |
// System.out.println("what: ");
|
|
273 |
// NodeTest.debugWhatToShow(what);
|
|
274 |
if(DTMFilter.SHOW_ALL == what
|
|
275 |
|| NodeTest.WILD.equals(localName)
|
|
276 |
|| NodeTest.WILD.equals(namespace))
|
|
277 |
{
|
|
278 |
m_extendedTypeID = 0;
|
|
279 |
}
|
|
280 |
else
|
|
281 |
{
|
|
282 |
int type = getNodeTypeTest(what);
|
|
283 |
m_extendedTypeID = m_cdtm.getExpandedTypeID(namespace, localName, type);
|
|
284 |
}
|
|
285 |
|
|
286 |
}
|
|
287 |
|
|
288 |
/**
|
|
289 |
* Return the first node out of the nodeset, if this expression is
|
|
290 |
* a nodeset expression. This is the default implementation for
|
|
291 |
* nodesets.
|
|
292 |
* <p>WARNING: Do not mutate this class from this function!</p>
|
|
293 |
* @param xctxt The XPath runtime context.
|
|
294 |
* @return the first node out of the nodeset, or DTM.NULL.
|
|
295 |
*/
|
|
296 |
public int asNode(XPathContext xctxt)
|
|
297 |
throws javax.xml.transform.TransformerException
|
|
298 |
{
|
|
299 |
if(getPredicateCount() > 0)
|
|
300 |
return super.asNode(xctxt);
|
|
301 |
|
|
302 |
int current = xctxt.getCurrentNode();
|
|
303 |
|
|
304 |
DTM dtm = xctxt.getDTM(current);
|
|
305 |
DTMAxisTraverser traverser = dtm.getAxisTraverser(m_axis);
|
|
306 |
|
|
307 |
String localName = getLocalName();
|
|
308 |
String namespace = getNamespace();
|
|
309 |
int what = m_whatToShow;
|
|
310 |
|
|
311 |
// System.out.print(" (DescendantIterator) ");
|
|
312 |
|
|
313 |
// System.out.println("what: ");
|
|
314 |
// NodeTest.debugWhatToShow(what);
|
|
315 |
if(DTMFilter.SHOW_ALL == what
|
|
316 |
|| localName == NodeTest.WILD
|
|
317 |
|| namespace == NodeTest.WILD)
|
|
318 |
{
|
|
319 |
return traverser.first(current);
|
|
320 |
}
|
|
321 |
else
|
|
322 |
{
|
|
323 |
int type = getNodeTypeTest(what);
|
|
324 |
int extendedType = dtm.getExpandedTypeID(namespace, localName, type);
|
|
325 |
return traverser.first(current, extendedType);
|
|
326 |
}
|
|
327 |
}
|
|
328 |
|
|
329 |
/**
|
|
330 |
* Detaches the iterator from the set which it iterated over, releasing
|
|
331 |
* any computational resources and placing the iterator in the INVALID
|
|
332 |
* state. After<code>detach</code> has been invoked, calls to
|
|
333 |
* <code>nextNode</code> or<code>previousNode</code> will raise the
|
|
334 |
* exception INVALID_STATE_ERR.
|
|
335 |
*/
|
|
336 |
public void detach()
|
|
337 |
{
|
|
338 |
if (m_allowDetach) {
|
|
339 |
m_traverser = null;
|
|
340 |
m_extendedTypeID = 0;
|
|
341 |
|
|
342 |
// Always call the superclass detach last!
|
|
343 |
super.detach();
|
|
344 |
}
|
|
345 |
}
|
|
346 |
|
|
347 |
/**
|
|
348 |
* Returns the axis being iterated, if it is known.
|
|
349 |
*
|
|
350 |
* @return Axis.CHILD, etc., or -1 if the axis is not known or is of multiple
|
|
351 |
* types.
|
|
352 |
*/
|
|
353 |
public int getAxis()
|
|
354 |
{
|
|
355 |
return m_axis;
|
|
356 |
}
|
|
357 |
|
|
358 |
|
|
359 |
/** The traverser to use to navigate over the descendants. */
|
|
360 |
transient protected DTMAxisTraverser m_traverser;
|
|
361 |
|
|
362 |
/** The axis that we are traversing. */
|
|
363 |
protected int m_axis;
|
|
364 |
|
|
365 |
/** The extended type ID, not set until setRoot. */
|
|
366 |
protected int m_extendedTypeID;
|
|
367 |
|
|
368 |
/**
|
|
369 |
* @see Expression#deepEquals(Expression)
|
|
370 |
*/
|
|
371 |
public boolean deepEquals(Expression expr)
|
|
372 |
{
|
|
373 |
if(!super.deepEquals(expr))
|
|
374 |
return false;
|
|
375 |
|
|
376 |
if(m_axis != ((DescendantIterator)expr).m_axis)
|
|
377 |
return false;
|
|
378 |
|
|
379 |
return true;
|
|
380 |
}
|
|
381 |
|
|
382 |
|
|
383 |
}
|