KickJava   Java API By Example, From Geeks To Geeks.

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


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.Node JavaDoc;
35
36 import java.util.logging.Level JavaDoc;
37
38 /**
39  * Selects namespace nodes.
40  */

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

60   public NamespaceIterator(Node JavaDoc node,
61                            NodeIterator parentIter,
62                            ExprEnvironment env,
63                            AbstractPattern match)
64     throws XPathException
65   {
66     super(env);
67     
68     _parentIter = parentIter;
69     _match = match;
70
71     if (parentIter == null)
72       _node = NamespaceNode.create(node);
73     
74     _node = findFirstMatchingNode();
75     _next = _node;
76   }
77   
78   /**
79    * True if there's more data.
80    */

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

97   public Node JavaDoc nextNode()
98     throws XPathException
99   {
100     if (_next != null) {
101       _node = _next;
102       _next = null;
103       
104       return _node;
105     }
106
107     if (_node != null) {
108       _node = (NamespaceNode) _node.getNextSibling();
109       _node = findFirstMatchingNode();
110     }
111     
112     _next = null;
113
114     return _node;
115   }
116   
117   /**
118    * Returns the next selected node.
119    */

120   public SelectedNode nextSelectedNode()
121     throws XPathException
122   {
123     Node JavaDoc node = nextNode();
124
125     return node == null ? null : new SelectedAttribute(node);
126   }
127
128   /**
129    * Finds the next matching node.
130    */

131   private NamespaceNode findFirstMatchingNode()
132     throws XPathException
133   {
134     while (true) {
135       Node JavaDoc parentNode;
136
137       if (_node != null) {
138         if (_match == null || _match.match(_node, _env)) {
139           _position++;
140           return _node;
141         }
142         else {
143           _node = (NamespaceNode) _node.getNextSibling();
144           continue;
145         }
146       }
147       
148       else if (_parentIter == null ||
149                (parentNode = _parentIter.nextNode()) == null)
150         return null;
151       else {
152         _position = 0;
153         _size = 0;
154         _node = NamespaceNode.create(parentNode);
155       }
156     }
157   }
158   
159   /**
160    * Returns the number of nodes in the context list.
161    */

162   public int getContextSize()
163   {
164     return _position;
165   }
166
167   public Object JavaDoc clone()
168   {
169     return null;
170   }
171   
172   public String JavaDoc toString()
173   {
174     return "NamespaceIterator[" + _match + "]";
175   }
176 }
177
Popular Tags