1 |
| package net.sourceforge.pmd.dfa.report; |
2 |
| |
3 |
| import net.sourceforge.pmd.RuleViolation; |
4 |
| |
5 |
| import java.util.ArrayList; |
6 |
| import java.util.Iterator; |
7 |
| import java.util.List; |
8 |
| import java.util.StringTokenizer; |
9 |
| |
10 |
| public class ReportTree { |
11 |
| |
12 |
| private PackageNode rootNode = new PackageNode(""); |
13 |
| private AbstractReportNode level; |
14 |
| |
15 |
| private class TreeIterator implements Iterator { |
16 |
| |
17 |
| private AbstractReportNode iterNode = rootNode; |
18 |
| private boolean hasNextFlag; |
19 |
| |
20 |
0
| public void remove() {
|
21 |
0
| throw new UnsupportedOperationException();
|
22 |
| } |
23 |
| |
24 |
0
| public boolean hasNext() {
|
25 |
0
| this.hasNextFlag = true;
|
26 |
0
| return this.getNext() != null;
|
27 |
| } |
28 |
| |
29 |
0
| public Object next() {
|
30 |
| |
31 |
0
| if (!this.hasNextFlag) {
|
32 |
0
| this.getNext();
|
33 |
| } else { |
34 |
0
| this.hasNextFlag = false;
|
35 |
| } |
36 |
| |
37 |
0
| if (this.iterNode instanceof ViolationNode) {
|
38 |
0
| return ((ViolationNode) this.iterNode).getRuleViolation();
|
39 |
| } |
40 |
0
| return null;
|
41 |
| } |
42 |
| |
43 |
| |
44 |
| |
45 |
| |
46 |
| |
47 |
| |
48 |
| |
49 |
| |
50 |
| |
51 |
| |
52 |
| |
53 |
| |
54 |
| |
55 |
0
| private Object getNext() {
|
56 |
0
| AbstractReportNode node;
|
57 |
| |
58 |
0
| while (true) {
|
59 |
0
| if (this.iterNode.isLeaf()) {
|
60 |
| |
61 |
0
| while ((node = (this.iterNode).getNextSibling()) == null) {
|
62 |
| |
63 |
0
| node = this.iterNode.getParent();
|
64 |
0
| if (node == null) {
|
65 |
0
| return null;
|
66 |
| } else { |
67 |
0
| this.iterNode = node;
|
68 |
| } |
69 |
| } |
70 |
| |
71 |
0
| this.iterNode = node;
|
72 |
0
| if (this.iterNode.isLeaf()) {
|
73 |
0
| return this.iterNode;
|
74 |
| } else { |
75 |
0
| continue;
|
76 |
| } |
77 |
| } else { |
78 |
0
| this.iterNode = this.iterNode.getFirstChild();
|
79 |
0
| if (this.iterNode.isLeaf()) {
|
80 |
0
| return this.iterNode;
|
81 |
| } else { |
82 |
0
| continue;
|
83 |
| } |
84 |
| } |
85 |
| } |
86 |
| } |
87 |
| } |
88 |
| |
89 |
| |
90 |
0
| public Iterator iterator() {
|
91 |
0
| return new TreeIterator();
|
92 |
| } |
93 |
| |
94 |
0
| public int size() {
|
95 |
0
| int count = 0;
|
96 |
0
| for (Iterator i = iterator(); i.hasNext();) {
|
97 |
0
| i.next();
|
98 |
0
| count++;
|
99 |
| } |
100 |
0
| return count;
|
101 |
| } |
102 |
| |
103 |
0
| public AbstractReportNode getRootNode() {
|
104 |
0
| return rootNode;
|
105 |
| } |
106 |
| |
107 |
| |
108 |
| |
109 |
| |
110 |
| |
111 |
3233
| public void addRuleViolation(RuleViolation violation) {
|
112 |
3233
| String pack = violation.getPackageName();
|
113 |
3233
| String[] a = {};
|
114 |
3233
| if (pack == null) {
|
115 |
9
| a = new String[]{""};
|
116 |
3224
| } else if (pack.indexOf(".") != -1) {
|
117 |
| |
118 |
2
| try {
|
119 |
2
| if (String.class.getMethod("split", new Class[]{String.class}) != null) {
|
120 |
| |
121 |
2
| a = pack.split("\\.");
|
122 |
| } |
123 |
| } catch (NoSuchMethodException nsme) { |
124 |
| |
125 |
0
| StringTokenizer toker = new StringTokenizer(pack, ".");
|
126 |
0
| List parts = new ArrayList();
|
127 |
0
| while (toker.hasMoreTokens()) {
|
128 |
0
| parts.add(toker.nextToken());
|
129 |
| } |
130 |
0
| a = (String[])parts.toArray(new String[parts.size()]);
|
131 |
| } |
132 |
| } else { |
133 |
3222
| a = new String[]{pack};
|
134 |
| } |
135 |
| |
136 |
3233
| this.level = this.rootNode;
|
137 |
3233
| String plugedPackageName = "";
|
138 |
| |
139 |
3233
| for (int i = 0; i < a.length; i++) {
|
140 |
3235
| String packageName = a[i];
|
141 |
3235
| plugedPackageName += packageName + ".";
|
142 |
| |
143 |
3235
| if (!this.isStringInLevel(plugedPackageName)) {
|
144 |
428
| PackageNode node = new PackageNode(plugedPackageName);
|
145 |
428
| this.level.addFirst(node);
|
146 |
| |
147 |
428
| this.level = node;
|
148 |
| } |
149 |
| } |
150 |
| |
151 |
3233
| String cl = violation.getClassName();
|
152 |
| |
153 |
3233
| if (!this.isStringInLevel(cl)) {
|
154 |
438
| ClassNode node = new ClassNode(cl);
|
155 |
438
| this.level.addFirst(node);
|
156 |
| |
157 |
438
| this.level = node;
|
158 |
| } |
159 |
| |
160 |
| |
161 |
| |
162 |
| |
163 |
| |
164 |
3233
| ViolationNode tmp = new ViolationNode(violation);
|
165 |
3233
| if (!this.equalsNodeInLevel(this.level, tmp)) {
|
166 |
3228
| this.level.add(tmp);
|
167 |
| } |
168 |
| } |
169 |
| |
170 |
| |
171 |
| |
172 |
| |
173 |
3233
| private boolean equalsNodeInLevel(AbstractReportNode level, AbstractReportNode node) {
|
174 |
3233
| for (int i = 0; i < level.getChildCount(); i++) {
|
175 |
73324
| if ((level.getChildAt(i)).equalsNode(node)) {
|
176 |
5
| return true;
|
177 |
| } |
178 |
| } |
179 |
3228
| return false;
|
180 |
| } |
181 |
| |
182 |
| |
183 |
| |
184 |
| |
185 |
| |
186 |
| |
187 |
6468
| private boolean isStringInLevel(String str) {
|
188 |
| |
189 |
6468
| for (int i = 0; i < this.level.getChildCount(); i++) {
|
190 |
5614
| AbstractReportNode child = this.level.getChildAt(i);
|
191 |
5614
| String tmp = null;
|
192 |
| |
193 |
5614
| if (child instanceof PackageNode) {
|
194 |
2807
| tmp = ((PackageNode) child).getPackageName();
|
195 |
| } |
196 |
5614
| if (child instanceof ClassNode) {
|
197 |
2807
| tmp = ((ClassNode) child).getClassName();
|
198 |
| } |
199 |
| |
200 |
5614
| if (tmp == null) {
|
201 |
4
| return false;
|
202 |
| } |
203 |
| |
204 |
5610
| if (tmp.equals(str)) {
|
205 |
| |
206 |
5602
| this.level = child;
|
207 |
5602
| return true;
|
208 |
| } |
209 |
| } |
210 |
862
| return false;
|
211 |
| } |
212 |
| |
213 |
| } |