KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > xpath > pattern > FilterIterator


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.xpath.pattern;
30
31 import com.caucho.xpath.Expr;
32 import com.caucho.xpath.ExprEnvironment;
33 import com.caucho.xpath.XPathException;
34
35 import org.w3c.dom.Node JavaDoc;
36
37 import java.util.logging.Level JavaDoc;
38
39 /**
40  * Uses the axis to select new nodes.
41  */

42 public class FilterIterator extends NodeIterator {
43   private NodeIterator _parentIter;
44   
45   private Expr _expr;
46   
47   private Node JavaDoc _node;
48   private Node JavaDoc _next;
49   
50   /**
51    * Creates the new AxisIterator.
52    *
53    * @param parentIter the parent iterator
54    * @param expr the filter expression
55    * @param env the xpath environment
56    * @param context the context node
57    */

58   public FilterIterator(NodeIterator parentIter, Expr expr,
59                         ExprEnvironment env, Node JavaDoc context)
60     throws XPathException
61   {
62     super(env);
63     
64     _parentIter = parentIter;
65     _expr = expr;
66     _env = env;
67     
68     _contextNode = context;
69
70     _node = findFirstMatchingNode(parentIter);
71     _position = 1;
72     _next = _node;
73   }
74   
75   /**
76    * True if there's more data.
77    */

78   public boolean hasNext()
79   {
80     if (_next == null) {
81       try {
82         _next = nextNode();
83       } catch (XPathException e) {
84         log.log(Level.FINE, e.toString(), e);
85       }
86     }
87     
88     return _next != null;
89   }
90   
91   /**
92    * Returns the next selected node.
93    */

94   public Node JavaDoc nextNode()
95     throws XPathException
96   {
97     if (_next != null) {
98       _node = _next;
99       _next = null;
100       
101       return _node;
102     }
103
104     if (_node != null) {
105       _node = findFirstMatchingNode(_parentIter);
106       _position++;
107     }
108     _next = null;
109
110     return _node;
111   }
112
113   /**
114    * Finds the next matching node.
115    */

116   private Node JavaDoc findFirstMatchingNode(NodeIterator parentIter)
117     throws XPathException
118   {
119     Node JavaDoc node = parentIter.nextNode();
120
121     while (true) {
122       if (node != null) {
123         if (_expr.isNumber()) {
124           double value = _expr.evalNumber(node, parentIter);
125           
126           if (value == parentIter.getContextPosition())
127             return node;
128         }
129         else if (_expr.isBoolean()) {
130       if (_expr.evalBoolean(node, parentIter))
131         return node;
132         }
133     else {
134       Object JavaDoc value = _expr.evalObject(node, parentIter);
135
136       if (value instanceof Number JavaDoc) {
137         if (Expr.toDouble(value) == parentIter.getContextPosition())
138           return node;
139       }
140       else if (Expr.toBoolean(value))
141         return node;
142     }
143       }
144
145       if (parentIter == null || (node = parentIter.nextNode()) == null) {
146         return null;
147       }
148       else if (parentIter.getContextPosition() == 1) {
149         _position = 0;
150         _size = 0;
151       }
152       _contextNode = node;
153       parentIter.setContextNode(node);
154     }
155   }
156
157   public Object JavaDoc clone()
158   {
159     FilterIterator clone = null;;
160
161     try {
162       if (_parentIter == null)
163         clone = new FilterIterator(null, _expr, _env, _contextNode);
164       else
165         clone = new FilterIterator((NodeIterator) _parentIter.clone(),
166                                    _expr, _env, _contextNode);
167
168       clone._env = _env;
169       clone._position = _position;
170       clone._node = _node;
171     } catch (Exception JavaDoc e) {
172       log.log(Level.FINE, e.toString(), e);
173     }
174
175     return clone;
176   }
177
178   public String JavaDoc toString()
179   {
180     return "FilterIterator[" + _expr + "]";
181   }
182 }
183
Popular Tags