/
PrismContext.java
385 lines (327 loc) · 13.8 KB
/
PrismContext.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
/*
* Copyright (c) 2010-2013 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;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import com.evolveum.midpoint.util.DOMUtil;
import com.evolveum.midpoint.util.DebugUtil;
import com.evolveum.midpoint.util.logging.LoggingUtils;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;
import com.evolveum.midpoint.prism.delta.ObjectDelta;
import com.evolveum.midpoint.prism.parser.DomParser;
import com.evolveum.midpoint.prism.parser.JaxbDomHack;
import com.evolveum.midpoint.prism.parser.JsonParser;
import com.evolveum.midpoint.prism.parser.Parser;
import com.evolveum.midpoint.prism.parser.PrismBeanConverter;
import com.evolveum.midpoint.prism.parser.XNodeProcessor;
import com.evolveum.midpoint.prism.parser.YamlParser;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.prism.polystring.PolyStringNormalizer;
import com.evolveum.midpoint.prism.polystring.PrismDefaultPolyStringNormalizer;
import com.evolveum.midpoint.prism.schema.SchemaDefinitionFactory;
import com.evolveum.midpoint.prism.schema.SchemaRegistry;
import com.evolveum.midpoint.prism.util.JaxbTestUtil;
import com.evolveum.midpoint.prism.xnode.MapXNode;
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 javax.xml.namespace.QName;
/**
* @author semancik
*
*/
public class PrismContext {
public static final String LANG_XML = "xml";
public static final String LANG_JSON = "json";
public static final String LANG_YAML = "yaml";
private static final Trace LOGGER = TraceManager.getTrace(PrismContext.class);
private static boolean allowSchemalessSerialization = true;
private SchemaRegistry schemaRegistry;
private XNodeProcessor xnodeProcessor;
private PrismBeanConverter beanConverter;
private SchemaDefinitionFactory definitionFactory;
private PolyStringNormalizer defaultPolyStringNormalizer;
private Map<String, Parser> parserMap;
// We need to keep this because of deprecated methods and various hacks
private DomParser parserDom;
private JaxbDomHack jaxbDomHack;
private PrismContext() {
// empty
}
public static PrismContext create(SchemaRegistry schemaRegistry) {
PrismContext prismContext = new PrismContext();
prismContext.schemaRegistry = schemaRegistry;
schemaRegistry.setPrismContext(prismContext);
prismContext.xnodeProcessor = new XNodeProcessor(prismContext);
prismContext.beanConverter = new PrismBeanConverter(prismContext);
prismContext.parserMap = new HashMap<String, Parser>();
DomParser parserDom = new DomParser(schemaRegistry);
prismContext.parserMap.put(LANG_XML, parserDom);
JsonParser parserJson = new JsonParser();
prismContext.parserMap.put(LANG_JSON, parserJson);
YamlParser parserYaml = new YamlParser();
prismContext.parserMap.put(LANG_YAML, parserYaml);
prismContext.parserDom = parserDom;
prismContext.jaxbDomHack = new JaxbDomHack(parserDom, prismContext);
return prismContext;
}
public static PrismContext createEmptyContext(SchemaRegistry schemaRegistry){
PrismContext prismContext = new PrismContext();
prismContext.schemaRegistry = schemaRegistry;
schemaRegistry.setPrismContext(prismContext);
return prismContext;
}
public void initialize() throws SchemaException, SAXException, IOException {
schemaRegistry.initialize();
if (defaultPolyStringNormalizer == null) {
defaultPolyStringNormalizer = new PrismDefaultPolyStringNormalizer();
}
}
public static boolean isAllowSchemalessSerialization() {
return allowSchemalessSerialization;
}
public static void setAllowSchemalessSerialization(boolean allowSchemalessSerialization) {
PrismContext.allowSchemalessSerialization = allowSchemalessSerialization;
}
public SchemaRegistry getSchemaRegistry() {
return schemaRegistry;
}
public void setSchemaRegistry(SchemaRegistry schemaRegistry) {
this.schemaRegistry = schemaRegistry;
}
public XNodeProcessor getXnodeProcessor() {
return xnodeProcessor;
}
/**
* WARNING! This is not really public method. It should NOT not used outside the prism implementation.
*/
public DomParser getParserDom() {
return parserDom;
}
public PrismBeanConverter getBeanConverter() {
return beanConverter;
}
public JaxbDomHack getJaxbDomHack() {
return jaxbDomHack;
}
public SchemaDefinitionFactory getDefinitionFactory() {
if (definitionFactory == null) {
definitionFactory = new SchemaDefinitionFactory();
}
return definitionFactory;
}
public void setDefinitionFactory(SchemaDefinitionFactory definitionFactory) {
this.definitionFactory = definitionFactory;
}
public PolyStringNormalizer getDefaultPolyStringNormalizer() {
return defaultPolyStringNormalizer;
}
public void setDefaultPolyStringNormalizer(PolyStringNormalizer defaultPolyStringNormalizer) {
this.defaultPolyStringNormalizer = defaultPolyStringNormalizer;
}
private Parser getParser(String language) {
return parserMap.get(language);
}
private Parser getParserNotNull(String language) {
Parser parser = getParser(language);
if (parser == null) {
throw new SystemException("No parser for language '"+language+"'");
}
return parser;
}
/**
* Parses a DOM object and creates a prism from it.
*/
@Deprecated
public <T extends Objectable> PrismObject<T> parseObject(Element objectElement) throws SchemaException {
RootXNode xroot = parserDom.parseElementAsRoot(objectElement);
return xnodeProcessor.parseObject(xroot);
}
/**
* Parses a file and creates a prism from it. Autodetect language.
* @throws IOException
*/
public <T extends Objectable> PrismObject<T> parseObject(File file) throws SchemaException, IOException {
Parser parser = findParser(file);
XNode xnode = parser.parse(file);
return xnodeProcessor.parseObject(xnode);
}
private Parser findParser(File file) throws IOException{
Parser parser = null;
for (Entry<String,Parser> entry: parserMap.entrySet()) {
Parser aParser = entry.getValue();
if (aParser.canParse(file)) {
parser = aParser;
break;
}
}
if (parser == null) {
throw new SystemException("No parser for file '"+file+"' (autodetect)");
}
return parser;
}
/**
* Parses a file and creates a prism from it.
*/
public <T extends Objectable> PrismObject<T> parseObject(File file, String language) throws SchemaException, IOException {
Parser parser = getParserNotNull(language);
XNode xnode = parser.parse(file);
return xnodeProcessor.parseObject(xnode);
}
/**
* Parses a string and creates a prism from it. Autodetect language.
* Used mostly for testing, but can also be used for built-in editors, etc.
*/
public <T extends Objectable> PrismObject<T> parseObject(String dataString) throws SchemaException {
Parser parser = findParser(dataString);
XNode xnode = parser.parse(dataString);
return xnodeProcessor.parseObject(xnode);
}
private Parser findParser(String data){
Parser parser = null;
for (Entry<String,Parser> entry: parserMap.entrySet()) {
Parser aParser = entry.getValue();
if (aParser.canParse(data)) {
parser = aParser;
break;
}
}
if (parser == null) {
throw new SystemException("No parser for data '"+DebugUtil.excerpt(data,16)+"' (autodetect)");
}
return parser;
}
/**
* Parses a string and creates a prism from it. Used mostly for testing, but can also be used for built-in editors, etc.
*/
public <T extends Objectable> PrismObject<T> parseObject(String dataString, String language) throws SchemaException {
Parser parser = getParserNotNull(language);
XNode xnode = parser.parse(dataString);
return xnodeProcessor.parseObject(xnode);
}
public <C extends Containerable> PrismContainer<C> parseContainer(File file, Class<C> type, String language) throws SchemaException, IOException {
Parser parser = getParserNotNull(language);
XNode xnode = parser.parse(file);
return xnodeProcessor.parseContainer(xnode, type);
}
public <C extends Containerable> PrismContainer<C> parseContainer(File file, PrismContainerDefinition<C> containerDef, String language) throws SchemaException, IOException {
Parser parser = getParserNotNull(language);
XNode xnode = parser.parse(file);
return xnodeProcessor.parseContainer(xnode, containerDef);
}
public <C extends Containerable> PrismContainer<C> parseContainer(String dataString, Class<C> type, String language) throws SchemaException {
Parser parser = getParserNotNull(language);
XNode xnode = parser.parse(dataString);
return xnodeProcessor.parseContainer(xnode, type);
}
public <C extends Containerable> PrismContainer<C> parseContainer(String dataString, PrismContainerDefinition<C> containerDef, String language) throws SchemaException {
Parser parser = getParserNotNull(language);
XNode xnode = parser.parse(dataString);
return xnodeProcessor.parseContainer(xnode, containerDef);
}
/**
* Set up the specified object with prism context instance and schema definition.
*/
public <T extends Objectable> void adopt(PrismObject<T> object, Class<T> declaredType) throws SchemaException {
object.revive(this);
getSchemaRegistry().applyDefinition(object, declaredType, false);
}
public <T extends Objectable> void adopt(PrismObject<T> object) throws SchemaException {
adopt(object, object.getCompileTimeClass());
}
public void adopt(Objectable objectable) throws SchemaException {
adopt(objectable.asPrismObject(), objectable.getClass());
}
public <T extends Objectable> void adopt(ObjectDelta<T> delta) throws SchemaException {
delta.revive(this);
getSchemaRegistry().applyDefinition(delta, delta.getObjectTypeClass(), false);
}
public <C extends Containerable, O extends Objectable> void adopt(C containerable, Class<O> type, ItemPath path) throws SchemaException {
PrismContainerValue<C> prismContainerValue = containerable.asPrismContainerValue();
adopt(prismContainerValue, type, path);
}
public <C extends Containerable, O extends Objectable> void adopt(PrismContainerValue<C> prismContainerValue, Class<O> type, ItemPath path) throws SchemaException {
prismContainerValue.revive(this);
getSchemaRegistry().applyDefinition(prismContainerValue, type, path, false);
}
public <C extends Containerable, O extends Objectable> void adopt(PrismContainerValue<C> prismContainerValue, QName typeName, ItemPath path) throws SchemaException {
prismContainerValue.revive(this);
getSchemaRegistry().applyDefinition(prismContainerValue, typeName, path, false);
}
public List<PrismObject<? extends Objectable>> parseObjects(File file) throws SchemaException, IOException {
Parser parser = findParser(file);
Collection<XNode> nodes = parser.parseCollection(file);
Iterator<XNode> nodesIterator = nodes.iterator();
List<PrismObject<? extends Objectable>> objects = new ArrayList<PrismObject<? extends Objectable>>();
while (nodesIterator.hasNext()){
XNode node = nodesIterator.next();
PrismObject object = xnodeProcessor.parseObject(node);
objects.add(object);
}
return objects;
}
public <O extends Objectable> String serializeObjectToString(PrismObject<O> object, String language) throws SchemaException {
Parser parser = getParserNotNull(language);
RootXNode xroot = xnodeProcessor.serializeObject(object);
return parser.serializeToString(xroot);
}
public <C extends Containerable> String serializeContainerValueToString(PrismContainerValue<C> cval, QName elementName, String language) throws SchemaException {
Parser parser = getParserNotNull(language);
RootXNode xroot = xnodeProcessor.serializeContainerValueRoot(cval);
return parser.serializeToString(xroot);
}
@Deprecated
public <O extends Objectable> Element serializeToDom(PrismObject<O> object) throws SchemaException {
RootXNode xroot = xnodeProcessor.serializeObject(object);
return parserDom.serializeXRootToElement(xroot);
}
/**
* Method used to marshal objects to xml in debug messages.
* @param object
* @return xml as string
*/
// public String silentMarshalObject(Object object, Trace logger) {
// String xml = null;
// try {
// QName fakeQName=new QName(PrismConstants.NS_PREFIX + "debug", "debugPrintObject");
// if (object instanceof Objectable) {
// xml = prismDomProcessor.serializeObjectToString(((Objectable) object).asPrismObject());
// } else if (object instanceof Containerable) {
// Element fakeParent = DOMUtil.createElement(DOMUtil.getDocument(), fakeQName);
// xml = prismDomProcessor.serializeObjectToString(((Containerable) object).asPrismContainerValue(),
// fakeParent);
// } else {
// xml = prismJaxbProcessor.marshalElementToString(new JAXBElement<Object>(fakeQName, Object.class, object));
// }
// } catch (Exception ex) {
// Trace log = logger != null ? logger : LOGGER;
// LoggingUtils.logException(log, "Couldn't marshal element to string {}", ex, object);
// }
// return xml;
// }
}