KickJava   Java API By Example, From Geeks To Geeks.

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


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

19 package org.apache.xpath.axes;
20
21 import org.apache.xml.dtm.DTMIterator;
22 import org.apache.xpath.XPathContext;
23 import org.apache.xpath.objects.XObject;
24 import org.apache.xpath.patterns.NodeTest;
25
26 /**
27  * This class defines a simplified type of union iterator that only
28  * tests along the child axes. If the conditions are right, it is
29  * much faster than using a UnionPathIterator.
30  */

31 public class UnionChildIterator extends ChildTestIterator
32 {
33   /**
34    * Even though these may hold full LocPathIterators, this array does
35    * not have to be cloned, since only the node test and predicate
36    * portion are used, and these only need static information. However,
37    * also note that index predicates can not be used!
38    */

39   private PredicatedNodeTest[] m_nodeTests = null;
40
41   /**
42    * Constructor for UnionChildIterator
43    */

44   public UnionChildIterator()
45   {
46     super(null);
47   }
48
49   /**
50    * Add a node test to the union list.
51    *
52    * @param test reference to a NodeTest, which will be added
53    * directly to the list of node tests (in other words, it will
54    * not be cloned). The parent of this test will be set to
55    * this object.
56    */

57   public void addNodeTest(PredicatedNodeTest test)
58   {
59
60     // Increase array size by only 1 at a time. Fix this
61
// if it looks to be a problem.
62
if (null == m_nodeTests)
63     {
64       m_nodeTests = new PredicatedNodeTest[1];
65       m_nodeTests[0] = test;
66     }
67     else
68     {
69       PredicatedNodeTest[] tests = m_nodeTests;
70       int len = m_nodeTests.length;
71
72       m_nodeTests = new PredicatedNodeTest[len + 1];
73
74       System.arraycopy(tests, 0, m_nodeTests, 0, len);
75
76       m_nodeTests[len] = test;
77     }
78     test.exprSetParent(this);
79   }
80
81   /**
82    * This function is used to fixup variables from QNames to stack frame
83    * indexes at stylesheet build time.
84    * @param vars List of QNames that correspond to variables. This list
85    * should be searched backwards for the first qualified name that
86    * corresponds to the variable reference qname. The position of the
87    * QName in the vector from the start of the vector will be its position
88    * in the stack frame (but variables above the globalsTop value will need
89    * to be offset to the current stack frame).
90    */

91   public void fixupVariables(java.util.Vector JavaDoc vars, int globalsSize)
92   {
93     super.fixupVariables(vars, globalsSize);
94     if (m_nodeTests != null) {
95       for (int i = 0; i < m_nodeTests.length; i++) {
96         m_nodeTests[i].fixupVariables(vars, globalsSize);
97       }
98     }
99   }
100
101   /**
102    * Test whether a specified node is visible in the logical view of a
103    * TreeWalker or NodeIterator. This function will be called by the
104    * implementation of TreeWalker and NodeIterator; it is not intended to
105    * be called directly from user code.
106    * @param n The node to check to see if it passes the filter or not.
107    * @return a constant to determine whether the node is accepted,
108    * rejected, or skipped, as defined above .
109    */

110   public short acceptNode(int n)
111   {
112     XPathContext xctxt = getXPathContext();
113     try
114     {
115       xctxt.pushCurrentNode(n);
116       for (int i = 0; i < m_nodeTests.length; i++)
117       {
118         PredicatedNodeTest pnt = m_nodeTests[i];
119         XObject score = pnt.execute(xctxt, n);
120         if (score != NodeTest.SCORE_NONE)
121         {
122           // Note that we are assuming there are no positional predicates!
123
if (pnt.getPredicateCount() > 0)
124           {
125             if (pnt.executePredicates(n, xctxt))
126               return DTMIterator.FILTER_ACCEPT;
127           }
128           else
129             return DTMIterator.FILTER_ACCEPT;
130
131         }
132       }
133     }
134     catch (javax.xml.transform.TransformerException JavaDoc se)
135     {
136
137       // TODO: Fix this.
138
throw new RuntimeException JavaDoc(se.getMessage());
139     }
140     finally
141     {
142       xctxt.popCurrentNode();
143     }
144     return DTMIterator.FILTER_SKIP;
145   }
146
147 }
148
Popular Tags