/
XNodeProcessor.java
1209 lines (1089 loc) · 55.3 KB
/
XNodeProcessor.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
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2014 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.parser;
import java.util.Collection;
import java.util.Map.Entry;
import javax.xml.bind.JAXBElement;
import javax.xml.namespace.QName;
import com.evolveum.midpoint.prism.PrismConstants;
import com.evolveum.midpoint.util.JAXBUtil;
import com.evolveum.prism.xml.ns._public.query_3.SearchFilterType;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.Validate;
import org.w3c.dom.Element;
import com.evolveum.midpoint.prism.Containerable;
import com.evolveum.midpoint.prism.Item;
import com.evolveum.midpoint.prism.ItemDefinition;
import com.evolveum.midpoint.prism.Objectable;
import com.evolveum.midpoint.prism.PrismContainer;
import com.evolveum.midpoint.prism.PrismContainerDefinition;
import com.evolveum.midpoint.prism.PrismContainerValue;
import com.evolveum.midpoint.prism.PrismContext;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.PrismObjectDefinition;
import com.evolveum.midpoint.prism.PrismProperty;
import com.evolveum.midpoint.prism.PrismPropertyDefinition;
import com.evolveum.midpoint.prism.PrismPropertyValue;
import com.evolveum.midpoint.prism.PrismReference;
import com.evolveum.midpoint.prism.PrismReferenceDefinition;
import com.evolveum.midpoint.prism.PrismReferenceValue;
import com.evolveum.midpoint.prism.PrismValue;
import com.evolveum.midpoint.prism.parser.util.XNodeProcessorUtil;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.prism.polystring.PolyString;
import com.evolveum.midpoint.prism.schema.SchemaRegistry;
import com.evolveum.midpoint.prism.util.PrismUtil;
import com.evolveum.midpoint.prism.xnode.ListXNode;
import com.evolveum.midpoint.prism.xnode.MapXNode;
import com.evolveum.midpoint.prism.xnode.PrimitiveXNode;
import com.evolveum.midpoint.prism.xnode.RootXNode;
import com.evolveum.midpoint.prism.xnode.SchemaXNode;
import com.evolveum.midpoint.prism.xnode.XNode;
import com.evolveum.midpoint.util.DOMUtil;
import com.evolveum.midpoint.util.DisplayableValue;
import com.evolveum.midpoint.util.QNameUtil;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.util.exception.SystemException;
import com.evolveum.prism.xml.ns._public.types_3.ItemPathType;
import com.evolveum.prism.xml.ns._public.types_3.PolyStringType;
import com.evolveum.prism.xml.ns._public.types_3.ProtectedByteArrayType;
import com.evolveum.prism.xml.ns._public.types_3.ProtectedStringType;
import com.evolveum.prism.xml.ns._public.types_3.SchemaDefinitionType;
public class XNodeProcessor {
public static final String ARTIFICIAL_OBJECT_NAME = "anObject";
private PrismContext prismContext;
public XNodeProcessor() { }
public XNodeProcessor(PrismContext prismContext) {
this.prismContext = prismContext;
}
public SchemaRegistry getSchemaRegistry() {
return prismContext.getSchemaRegistry();
}
public PrismContext getPrismContext() {
return prismContext;
}
public void setPrismContext(PrismContext prismContext) {
this.prismContext = prismContext;
}
//region Parsing prism objects
public <O extends Objectable> PrismObject<O> parseObject(XNode xnode) throws SchemaException {
if (xnode instanceof RootXNode) {
return parseObject((RootXNode)xnode);
} else if (xnode instanceof MapXNode) {
return parseObject((MapXNode)xnode);
} else {
throw new IllegalArgumentException("Cannot parse object from "+xnode);
}
}
public <O extends Objectable> PrismObject<O> parseObject(RootXNode rootXnode) throws SchemaException {
QName rootElementName = rootXnode.getRootElementName();
PrismObjectDefinition<O> objectDefinition = null;
if (rootXnode.getTypeQName() != null) {
objectDefinition = getSchemaRegistry().findObjectDefinitionByType(rootXnode.getTypeQName());
if (objectDefinition == null) {
throw new SchemaException("No object definition for type "+rootXnode.getTypeQName());
}
} else {
objectDefinition = getSchemaRegistry().findObjectDefinitionByElementName(rootElementName);
if (objectDefinition == null) {
throw new SchemaException("No object definition for element name "+rootElementName);
}
}
if (objectDefinition == null) {
throw new SchemaException("Cannot locate object definition (unspecified reason)");
}
XNode subnode = rootXnode.getSubnode();
if (!(subnode instanceof MapXNode)) {
throw new IllegalArgumentException("Cannot parse object from "+subnode.getClass().getSimpleName()+", we need a map");
}
return parseObject((MapXNode)subnode, rootElementName, objectDefinition);
}
public <O extends Objectable> PrismObject<O> parseObject(MapXNode xmap) throws SchemaException {
// There is no top-level element to detect type. We have only one chance ...
QName typeQName = xmap.getTypeQName();
if (typeQName == null) {
throw new SchemaException("No type specified in top-level xnode, cannot determine object type");
}
PrismObjectDefinition<O> objectDefinition = getSchemaRegistry().findObjectDefinitionByType(typeQName);
return parseObject(xmap, objectDefinition);
}
private <O extends Objectable> PrismObject<O> parseObject(MapXNode xnode, PrismObjectDefinition<O> objectDefinition) throws SchemaException {
QName elementName;
if (objectDefinition != null) {
elementName = objectDefinition.getName();
} else {
elementName = new QName(null, ARTIFICIAL_OBJECT_NAME);
}
return parseObject(xnode, elementName, objectDefinition);
}
private <O extends Objectable> PrismObject<O> parseObject(XNode xnode, QName elementName, PrismObjectDefinition<O> objectDefinition) throws SchemaException {
if (xnode instanceof RootXNode) {
return parseObject(((RootXNode) xnode).getSubnode(), elementName, objectDefinition); // elementName from RootXNode is silently ignored
} else if (xnode instanceof MapXNode) {
return parseObject((MapXNode)xnode, elementName, objectDefinition);
} else {
throw new IllegalArgumentException("Cannot parse object from "+xnode);
}
}
private <O extends Objectable> PrismObject<O> parseObject(MapXNode xnode, QName elementName, PrismObjectDefinition<O> objectDefinition) throws SchemaException {
PrismObject<O> object = (PrismObject<O>) parseContainerFromMapInternal(xnode, elementName, objectDefinition,
QNameUtil.createCollection(XNode.KEY_OID, XNode.KEY_VERSION));
object.setOid(getOid(xnode));
object.setVersion(getVersion(xnode));
return object;
}
private String getOid(MapXNode xmap) throws SchemaException {
return xmap.getParsedPrimitiveValue(XNode.KEY_OID, DOMUtil.XSD_STRING);
}
private String getVersion(MapXNode xmap) throws SchemaException {
return xmap.getParsedPrimitiveValue(XNode.KEY_VERSION, DOMUtil.XSD_STRING);
}
//endregion
//region Parsing prism containers
public <C extends Containerable> PrismContainer<C> parseContainer(XNode xnode) throws SchemaException {
Validate.notNull(xnode);
if (xnode instanceof RootXNode) {
return parseContainer((RootXNode) xnode);
} else if (xnode.getTypeQName() != null) {
PrismContainerDefinition<C> definition = getSchemaRegistry().findContainerDefinitionByType(xnode.getTypeQName());
if (definition == null) {
throw new SchemaException("No container definition for type "+xnode.getTypeQName());
}
return parseContainer(xnode, definition);
} else {
throw new SchemaException("Couldn't parse container because no element name nor type name is known");
}
}
public <C extends Containerable> PrismContainer<C> parseContainer(RootXNode rootXnode) throws SchemaException {
Validate.notNull(rootXnode);
QName rootElementName = rootXnode.getRootElementName();
PrismContainerDefinition<C> definition;
if (rootXnode.getTypeQName() != null) {
definition = getSchemaRegistry().findContainerDefinitionByType(rootXnode.getTypeQName());
if (definition == null) {
throw new SchemaException("No container definition for type "+rootXnode.getTypeQName());
}
} else {
definition = getSchemaRegistry().findContainerDefinitionByElementName(rootElementName);
if (definition == null) {
throw new SchemaException("No container definition for element name "+rootElementName);
}
}
XNode subnode = rootXnode.getSubnode();
if (!(subnode instanceof MapXNode)) {
throw new IllegalArgumentException("Cannot parse object from "+subnode.getClass().getSimpleName()+", we need a map");
}
return parseContainerInternal(subnode, rootElementName, definition);
}
public <C extends Containerable> PrismContainer<C> parseContainer(XNode xnode, Class<C> type) throws SchemaException {
Validate.notNull(xnode);
Validate.notNull(type);
PrismContainerDefinition<C> definition = getSchemaRegistry().findContainerDefinitionByCompileTimeClass(type);
if (definition == null) {
throw new SchemaException("No container definition for class " + type);
}
return parseContainer(xnode, definition);
}
public <C extends Containerable> PrismContainer<C> parseContainer(XNode xnode, PrismContainerDefinition<C> definition) throws SchemaException {
Validate.notNull(xnode);
Validate.notNull(definition);
if (xnode instanceof RootXNode) {
RootXNode xroot = (RootXNode)xnode;
return parseContainerInternal(xroot.getSubnode(), xroot.getRootElementName(), definition);
} else if (xnode instanceof MapXNode) {
return parseContainerInternal(xnode, definition.getName(), definition);
} else {
throw new SchemaException("Cannot parse container from "+xnode);
}
}
private <C extends Containerable> PrismContainer<C> parseContainerInternal(XNode xnode, QName elementName,
PrismContainerDefinition<C> containerDef) throws SchemaException {
if (xnode instanceof RootXNode) {
RootXNode rootXnode = (RootXNode) xnode;
return parseContainerInternal(rootXnode.getSubnode(), rootXnode.getRootElementName(), containerDef); // ignoring elementName from parameters (probably set to be root element name)
} else if (xnode instanceof MapXNode) {
return parseContainerFromMapInternal((MapXNode) xnode, elementName, containerDef, null);
} else if (xnode instanceof ListXNode) {
PrismContainer<C> container = containerDef.instantiate(elementName);
for (XNode xsubnode: (ListXNode)xnode) {
PrismContainerValue<C> containerValue = parsePrismContainerValue(xsubnode, containerDef);
container.add(containerValue);
}
return container;
} else if (xnode instanceof PrimitiveXNode<?>) {
PrimitiveXNode<?> xprim = (PrimitiveXNode<?>)xnode;
if (xprim.isEmpty()) {
PrismContainer<C> container = containerDef.instantiate(elementName);
container.createNewValue(); // empty primitive node corresponds actually to empty container VALUE, not empty (value-less) container ITEM
return container;
} else {
throw new IllegalArgumentException("Cannot parse container from (non-empty) "+xnode);
}
} else {
throw new IllegalArgumentException("Cannot parse container from "+xnode);
}
}
private <C extends Containerable> PrismContainer<C> parseContainerFromMapInternal(MapXNode xmap, QName elementName,
PrismContainerDefinition<C> containerDef, Collection<QName> ignoredItems) throws SchemaException {
PrismContainer<C> container = containerDef.instantiate(elementName);
PrismContainerValue<C> cval = parsePrismContainerValueFromMap(xmap, containerDef, ignoredItems);
container.add(cval);
return container;
}
public <C extends Containerable> PrismContainerValue<C> parsePrismContainerValue(XNode xnode, PrismContainerDefinition<C> containerDef)
throws SchemaException {
if (xnode instanceof MapXNode) {
return parsePrismContainerValueFromMap((MapXNode)xnode, containerDef, null);
} else if (xnode instanceof PrimitiveXNode) {
PrimitiveXNode<?> xprim = (PrimitiveXNode<?>)xnode;
if (xprim.isEmpty()) {
PrismContainer<C> container = containerDef.instantiate();
PrismContainerValue pcv = container.createNewValue();
pcv.setParent(null);
return pcv;
} else {
throw new IllegalArgumentException("Cannot parse container value from (non-empty) "+xnode);
}
} else {
throw new IllegalArgumentException("Cannot parse container value from "+xnode);
}
}
private <C extends Containerable> PrismContainerValue<C> parsePrismContainerValueFromMap(MapXNode xmap, PrismContainerDefinition<C> containerDef,
Collection<QName> ignoredItems) throws SchemaException {
Long id = getContainerId(xmap);
PrismContainerValue<C> cval = new PrismContainerValue<C>(null, null, null, id, xmap.getTypeQName(), prismContext);
for (Entry<QName,XNode> xentry: xmap.entrySet()) {
QName itemQName = xentry.getKey();
if (QNameUtil.match(itemQName, XNode.KEY_CONTAINER_ID)) {
continue;
}
if (QNameUtil.matchAny(itemQName, ignoredItems)) {
continue;
}
ItemDefinition itemDef = locateItemDefinition(containerDef, itemQName, xentry.getValue());
if (itemDef == null) {
if (containerDef.isRuntimeSchema()) {
// No definition for item, but the schema is runtime. the definition may come later.
// Null is OK here.
} else {
throw new SchemaException("Item " + itemQName + " has no definition (in container "+containerDef+")" + "while parsing " + xmap.debugDump(), itemQName);
}
}
Item<?> item = parseItem(xentry.getValue(), itemQName, itemDef);
// Merge must be here, not just add. Some items (e.g. references) have alternative
// names and representations and these cannot be processed as one map or list
if (item != null) {
cval.merge(item);
}
}
return cval;
}
private Long getContainerId(MapXNode xmap) throws SchemaException {
return xmap.getParsedPrimitiveValue(XNode.KEY_CONTAINER_ID, DOMUtil.XSD_LONG);
}
//endregion
//region Parsing prism properties
private <T> PrismProperty<T> parsePrismProperty(XNode xnode, QName propName,
PrismPropertyDefinition<T> propertyDefinition) throws SchemaException {
Validate.notNull(propertyDefinition);
if (xnode == null){
return propertyDefinition.instantiate();
} else if (xnode instanceof ListXNode) {
return parsePrismPropertyFromList((ListXNode)xnode, propName, propertyDefinition);
} else if (xnode instanceof MapXNode) {
return parsePrismPropertyFromMap((MapXNode)xnode, propName, propertyDefinition);
} else if (xnode instanceof PrimitiveXNode<?>) {
return parsePrismPropertyFromPrimitive((PrimitiveXNode)xnode, propName, propertyDefinition);
} else if (xnode instanceof SchemaXNode){
return parsePrismPropertyFromSchema((SchemaXNode) xnode, propName, propertyDefinition);
} else if (xnode instanceof RootXNode) {
return parsePrismProperty(((RootXNode) xnode).getSubnode(), propName, propertyDefinition); // todo what about possibly lost type name of root node?
} else {
throw new IllegalArgumentException("Cannot parse property from " + xnode);
}
}
private <T> PrismProperty<T> parsePrismPropertyFromSchema(SchemaXNode xnode, QName propName,
PrismPropertyDefinition<T> propertyDefinition) throws SchemaException {
PrismProperty prop = propertyDefinition.instantiate();
SchemaDefinitionType schemaDefType = getPrismContext().getBeanConverter().unmarshalSchemaDefinitionType((SchemaXNode) xnode);
PrismPropertyValue<SchemaDefinitionType> val = new PrismPropertyValue<>(schemaDefType);
prop.add(val);
return prop;
}
private <T> PrismProperty<T> parsePrismPropertyFromList(ListXNode xlist, QName propName,
PrismPropertyDefinition<T> propertyDefinition) throws SchemaException {
if (xlist == null || xlist.isEmpty()) {
return null;
}
PrismProperty<T> prop = propertyDefinition.instantiate(propName);
if (!propertyDefinition.isMultiValue() && xlist.size() > 1) {
throw new SchemaException("Attempt to store multiple values in single-valued property " + propName);
}
for (XNode xsubnode : xlist) {
PrismPropertyValue<T> pval = parsePrismPropertyValue(xsubnode, prop);
if (pval != null) {
prop.add(pval);
}
}
return prop;
}
private <T> PrismProperty<T> parsePrismPropertyFromMap(MapXNode xmap, QName propName,
PrismPropertyDefinition<T> propertyDefinition) throws SchemaException {
PrismProperty<T> prop = propertyDefinition.instantiate(propName);
PrismPropertyValue<T> pval = parsePrismPropertyValue(xmap, prop);
if (pval != null) {
prop.add(pval);
}
return prop;
}
private <T> PrismProperty<T> parsePrismPropertyFromPrimitive(PrimitiveXNode<T> xprim, QName propName,
PrismPropertyDefinition<T> propertyDefinition) throws SchemaException {
PrismProperty<T> prop = propertyDefinition.instantiate(propName);
PrismPropertyValue<T> pval = parsePrismPropertyValue(xprim, prop);
if (pval != null) {
prop.add(pval);
}
return prop;
}
private <T> PrismPropertyValue<T> parsePrismPropertyValue(XNode xnode, PrismProperty<T> property) throws SchemaException {
Validate.notNull(xnode);
Validate.notNull(property);
T realValue = parsePrismPropertyRealValue(xnode, property.getDefinition());
if (realValue == null) {
return null;
}
return new PrismPropertyValue<T>(realValue);
}
public <T> T parsePrismPropertyRealValue(XNode xnode, PrismPropertyDefinition<T> propertyDef) throws SchemaException {
if (xnode instanceof PrimitiveXNode<?>) {
return parseAtomicValueFromPrimitive((PrimitiveXNode<T>) xnode, propertyDef, propertyDef.getTypeName());
} else if (xnode instanceof MapXNode) {
return parsePrismPropertyRealValueFromMap((MapXNode)xnode, null, propertyDef);
} else {
throw new IllegalArgumentException("Cannot parse property value from "+xnode);
}
}
/**
* Parses an atomic value - i.e. something that could present a property value, if such a property would exist.
*/
public <T> T parseAtomicValue(XNode xnode, QName typeName) throws SchemaException {
if (xnode instanceof RootXNode) {
return parseAtomicValue(((RootXNode) xnode).getSubnode(), typeName);
} else if (xnode instanceof PrimitiveXNode<?>) {
return parseAtomicValueFromPrimitive((PrimitiveXNode<T>) xnode, typeName);
} else if (xnode instanceof MapXNode) {
return parsePrismPropertyRealValueFromMap((MapXNode)xnode, typeName, null);
} else {
throw new IllegalArgumentException("Cannot parse atomic value from "+xnode);
}
}
private <T> T parseAtomicValueFromPrimitive(PrimitiveXNode<T> xprim, QName typeName) throws SchemaException {
return parseAtomicValueFromPrimitive(xprim, null, typeName);
}
private <T> T parseAtomicValueFromPrimitive(PrimitiveXNode<T> xprim, PrismPropertyDefinition def, QName typeName) throws SchemaException {
T realValue = null;
if (ItemPathType.COMPLEX_TYPE.equals(typeName)){
return (T) parseItemPathType(xprim);
} else if (ProtectedStringType.COMPLEX_TYPE.equals(typeName)){
return (T) parseProtectedTypeFromPrimitive(xprim);
} else
if (prismContext.getBeanConverter().canProcess(typeName) && !typeName.equals(PolyStringType.COMPLEX_TYPE) && !typeName.equals(ItemPathType.COMPLEX_TYPE)) {
// Primitive elements may also have complex Java representations (e.g. enums)
return prismContext.getBeanConverter().unmarshallPrimitive(xprim, typeName);
} else if (def != null && def.isRuntimeSchema() && def.getAllowedValues() != null && def.getAllowedValues().size() > 0){
//TODO: ugly hack to support enum in extension schemas --- need to be fixed
realValue = xprim.getParsedValue(DOMUtil.XSD_STRING);
if (!isAllowed(realValue, def.getAllowedValues())){
throw new SchemaException("Illegal value found in property "+xprim+". Allowed values are: "+ def.getAllowedValues());
}
} else {
realValue = xprim.getParsedValue(typeName);
}
if (realValue == null) {
return realValue;
}
if (realValue instanceof PolyStringType) {
PolyStringType polyStringType = (PolyStringType)realValue;
realValue = (T) new PolyString(polyStringType.getOrig(), polyStringType.getNorm());
}
if (!(realValue instanceof PolyString) && typeName.equals(PolyStringType.COMPLEX_TYPE)){
String val = (String) realValue;
realValue = (T) new PolyString(val);
}
PrismUtil.recomputeRealValue(realValue, prismContext);
return realValue;
}
private <T> boolean isAllowed(T realValue, Collection<DisplayableValue<T>> collection){
for (DisplayableValue<T> o : collection){
if (realValue.equals(o.getValue())){
return true;
}
}
return false;
}
private ProtectedStringType parseProtectedTypeFromPrimitive(PrimitiveXNode xPrim) throws SchemaException{
String clearValue = (String) xPrim.getParsedValue(DOMUtil.XSD_STRING);
ProtectedStringType protectedString = new ProtectedStringType();
protectedString.setClearValue(clearValue);
return protectedString;
}
/**
* This method is called either with a type name only, or with a property definition only, or with both.
* Property definition is useful to correctly formulate exception message.
*/
private <T> T parsePrismPropertyRealValueFromMap(MapXNode xmap, QName typeName, PrismPropertyDefinition<T> propertyDefinition) throws SchemaException {
if (typeName == null) {
if (propertyDefinition == null) {
throw new IllegalArgumentException("Couldn't parse prism property real value because of missing type name and property definition");
}
typeName = propertyDefinition.getTypeName();
}
if (prismContext.getBeanConverter().canProcess(typeName)) {
return prismContext.getBeanConverter().unmarshall(xmap, typeName);
} else {
if (propertyDefinition != null) {
if (propertyDefinition.isRuntimeSchema()) {
throw new SchemaException("Complex run-time properties are not supported: type "+typeName+" from "+xmap);
} else {
throw new SystemException("Cannot parse compile-time property "+propertyDefinition.getName()+" type "+typeName+" from "+xmap);
}
} else {
throw new SchemaException("Couldn't parse property real value with type "+typeName+" from "+xmap);
}
}
}
public PrismPropertyValue parsePrismPropertyFromGlobalXNodeValue(Entry<QName, XNode> entry) throws SchemaException {
Validate.notNull(entry);
QName globalElementName = entry.getKey();
if (globalElementName == null) {
throw new SchemaException("No global element name to look for");
}
ItemDefinition itemDefinition = prismContext.getSchemaRegistry().resolveGlobalItemDefinition(globalElementName);
if (itemDefinition == null) {
throw new SchemaException("No definition for item " + globalElementName);
}
if (itemDefinition instanceof PrismPropertyDefinition) {
PrismProperty prismProperty = parsePrismProperty(entry.getValue(), globalElementName, (PrismPropertyDefinition) itemDefinition);
if (prismProperty.size() > 1) {
throw new SchemaException("Retrieved more than one value from globally defined element " + globalElementName);
} else if (prismProperty.size() == 0) {
return null;
} else {
return (PrismPropertyValue) prismProperty.getValues().get(0);
}
} else {
throw new IllegalArgumentException("Parsing global elements with definitions other than PrismPropertyDefinition is not supported yet: element = " + globalElementName + " definition kind = " + itemDefinition.getClass().getSimpleName());
}
}
private ItemPathType parseItemPathType(PrimitiveXNode itemPath) throws SchemaException{
ItemPath path = (ItemPath) itemPath.getParsedValue(ItemPath.XSD_TYPE);
ItemPathType itemPathType = new ItemPathType(path);
return itemPathType;
}
// private <T> void parseProtectedType(ProtectedDataType<T> protectedType, MapXNode xmap) throws SchemaException {
// XNode xEncryptedData = xmap.get(ProtectedDataType.F_ENCRYPTED_DATA);
// if (xEncryptedData != null) {
// if (!(xEncryptedData instanceof MapXNode)) {
// throw new SchemaException("Cannot parse encryptedData from "+xEncryptedData);
// }
// EncryptedDataType encryptedDataType = prismContext.getBeanConverter().unmarshall((MapXNode)xEncryptedData, EncryptedDataType.class);
// protectedType.setEncryptedData(encryptedDataType);
// } else {
// // Check for legacy EncryptedData
// XNode xLegacyEncryptedData = xmap.get(ProtectedDataType.F_XML_ENC_ENCRYPTED_DATA);
// if (xLegacyEncryptedData != null) {
// if (!(xLegacyEncryptedData instanceof MapXNode)) {
// throw new SchemaException("Cannot parse EncryptedData from "+xEncryptedData);
// }
// MapXNode xConvertedEncryptedData = (MapXNode) xLegacyEncryptedData.cloneTransformKeys(new Transformer<QName>() {
// @Override
// public QName transform(QName in) {
// String elementName = StringUtils.uncapitalize(in.getLocalPart());
// if (elementName.equals("type")) {
// // this is rubbish, we don't need it, we don't want it
// return null;
// }
// return new QName(null, elementName);
// }
// });
// EncryptedDataType encryptedDataType = prismContext.getBeanConverter().unmarshall(xConvertedEncryptedData, EncryptedDataType.class);
// protectedType.setEncryptedData(encryptedDataType);
// }
// }
// // protected data empty..check for clear value
// if (protectedType.isEmpty()){
// XNode xClearValue = xmap.get(ProtectedDataType.F_CLEAR_VALUE);
// if (xClearValue == null){
// //TODO: try to use common namespace (only to be compatible with previous versions)
// //FIXME maybe add some warning, info...
// xClearValue = xmap.get(new QName(ProtectedDataType.F_CLEAR_VALUE.getLocalPart()));
// }
// if (xClearValue == null){
// return;
// }
// if (!(xClearValue instanceof PrimitiveXNode)){
// //this is maybe not good..
// throw new SchemaException("Cannot parse clear value from " + xClearValue);
// }
// // TODO: clearValue
// T clearValue = (T) ((PrimitiveXNode)xClearValue).getParsedValue(DOMUtil.XSD_STRING);
// protectedType.setClearValue(clearValue);
// }
//
// }
public static <T> PrismProperty<T> parsePrismPropertyRaw(XNode xnode, QName itemName, PrismContext prismContext)
throws SchemaException {
if (xnode instanceof ListXNode) {
return parsePrismPropertyRaw((ListXNode)xnode, itemName, prismContext);
} else {
PrismProperty<T> property = new PrismProperty<T>(itemName, prismContext);
PrismPropertyValue<T> pval = PrismPropertyValue.createRaw(xnode);
property.add(pval);
return property;
}
}
private static <T> PrismProperty<T> parsePrismPropertyRaw(ListXNode xlist, QName itemName, PrismContext prismContext)
throws SchemaException {
PrismProperty<T> property = new PrismProperty<T>(itemName, prismContext);
for (XNode xsubnode : xlist) {
PrismPropertyValue<T> pval = PrismPropertyValue.createRaw(xsubnode);
property.add(pval);
}
return property;
}
//endregion
//region Parsing prism references
public PrismReference parsePrismReference(XNode xnode, QName itemName,
PrismReferenceDefinition referenceDefinition) throws SchemaException {
if (xnode instanceof ListXNode) {
return parsePrismReferenceFromList((ListXNode)xnode, itemName, referenceDefinition);
} else if (xnode instanceof MapXNode) {
return parsePrismReferenceFromMap((MapXNode)xnode, itemName, referenceDefinition);
} else if (xnode instanceof PrimitiveXNode){
return referenceDefinition.instantiate();
} else {
throw new IllegalArgumentException("Cannot parse reference from "+xnode);
}
}
private PrismReference parsePrismReferenceFromList(ListXNode xlist, QName itemName,
PrismReferenceDefinition referenceDefinition) throws SchemaException {
if (xlist == null || xlist.isEmpty()) {
return null;
}
PrismReference ref = referenceDefinition.instantiate();
if (!referenceDefinition.isMultiValue() && xlist.size() > 1) {
throw new SchemaException("Attempt to store multiple values in single-valued reference " + itemName);
}
for (XNode subnode : xlist) {
parsePrismReferenceValueFromXNode(ref, subnode, referenceDefinition, itemName);
}
return ref;
}
private PrismReference parsePrismReferenceFromMap(MapXNode xmap, QName itemName,
PrismReferenceDefinition referenceDefinition) throws SchemaException {
PrismReference ref = referenceDefinition.instantiate();
parsePrismReferenceValueFromXNode(ref, xmap, referenceDefinition, itemName);
return ref;
}
private void parsePrismReferenceValueFromXNode(PrismReference ref, XNode subnode, PrismReferenceDefinition referenceDefinition, QName itemName) throws SchemaException {
/*
* We distinguish between "real" references and composite objects by
* (1) looking at type QName of XNode passed (whether it's ObjectType or ObjectReferenceType)
* (2) comparing itemName and name from reference definition - e.g. linkRef vs. link
*/
boolean isComposite;
if (subnode.getTypeQName() != null) {
QName typeName = subnode.getTypeQName();
if (prismContext != null) {
ItemDefinition definition = prismContext.getSchemaRegistry().findItemDefinitionByType(typeName);
isComposite = definition instanceof PrismObjectDefinition;
} else {
isComposite = PrismConstants.REFERENCE_TYPE_NAME.equals(typeName.getLocalPart());
}
} else {
isComposite = !QNameUtil.match(itemName, referenceDefinition.getName());
}
if (isComposite) {
// This is a composite object (complete object stored inside
// reference)
ref.add(parseReferenceAsCompositeObject(subnode, referenceDefinition));
} else {
// This is "real" reference (oid type and nothing more)
ref.add(parseReferenceValue(subnode, referenceDefinition));
}
}
public PrismReferenceValue parseReferenceValue(XNode xnode, PrismReferenceDefinition referenceDefinition) throws SchemaException {
if (xnode instanceof MapXNode) {
return parseReferenceValue((MapXNode)xnode, referenceDefinition);
} else {
throw new IllegalArgumentException("Cannot parse reference from "+xnode);
}
}
public PrismReferenceValue parseReferenceValue(MapXNode xmap, PrismReferenceDefinition referenceDefinition) throws SchemaException {
String oid = xmap.getParsedPrimitiveValue(XNode.KEY_REFERENCE_OID, DOMUtil.XSD_STRING);
PrismReferenceValue refVal = new PrismReferenceValue(oid);
QName type = xmap.getParsedPrimitiveValue(XNode.KEY_REFERENCE_TYPE, DOMUtil.XSD_QNAME);
if (type == null) {
type = referenceDefinition.getTargetTypeName();
if (type == null) {
throw new SchemaException("Target type specified neither in reference nor in the schema");
}
} else {
if (StringUtils.isBlank(type.getNamespaceURI())) {
// resolve type without namespace (only when prismContext is known)
if (prismContext == null) {
throw new SchemaException("Couldn't parse unqualified type name '"+type+"' without prismContext");
}
type = prismContext.getSchemaRegistry().resolveUnqualifiedTypeName(type);
}
QName defTargetType = referenceDefinition.getTargetTypeName();
if (defTargetType != null && !QNameUtil.match(defTargetType, type)) {
//one more check - if the type is not a subtype of the schema type
Class clazz = qnameToClass(type);
if (clazz == null) {
throw new SchemaException("Unknown target type: " + type);
}
if (!qnameToClass(defTargetType).isAssignableFrom(clazz)){
throw new SchemaException("Target type specified in reference ("+type+") does not match target type in schema ("+defTargetType+")");
}
}
}
PrismObjectDefinition<Objectable> objectDefinition = getSchemaRegistry().findObjectDefinitionByType(type);
if (objectDefinition == null) {
throw new SchemaException("No definition for type "+type+" in reference");
}
refVal.setTargetType(type);
QName relationAttribute = xmap.getParsedPrimitiveValue(XNode.KEY_REFERENCE_RELATION, DOMUtil.XSD_QNAME);
refVal.setRelation(relationAttribute);
refVal.setDescription((String) xmap.getParsedPrimitiveValue(XNode.KEY_REFERENCE_DESCRIPTION, DOMUtil.XSD_STRING));
refVal.setFilter(parseFilter(xmap.get(XNode.KEY_REFERENCE_FILTER)));
XNode xrefObject = xmap.get(XNode.KEY_REFERENCE_OBJECT);
if (xrefObject != null) {
if (!(xrefObject instanceof MapXNode)) {
throw new SchemaException("Cannot parse object from "+xrefObject);
}
PrismObject<Objectable> object = parseObject((MapXNode)xrefObject, objectDefinition);
setReferenceObject(refVal, object);
}
return refVal;
}
private void setReferenceObject(PrismReferenceValue refVal, PrismObject<Objectable> object) throws SchemaException {
refVal.setObject(object);
if (object.getOid() != null) {
if (refVal.getOid() == null) {
refVal.setOid(object.getOid());
} else {
if (!refVal.getOid().equals(object.getOid())) {
throw new SchemaException("OID in reference ("+refVal.getOid()+") does not match OID in composite object ("+object.getOid()+")");
}
}
}
QName objectTypeName = object.getDefinition().getTypeName();
if (refVal.getTargetType() == null) {
refVal.setTargetType(objectTypeName);
} else {
if (!refVal.getTargetType().equals(objectTypeName)) {
throw new SchemaException("Target type in reference ("+refVal.getTargetType()+") does not match OID in composite object ("+objectTypeName+")");
}
}
}
private PrismReferenceValue parseReferenceAsCompositeObject(XNode xnode,
PrismReferenceDefinition referenceDefinition) throws SchemaException {
if (xnode instanceof MapXNode) {
return parseReferenceAsCompositeObject((MapXNode)xnode, referenceDefinition);
} else {
throw new IllegalArgumentException("Cannot parse reference composite object from "+xnode);
}
}
private PrismReferenceValue parseReferenceAsCompositeObject(MapXNode xmap,
PrismReferenceDefinition referenceDefinition) throws SchemaException {
QName targetTypeName = referenceDefinition.getTargetTypeName();
PrismObjectDefinition<Objectable> objectDefinition = null;
if (xmap.getTypeQName() != null) {
objectDefinition = getSchemaRegistry().findObjectDefinitionByType(xmap.getTypeQName());
}
if (objectDefinition == null && targetTypeName != null) {
objectDefinition = prismContext.getSchemaRegistry().findObjectDefinitionByType(targetTypeName);
}
if (objectDefinition == null) {
throw new SchemaException("No object definition for composite object in reference element "
+ referenceDefinition.getCompositeObjectElementName());
}
PrismObject<Objectable> compositeObject = null;
try {
compositeObject = parseObject(xmap, objectDefinition);
} catch (SchemaException e) {
throw new SchemaException(e.getMessage() + " while parsing composite object in reference element "
+ referenceDefinition.getCompositeObjectElementName(), e);
}
PrismReferenceValue refVal = new PrismReferenceValue();
setReferenceObject(refVal, compositeObject);
referenceDefinition.setComposite(true);
return refVal;
}
private SearchFilterType parseFilter(XNode xnode) throws SchemaException {
if (xnode == null) {
return null;
}
// TODO: is this warning needed?
if (xnode.isEmpty()){
//System.out.println("Empty filter. Skipping parsing.");
return null;
}
return SearchFilterType.createFromXNode(xnode, prismContext);
}
private Class qnameToClass(QName type){
Class c = getSchemaRegistry().determineCompileTimeClass(type);
if (c == null) {
throw new IllegalStateException("No class for " + type);
}
return c;
}
//endregion
//region Resolving definitions
public <T extends Containerable> ItemDefinition locateItemDefinition(
PrismContainerDefinition<T> containerDefinition, QName elementQName, XNode xnode)
throws SchemaException {
ItemDefinition def = containerDefinition.findItemDefinition(elementQName);
if (def != null) {
return def;
}
def = resolveDynamicItemDefinition(containerDefinition, elementQName, xnode);
if (def != null) {
return def;
}
if (containerDefinition.isRuntimeSchema()) {
// Try to locate global definition in any of the schemas
def = resolveGlobalItemDefinition(containerDefinition, elementQName, xnode);
}
return def;
}
private ItemDefinition resolveDynamicItemDefinition(ItemDefinition parentDefinition, QName elementName,
XNode xnode) throws SchemaException {
if (xnode == null) {
return null;
}
QName typeName = xnode.getTypeQName();
if (typeName == null) {
if (xnode instanceof ListXNode) {
// there may be type definitions in individual list members
for (XNode subnode: ((ListXNode)xnode)) {
ItemDefinition subdef = resolveDynamicItemDefinition(parentDefinition, elementName, subnode);
// TODO: make this smarter, e.g. detect conflicting type definitions
if (subdef != null) {
return subdef;
}
}
}
}
if (typeName == null) {
return null;
}
PrismPropertyDefinition propDef = new PrismPropertyDefinition(elementName, typeName, prismContext);
Integer maxOccurs = xnode.getMaxOccurs();
if (maxOccurs != null) {
propDef.setMaxOccurs(maxOccurs);
} else {
// Make this multivalue by default, this is more "open"
propDef.setMaxOccurs(-1);
}
propDef.setDynamic(true);
return propDef;
}
private <T extends Containerable> ItemDefinition resolveGlobalItemDefinition(
PrismContainerDefinition<T> containerDefinition, QName elementQName, XNode xnode)
throws SchemaException {
return prismContext.getSchemaRegistry().resolveGlobalItemDefinition(elementQName);
}
//endregion
//region Parsing general items and beans
/**
* This gets definition of an unspecified type. It has to find the right
* method to call. Value elements have the same element name. They may be
* elements of a property or a container.
*/
public <V extends PrismValue> Item<V> parseItem(XNode xnode, QName itemName, ItemDefinition itemDef)
throws SchemaException {
if (itemDef == null) {
// Assume property in a container with runtime definition
return (Item<V>) parsePrismPropertyRaw(xnode, itemName, prismContext);
}
if (itemDef instanceof PrismObjectDefinition) {
return parseObject(xnode, itemName, (PrismObjectDefinition) itemDef);
} else if (itemDef instanceof PrismContainerDefinition) {
return (Item<V>) parseContainerInternal(xnode, itemName, (PrismContainerDefinition<?>) itemDef);
} else if (itemDef instanceof PrismPropertyDefinition) {
return (Item<V>) parsePrismProperty(xnode, itemName, (PrismPropertyDefinition) itemDef);
} else if (itemDef instanceof PrismReferenceDefinition) {
return (Item<V>) parsePrismReference(xnode, itemName, (PrismReferenceDefinition) itemDef);
} else {
throw new IllegalArgumentException("Attempt to parse unknown definition type " + itemDef.getClass().getName());
}
}
/**
* Parses (almost) anything: either an item with a definition, or an atomic (i.e. property-like) value.
* Does not care for schemaless items!
*
* @param node
* @return either Item or a unmarshalled bean value
* @throws SchemaException
*/
public Object parseAnyData(XNode node) throws SchemaException {
// is the type name explicitly specified? (if not, guess that we have a string)
QName typeName = getExplicitType(node);
if (typeName == null && node instanceof PrimitiveXNode) {
typeName = DOMUtil.XSD_STRING;
}
if (typeName != null) {
ItemDefinition itemDefinition = getSchemaRegistry().findItemDefinitionByType(typeName);
if (itemDefinition != null) {
return parseItem(node, getElementName(node, itemDefinition), itemDefinition);
} else {
return parseAtomicValue(node, typeName);
}
} else {
// if type name is not known, we have to derive it from the element name
if (!(node instanceof RootXNode)) {
throw new SchemaException("Couldn't parse general object with no type name and no root element name: " + node);
}
QName elementName = ((RootXNode) node).getRootElementName();
ItemDefinition itemDefinition = getSchemaRegistry().findItemDefinitionByElementName(elementName);
if (itemDefinition == null) {
throw new SchemaException("Couldn't parse general object with no type name and unknown element name: " + elementName);
}
return parseItem(((RootXNode) node).getSubnode(), elementName, itemDefinition);
}
}
// emulates JAXB unmarshal method
public <T> T parseAnyValue(XNode node) throws SchemaException {
// is the type name explicitly specified? (if not, guess that we have a string)
QName typeName = getExplicitType(node);
if (typeName == null && node instanceof PrimitiveXNode) {
typeName = DOMUtil.XSD_STRING;
}
if (typeName != null) {
ItemDefinition itemDefinition = getSchemaRegistry().findItemDefinitionByType(typeName);
if (itemDefinition != null) {
Item item = parseItem(node, getElementName(node, itemDefinition), itemDefinition);
return (T) getItemRealValue(item);
} else {
return parseAtomicValue(node, typeName);
}
} else {
// if type name is not known, we have to derive it from the element name
if (!(node instanceof RootXNode)) {
throw new SchemaException("Couldn't parse general object with no type name and no root element name: " + node);
}
QName elementName = ((RootXNode) node).getRootElementName();
ItemDefinition itemDefinition = getSchemaRegistry().findItemDefinitionByElementName(elementName);
if (itemDefinition == null) {
throw new SchemaException("Couldn't parse general object with no type name and unknown element name: " + elementName);
}
Item item = parseItem(((RootXNode) node).getSubnode(), elementName, itemDefinition);
return (T) getItemRealValue(item);
}
}
// experimental
public <T> JAXBElement<T> parseAnyValueAsJAXBElement(XNode node) throws SchemaException {
// is the type name explicitly specified? (if not, guess that we have a string)
QName typeName = getExplicitType(node);
if (typeName == null && node instanceof PrimitiveXNode) {
typeName = DOMUtil.XSD_STRING;