|
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 |
6368
| public SimpleXPath(final String xpath) {
|
|
71 |
6368
| elements = new ArrayList();
|
|
72 |
6368
| numbers = new ArrayList();
|
|
73 |
6368
| attribute = null;
|
|
74 |
6368
| init(xpath);
|
|
75 |
| } |
|
76 |
| |
|
77 |
| |
|
78 |
| |
|
79 |
| |
|
80 |
6320
| public SimpleXPath() {
|
|
81 |
6320
| elements = new ArrayList();
|
|
82 |
6320
| numbers = new ArrayList();
|
|
83 |
6320
| attribute = null;
|
|
84 |
| } |
|
85 |
| |
|
86 |
| |
|
87 |
| |
|
88 |
| |
|
89 |
| |
|
90 |
| |
|
91 |
| |
|
92 |
| |
|
93 |
| |
|
94 |
6368
| private void init(final String xpath) {
|
|
95 |
6368
| final StringTokenizer tokenizer = new StringTokenizer(xpath, "/");
|
|
96 |
6368
| while (tokenizer.hasMoreTokens()) {
|
|
97 |
36371
| String token = tokenizer.nextToken();
|
|
98 |
36371
| if (!tokenizer.hasMoreTokens() && token.indexOf('@') >= 0) {
|
|
99 |
314
| attribute = token.substring(token.indexOf('@') + 1);
|
|
100 |
314
| token = token.substring(0, token.indexOf('@'));
|
|
101 |
| } |
|
102 |
36371
| if (token.indexOf('[') < 0) {
|
|
103 |
21513
| elements.add(token);
|
|
104 |
21513
| numbers.add(new Integer(1));
|
|
105 |
| } else { |
|
106 |
14858
| final StringTokenizer getnu = new StringTokenizer(token, "[]");
|
|
107 |
14858
| elements.add(getnu.nextToken());
|
|
108 |
14858
| numbers.add(new Integer(getnu.nextToken()));
|
|
109 |
| } |
|
110 |
| } |
|
111 |
| } |
|
112 |
| |
|
113 |
| |
|
114 |
| |
|
115 |
| |
|
116 |
| |
|
117 |
| |
|
118 |
23841109
| public final int size() {
|
|
119 |
23841109
| return elements.size();
|
|
120 |
| } |
|
121 |
| |
|
122 |
| |
|
123 |
| |
|
124 |
| |
|
125 |
| |
|
126 |
| |
|
127 |
| |
|
128 |
9879584
| public final String getElementName(final int i) {
|
|
129 |
9879584
| return (String) elements.get(i);
|
|
130 |
| } |
|
131 |
| |
|
132 |
| |
|
133 |
| |
|
134 |
| |
|
135 |
| |
|
136 |
| |
|
137 |
| |
|
138 |
6674928
| public final int getElementOccurrence(final int i) {
|
|
139 |
6674928
| return ((Integer) numbers.get(i)).intValue();
|
|
140 |
| } |
|
141 |
| |
|
142 |
| |
|
143 |
| |
|
144 |
| |
|
145 |
| |
|
146 |
| |
|
147 |
276
| public final void addElement(final String elementName) {
|
|
148 |
276
| elements.add(elementName);
|
|
149 |
276
| numbers.add(new Integer(1));
|
|
150 |
| } |
|
151 |
| |
|
152 |
| |
|
153 |
| |
|
154 |
| |
|
155 |
| |
|
156 |
| |
|
157 |
| |
|
158 |
11237432
| public final void addElement(final String elementName, final int occurrence) {
|
|
159 |
11237432
| elements.add(elementName);
|
|
160 |
11237432
| numbers.add(new Integer(occurrence));
|
|
161 |
| } |
|
162 |
| |
|
163 |
| |
|
164 |
| |
|
165 |
| |
|
166 |
| |
|
167 |
| |
|
168 |
3153
| public final String getLastElement() {
|
|
169 |
3153
| int size = elements.size();
|
|
170 |
3153
| if (size <= 0) {
|
|
171 |
0
| return null;
|
|
172 |
| } |
|
173 |
3153
| return (String) elements.get(size - 1);
|
|
174 |
| } |
|
175 |
| |
|
176 |
| |
|
177 |
| |
|
178 |
| |
|
179 |
| |
|
180 |
| |
|
181 |
| |
|
182 |
3153
| public final String getBeforeLastElement() {
|
|
183 |
3153
| int size = elements.size();
|
|
184 |
3153
| if (size <= 1) {
|
|
185 |
0
| return null;
|
|
186 |
| } |
|
187 |
3153
| return (String) elements.get(size - 2);
|
|
188 |
| } |
|
189 |
| |
|
190 |
| |
|
191 |
| |
|
192 |
| |
|
193 |
11237765
| public void deleteLastElement() {
|
|
194 |
11237765
| int size = elements.size();
|
|
195 |
11237765
| if (size > 0) {
|
|
196 |
11237765
| elements.remove(size - 1);
|
|
197 |
11237765
| numbers.remove(size - 1);
|
|
198 |
11237765
| attribute = null;
|
|
199 |
| } |
|
200 |
| } |
|
201 |
| |
|
202 |
| |
|
203 |
| |
|
204 |
| |
|
205 |
| |
|
206 |
| |
|
207 |
293
| public final void setAttribute(final String attribute) {
|
|
208 |
293
| this.attribute = attribute;
|
|
209 |
| } |
|
210 |
| |
|
211 |
| |
|
212 |
| |
|
213 |
| |
|
214 |
| |
|
215 |
| |
|
216 |
13457
| public final String getAttribute() {
|
|
217 |
13457
| return attribute;
|
|
218 |
| } |
|
219 |
| |
|
220 |
| |
|
221 |
| |
|
222 |
| |
|
223 |
| |
|
224 |
| |
|
225 |
3455
| public final void setStartLocation(final SourcePosition position) {
|
|
226 |
3455
| start = position;
|
|
227 |
| } |
|
228 |
| |
|
229 |
| |
|
230 |
| |
|
231 |
| |
|
232 |
| |
|
233 |
| |
|
234 |
3177
| public final SourcePosition getStartLocation() {
|
|
235 |
3177
| return start;
|
|
236 |
| } |
|
237 |
| |
|
238 |
| |
|
239 |
| |
|
240 |
| |
|
241 |
| |
|
242 |
| |
|
243 |
3160
| public final void setEndLocation(final SourcePosition position) {
|
|
244 |
3160
| end = position;
|
|
245 |
| } |
|
246 |
| |
|
247 |
| |
|
248 |
| |
|
249 |
| |
|
250 |
| |
|
251 |
| |
|
252 |
3177
| public final SourcePosition getEndLocation() {
|
|
253 |
3177
| return end;
|
|
254 |
| } |
|
255 |
| |
|
256 |
53
| public final boolean equals(final Object obj) {
|
|
257 |
53
| if (!(obj instanceof SimpleXPath)) {
|
|
258 |
0
| return false;
|
|
259 |
| } |
|
260 |
53
| final SimpleXPath other = (SimpleXPath) obj;
|
|
261 |
53
| if (!EqualsUtility.equals(this.getAttribute(), other.getAttribute())) {
|
|
262 |
19
| return false;
|
|
263 |
| } |
|
264 |
34
| final int size = this.size();
|
|
265 |
34
| if (size != other.size()) {
|
|
266 |
15
| return false;
|
|
267 |
| } |
|
268 |
| |
|
269 |
19
| for (int i = 0; i < size; i++) {
|
|
270 |
51
| if (!EqualsUtility.equals(this.getElementName(i), other.getElementName(i))) {
|
|
271 |
7
| return false;
|
|
272 |
| } |
|
273 |
44
| if (this.getElementOccurrence(i) != other.getElementOccurrence(i)) {
|
|
274 |
1
| return false;
|
|
275 |
| } |
|
276 |
| } |
|
277 |
11
| return true;
|
|
278 |
| } |
|
279 |
| |
|
280 |
| |
|
281 |
| |
|
282 |
| |
|
283 |
| |
|
284 |
| |
|
285 |
| |
|
286 |
| |
|
287 |
0
| public final boolean equalsElements(final SimpleXPath other) {
|
|
288 |
0
| final int size = this.size();
|
|
289 |
0
| if (size != other.size()) {
|
|
290 |
0
| return false;
|
|
291 |
| } |
|
292 |
| |
|
293 |
0
| for (int i = 0; i < size; i++) {
|
|
294 |
0
| if (!EqualsUtility.equals(this.getElementName(i), other.getElementName(i))) {
|
|
295 |
0
| return false;
|
|
296 |
| } |
|
297 |
0
| if (getElementOccurrence(i) != other.getElementOccurrence(i)) {
|
|
298 |
0
| return false;
|
|
299 |
| } |
|
300 |
| } |
|
301 |
0
| return true;
|
|
302 |
| } |
|
303 |
| |
|
304 |
| |
|
305 |
| |
|
306 |
| |
|
307 |
| |
|
308 |
| |
|
309 |
| |
|
310 |
| |
|
311 |
| |
|
312 |
| |
|
313 |
| |
|
314 |
11237236
| public final boolean matchesElements(final SimpleXPath current,
|
|
315 |
| final SimpleXPath currentSummary) { |
|
316 |
11237236
| final int size = current.size();
|
|
317 |
11237236
| if (size != size()) {
|
|
318 |
9913538
| return false;
|
|
319 |
| } |
|
320 |
1323698
| if (size != currentSummary.size()) {
|
|
321 |
0
| throw new IllegalArgumentException("summary size doesn't match");
|
|
322 |
| } |
|
323 |
| |
|
324 |
1323698
| for (int i = 0; i < size; i++) {
|
|
325 |
3373602
| if ("*".equals(getElementName(i))) {
|
|
326 |
138824
| if (getElementOccurrence(i) != currentSummary.getElementOccurrence(i)) {
|
|
327 |
118440
| return false;
|
|
328 |
| } |
|
329 |
20384
| continue;
|
|
330 |
| } |
|
331 |
3234778
| if (!EqualsUtility.equals(current.getElementName(i), getElementName(i))) {
|
|
332 |
60560
| return false;
|
|
333 |
| } |
|
334 |
3174218
| if (current.getElementOccurrence(i) != getElementOccurrence(i)) {
|
|
335 |
1138378
| return false;
|
|
336 |
| } |
|
337 |
| } |
|
338 |
6320
| return true;
|
|
339 |
| } |
|
340 |
| |
|
341 |
| |
|
342 |
| |
|
343 |
| |
|
344 |
| |
|
345 |
| |
|
346 |
| |
|
347 |
| |
|
348 |
| |
|
349 |
| |
|
350 |
| |
|
351 |
0
| public final boolean matchesElementsBegining(final SimpleXPath current,
|
|
352 |
| final SimpleXPath currentSummary) { |
|
353 |
0
| final int size = current.size();
|
|
354 |
0
| if (size > size()) {
|
|
355 |
0
| return false;
|
|
356 |
| } |
|
357 |
0
| if (size != currentSummary.size()) {
|
|
358 |
0
| throw new IllegalArgumentException("summary size doesn't match");
|
|
359 |
| } |
|
360 |
| |
|
361 |
0
| for (int i = 0; i < size; i++) {
|
|
362 |
0
| if ("*".equals(getElementName(i))) {
|
|
363 |
0
| if (getElementOccurrence(i) != currentSummary.getElementOccurrence(i)) {
|
|
364 |
0
| return false;
|
|
365 |
| } |
|
366 |
0
| continue;
|
|
367 |
| } |
|
368 |
0
| if (!EqualsUtility.equals(current.getElementName(i), getElementName(i))) {
|
|
369 |
0
| return false;
|
|
370 |
| } |
|
371 |
0
| if (current.getElementOccurrence(i) != getElementOccurrence(i)) {
|
|
372 |
0
| return false;
|
|
373 |
| } |
|
374 |
| } |
|
375 |
0
| return true;
|
|
376 |
| } |
|
377 |
| |
|
378 |
6320
| public final String toString() {
|
|
379 |
6320
| final StringBuffer buffer = new StringBuffer();
|
|
380 |
6320
| for (int i = 0; i < size(); i++) {
|
|
381 |
36140
| if (i != 0) {
|
|
382 |
29820
| buffer.append("/");
|
|
383 |
| } |
|
384 |
36140
| buffer.append(getElementName(i));
|
|
385 |
36140
| if (getElementOccurrence(i) != 1) {
|
|
386 |
12236
| buffer.append("[");
|
|
387 |
12236
| buffer.append(getElementOccurrence(i));
|
|
388 |
12236
| buffer.append("]");
|
|
389 |
| } |
|
390 |
| } |
|
391 |
6320
| if (getAttribute() != null) {
|
|
392 |
295
| buffer.append("@");
|
|
393 |
295
| buffer.append(getAttribute());
|
|
394 |
| } |
|
395 |
6320
| return buffer.toString();
|
|
396 |
| } |
|
397 |
| |
|
398 |
46
| public final int hashCode() {
|
|
399 |
46
| int code = 0;
|
|
400 |
46
| if (attribute != null) {
|
|
401 |
14
| code ^= attribute.hashCode();
|
|
402 |
| } |
|
403 |
46
| for (int i = 0; i < size(); i++) {
|
|
404 |
164
| code ^= i + 1;
|
|
405 |
164
| code ^= getElementName(i).hashCode();
|
|
406 |
164
| code ^= getElementOccurrence(i);
|
|
407 |
| } |
|
408 |
46
| return code;
|
|
409 |
| } |
|
410 |
| |
|
411 |
| } |