/
RawType.java
409 lines (363 loc) · 12.9 KB
/
RawType.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
/*
* Copyright (c) 2010-2018 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.prism.xml.ns._public.types_3;
import com.evolveum.midpoint.prism.*;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.prism.xnode.*;
import com.evolveum.midpoint.util.ShortDumpable;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.util.exception.SystemException;
import org.apache.commons.lang.Validate;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jvnet.jaxb2_commons.lang.Equals;
import org.jvnet.jaxb2_commons.lang.EqualsStrategy;
import org.jvnet.jaxb2_commons.locator.ObjectLocator;
import javax.xml.namespace.QName;
import java.io.Serializable;
import java.util.Objects;
/**
* A class used to hold raw XNodes until the definition for such an object is known.
*/
public class RawType implements Serializable, Cloneable, Equals, Revivable, ShortDumpable {
private static final long serialVersionUID = 4430291958902286779L;
/**
* This is obligatory.
*/
private transient PrismContext prismContext;
/*
* At most one of these two values (xnode, parsed) should be set.
*/
/**
* Unparsed value. It is set on RawType instance construction.
*/
private XNode xnode;
/**
* Parsed value. It is computed when calling getParsedValue/getParsedItem methods.
*
* Beware: At most one of these fields (xnode, parsed) may be non-null at any instant.
*/
private PrismValue parsed;
private QName explicitTypeName;
private boolean explicitTypeDeclaration;
public RawType(PrismContext prismContext) {
Validate.notNull(prismContext, "prismContext is not set - perhaps a forgotten call to adopt() somewhere?");
this.prismContext = prismContext;
}
public RawType(XNode node, @NotNull PrismContext prismContext) {
this(prismContext);
this.xnode = node;
if (this.xnode != null) {
this.explicitTypeName = xnode.getTypeQName();
this.explicitTypeDeclaration = xnode.isExplicitTypeDeclaration();
}
}
public RawType(PrismValue parsed, QName explicitTypeName, @NotNull PrismContext prismContext) {
this.prismContext = prismContext;
this.parsed = parsed;
if (explicitTypeName != null) {
this.explicitTypeName = explicitTypeName;
this.explicitTypeDeclaration = true; // todo
} else if (parsed != null && parsed.getTypeName() != null) {
this.explicitTypeName = parsed.getTypeName();
this.explicitTypeDeclaration = true; // todo
}
}
public static RawType fromPropertyRealValue(Object realValue, QName explicitTypeName, @NotNull PrismContext prismContext) {
return new RawType(prismContext.itemFactory().createPropertyValue(realValue), explicitTypeName, prismContext);
}
/**
* TEMPORARY. EXPERIMENTAL. DO NOT USE.
*/
public String extractString() {
if (xnode instanceof PrimitiveXNode) {
return ((PrimitiveXNode<?>) xnode).getStringValue();
} else {
return toString();
}
}
public String extractString(String defaultValue) {
if (xnode instanceof PrimitiveXNode) {
return ((PrimitiveXNode<?>) xnode).getStringValue();
} else {
return defaultValue;
}
}
@Override
public void revive(PrismContext prismContext) throws SchemaException {
Validate.notNull(prismContext);
this.prismContext = prismContext;
if (parsed != null) {
parsed.revive(prismContext);
}
}
//region General getters/setters
public XNode getXnode() {
return xnode;
}
@NotNull
public RootXNode getRootXNode(@NotNull QName itemName) {
return prismContext.xnodeFactory().root(itemName, xnode);
}
public PrismContext getPrismContext() {
return prismContext;
}
// experimental
public QName getExplicitTypeName() {
return explicitTypeName;
}
//endregion
//region Parsing and serialization
// itemDefinition may be null; in that case we do the best what we can
public <IV extends PrismValue,ID extends ItemDefinition> IV getParsedValue(@Nullable ItemDefinition itemDefinition, @Nullable QName itemName) throws SchemaException {
if (parsed != null) {
// Check too intense for normal operation?
// if (!itemDefinition.canBeDefinitionOf(parsed)) {
// throw new SchemaException("Attempt to return parsed raw value "+parsed+" that does not match provided definition "+itemDefinition);
// }
return (IV) parsed;
} else if (xnode != null) {
IV value;
if (itemDefinition != null
&& !(itemDefinition instanceof PrismPropertyDefinition && ((PrismPropertyDefinition) itemDefinition).isAnyType())) {
if (itemName == null) {
itemName = itemDefinition.getName();
}
checkPrismContext();
Item<IV,ID> subItem = prismContext.parserFor(getRootXNode(itemName)).name(itemName).definition(itemDefinition).parseItem();
if (!subItem.isEmpty()){
value = subItem.getValue(0);
} else {
value = null;
}
if (value != null && !itemDefinition.canBeDefinitionOf(value)) {
throw new SchemaException("Attempt to parse raw value into "+value+" that does not match provided definition "+itemDefinition);
}
xnode = null;
parsed = value;
explicitTypeName = itemDefinition.getTypeName();
explicitTypeDeclaration = true; // todo
return (IV) parsed;
} else {
// we don't really want to set 'parsed', as we didn't performed real parsing
//noinspection unchecked
return (IV) prismContext.itemFactory().createPropertyValue(xnode);
}
} else {
return null;
}
}
public <V,ID extends ItemDefinition> V getParsedRealValue(ID itemDefinition, ItemPath itemPath) throws SchemaException {
if (parsed == null && xnode != null) {
if (itemDefinition == null) {
return prismContext.parserFor(xnode.toRootXNode()).parseRealValue(); // TODO what will be the result without definition?
} else {
getParsedValue(itemDefinition, itemPath.lastName());
}
}
if (parsed != null) {
return parsed.getRealValue();
}
return null;
}
public PrismValue getAlreadyParsedValue() {
return parsed;
}
public <T> T getParsedRealValue(@NotNull Class<T> clazz) throws SchemaException {
if (parsed != null) {
if (clazz.isAssignableFrom(parsed.getRealValue().getClass())) {
return (T) parsed.getRealValue();
} else {
throw new IllegalArgumentException("Parsed value ("+parsed.getClass()+") is not assignable to "+clazz);
}
} else if (xnode != null) {
return prismContext.parserFor(xnode.toRootXNode()).parseRealValue(clazz);
} else {
return null;
}
}
public <IV extends PrismValue,ID extends ItemDefinition> Item<IV,ID> getParsedItem(ID itemDefinition) throws SchemaException {
Validate.notNull(itemDefinition);
return getParsedItem(itemDefinition, itemDefinition.getName());
}
public <IV extends PrismValue,ID extends ItemDefinition> Item<IV,ID> getParsedItem(ID itemDefinition, QName itemName) throws SchemaException {
Validate.notNull(itemDefinition);
Validate.notNull(itemName);
Item<IV,ID> item = itemDefinition.instantiate();
IV newValue = getParsedValue(itemDefinition, itemName);
if (newValue != null) {
item.add((IV) newValue.clone());
}
return item;
}
// // Returns either an item or a real value.
// // VERY EXPERIMENTAL.
// public Object getParsedItemOrRealValue() throws SchemaException {
// if (parsed != null) {
// return
// } else if (xnode != null) {
// return prismContext.parserFor(xnode.toRootXNode()).parseItemOrRealValue();
// } else {
// return null;
// }
// }
public XNode serializeToXNode() throws SchemaException {
if (xnode != null) {
// QName type = xnode.getTypeQName();
// if (xnode instanceof PrimitiveXNode && type != null){
// if (!((PrimitiveXNode)xnode).isParsed()){
// Object realValue = PrismUtil.getXnodeProcessor(prismContext).parseAnyValue(xnode, ParsingContext.createDefault());
// ((PrimitiveXNode)xnode).setValue(realValue, type);
// }
// }
return xnode;
} else if (parsed != null) {
checkPrismContext();
XNode rv = prismContext.xnodeSerializer().root(new QName("dummy")).serialize(parsed).getSubnode();
prismContext.hacks().setXNodeType(rv, explicitTypeName, explicitTypeDeclaration);
return rv;
} else {
return null; // or an exception here?
}
}
//endregion
//region Cloning, comparing, dumping (TODO)
public RawType clone() {
RawType clone = new RawType(prismContext);
if (xnode != null) {
clone.xnode = xnode.clone();
} else if (parsed != null) {
clone.parsed = parsed.clone();
}
clone.explicitTypeName = explicitTypeName;
clone.explicitTypeDeclaration = explicitTypeDeclaration;
return clone;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((xnode == null) ? 0 : xnode.hashCode());
result = prime * result + ((parsed == null) ? 0 : parsed.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
RawType other = (RawType) obj;
if (xnode != null && other.xnode != null) {
return xnode.equals(other.xnode);
} else if (parsed != null && other.parsed != null) {
return parsed.equals(other.parsed);
} else {
return xnodeSerializationsAreEqual(other);
}
// TODO explicit type declaration? (probably should be ignored as it is now)
}
private boolean xnodeSerializationsAreEqual(RawType other) {
try {
return Objects.equals(serializeToXNode(), other.serializeToXNode());
} catch (SchemaException e) {
// or should we silently return false?
throw new SystemException("Couldn't serialize RawType to XNode when comparing them", e);
}
}
@Override
public boolean equals(ObjectLocator thisLocator, ObjectLocator thatLocator, Object that,
EqualsStrategy equalsStrategy) {
return equals(that);
}
//endregion
private void checkPrismContext() {
if (prismContext == null) {
throw new IllegalStateException("prismContext is not set - perhaps a forgotten call to adopt() somewhere?");
}
}
public static RawType create(String value, PrismContext prismContext) {
PrimitiveXNode<String> xnode = prismContext.xnodeFactory().primitive(value);
return new RawType(xnode, prismContext);
}
public static RawType create(XNode node, PrismContext prismContext) {
return new RawType(node, prismContext);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("RawType: ");
if (xnode != null) {
sb.append("(raw");
toStringExplicitType(sb);
sb.append("): ").append(xnode);
} else if (parsed != null) {
sb.append("(parsed");
toStringExplicitType(sb);
sb.append("): ").append(parsed);
} else {
sb.append("(empty");
toStringExplicitType(sb);
sb.append(")");
}
return sb.toString();
}
private void toStringExplicitType(StringBuilder sb) {
if (explicitTypeDeclaration) {
sb.append(":");
if (explicitTypeName == null) {
sb.append("null");
} else {
sb.append(explicitTypeName.getLocalPart());
}
}
}
@Override
public void shortDump(StringBuilder sb) {
if (xnode != null) {
sb.append("(raw");
sb.append("):").append(xnode);
} else if (parsed != null) {
if (parsed instanceof ShortDumpable) {
((ShortDumpable)parsed).shortDump(sb);
} else {
Object realValue = parsed.getRealValue();
if (realValue == null) {
sb.append("null");
} else if (realValue instanceof ShortDumpable) {
((ShortDumpable)realValue).shortDump(sb);
} else {
sb.append(realValue.toString());
}
}
}
}
public boolean isParsed() {
return parsed != null;
}
// avoid if possible
public String guessFormattedValue() throws SchemaException {
if (parsed != null) {
return parsed.getRealValue().toString(); // todo reconsider this
} else if (xnode instanceof PrimitiveXNode) {
return ((PrimitiveXNode) xnode).getGuessedFormattedValue();
} else {
return null;
}
}
}