-
Notifications
You must be signed in to change notification settings - Fork 188
/
JaxbDomHack.java
275 lines (250 loc) · 10.6 KB
/
JaxbDomHack.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
/*
* 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 java.util.List;
import javax.xml.bind.JAXBElement;
import javax.xml.namespace.QName;
import com.evolveum.midpoint.prism.*;
import com.evolveum.midpoint.prism.parser.dom.DomParser;
import com.evolveum.midpoint.util.logging.LoggingUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.Validate;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import com.evolveum.midpoint.prism.xml.XmlTypeConverter;
import com.evolveum.midpoint.prism.xml.XsdTypeMapper;
import com.evolveum.midpoint.prism.xnode.MapXNode;
import com.evolveum.midpoint.prism.xnode.PrimitiveXNode;
import com.evolveum.midpoint.prism.xnode.XNode;
import com.evolveum.midpoint.util.DOMUtil;
import com.evolveum.midpoint.util.JAXBUtil;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
/**
* A set of ugly hacks that are needed for prism and "real" JAXB to coexist. We hate it be we need it.
* This is a mix of DOM and JAXB code that allows the use of "any" methods on JAXB-generated objects.
* Prism normally does not use of of that. But JAXB code (such as JAX-WS) can invoke it and therefore
* it has to return correct DOM/JAXB elements as expected.
*
* @author Radovan Semancik
*/
public class JaxbDomHack {
private static final Trace LOGGER = TraceManager.getTrace(JaxbDomHack.class);
private PrismContext prismContext;
private DomParser domParser;
public JaxbDomHack(DomParser domParser, PrismContext prismContext) {
super();
this.domParser = domParser;
this.prismContext = prismContext;
}
private <T extends Containerable> ItemDefinition locateItemDefinition(
PrismContainerDefinition<T> containerDefinition, QName elementQName, Object valueElements)
throws SchemaException {
ItemDefinition def = containerDefinition.findItemDefinition(elementQName);
if (def != null) {
return def;
}
if (valueElements instanceof Element) {
// Try to locate xsi:type definition in the element
def = resolveDynamicItemDefinition(containerDefinition, elementQName, (Element) valueElements,
prismContext);
}
if (valueElements instanceof List){
List elements = (List) valueElements;
if (elements.size() == 1){
Object element = elements.get(0);
if (element instanceof JAXBElement){
Object val = ((JAXBElement) element).getValue();
if (val.getClass().isPrimitive()){
QName typeName = XsdTypeMapper.toXsdType(val.getClass());
PrismPropertyDefinition propDef = new PrismPropertyDefinition(elementQName, typeName, prismContext);
// propDef.setMaxOccurs(maxOccurs);
propDef.setDynamic(true);
return propDef;
}
}
}
}
if (def != null) {
return def;
}
if (containerDefinition.isRuntimeSchema()) {
// Try to locate global definition in any of the schemas
def = resolveGlobalItemDefinition(containerDefinition, elementQName);
}
return def;
}
private ItemDefinition resolveDynamicItemDefinition(ItemDefinition parentDefinition, QName elementName,
Element element, PrismContext prismContext) throws SchemaException {
QName typeName = null;
// QName elementName = null;
// Set it to multi-value to be on the safe side
int maxOccurs = -1;
// for (Object element : valueElements) {
// if (elementName == null) {
// elementName = JAXBUtil.getElementQName(element);
// }
// TODO: try JAXB types
if (element instanceof Element) {
Element domElement = (Element) element;
if (DOMUtil.hasXsiType(domElement)) {
typeName = DOMUtil.resolveXsiType(domElement);
if (typeName != null) {
String maxOccursString = domElement.getAttributeNS(
PrismConstants.A_MAX_OCCURS.getNamespaceURI(),
PrismConstants.A_MAX_OCCURS.getLocalPart());
if (!StringUtils.isBlank(maxOccursString)) {
// TODO
// maxOccurs = parseMultiplicity(maxOccursString, elementName);
}
// break;
}
}
}
// }
// FIXME: now the definition assumes property, may also be property
// container?
if (typeName == null) {
return null;
}
PrismPropertyDefinition propDef = new PrismPropertyDefinition(elementName, typeName, prismContext);
propDef.setMaxOccurs(maxOccurs);
propDef.setDynamic(true);
return propDef;
}
private <T extends Containerable> ItemDefinition resolveGlobalItemDefinition(
PrismContainerDefinition<T> containerDefinition, QName elementQName)
throws SchemaException {
// Object firstElement = valueElements.get(0);
// QName elementQName = JAXBUtil.getElementQName(firstElement);
return prismContext.getSchemaRegistry().resolveGlobalItemDefinition(elementQName, containerDefinition);
}
/**
* This is used in a form of "fromAny" to parse elements from a JAXB getAny method to prism.
*/
public <IV extends PrismValue,ID extends ItemDefinition,C extends Containerable> Item<IV,ID> parseRawElement(Object element, PrismContainerDefinition<C> definition) throws SchemaException {
Validate.notNull(definition, "Attempt to parse raw element in a container without definition");
QName elementName = JAXBUtil.getElementQName(element);
ItemDefinition itemDefinition = definition.findItemDefinition(elementName);
if (itemDefinition == null) {
itemDefinition = locateItemDefinition(definition, elementName, element);
if (itemDefinition == null) {
throw new SchemaException("No definition for item "+elementName);
}
}
PrismContext prismContext = definition.getPrismContext();
Item<IV,ID> subItem;
if (element instanceof Element) {
// DOM Element
DomParser domParser = prismContext.getParserDom();
XNode xnode = domParser.parseElementContent((Element)element);
subItem = (Item<IV, ID>) prismContext.getXnodeProcessor().parseItem(xnode, elementName, itemDefinition, ParsingContext.createDefault());
} else if (element instanceof JAXBElement<?>) {
// JAXB Element
JAXBElement<?> jaxbElement = (JAXBElement<?>)element;
Object jaxbBean = jaxbElement.getValue();
if (itemDefinition == null) {
throw new SchemaException("No definition for item "+elementName+" in container "+definition+" (parsed from raw element)", elementName);
}
if (itemDefinition instanceof PrismPropertyDefinition<?>) {
// property
PrismProperty<?> property = ((PrismPropertyDefinition<?>)itemDefinition).instantiate();
property.setRealValue(jaxbBean);
subItem = (Item<IV,ID>) property;
} else if (itemDefinition instanceof PrismContainerDefinition<?>) {
if (jaxbBean instanceof Containerable) {
PrismContainer<?> container = ((PrismContainerDefinition<?>)itemDefinition).instantiate();
PrismContainerValue subValue = ((Containerable)jaxbBean).asPrismContainerValue();
container.add(subValue);
subItem = (Item<IV,ID>) container;
} else {
throw new IllegalArgumentException("Unsupported JAXB bean "+jaxbBean.getClass());
}
} else if (itemDefinition instanceof PrismReferenceDefinition) {
// TODO
if (jaxbBean instanceof Referencable){
PrismReference reference = ((PrismReferenceDefinition)itemDefinition).instantiate();
PrismReferenceValue refValue = ((Referencable) jaxbBean).asReferenceValue();
reference.merge(refValue);
subItem = (Item<IV,ID>) reference;
} else{
throw new IllegalArgumentException("Unsupported JAXB bean" + jaxbBean);
}
} else {
throw new IllegalArgumentException("Unsupported definition type "+itemDefinition.getClass());
}
} else {
throw new IllegalArgumentException("Unsupported element type "+element.getClass());
}
return subItem;
}
/**
* Serializes prism value to JAXB "any" format as returned by JAXB getAny() methods.
*/
public Object toAny(PrismValue value) throws SchemaException {
Document document = DOMUtil.getDocument();
if (value == null) {
return value;
}
QName elementName = value.getParent().getElementName();
Object xmlValue;
if (value instanceof PrismPropertyValue) {
PrismPropertyValue<Object> pval = (PrismPropertyValue)value;
if (pval.isRaw() && (pval.getParent() == null || pval.getParent().getDefinition() == null)) {
Object rawElement = pval.getRawElement();
if (rawElement instanceof Element) {
return ((Element)rawElement).cloneNode(true);
} else if (rawElement instanceof MapXNode) {
return domParser.serializeXMapToElement((MapXNode)rawElement, elementName);
} else if (rawElement instanceof PrimitiveXNode<?>) {
PrimitiveXNode<?> xprim = (PrimitiveXNode<?>)rawElement;
String stringValue = xprim.getStringValue();
Element element = DOMUtil.createElement(document, elementName);
element.setTextContent(stringValue);
DOMUtil.setNamespaceDeclarations(element, xprim.getRelevantNamespaceDeclarations());
return element;
} else {
throw new IllegalArgumentException("Cannot convert raw element "+rawElement+" to xsd:any");
}
}
Object realValue = pval.getValue();
xmlValue = realValue;
if (XmlTypeConverter.canConvert(realValue.getClass())) {
// Always record xsi:type. This is FIXME, but should work OK for now (until we put definition into deltas)
xmlValue = XmlTypeConverter.toXsdElement(realValue, elementName, document, true);
}
} else if (value instanceof PrismReferenceValue) {
PrismReferenceValue rval = (PrismReferenceValue)value;
xmlValue = prismContext.serializeValueToDom(rval, elementName, document);
} else if (value instanceof PrismContainerValue<?>) {
PrismContainerValue<?> pval = (PrismContainerValue<?>)value;
if (pval.getParent().getCompileTimeClass() == null) {
// This has to be runtime schema without a compile-time representation.
// We need to convert it to DOM
xmlValue = prismContext.serializeValueToDom(pval, elementName, document);
} else {
xmlValue = pval.asContainerable();
}
} else {
throw new IllegalArgumentException("Unknown type "+value);
}
if (!(xmlValue instanceof Element) && !(xmlValue instanceof JAXBElement)) {
xmlValue = new JAXBElement(elementName, xmlValue.getClass(), xmlValue);
}
return xmlValue;
}
}