Source code: com/puppycrawl/tools/checkstyle/checks/xpath/AttributeAxisIterator.java
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.lang.reflect.InvocationTargetException;
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.Iterator;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Set;
28
29 import org.apache.commons.beanutils.BeanUtils;
30
31 import com.puppycrawl.tools.checkstyle.api.DetailAST;
32
33 /**
34 * Iterator for an attribute axis of an XPath element. The XPath
35 * element is a DetailAST. Attributes correspond to bean
36 * properties of the DetailAST.
37 * @author Rick Giles
38 */
39 public class AttributeAxisIterator
40 implements Iterator
41 {
42 /** actual iterator */
43 private Iterator mIter = (new ArrayList()).iterator();
44
45 /**
46 * Constructs a <code>AttributeAxisIterator</code> for a
47 * DetailAST element.
48 * @param aAST the DetailAST element
49 */
50 public AttributeAxisIterator(DetailAST aAST)
51 {
52 Map props = new HashMap();
53 //use BeanUtils to get the properties
54 try {
55 props = BeanUtils.describe(aAST);
56 }
57 catch (IllegalAccessException e) {
58 // TODO Auto-generated catch block
59 e.printStackTrace();
60 }
61 catch (InvocationTargetException e) {
62 // TODO Auto-generated catch block
63 e.printStackTrace();
64 }
65 catch (NoSuchMethodException e) {
66 // TODO Auto-generated catch block
67 e.printStackTrace();
68 }
69 // add attributes for the properties to a list and
70 // set the iterator to an iterator over that list.
71 final List attributes = new ArrayList(props.size());
72 final Set values = props.keySet();
73 for (Iterator iter = values.iterator(); iter.hasNext();) {
74 final String name = (String) iter.next();
75 final String value = (String) props.get(name);
76 attributes.add(new Attribute(aAST, name, value));
77 }
78 mIter = attributes.iterator();
79 }
80
81 /**@see java.util.Iterator#next() */
82 public Object next()
83 {
84 return mIter.next();
85 }
86
87 /** @see java.util.Iterator#hasNext() */
88 public boolean hasNext()
89 {
90 return mIter.hasNext();
91 }
92
93 /** @see java.util.Iterator#remove() */
94 public void remove()
95 {
96 mIter.remove();
97 }
98 }