KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > checks > xpath > NodeIterator


1 ////////////////////////////////////////////////////////////////////////////////
2
// checkstyle: Checks Java source code for adherence to a set of rules.
3
// Copyright (C) 2001-2003 Oliver Burn
4
//
5
// This library 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 library 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 library; if not, write to the Free Software
17
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
////////////////////////////////////////////////////////////////////////////////
19
package com.puppycrawl.tools.checkstyle.checks.xpath;
20
21 import java.util.Iterator JavaDoc;
22 import java.util.NoSuchElementException JavaDoc;
23
24 import com.puppycrawl.tools.checkstyle.api.DetailAST;
25
26 /**
27  * Iterator for DetailAST nodes in a syntax tree.
28  * @author Rick Giles
29  */

30 public abstract class NodeIterator
31     implements Iterator JavaDoc
32 {
33
34     /** The DetailAST for this iterator */
35     private DetailAST mNode;
36
37     /**
38      * Constructs a <code>NodeIterator</code> for a DetailAST.
39      * @param aAST the DetailAST.
40      */

41     public NodeIterator(DetailAST aAST)
42     {
43         this.mNode = getFirstNode(aAST);
44     }
45
46     /** @see java.util.Iterator#hasNext() */
47     public boolean hasNext()
48     {
49         return mNode != null;
50     }
51
52     /** @see java.util.Iterator#next() */
53     public Object JavaDoc next()
54     {
55         if (mNode == null) {
56             throw new NoSuchElementException JavaDoc();
57         }
58         final DetailAST ret = mNode;
59         mNode = getNextNode(mNode);
60         return ret;
61     }
62
63     /** @see java.util.Iterator#remove() */
64     public void remove()
65     {
66         throw new UnsupportedOperationException JavaDoc();
67     }
68
69     /**
70      * Gets the first node of an iterator over a DetailAST.
71      * @param aAST the DetailAST.
72      * @return the first node of an iterator over aAST.
73      */

74     protected abstract DetailAST getFirstNode(DetailAST aAST);
75
76     /**
77      * Gets the next node for an iterator over a DetailAST.
78      * @param aAST the DetailAST.
79      * @return the next node of aAST.
80      */

81     protected abstract DetailAST getNextNode(DetailAST aAST);
82
83     /**
84      * Gets the previous sibling of a DetailAST.
85      * @param aAST the DetailAST.
86      * @return the previous sibling of aAST.
87      */

88     protected DetailAST getPreviousSibling(DetailAST aAST)
89     {
90         return aAST.getPreviousSibling();
91     }
92
93     /**
94      * Get the next sibling of a DetailAST.
95      * @param aAST the DetailAST.
96      * @return the next sibling of aAST.
97      */

98     protected DetailAST getNextSibling(DetailAST aAST)
99     {
100         return (DetailAST) aAST.getNextSibling();
101     }
102
103     /**
104      * Get the first child of a DetailAST.
105      * @param aAST the DetailAST.
106      * @return the first child of aAST.
107      */

108     protected DetailAST getFirstChild(DetailAST aAST)
109     {
110         return (DetailAST) aAST.getFirstChild();
111     }
112
113     /**
114      * Get the last child of a DetailAST.
115      * @param aAST the DetailAST.
116      * @return the last child of aAST.
117      */

118     protected DetailAST getLastChild(DetailAST aAST)
119     {
120         return aAST.getLastChild();
121     }
122 }
Popular Tags