-
Notifications
You must be signed in to change notification settings - Fork 188
/
RawType.java
242 lines (210 loc) · 7.94 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
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.util.PrismUtil;
import com.evolveum.midpoint.prism.xnode.PrimitiveXNode;
import com.evolveum.midpoint.prism.xnode.RootXNode;
import com.evolveum.midpoint.prism.xnode.XNode;
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;
/**
* A class used to hold raw XNodes until the definition for such an object is known.
*/
public class RawType implements Serializable, Cloneable, Equals, Revivable {
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;
public RawType(PrismContext prismContext) {
Validate.notNull(prismContext, "prismContext is not set - perhaps a forgotten call to adopt() somewhere?");
this.prismContext = prismContext;
}
public RawType(XNode xnode, PrismContext prismContext) {
this(prismContext);
this.xnode = xnode;
}
@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 new RootXNode(itemName, xnode);
}
public PrismContext getPrismContext() {
return prismContext;
}
//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 {
Validate.isTrue(itemDefinition != null || itemName != null);
if (parsed != null) {
return (IV) parsed;
} else if (xnode != null) {
IV value;
if (itemDefinition != null) {
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;
}
} else {
assert itemName != null;
PrismProperty subItem = PrismProperty.createRaw(xnode, itemName, prismContext);
value = (IV) subItem.getValue();
}
xnode = null;
parsed = value;
return (IV) parsed;
} else {
return null;
}
}
public <V,ID extends ItemDefinition> V getParsedRealValue(ItemDefinition 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 {
QName itemName = ItemPath.getName(itemPath.lastNamed());
getParsedValue(itemDefinition, itemName);
}
}
if (parsed != null){
if (parsed instanceof PrismPropertyValue){
return (V) ((PrismPropertyValue) parsed).getValue();
} else if (parsed instanceof PrismContainerValue){
return (V) ((PrismContainerValue) parsed).asContainerable();
} else if (parsed instanceof PrismReferenceValue){
return (V) ((PrismReferenceValue) parsed).asReferencable();
}
}
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;
}
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();
return prismContext.xnodeSerializer().serialize(parsed).getSubnode();
} 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();
}
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);
}
}
private boolean xnodeSerializationsAreEqual(RawType other) {
try {
return serializeToXNode().equals(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 = new PrimitiveXNode<>(value);
RawType rv = new RawType(xnode, prismContext);
return rv;
}
}