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