KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > xpath > XTreeIterator


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23
24 package org.xquark.xpath;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.NoSuchElementException JavaDoc;
29
30 /**
31  * Simple XTree depth-first iterator.
32  */

33 public class XTreeIterator implements Iterator JavaDoc
34 {
35 private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
36 private static final String JavaDoc RCSName = "$Name: $";
37     private ArrayList JavaDoc childIterators = new ArrayList JavaDoc();
38     private XTreeNode nextNode;
39     private Iterator JavaDoc it;
40     
41     /** Creates new XTreeIterator */
42     public XTreeIterator(XTree tree)
43     {
44         nextNode = tree.getRoot();
45     }
46     
47     /** Creates new XTreeIterator */
48     public XTreeIterator(XTreeNode iterationRoot)
49     {
50         nextNode = iterationRoot;
51     }
52     
53     /**
54      * Returns <tt>true</tt> if the iteration has more elements. (In other
55      * words, returns <tt>true</tt> if <tt>next</tt> would return an element
56      * rather than throwing an exception.)
57      *
58      * @return <tt>true</tt> if the iterator has more elements.
59      */

60     public boolean hasNext()
61     {
62         return nextNode != null;
63     }
64     
65     /**
66      * Returns the next element in the interation.
67      *
68      * @return the next element in the iteration.
69      * @exception NoSuchElementException iteration has no more elements.
70      */

71     public Object JavaDoc next()
72     {
73         if (!hasNext())
74             throw new NoSuchElementException JavaDoc();
75         // prepare return
76
XTreeNode wNode = nextNode;
77         nextNode = null;
78         
79         // prepare next
80

81         // explore
82
if ((wNode.getChildren() != null) && !wNode.getChildren().isEmpty())
83         {
84             childIterators.add(it);
85             it = wNode.getChildren().iterator();
86             nextNode = (XTreeNode)it.next();
87         }
88         else // going backwards
89
{
90             while (!childIterators.isEmpty() && (nextNode == null))
91             {
92                 if (it.hasNext())
93                     nextNode = (XTreeNode)it.next();
94                 else
95                     it = (Iterator JavaDoc)childIterators.remove(childIterators.size() - 1);
96             }
97         }
98         return wNode;
99     }
100     
101     /**
102      *
103      * Removes from the underlying collection the last element returned by the
104      * iterator (optional operation). This method can be called only once per
105      * call to <tt>next</tt>. The behavior of an iterator is unspecified if
106      * the underlying collection is modified while the iteration is in
107      * progress in any way other than by calling this method.
108      *
109      * @exception UnsupportedOperationException if the <tt>remove</tt>
110      * operation is not supported by this Iterator.
111      *
112      * @exception IllegalStateException if the <tt>next</tt> method has not
113      * yet been called, or the <tt>remove</tt> method has already
114      * been called after the last call to the <tt>next</tt>
115      * method.
116      */

117     public void remove()
118     {
119         throw new UnsupportedOperationException JavaDoc();
120     }
121     
122 }
123
Popular Tags