1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
package org.qedeq.kernel.xml.tracker; |
17 |
|
|
18 |
|
import java.util.ArrayList; |
19 |
|
import java.util.List; |
20 |
|
import java.util.StringTokenizer; |
21 |
|
|
22 |
|
import org.qedeq.base.utility.EqualsUtility; |
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
@author |
29 |
|
|
|
|
| 72.6% |
Uncovered Elements: 62 (226) |
Complexity: 60 |
Complexity Density: 0.46 |
|
30 |
|
public class SimpleXPath { |
31 |
|
|
32 |
|
|
33 |
|
private final List elements; |
34 |
|
|
35 |
|
|
36 |
|
private final List numbers; |
37 |
|
|
38 |
|
|
39 |
|
private String attribute; |
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
@param |
58 |
|
|
59 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
60 |
96
|
public SimpleXPath(final String xpath) {... |
61 |
96
|
elements = new ArrayList(); |
62 |
96
|
numbers = new ArrayList(); |
63 |
96
|
attribute = null; |
64 |
96
|
init(xpath); |
65 |
|
} |
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
70 |
59
|
public SimpleXPath() {... |
71 |
59
|
elements = new ArrayList(); |
72 |
59
|
numbers = new ArrayList(); |
73 |
59
|
attribute = null; |
74 |
|
} |
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
@param |
80 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
81 |
113
|
public SimpleXPath(final SimpleXPath original) {... |
82 |
113
|
elements = new ArrayList(); |
83 |
113
|
numbers = new ArrayList(); |
84 |
113
|
attribute = null; |
85 |
113
|
init(original.toString()); |
86 |
|
} |
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
@see |
92 |
|
|
93 |
|
@param |
94 |
|
|
95 |
|
|
|
|
| 94% |
Uncovered Elements: 3 (50) |
Complexity: 13 |
Complexity Density: 0.43 |
|
96 |
209
|
private void init(final String xpath) {... |
97 |
209
|
if (xpath == null) { |
98 |
0
|
throw new NullPointerException(); |
99 |
|
} |
100 |
209
|
if (xpath.length() <= 0) { |
101 |
2
|
throw new RuntimeException("XPath must not be empty"); |
102 |
|
} |
103 |
207
|
if (xpath.charAt(0) != '/') { |
104 |
3
|
throw new RuntimeException("XPath must start with '/': " + xpath); |
105 |
|
} |
106 |
204
|
if (xpath.indexOf("//") >= 0) { |
107 |
1
|
throw new RuntimeException("empty tag not permitted: " + xpath); |
108 |
|
} |
109 |
203
|
if (xpath.endsWith("/")) { |
110 |
1
|
throw new RuntimeException("XPath must not end with '/': " + xpath); |
111 |
|
} |
112 |
202
|
final StringTokenizer tokenizer = new StringTokenizer(xpath, "/"); |
113 |
928
|
while (tokenizer.hasMoreTokens()) { |
114 |
728
|
String token = tokenizer.nextToken(); |
115 |
728
|
if (!tokenizer.hasMoreTokens() && token.indexOf('@') >= 0) { |
116 |
28
|
attribute = token.substring(token.indexOf('@') + 1); |
117 |
28
|
if (attribute.length() <= 0) { |
118 |
1
|
throw new RuntimeException("empty attribute not permitted: " + xpath); |
119 |
|
} |
120 |
27
|
token = token.substring(0, token.indexOf('@')); |
121 |
|
} |
122 |
727
|
if (token.indexOf('[') < 0) { |
123 |
681
|
elements.add(token); |
124 |
681
|
numbers.add(new Integer(1)); |
125 |
|
} else { |
126 |
46
|
final StringTokenizer getnu = new StringTokenizer(token, "[]"); |
127 |
46
|
elements.add(getnu.nextToken()); |
128 |
46
|
final Integer i; |
129 |
46
|
try { |
130 |
46
|
i = new Integer(getnu.nextToken()); |
131 |
|
} catch (RuntimeException e) { |
132 |
0
|
throw new RuntimeException("not an integer: " + xpath, e); |
133 |
|
} |
134 |
46
|
if (i.intValue() <= 0) { |
135 |
1
|
throw new RuntimeException("integer must be greater zero: " + xpath); |
136 |
|
} |
137 |
45
|
numbers.add(i); |
138 |
|
} |
139 |
|
} |
140 |
|
} |
141 |
|
|
142 |
|
|
143 |
|
|
144 |
|
|
145 |
|
@return |
146 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
147 |
8071
|
public final int size() {... |
148 |
8071
|
return elements.size(); |
149 |
|
} |
150 |
|
|
151 |
|
|
152 |
|
|
153 |
|
|
154 |
|
@param@link |
155 |
|
@return |
156 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
157 |
3674
|
public final String getElementName(final int i) {... |
158 |
3674
|
return (String) elements.get(i); |
159 |
|
} |
160 |
|
|
161 |
|
|
162 |
|
|
163 |
|
|
164 |
|
@param@link |
165 |
|
@return |
166 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
167 |
2194
|
public final int getElementOccurrence(final int i) {... |
168 |
2194
|
return ((Integer) numbers.get(i)).intValue(); |
169 |
|
} |
170 |
|
|
171 |
|
|
172 |
|
|
173 |
|
|
174 |
|
@param |
175 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
176 |
0
|
public final void addElement(final String elementName) {... |
177 |
0
|
attribute = null; |
178 |
0
|
elements.add(elementName); |
179 |
0
|
numbers.add(new Integer(1)); |
180 |
|
} |
181 |
|
|
182 |
|
|
183 |
|
|
184 |
|
|
185 |
|
@param |
186 |
|
@param |
187 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
188 |
3666
|
public final void addElement(final String elementName, final int occurrence) {... |
189 |
3666
|
attribute = null; |
190 |
3666
|
elements.add(elementName); |
191 |
3666
|
numbers.add(new Integer(occurrence)); |
192 |
|
} |
193 |
|
|
194 |
|
|
195 |
|
|
196 |
|
|
197 |
|
@return |
198 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
199 |
0
|
public final String getLastElement() {... |
200 |
0
|
int size = elements.size(); |
201 |
0
|
if (size <= 0) { |
202 |
0
|
return null; |
203 |
|
} |
204 |
0
|
return (String) elements.get(size - 1); |
205 |
|
} |
206 |
|
|
207 |
|
|
208 |
|
|
209 |
|
|
210 |
|
@return |
211 |
|
|
212 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
213 |
0
|
public final String getBeforeLastElement() {... |
214 |
0
|
int size = elements.size(); |
215 |
0
|
if (size <= 1) { |
216 |
0
|
return null; |
217 |
|
} |
218 |
0
|
return (String) elements.get(size - 2); |
219 |
|
} |
220 |
|
|
221 |
|
|
222 |
|
|
223 |
|
|
|
|
| 85.7% |
Uncovered Elements: 1 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
224 |
3363
|
public void deleteLastElement() {... |
225 |
3363
|
int size = elements.size(); |
226 |
3363
|
if (size > 0) { |
227 |
3363
|
elements.remove(size - 1); |
228 |
3363
|
numbers.remove(size - 1); |
229 |
3363
|
attribute = null; |
230 |
|
} |
231 |
|
} |
232 |
|
|
233 |
|
|
234 |
|
|
235 |
|
|
236 |
|
@param |
237 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
238 |
126
|
public final void setAttribute(final String attribute) {... |
239 |
126
|
this.attribute = attribute; |
240 |
|
} |
241 |
|
|
242 |
|
|
243 |
|
|
244 |
|
|
245 |
|
@return |
246 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
247 |
313
|
public final String getAttribute() {... |
248 |
313
|
return attribute; |
249 |
|
} |
250 |
|
|
251 |
|
|
|
|
| 80% |
Uncovered Elements: 2 (10) |
Complexity: 3 |
Complexity Density: 0.5 |
|
252 |
53
|
public final boolean equals(final Object obj) {... |
253 |
53
|
if (!(obj instanceof SimpleXPath)) { |
254 |
0
|
return false; |
255 |
|
} |
256 |
53
|
final SimpleXPath other = (SimpleXPath) obj; |
257 |
53
|
if (!EqualsUtility.equals(this.getAttribute(), other.getAttribute())) { |
258 |
19
|
return false; |
259 |
|
} |
260 |
34
|
return equalsElements(other); |
261 |
|
} |
262 |
|
|
263 |
|
|
264 |
|
|
265 |
|
|
266 |
|
|
267 |
|
@param |
268 |
|
@return |
269 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (17) |
Complexity: 5 |
Complexity Density: 0.56 |
|
270 |
34
|
public final boolean equalsElements(final SimpleXPath other) {... |
271 |
34
|
final int size = this.size(); |
272 |
34
|
if (size != other.size()) { |
273 |
15
|
return false; |
274 |
|
} |
275 |
|
|
276 |
62
|
for (int i = 0; i < size; i++) { |
277 |
51
|
if (!EqualsUtility.equals(this.getElementName(i), other.getElementName(i))) { |
278 |
7
|
return false; |
279 |
|
} |
280 |
44
|
if (getElementOccurrence(i) != other.getElementOccurrence(i)) { |
281 |
1
|
return false; |
282 |
|
} |
283 |
|
} |
284 |
11
|
return true; |
285 |
|
} |
286 |
|
|
287 |
|
|
288 |
|
|
289 |
|
|
290 |
|
|
291 |
|
@param |
292 |
|
|
293 |
|
@param |
294 |
|
|
295 |
|
@return |
296 |
|
|
|
|
| 72.4% |
Uncovered Elements: 8 (29) |
Complexity: 8 |
Complexity Density: 0.53 |
|
297 |
3368
|
public final boolean matchesElements(final SimpleXPath current,... |
298 |
|
final SimpleXPath currentSummary) { |
299 |
3368
|
final int size = current.size(); |
300 |
3368
|
if (size != size()) { |
301 |
3046
|
return false; |
302 |
|
} |
303 |
322
|
if (size != currentSummary.size()) { |
304 |
0
|
throw new IllegalArgumentException("summary size doesn't match"); |
305 |
|
} |
306 |
|
|
307 |
968
|
for (int i = 0; i < size; i++) { |
308 |
936
|
if ("*".equals(getElementName(i))) { |
309 |
0
|
if (getElementOccurrence(i) != currentSummary.getElementOccurrence(i)) { |
310 |
0
|
return false; |
311 |
|
} |
312 |
0
|
continue; |
313 |
|
} |
314 |
936
|
if (!EqualsUtility.equals(current.getElementName(i), getElementName(i))) { |
315 |
278
|
return false; |
316 |
|
} |
317 |
658
|
if (current.getElementOccurrence(i) != getElementOccurrence(i)) { |
318 |
12
|
return false; |
319 |
|
} |
320 |
|
} |
321 |
32
|
return true; |
322 |
|
} |
323 |
|
|
324 |
|
|
325 |
|
|
326 |
|
|
327 |
|
|
328 |
|
@param |
329 |
|
|
330 |
|
@param |
331 |
|
|
332 |
|
@return |
333 |
|
|
|
|
| 0% |
Uncovered Elements: 29 (29) |
Complexity: 8 |
Complexity Density: 0.53 |
|
334 |
0
|
public final boolean matchesElementsBegining(final SimpleXPath current,... |
335 |
|
final SimpleXPath currentSummary) { |
336 |
0
|
final int size = current.size(); |
337 |
0
|
if (size > size()) { |
338 |
0
|
return false; |
339 |
|
} |
340 |
0
|
if (size != currentSummary.size()) { |
341 |
0
|
throw new IllegalArgumentException("summary size doesn't match"); |
342 |
|
} |
343 |
|
|
344 |
0
|
for (int i = 0; i < size; i++) { |
345 |
0
|
if ("*".equals(getElementName(i))) { |
346 |
0
|
if (getElementOccurrence(i) != currentSummary.getElementOccurrence(i)) { |
347 |
0
|
return false; |
348 |
|
} |
349 |
0
|
continue; |
350 |
|
} |
351 |
0
|
if (!EqualsUtility.equals(current.getElementName(i), getElementName(i))) { |
352 |
0
|
return false; |
353 |
|
} |
354 |
0
|
if (current.getElementOccurrence(i) != getElementOccurrence(i)) { |
355 |
0
|
return false; |
356 |
|
} |
357 |
|
} |
358 |
0
|
return true; |
359 |
|
} |
360 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (18) |
Complexity: 4 |
Complexity Density: 0.33 |
|
361 |
150
|
public final String toString() {... |
362 |
150
|
final StringBuffer buffer = new StringBuffer(); |
363 |
730
|
for (int i = 0; i < size(); i++) { |
364 |
580
|
buffer.append("/"); |
365 |
580
|
buffer.append(getElementName(i)); |
366 |
580
|
if (getElementOccurrence(i) != 1) { |
367 |
26
|
buffer.append("["); |
368 |
26
|
buffer.append(getElementOccurrence(i)); |
369 |
26
|
buffer.append("]"); |
370 |
|
} |
371 |
|
} |
372 |
150
|
if (getAttribute() != null) { |
373 |
10
|
buffer.append("@"); |
374 |
10
|
buffer.append(getAttribute()); |
375 |
|
} |
376 |
150
|
return buffer.toString(); |
377 |
|
} |
378 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 3 |
Complexity Density: 0.38 |
|
379 |
46
|
public final int hashCode() {... |
380 |
46
|
int code = 0; |
381 |
46
|
if (attribute != null) { |
382 |
14
|
code ^= attribute.hashCode(); |
383 |
|
} |
384 |
210
|
for (int i = 0; i < size(); i++) { |
385 |
164
|
code ^= i + 1; |
386 |
164
|
code ^= getElementName(i).hashCode(); |
387 |
164
|
code ^= getElementOccurrence(i); |
388 |
|
} |
389 |
46
|
return code; |
390 |
|
} |
391 |
|
|
392 |
|
} |