|
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.utility.EqualsUtility; |
|
25 |
| import org.qedeq.kernel.xml.parser.SourcePosition; |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| public final 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 |
26608
| public SimpleXPath(final String xpath) {
|
|
71 |
26608
| elements = new ArrayList();
|
|
72 |
26608
| numbers = new ArrayList();
|
|
73 |
26608
| attribute = null;
|
|
74 |
26608
| init(xpath);
|
|
75 |
| } |
|
76 |
| |
|
77 |
| |
|
78 |
| |
|
79 |
| |
|
80 |
79652
| public SimpleXPath() {
|
|
81 |
79652
| elements = new ArrayList();
|
|
82 |
79652
| numbers = new ArrayList();
|
|
83 |
79652
| attribute = null;
|
|
84 |
| } |
|
85 |
| |
|
86 |
| |
|
87 |
| |
|
88 |
| |
|
89 |
| |
|
90 |
| |
|
91 |
273370
| public SimpleXPath(final SimpleXPath original) {
|
|
92 |
273370
| elements = new ArrayList();
|
|
93 |
273370
| numbers = new ArrayList();
|
|
94 |
273370
| attribute = null;
|
|
95 |
273370
| init(original.toString());
|
|
96 |
| } |
|
97 |
| |
|
98 |
| |
|
99 |
| |
|
100 |
| |
|
101 |
| |
|
102 |
| |
|
103 |
| |
|
104 |
| |
|
105 |
| |
|
106 |
299978
| private void init(final String xpath) {
|
|
107 |
299978
| final StringTokenizer tokenizer = new StringTokenizer(xpath, "/");
|
|
108 |
299978
| while (tokenizer.hasMoreTokens()) {
|
|
109 |
1434917
| String token = tokenizer.nextToken();
|
|
110 |
1434917
| if (!tokenizer.hasMoreTokens() && token.indexOf('@') >= 0) {
|
|
111 |
3321
| attribute = token.substring(token.indexOf('@') + 1);
|
|
112 |
3321
| token = token.substring(0, token.indexOf('@'));
|
|
113 |
| } |
|
114 |
1434917
| if (token.indexOf('[') < 0) {
|
|
115 |
927242
| elements.add(token);
|
|
116 |
927242
| numbers.add(new Integer(1));
|
|
117 |
| } else { |
|
118 |
507675
| final StringTokenizer getnu = new StringTokenizer(token, "[]");
|
|
119 |
507675
| elements.add(getnu.nextToken());
|
|
120 |
507675
| numbers.add(new Integer(getnu.nextToken()));
|
|
121 |
| } |
|
122 |
| } |
|
123 |
| } |
|
124 |
| |
|
125 |
| |
|
126 |
| |
|
127 |
| |
|
128 |
| |
|
129 |
| |
|
130 |
254036577
| public final int size() {
|
|
131 |
254036577
| return elements.size();
|
|
132 |
| } |
|
133 |
| |
|
134 |
| |
|
135 |
| |
|
136 |
| |
|
137 |
| |
|
138 |
| |
|
139 |
| |
|
140 |
113002092
| public final String getElementName(final int i) {
|
|
141 |
113002092
| return (String) elements.get(i);
|
|
142 |
| } |
|
143 |
| |
|
144 |
| |
|
145 |
| |
|
146 |
| |
|
147 |
| |
|
148 |
| |
|
149 |
| |
|
150 |
75954189
| public final int getElementOccurrence(final int i) {
|
|
151 |
75954189
| 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 |
119991972
| public final void addElement(final String elementName, final int occurrence) {
|
|
172 |
119991972
| attribute = null;
|
|
173 |
119991972
| elements.add(elementName);
|
|
174 |
119991972
| 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 |
119754637
| public void deleteLastElement() {
|
|
208 |
119754637
| int size = elements.size();
|
|
209 |
119754637
| if (size > 0) {
|
|
210 |
119754637
| elements.remove(size - 1);
|
|
211 |
119754637
| numbers.remove(size - 1);
|
|
212 |
119754637
| attribute = null;
|
|
213 |
| } |
|
214 |
| } |
|
215 |
| |
|
216 |
| |
|
217 |
| |
|
218 |
| |
|
219 |
| |
|
220 |
| |
|
221 |
651488
| public final void setAttribute(final String attribute) {
|
|
222 |
651488
| this.attribute = attribute;
|
|
223 |
| } |
|
224 |
| |
|
225 |
| |
|
226 |
| |
|
227 |
| |
|
228 |
| |
|
229 |
| |
|
230 |
389503
| public final String getAttribute() {
|
|
231 |
389503
| return attribute;
|
|
232 |
| } |
|
233 |
| |
|
234 |
| |
|
235 |
| |
|
236 |
| |
|
237 |
| |
|
238 |
| |
|
239 |
29685
| public final void setStartLocation(final SourcePosition position) {
|
|
240 |
29685
| start = position;
|
|
241 |
| } |
|
242 |
| |
|
243 |
| |
|
244 |
| |
|
245 |
| |
|
246 |
| |
|
247 |
| |
|
248 |
26575
| public final SourcePosition getStartLocation() {
|
|
249 |
26575
| return start;
|
|
250 |
| } |
|
251 |
| |
|
252 |
| |
|
253 |
| |
|
254 |
| |
|
255 |
| |
|
256 |
| |
|
257 |
26553
| public final void setEndLocation(final SourcePosition position) {
|
|
258 |
26553
| end = position;
|
|
259 |
| } |
|
260 |
| |
|
261 |
| |
|
262 |
| |
|
263 |
| |
|
264 |
| |
|
265 |
| |
|
266 |
26569
| public final SourcePosition getEndLocation() {
|
|
267 |
26569
| 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 |
119308772
| public final boolean matchesElements(final SimpleXPath current,
|
|
329 |
| final SimpleXPath currentSummary) { |
|
330 |
119308772
| final int size = current.size();
|
|
331 |
119308772
| if (size != size()) {
|
|
332 |
105888522
| return false;
|
|
333 |
| } |
|
334 |
13420250
| if (size != currentSummary.size()) {
|
|
335 |
0
| throw new IllegalArgumentException("summary size doesn't match");
|
|
336 |
| } |
|
337 |
| |
|
338 |
13420250
| for (int i = 0; i < size; i++) {
|
|
339 |
37109924
| if ("*".equals(getElementName(i))) {
|
|
340 |
0
| if (getElementOccurrence(i) != currentSummary.getElementOccurrence(i)) {
|
|
341 |
0
| return false;
|
|
342 |
| } |
|
343 |
0
| continue;
|
|
344 |
| } |
|
345 |
37109924
| if (!EqualsUtility.equals(current.getElementName(i), getElementName(i))) {
|
|
346 |
260524
| return false;
|
|
347 |
| } |
|
348 |
36849400
| if (current.getElementOccurrence(i) != getElementOccurrence(i)) {
|
|
349 |
13106620
| return false;
|
|
350 |
| } |
|
351 |
| } |
|
352 |
53106
| 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 |
326466
| public final String toString() {
|
|
393 |
326466
| final StringBuffer buffer = new StringBuffer();
|
|
394 |
326466
| for (int i = 0; i < size(); i++) {
|
|
395 |
1672034
| if (i != 0) {
|
|
396 |
1345568
| buffer.append("/");
|
|
397 |
| } |
|
398 |
1672034
| buffer.append(getElementName(i));
|
|
399 |
1672034
| if (getElementOccurrence(i) != 1) {
|
|
400 |
583083
| buffer.append("[");
|
|
401 |
583083
| buffer.append(getElementOccurrence(i));
|
|
402 |
583083
| buffer.append("]");
|
|
403 |
| } |
|
404 |
| } |
|
405 |
326466
| if (getAttribute() != null) {
|
|
406 |
6432
| buffer.append("@");
|
|
407 |
6432
| buffer.append(getAttribute());
|
|
408 |
| } |
|
409 |
326466
| 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 |
| } |