/
ItemPath.java
657 lines (580 loc) · 18.8 KB
/
ItemPath.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
/*
* Copyright (c) 2010-2016 Evolveum
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.evolveum.midpoint.prism.path;
import com.evolveum.midpoint.prism.PrismConstants;
import com.evolveum.midpoint.util.QNameUtil;
import com.evolveum.prism.xml.ns._public.types_3.ItemPathType;
import org.apache.commons.lang.Validate;
import javax.xml.namespace.QName;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* @author semancik
*
*/
public class ItemPath implements Serializable, Cloneable {
public static final QName XSD_TYPE = ItemPathType.COMPLEX_TYPE;
public static final ItemPath EMPTY_PATH = new ItemPath();
private List<ItemPathSegment> segments;
private Map<String, String> namespaceMap;
public void setNamespaceMap(Map<String, String> namespaceMap) {
this.namespaceMap = namespaceMap;
}
public Map<String, String> getNamespaceMap() {
return namespaceMap;
}
public ItemPath() {
segments = new ArrayList<ItemPathSegment>(0);
}
public ItemPath(QName... qnames) {
this.segments = new ArrayList<>(qnames.length);
for (QName qname : qnames) {
add(qname);
}
}
public ItemPath(String... names) {
this.segments = new ArrayList<>(names.length);
for (String name : names) {
add(stringToQName(name));
}
}
public ItemPath(Object[] namesOrIds) {
this.segments = new ArrayList<>(namesOrIds.length);
for (Object nameOrId : namesOrIds) {
if (nameOrId instanceof QName) {
add((QName) nameOrId);
} else if (nameOrId instanceof String) {
add(stringToQName((String) nameOrId));
} else if (nameOrId instanceof Long) {
this.segments.add(new IdItemPathSegment((Long) nameOrId));
} else if (nameOrId instanceof Integer) {
this.segments.add(new IdItemPathSegment(((Integer) nameOrId).longValue()));
} else {
throw new IllegalArgumentException("Invalid item path segment value: " + nameOrId);
}
}
}
private QName stringToQName(String name) {
Validate.notNull(name, "name");
if (ParentPathSegment.SYMBOL.equals(name)) {
return ParentPathSegment.QNAME;
} else if (ObjectReferencePathSegment.SYMBOL.equals(name)) {
return ObjectReferencePathSegment.QNAME;
} else if (IdentifierPathSegment.SYMBOL.equals(name)) {
return IdentifierPathSegment.QNAME;
} else {
return new QName(name);
}
}
public ItemPath(ItemPath parentPath, QName subName) {
this.segments = new ArrayList<>(parentPath.segments.size()+1);
segments.addAll(parentPath.segments);
add(subName);
}
public ItemPath(ItemPath parentPath, ItemPath childPath) {
this.segments = new ArrayList<>(parentPath.segments.size()+childPath.segments.size());
segments.addAll(parentPath.segments);
segments.addAll(childPath.segments);
}
public ItemPath(List<ItemPathSegment> segments) {
this.segments = new ArrayList<>(segments.size());
this.segments.addAll(segments);
}
public ItemPath(List<ItemPathSegment> segments, ItemPathSegment subSegment) {
this.segments = new ArrayList<>(segments.size()+1);
this.segments.addAll(segments);
this.segments.add(subSegment);
}
public ItemPath(List<ItemPathSegment> segments, QName subName) {
this.segments = new ArrayList<>(segments.size()+1);
this.segments.addAll(segments);
add(subName);
}
public ItemPath(ItemPathSegment... segments) {
this.segments = new ArrayList<>(segments.length);
Collections.addAll(this.segments, segments);
}
public ItemPath(ItemPath parentPath, ItemPathSegment subSegment) {
this.segments = new ArrayList<>(parentPath.segments.size() + 1);
this.segments.addAll(parentPath.segments);
this.segments.add(subSegment);
}
public ItemPath subPath(QName subName) {
return new ItemPath(segments, subName);
}
public ItemPath subPath(Long id) {
return subPath(new IdItemPathSegment(id));
}
public ItemPath subPath(ItemPathSegment subSegment) {
return new ItemPath(segments, subSegment);
}
public ItemPath subPath(ItemPath subPath) {
ItemPath newPath = new ItemPath(segments);
newPath.segments.addAll(subPath.getSegments());
return newPath;
}
/**
* Null-proof static version.
*/
public static ItemPath subPath(ItemPath prefix, ItemPathSegment subSegment) {
if (prefix == null && subSegment == null) {
return EMPTY_PATH;
}
if (prefix == null) {
return new ItemPath(subSegment);
}
return prefix.subPath(subSegment);
}
private void add(QName qname) {
this.segments.add(createSegment(qname, false));
}
public static ItemPathSegment createSegment(QName qname, boolean variable) {
if (ParentPathSegment.QNAME.equals(qname)) {
return new ParentPathSegment();
} else if (ObjectReferencePathSegment.QNAME.equals(qname)) {
return new ObjectReferencePathSegment();
} else if (IdentifierPathSegment.QNAME.equals(qname)) {
return new IdentifierPathSegment();
} else {
return new NameItemPathSegment(qname, variable);
}
}
public List<ItemPathSegment> getSegments() {
return segments;
}
public ItemPathSegment first() {
if (segments.size() == 0) {
return null;
}
return segments.get(0);
}
public ItemPath rest() {
return tail();
}
public NameItemPathSegment lastNamed() {
for (int i = segments.size()-1; i >= 0; i--) {
if (segments.get(i) instanceof NameItemPathSegment) {
return (NameItemPathSegment) segments.get(i);
}
}
return null;
}
public ItemPathSegment last() {
if (segments.size() == 0) {
return null;
}
return segments.get(segments.size()-1);
}
/**
* Returns first segment in a form of path.
*/
public ItemPath head() {
return new ItemPath(first());
}
/**
* Returns path containing all segments except the first N.
*/
public ItemPath tail(int n) {
if (segments.size() < n) {
return EMPTY_PATH;
}
return new ItemPath(segments.subList(n, segments.size()));
}
public ItemPath tail() {
return tail(1);
}
/**
* Returns a path containing all segments except the last one.
*/
public ItemPath allExceptLast() {
if (segments.size() == 0) {
return EMPTY_PATH;
}
return new ItemPath(segments.subList(0, segments.size()-1));
}
/**
* Returns a path containing all segments up to (and not including) the last one.
*/
public ItemPath allUpToLastNamed() {
for (int i = segments.size()-1; i >= 0; i--) {
if (segments.get(i) instanceof NameItemPathSegment) {
return new ItemPath(segments.subList(0, i));
}
}
return EMPTY_PATH;
}
/**
* Returns a path containing all segments up to (not including) the specified one;
* counted from backwards.
* If the segment is not present, returns empty path.
*/
public ItemPath allUpTo(ItemPathSegment segment) {
int i = segments.lastIndexOf(segment);
if (i < 0) {
return EMPTY_PATH;
} else {
return new ItemPath(segments.subList(0, i));
}
}
/**
* Returns a path containing all segments up to (including) the specified one;
* counted from backwards.
* If the segment is not present, returns empty path.
*/
public ItemPath allUpToIncluding(ItemPathSegment segment) {
int i = segments.lastIndexOf(segment);
if (i < 0) {
return EMPTY_PATH;
} else {
return allUpToIncluding(i);
}
}
public ItemPath allUpToIncluding(int i) {
return new ItemPath(segments.subList(0, i+1));
}
public int size() {
return segments.size();
}
public boolean isEmpty() {
return segments.isEmpty();
}
/**
* Makes the path "normal" by inserting null Id segments where they were omitted.
*/
public ItemPath normalize() {
ItemPath normalizedPath = new ItemPath();
ItemPathSegment lastSegment = null;
Iterator<ItemPathSegment> iterator = segments.iterator();
while (iterator.hasNext()) {
ItemPathSegment origSegment = iterator.next();
if (lastSegment != null && !(lastSegment instanceof IdItemPathSegment) &&
!(origSegment instanceof IdItemPathSegment)) {
normalizedPath.segments.add(new IdItemPathSegment());
}
normalizedPath.segments.add(origSegment);
lastSegment = origSegment;
}
if (lastSegment != null && !(lastSegment instanceof IdItemPathSegment) &&
// Make sure we do not insert the Id segment as a last one. That is not correct and it would spoil comparing paths
iterator.hasNext()) {
normalizedPath.segments.add(new IdItemPathSegment());
}
return normalizedPath;
}
public CompareResult compareComplex(ItemPath otherPath) {
ItemPath thisNormalized = this.normalize();
ItemPath otherNormalized = otherPath == null ? EMPTY_PATH : otherPath.normalize();
int i = 0;
while (i < thisNormalized.segments.size() && i < otherNormalized.segments.size()) {
ItemPathSegment thisSegment = thisNormalized.segments.get(i);
ItemPathSegment otherSegment = otherNormalized.segments.get(i);
if (!thisSegment.equivalent(otherSegment)) {
return CompareResult.NO_RELATION;
}
i++;
}
if (i < thisNormalized.size()) {
return CompareResult.SUPERPATH;
}
if (i < otherNormalized.size()) {
return CompareResult.SUBPATH;
}
return CompareResult.EQUIVALENT;
}
public static boolean containsEquivalent(Collection<ItemPath> paths, ItemPath pathToBeFound) {
for (ItemPath path : paths) {
if (path.equivalent(pathToBeFound)) {
return true;
}
}
return false;
}
public static boolean containsSubpathOrEquivalent(Collection<ItemPath> paths, ItemPath pathToBeFound) {
for (ItemPath path : paths) {
CompareResult r = pathToBeFound.compareComplex(path);
if (r == CompareResult.SUBPATH || r == CompareResult.EQUIVALENT) {
return true;
}
}
return false;
}
public ItemPath namedSegmentsOnly() {
ItemPath rv = new ItemPath();
for (ItemPathSegment segment : segments) {
if (segment instanceof NameItemPathSegment) {
rv.add(((NameItemPathSegment) segment).getName());
}
}
return rv;
}
public static boolean isNullOrEmpty(ItemPath itemPath) {
return itemPath == null || itemPath.isEmpty();
}
public static boolean containsSingleNameSegment(ItemPath path) {
return path != null && path.size() == 1 && path.first() instanceof NameItemPathSegment;
}
public boolean startsWith(Class<? extends ItemPathSegment> clazz) {
if (isEmpty()) {
return false;
} else {
return clazz.isAssignableFrom(first().getClass());
}
}
public boolean startsWith(ItemPath other) {
if (other == null) {
return true;
}
return other.isSubPathOrEquivalent(this);
}
public boolean startsWithName(QName name) {
if (!isEmpty() && startsWith(NameItemPathSegment.class)) {
return QNameUtil.match(name, ((NameItemPathSegment) first()).getName());
} else {
return false;
}
}
public QName asSingleName() {
if (size() == 1 && startsWith(NameItemPathSegment.class)) {
return ((NameItemPathSegment) first()).getName();
} else {
return null;
}
}
public static QName asSingleName(ItemPath path) {
return path != null ? path.asSingleName() : null;
}
public static ItemPath[] asPathArray(QName... names) {
ItemPath[] paths = new ItemPath[names.length];
int i = 0;
for (QName name : names) {
paths[i++] = new ItemPath(name);
}
return paths;
}
public enum CompareResult {
EQUIVALENT,
SUPERPATH,
SUBPATH,
NO_RELATION;
}
public boolean isSubPath(ItemPath otherPath) {
return compareComplex(otherPath) == CompareResult.SUBPATH;
}
public boolean isSuperPath(ItemPath otherPath) {
return compareComplex(otherPath) == CompareResult.SUPERPATH;
}
public boolean isSubPathOrEquivalent(ItemPath otherPath) {
CompareResult result = compareComplex(otherPath);
return result == CompareResult.SUBPATH || result == CompareResult.EQUIVALENT;
}
/**
* Compares two paths semantically.
*
* @param otherPath
* @return
*/
public boolean equivalent(ItemPath otherPath) {
return compareComplex(otherPath) == CompareResult.EQUIVALENT;
}
public ItemPath substract(ItemPath otherPath) {
// return remainder(otherPath); // the code seems to be equivalent to the one of remainder()
ItemPath thisNormalized = this.normalize();
ItemPath otherNormalized = otherPath.normalize();
if (thisNormalized.size() < otherNormalized.size()) {
throw new IllegalArgumentException("Cannot substract path '"+otherPath+"' from '"+this+"' because it is not a subset");
}
int i = 0;
while (i < otherNormalized.segments.size()) {
ItemPathSegment thisSegment = thisNormalized.segments.get(i);
ItemPathSegment otherSegment = otherNormalized.segments.get(i);
if (!thisSegment.equivalent(otherSegment)) {
throw new IllegalArgumentException("Cannot subtract segment '"+otherSegment+"' from path '"+this+
"' because it does not contain corresponding segment; it has '"+thisSegment+"' instead.");
}
i++;
}
List<ItemPathSegment> substractSegments = thisNormalized.segments.subList(i, thisNormalized.segments.size());
return new ItemPath(substractSegments);
}
/**
* Returns the remainder of "this" path after passing all segments from the other path.
* (I.e. this path must begin with the content of the other path. Throws an exception when
* it is not the case.)
*
* @param otherPath
* @return
*/
public ItemPath remainder(ItemPath otherPath) {
ItemPath thisNormalized = this.normalize();
ItemPath otherNormalized = otherPath.normalize();
if (thisNormalized.size() < otherNormalized.size()) {
throw new IllegalArgumentException("Cannot compute remainder of path '"+this+"' after '"+otherPath+"' because this path is not a superset");
}
int i = 0;
while (i < otherNormalized.segments.size()) {
ItemPathSegment thisSegment = thisNormalized.segments.get(i);
ItemPathSegment otherSegment = otherNormalized.segments.get(i);
if (!thisSegment.equivalent(otherSegment)) {
throw new IllegalArgumentException("Cannot subtract segment '"+otherSegment+"' from path '"+this+
"' because it does not contain corresponding segment; it has '"+thisSegment+"' instead.");
}
i++;
}
List<ItemPathSegment> substractSegments = thisNormalized.segments.subList(i, thisNormalized.segments.size());
return new ItemPath(substractSegments);
}
/**
* Convenience static method with checks
* @throw IllegalArgumentException
*/
public static QName getName(ItemPathSegment segment) {
if (segment == null) {
return null;
}
if (!(segment instanceof NameItemPathSegment)) {
throw new IllegalArgumentException("Unable to get name from non-name path segment "+segment);
}
return ((NameItemPathSegment)segment).getName();
}
public static IdItemPathSegment getFirstIdSegment(ItemPath itemPath) {
ItemPathSegment first = itemPath.first();
if (first instanceof IdItemPathSegment) {
return (IdItemPathSegment)first;
}
return null;
}
public static NameItemPathSegment getFirstNameSegment(ItemPath itemPath) {
if (itemPath == null) {
return null;
}
ItemPathSegment first = itemPath.first();
if (first instanceof NameItemPathSegment) {
return (NameItemPathSegment)first;
}
if (first instanceof IdItemPathSegment) {
return getFirstNameSegment(itemPath.rest());
}
return null;
}
public static QName getFirstName(ItemPath itemPath) {
NameItemPathSegment nameSegment = getFirstNameSegment(itemPath);
if (nameSegment == null) {
return null;
}
return nameSegment.getName();
}
public static ItemPath pathRestStartingWithName(ItemPath path) {
ItemPathSegment pathSegment = path.first();
if (pathSegment instanceof NameItemPathSegment) {
return path;
} else if (pathSegment instanceof IdItemPathSegment) {
return path.rest();
} else {
throw new IllegalArgumentException("Unexpected path segment "+pathSegment);
}
}
public boolean containsName(QName name) {
for (ItemPathSegment segment: segments) {
if (segment instanceof NameItemPathSegment && ((NameItemPathSegment)segment).getName().equals(name)) {
return true;
}
}
return false;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
Iterator<ItemPathSegment> iterator = segments.iterator();
while (iterator.hasNext()) {
sb.append(iterator.next());
if (iterator.hasNext()) {
sb.append("/");
}
}
return sb.toString();
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((segments == null) ? 0 : segments.hashCode());
return result;
}
public boolean equals(Object obj, boolean exact) {
if (exact) {
return equals(obj);
} else {
return obj instanceof ItemPath && equivalent((ItemPath) obj);
}
}
/**
* More strict version of ItemPath comparison. Does not use any normalization
* nor approximate matching QNames via QNameUtil.match.
*
* For semantic-level comparison, please use equivalent(..) method.
*
* @param obj
* @return
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ItemPath other = (ItemPath) obj;
if (segments == null) {
if (other.segments != null)
return false;
} else if (!segments.equals(other.segments))
return false;
return true;
}
public ItemPath clone() {
ItemPath clone = new ItemPath();
for (ItemPathSegment segment : segments) {
clone.segments.add(segment.clone());
}
if (namespaceMap != null) {
clone.namespaceMap = new HashMap<>(namespaceMap);
}
return clone;
}
public static boolean containsSpecialSymbols(ItemPath path) {
return path != null && path.containsSpecialSymbols();
}
public boolean containsSpecialSymbols() {
for (ItemPathSegment segment : segments) {
if (segment instanceof ReferencePathSegment || segment instanceof IdentifierPathSegment) {
return true;
}
}
return false;
}
public static void checkNoReferences(ItemPath path) {
if (containsSpecialSymbols(path)) {
throw new IllegalStateException("Item path shouldn't contain references but it does: " + path);
}
}
}