KickJava   Java API By Example, From Geeks To Geeks.

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


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.ExprEnvironment;
32 import com.caucho.xpath.XPathException;
33
34 import org.w3c.dom.Element JavaDoc;
35 import org.w3c.dom.NamedNodeMap JavaDoc;
36 import org.w3c.dom.Node JavaDoc;
37
38 /**
39  * Uses the axis to select new nodes.
40  */

41 public class AttributeListIterator extends NodeIterator {
42   protected NodeIterator _parentIter;
43
44   protected NamedNodeMap JavaDoc _attributeMap;
45   protected Node JavaDoc _node;
46   protected int _index;
47   protected AbstractPattern _match;
48   
49   protected AttributeListIterator(ExprEnvironment env)
50   {
51     super(env);
52   }
53   
54   /**
55    * Creates the new AxisIterator.
56    *
57    * @param parentIter the parent iterator
58    * @param env the variable environment
59    * @param match the node matching pattern
60    */

61   public AttributeListIterator(NodeIterator parentIter,
62                                ExprEnvironment env,
63                                AbstractPattern match)
64     throws XPathException
65   {
66     super(env);
67     
68     _parentIter = parentIter;
69     _match = match;
70
71     _node = findFirstMatchingNode();
72   }
73   
74   /**
75    * True if there's more data.
76    */

77   public boolean hasNext()
78   {
79     return _node != null;
80   }
81   
82   /**
83    * Returns the next selected node.
84    */

85   public Node JavaDoc nextNode()
86     throws XPathException
87   {
88     Node JavaDoc node = _node;
89
90     _node = findFirstMatchingNode();
91
92     return node;
93   }
94
95   /**
96    * Finds the next matching node.
97    */

98   private Node JavaDoc findFirstMatchingNode()
99     throws XPathException
100   {
101     Node JavaDoc node = null;
102     
103     while (true) {
104       Node JavaDoc parent;
105       
106       if (node != null && (_match == null || _match.match(node, _env))) {
107         _position++;
108         return node;
109       }
110           
111       if (_attributeMap != null && _index < _attributeMap.getLength())
112         node = _attributeMap.item(_index++);
113       else if (_parentIter == null ||
114            (parent = _parentIter.nextNode()) == null)
115         return null;
116       else if (parent instanceof Element JavaDoc) {
117         _position = 0;
118         _size = 0;
119         _index = 0;
120         _attributeMap = ((Element JavaDoc) parent).getAttributes();
121       }
122     }
123   }
124   
125   /**
126    * Returns the number of nodes in the context list.
127    */

128   public int getContextSize()
129   {
130     if (_attributeMap == null)
131       return 0;
132     else
133       return _attributeMap.getLength();
134   }
135
136
137   public Object JavaDoc clone()
138   {
139     AttributeListIterator iter = new AttributeListIterator(_env);
140
141     iter.copy(this);
142
143     if (_parentIter != null)
144       iter._parentIter = (NodeIterator) _parentIter.clone();
145     iter._node = _node;
146     iter._index = _index;
147     iter._attributeMap = _attributeMap;
148     iter._match = _match;
149
150     return iter;
151   }
152
153   public String JavaDoc toString()
154   {
155     return "AttributeListIterator[" + _match + "]";
156   }
157 }
158
Popular Tags