-
Notifications
You must be signed in to change notification settings - Fork 188
/
ItemInfo.java
286 lines (260 loc) · 11.7 KB
/
ItemInfo.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
/*
* 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.marshaller;
import com.evolveum.midpoint.prism.*;
import com.evolveum.midpoint.prism.schema.SchemaRegistry;
import com.evolveum.midpoint.util.QNameUtil;
import com.evolveum.midpoint.util.exception.SchemaException;
import org.jetbrains.annotations.NotNull;
import javax.xml.namespace.QName;
import java.util.List;
import static com.evolveum.midpoint.prism.schema.SchemaRegistry.ComparisonResult.EQUAL;
/**
* @author mederly
*/
public class ItemInfo<ID extends ItemDefinition> {
private QName itemName;
private ID itemDefinition;
private QName typeName;
private ComplexTypeDefinition complexTypeDefinition;
/**
* This method is to be called ONLY on the root level, i.e. when unmarshalling starts.
*/
@NotNull
public static <ID extends ItemDefinition> ItemInfo determine(ItemDefinition itemDefinitionExplicit,
QName itemNameFromSource, QName itemNameExplicit, QName itemNameDefault,
QName typeNameFromSource, QName typeNameExplicit,
Class<?> classExplicit,
@NotNull Class<ID> definitionClass, @NotNull ParsingContext pc, @NotNull SchemaRegistry schemaRegistry) throws SchemaException {
// deriving definition
ID definition;
if (itemDefinitionExplicit != null) {
if (!definitionClass.isAssignableFrom(itemDefinitionExplicit.getClass())) {
throw new IllegalArgumentException("ItemDefinition of " + itemDefinitionExplicit + " doesn't match expected "
+ "definition class of " + definitionClass);
}
@SuppressWarnings("unchecked")
ID narrowedDefinitionExplicit = (ID) itemDefinitionExplicit;
definition = narrowedDefinitionExplicit;
} else {
definition = null;
}
definition = augmentWithType(definition, definitionClass, schemaRegistry, typeNameExplicit);
definition = augmentWithType(definition, definitionClass, schemaRegistry, typeNameFromSource);
definition = augmentWithClass(definition, definitionClass, schemaRegistry, classExplicit);
definition = augmentWithItemName(definition, definitionClass, schemaRegistry, itemNameFromSource);
// Sanity check: if typeNameFromSource or typeNameExplicit are not compatible with the type derived from definition,
// we will NOT use the definition. For example, if client is looking for ConditionalSearchFilterType (has no ItemDefinition)
// the definition here would be very probably SearchFilterType (based e.g. on the itemNameFromSource). And it has not
// to be used, as the client explicitly requested ConditionalSearchFilterType.
//
// Exception is PrismReferenceDefinition, as there are no subtypes in that case (yet)
if (definition != null && !(definition instanceof PrismReferenceDefinition)) {
QName typeName = definition.getTypeName();
if (typeNameExplicit != null && !schemaRegistry.isAssignableFrom(typeNameExplicit, typeName)
|| typeNameFromSource != null && !schemaRegistry.isAssignableFrom(typeNameFromSource, typeName)) {
// the result would NOT match typeNameExplicit/typeNameFromSource (very probably)
// so we have to abandon this definition
definition = null;
}
}
ItemInfo<ID> info = new ItemInfo<>();
info.itemDefinition = definition;
// deriving item name
if (itemNameExplicit != null) {
info.itemName = itemNameExplicit;
} else if (itemNameFromSource != null) {
if (QNameUtil.noNamespace(itemNameFromSource) && definition != null) {
info.itemName = new QName(definition.getName().getNamespaceURI(), itemNameFromSource.getLocalPart());
} else {
info.itemName = itemNameFromSource;
}
} else if (definition != null) {
info.itemName = definition.getName();
} else {
info.itemName = itemNameDefault;
}
// type name
if (definition != null) {
info.typeName = definition.getTypeName();
} else if (typeNameExplicit == null && typeNameFromSource == null && classExplicit != null) {
info.typeName = schemaRegistry.determineTypeForClass(classExplicit);
if (info.typeName != null && info.itemName != null) {
// Create artificial definition. We do NOT mark it as dynamic, because it might come from client code-supplied
// information (i.e. not from the dynamic one). This might cause problems during serialization; but more probably
// not - the decision on putting explicit type declaration on the root level is not driven by the 'dynamic' flag.
// And this method is called only at the root level.
if (Objectable.class.isAssignableFrom(classExplicit)) {
throw new IllegalStateException("Prism object type " + info.typeName + " without definition");
} else if (Containerable.class.isAssignableFrom(classExplicit)) {
@SuppressWarnings("unchecked")
Class<Containerable> containerableClass = (Class<Containerable>) classExplicit;
ComplexTypeDefinition ctd = schemaRegistry.findComplexTypeDefinitionByCompileTimeClass(containerableClass);
@SuppressWarnings("unchecked")
ID id = (ID) new PrismContainerDefinitionImpl<>(info.itemName, ctd,
schemaRegistry.getPrismContext(), containerableClass);
info.itemDefinition = id;
} else {
@SuppressWarnings("unchecked")
ID id = (ID) new PrismPropertyDefinitionImpl<>(info.itemName, info.typeName, schemaRegistry.getPrismContext());
info.itemDefinition = id;
}
}
} else if (typeNameExplicit == null) {
info.typeName = typeNameFromSource;
} else if (typeNameFromSource == null) {
info.typeName = typeNameExplicit;
} else if (schemaRegistry.isAssignableFrom(typeNameExplicit, typeNameFromSource)) {
info.typeName = typeNameFromSource;
} else {
info.typeName = typeNameExplicit;
}
// complex type definition
if (info.itemDefinition != null) {
if (info.itemDefinition instanceof PrismContainerDefinition) {
info.complexTypeDefinition = ((PrismContainerDefinition) info.itemDefinition).getComplexTypeDefinition();
} else {
info.complexTypeDefinition = null;
}
} else if (info.typeName != null) {
// TODO probably optimize (e.g. if we already know the class ... )
info.complexTypeDefinition = schemaRegistry.findComplexTypeDefinitionByType(info.typeName);
} else {
info.complexTypeDefinition = null;
}
return info;
}
private static <ID extends ItemDefinition> ID augmentWithType(ID definition, Class<ID> definitionClass,
SchemaRegistry schemaRegistry, QName typeName) throws SchemaException {
if (typeName == null) {
return definition;
}
if (definition != null && QNameUtil.match(definition.getTypeName(), typeName)) { // just an optimization to avoid needless lookups
return definition;
}
ItemDefinition rawDefFromType = schemaRegistry.findItemDefinitionByType(typeName);
if (rawDefFromType == null) {
return definition; // TODO warning if wrong type?
}
if (!definitionClass.isAssignableFrom(rawDefFromType.getClass())) {
throw new SchemaException("Wrong type name " + typeName + " (not a " + definitionClass.getClass().getSimpleName() + ")"); // TODO context of error
}
@SuppressWarnings("unchecked")
ID defFromType = (ID) rawDefFromType;
if (definition instanceof PrismReferenceDefinition && defFromType instanceof PrismObjectDefinition) {
return definition; // nothing to do; this is a reference holding a composite object
}
return schemaRegistry.selectMoreSpecific(definition, defFromType);
}
private static <ID extends ItemDefinition> ID augmentWithItemName(ID definition, Class<ID> definitionClass,
SchemaRegistry schemaRegistry, QName itemName) throws SchemaException {
if (itemName == null || definition != null && QNameUtil.match(definition.getName(), itemName)) { // just an optimization to avoid needless lookups
return definition;
}
List<ID> defsFromItemName = schemaRegistry.findItemDefinitionsByElementName(itemName, definitionClass);
if (defsFromItemName.size() != 1) {
return definition;
}
ID defFromItemName = defsFromItemName.get(0);
if (definition == null) {
return defFromItemName;
}
SchemaRegistry.ComparisonResult comparisonResult = schemaRegistry.compareDefinitions(definition, defFromItemName);
switch (comparisonResult) {
case EQUAL: return definition; // definition is generally considered 'at least as good' as the new one
case NO_STATIC_CLASS: return definition; // play safe, select original definition (item name is not very trustworthy source of information)
case INCOMPATIBLE: return definition; // TODO probably we should signal a problem
case FIRST_IS_CHILD: return definition;
case SECOND_IS_CHILD: return defFromItemName;
default: throw new AssertionError("Invalid result: " + comparisonResult);
}
}
private static <ID extends ItemDefinition> ID augmentWithClass(ID definition, Class<ID> definitionClass,
SchemaRegistry schemaRegistry, Class<?> clazz) throws SchemaException {
if (clazz == null) {
return definition;
}
if (definition instanceof PrismContainerDefinition && clazz.equals(((PrismContainerDefinition) definition).getCompileTimeClass())) {
return definition;
}
QName typeNameFromClass = schemaRegistry.determineTypeForClass(clazz);
if (typeNameFromClass == null) {
throw new SchemaException("Unknown class " + clazz.getName());
}
if (definition != null && definition.getTypeName().equals(typeNameFromClass)) {
return definition;
}
@SuppressWarnings("unchecked")
List<ID> defFromClass = schemaRegistry.findItemDefinitionsByCompileTimeClass((Class) clazz, (Class) definitionClass);
if (defFromClass.size() != 1) {
return definition;
} else {
return schemaRegistry.selectMoreSpecific(definition, defFromClass.get(0));
}
}
public QName getItemName() {
return itemName;
}
public ID getItemDefinition() {
return itemDefinition;
}
public ComplexTypeDefinition getComplexTypeDefinition() {
return complexTypeDefinition;
}
public QName getTypeName() {
return typeName;
}
@NotNull
public static ItemInfo determineFromValue(@NotNull PrismValue value, QName itemName, ItemDefinition itemDefinition,
@NotNull SchemaRegistry schemaRegistry) {
ItemInfo info = new ItemInfo();
// definition
info.itemDefinition = itemDefinition;
if (info.itemDefinition == null && value.getParent() != null) {
info.itemDefinition = itemDefinition = value.getParent().getDefinition();
if (info.itemDefinition == null) {
info.itemDefinition = schemaRegistry.findItemDefinitionByElementName(value.getParent().getElementName());
}
}
if (info.itemDefinition == null) {
Class<?> realClass = value.getRealClass();
if (realClass != null) {
List<ItemDefinition> definitions = schemaRegistry.findItemDefinitionsByCompileTimeClass(realClass, ItemDefinition.class);
if (definitions.size() == 1) {
info.itemDefinition = definitions.get(0);
}
}
}
// item name
info.itemName = itemName;
if (info.itemName == null && value.getParent() != null) {
info.itemName = value.getParent().getElementName();
}
if (info.itemName == null && info.itemDefinition != null) {
info.itemName = info.itemDefinition.getName();
}
// item type
if (itemDefinition != null) {
info.typeName = itemDefinition.getTypeName();
} else {
Class<?> realClass = value.getRealClass();
if (realClass != null) {
info.typeName = schemaRegistry.determineTypeForClass(realClass);
}
}
return info;
}
}