1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| package org.qedeq.kernel.xml.tracker; |
19 |
| |
20 |
| import java.util.ArrayList; |
21 |
| import java.util.List; |
22 |
| import java.util.StringTokenizer; |
23 |
| |
24 |
| import org.qedeq.kernel.common.SourcePosition; |
25 |
| import org.qedeq.kernel.utility.EqualsUtility; |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| public class SimpleXPath { |
35 |
| |
36 |
| |
37 |
| private final List elements; |
38 |
| |
39 |
| |
40 |
| private final List numbers; |
41 |
| |
42 |
| |
43 |
| private String attribute; |
44 |
| |
45 |
| |
46 |
| private SourcePosition start; |
47 |
| |
48 |
| |
49 |
| private SourcePosition end; |
50 |
| |
51 |
| |
52 |
| |
53 |
| |
54 |
| |
55 |
| |
56 |
| |
57 |
| |
58 |
| |
59 |
| |
60 |
| |
61 |
| |
62 |
| |
63 |
| |
64 |
| |
65 |
| |
66 |
| |
67 |
| |
68 |
| |
69 |
| |
70 |
29017
| public SimpleXPath(final String xpath) {
|
71 |
29017
| elements = new ArrayList();
|
72 |
29017
| numbers = new ArrayList();
|
73 |
29017
| attribute = null;
|
74 |
29017
| init(xpath);
|
75 |
| } |
76 |
| |
77 |
| |
78 |
| |
79 |
| |
80 |
86879
| public SimpleXPath() {
|
81 |
86879
| elements = new ArrayList();
|
82 |
86879
| numbers = new ArrayList();
|
83 |
86879
| attribute = null;
|
84 |
| } |
85 |
| |
86 |
| |
87 |
| |
88 |
| |
89 |
| |
90 |
| |
91 |
300195
| public SimpleXPath(final SimpleXPath original) {
|
92 |
300195
| elements = new ArrayList();
|
93 |
300195
| numbers = new ArrayList();
|
94 |
300195
| attribute = null;
|
95 |
300195
| init(original.toString());
|
96 |
| } |
97 |
| |
98 |
| |
99 |
| |
100 |
| |
101 |
| |
102 |
| |
103 |
| |
104 |
| |
105 |
| |
106 |
329212
| private void init(final String xpath) {
|
107 |
329212
| final StringTokenizer tokenizer = new StringTokenizer(xpath, "/");
|
108 |
329212
| while (tokenizer.hasMoreTokens()) {
|
109 |
1581843
| String token = tokenizer.nextToken();
|
110 |
1581843
| if (!tokenizer.hasMoreTokens() && token.indexOf('@') >= 0) {
|
111 |
3357
| attribute = token.substring(token.indexOf('@') + 1);
|
112 |
3357
| token = token.substring(0, token.indexOf('@'));
|
113 |
| } |
114 |
1581843
| if (token.indexOf('[') < 0) {
|
115 |
1028470
| elements.add(token);
|
116 |
1028470
| numbers.add(new Integer(1));
|
117 |
| } else { |
118 |
553373
| final StringTokenizer getnu = new StringTokenizer(token, "[]");
|
119 |
553373
| elements.add(getnu.nextToken());
|
120 |
553373
| numbers.add(new Integer(getnu.nextToken()));
|
121 |
| } |
122 |
| } |
123 |
| } |
124 |
| |
125 |
| |
126 |
| |
127 |
| |
128 |
| |
129 |
| |
130 |
263758639
| public final int size() {
|
131 |
263758639
| return elements.size();
|
132 |
| } |
133 |
| |
134 |
| |
135 |
| |
136 |
| |
137 |
| |
138 |
| |
139 |
| |
140 |
122524104
| public final String getElementName(final int i) {
|
141 |
122524104
| return (String) elements.get(i);
|
142 |
| } |
143 |
| |
144 |
| |
145 |
| |
146 |
| |
147 |
| |
148 |
| |
149 |
| |
150 |
82268527
| public final int getElementOccurrence(final int i) {
|
151 |
82268527
| return ((Integer) numbers.get(i)).intValue();
|
152 |
| } |
153 |
| |
154 |
| |
155 |
| |
156 |
| |
157 |
| |
158 |
| |
159 |
0
| public final void addElement(final String elementName) {
|
160 |
0
| attribute = null;
|
161 |
0
| elements.add(elementName);
|
162 |
0
| numbers.add(new Integer(1));
|
163 |
| } |
164 |
| |
165 |
| |
166 |
| |
167 |
| |
168 |
| |
169 |
| |
170 |
| |
171 |
124609732
| public final void addElement(final String elementName, final int occurrence) {
|
172 |
124609732
| attribute = null;
|
173 |
124609732
| elements.add(elementName);
|
174 |
124609732
| numbers.add(new Integer(occurrence));
|
175 |
| } |
176 |
| |
177 |
| |
178 |
| |
179 |
| |
180 |
| |
181 |
| |
182 |
0
| public final String getLastElement() {
|
183 |
0
| int size = elements.size();
|
184 |
0
| if (size <= 0) {
|
185 |
0
| return null;
|
186 |
| } |
187 |
0
| return (String) elements.get(size - 1);
|
188 |
| } |
189 |
| |
190 |
| |
191 |
| |
192 |
| |
193 |
| |
194 |
| |
195 |
| |
196 |
0
| public final String getBeforeLastElement() {
|
197 |
0
| int size = elements.size();
|
198 |
0
| if (size <= 1) {
|
199 |
0
| return null;
|
200 |
| } |
201 |
0
| return (String) elements.get(size - 2);
|
202 |
| } |
203 |
| |
204 |
| |
205 |
| |
206 |
| |
207 |
124348249
| public void deleteLastElement() {
|
208 |
124348249
| int size = elements.size();
|
209 |
124348249
| if (size > 0) {
|
210 |
124348249
| elements.remove(size - 1);
|
211 |
124348249
| numbers.remove(size - 1);
|
212 |
124348249
| attribute = null;
|
213 |
| } |
214 |
| } |
215 |
| |
216 |
| |
217 |
| |
218 |
| |
219 |
| |
220 |
| |
221 |
693911
| public final void setAttribute(final String attribute) {
|
222 |
693911
| this.attribute = attribute;
|
223 |
| } |
224 |
| |
225 |
| |
226 |
| |
227 |
| |
228 |
| |
229 |
| |
230 |
426077
| public final String getAttribute() {
|
231 |
426077
| return attribute;
|
232 |
| } |
233 |
| |
234 |
| |
235 |
| |
236 |
| |
237 |
| |
238 |
| |
239 |
32128
| public final void setStartLocation(final SourcePosition position) {
|
240 |
32128
| start = position;
|
241 |
| } |
242 |
| |
243 |
| |
244 |
| |
245 |
| |
246 |
| |
247 |
| |
248 |
28999
| public final SourcePosition getStartLocation() {
|
249 |
28999
| return start;
|
250 |
| } |
251 |
| |
252 |
| |
253 |
| |
254 |
| |
255 |
| |
256 |
| |
257 |
28962
| public final void setEndLocation(final SourcePosition position) {
|
258 |
28962
| end = position;
|
259 |
| } |
260 |
| |
261 |
| |
262 |
| |
263 |
| |
264 |
| |
265 |
| |
266 |
28978
| public final SourcePosition getEndLocation() {
|
267 |
28978
| return end;
|
268 |
| } |
269 |
| |
270 |
53
| public final boolean equals(final Object obj) {
|
271 |
53
| if (!(obj instanceof SimpleXPath)) {
|
272 |
0
| return false;
|
273 |
| } |
274 |
53
| final SimpleXPath other = (SimpleXPath) obj;
|
275 |
53
| if (!EqualsUtility.equals(this.getAttribute(), other.getAttribute())) {
|
276 |
19
| return false;
|
277 |
| } |
278 |
34
| final int size = this.size();
|
279 |
34
| if (size != other.size()) {
|
280 |
15
| return false;
|
281 |
| } |
282 |
| |
283 |
19
| for (int i = 0; i < size; i++) {
|
284 |
51
| if (!EqualsUtility.equals(this.getElementName(i), other.getElementName(i))) {
|
285 |
7
| return false;
|
286 |
| } |
287 |
44
| if (this.getElementOccurrence(i) != other.getElementOccurrence(i)) {
|
288 |
1
| return false;
|
289 |
| } |
290 |
| } |
291 |
11
| return true;
|
292 |
| } |
293 |
| |
294 |
| |
295 |
| |
296 |
| |
297 |
| |
298 |
| |
299 |
| |
300 |
| |
301 |
0
| public final boolean equalsElements(final SimpleXPath other) {
|
302 |
0
| final int size = this.size();
|
303 |
0
| if (size != other.size()) {
|
304 |
0
| return false;
|
305 |
| } |
306 |
| |
307 |
0
| for (int i = 0; i < size; i++) {
|
308 |
0
| if (!EqualsUtility.equals(this.getElementName(i), other.getElementName(i))) {
|
309 |
0
| return false;
|
310 |
| } |
311 |
0
| if (getElementOccurrence(i) != other.getElementOccurrence(i)) {
|
312 |
0
| return false;
|
313 |
| } |
314 |
| } |
315 |
0
| return true;
|
316 |
| } |
317 |
| |
318 |
| |
319 |
| |
320 |
| |
321 |
| |
322 |
| |
323 |
| |
324 |
| |
325 |
| |
326 |
| |
327 |
| |
328 |
123825848
| public final boolean matchesElements(final SimpleXPath current,
|
329 |
| final SimpleXPath currentSummary) { |
330 |
123825848
| final int size = current.size();
|
331 |
123825848
| if (size != size()) {
|
332 |
109920356
| return false;
|
333 |
| } |
334 |
13905492
| if (size != currentSummary.size()) {
|
335 |
0
| throw new IllegalArgumentException("summary size doesn't match");
|
336 |
| } |
337 |
| |
338 |
13905492
| for (int i = 0; i < size; i++) {
|
339 |
40226918
| if ("*".equals(getElementName(i))) {
|
340 |
0
| if (getElementOccurrence(i) != currentSummary.getElementOccurrence(i)) {
|
341 |
0
| return false;
|
342 |
| } |
343 |
0
| continue;
|
344 |
| } |
345 |
40226918
| if (!EqualsUtility.equals(current.getElementName(i), getElementName(i))) {
|
346 |
332460
| return false;
|
347 |
| } |
348 |
39894458
| if (current.getElementOccurrence(i) != getElementOccurrence(i)) {
|
349 |
13515108
| return false;
|
350 |
| } |
351 |
| } |
352 |
57924
| return true;
|
353 |
| } |
354 |
| |
355 |
| |
356 |
| |
357 |
| |
358 |
| |
359 |
| |
360 |
| |
361 |
| |
362 |
| |
363 |
| |
364 |
| |
365 |
0
| public final boolean matchesElementsBegining(final SimpleXPath current,
|
366 |
| final SimpleXPath currentSummary) { |
367 |
0
| final int size = current.size();
|
368 |
0
| if (size > size()) {
|
369 |
0
| return false;
|
370 |
| } |
371 |
0
| if (size != currentSummary.size()) {
|
372 |
0
| throw new IllegalArgumentException("summary size doesn't match");
|
373 |
| } |
374 |
| |
375 |
0
| for (int i = 0; i < size; i++) {
|
376 |
0
| if ("*".equals(getElementName(i))) {
|
377 |
0
| if (getElementOccurrence(i) != currentSummary.getElementOccurrence(i)) {
|
378 |
0
| return false;
|
379 |
| } |
380 |
0
| continue;
|
381 |
| } |
382 |
0
| if (!EqualsUtility.equals(current.getElementName(i), getElementName(i))) {
|
383 |
0
| return false;
|
384 |
| } |
385 |
0
| if (current.getElementOccurrence(i) != getElementOccurrence(i)) {
|
386 |
0
| return false;
|
387 |
| } |
388 |
| } |
389 |
0
| return true;
|
390 |
| } |
391 |
| |
392 |
358104
| public final String toString() {
|
393 |
358104
| final StringBuffer buffer = new StringBuffer();
|
394 |
358104
| for (int i = 0; i < size(); i++) {
|
395 |
1843064
| if (i != 0) {
|
396 |
1484960
| buffer.append("/");
|
397 |
| } |
398 |
1843064
| buffer.append(getElementName(i));
|
399 |
1843064
| if (getElementOccurrence(i) != 1) {
|
400 |
636275
| buffer.append("[");
|
401 |
636275
| buffer.append(getElementOccurrence(i));
|
402 |
636275
| buffer.append("]");
|
403 |
| } |
404 |
| } |
405 |
358104
| if (getAttribute() != null) {
|
406 |
6502
| buffer.append("@");
|
407 |
6502
| buffer.append(getAttribute());
|
408 |
| } |
409 |
358104
| return buffer.toString();
|
410 |
| } |
411 |
| |
412 |
46
| public final int hashCode() {
|
413 |
46
| int code = 0;
|
414 |
46
| if (attribute != null) {
|
415 |
14
| code ^= attribute.hashCode();
|
416 |
| } |
417 |
46
| for (int i = 0; i < size(); i++) {
|
418 |
164
| code ^= i + 1;
|
419 |
164
| code ^= getElementName(i).hashCode();
|
420 |
164
| code ^= getElementOccurrence(i);
|
421 |
| } |
422 |
46
| return code;
|
423 |
| } |
424 |
| |
425 |
| } |