/
XsdTypeMapper.java
266 lines (238 loc) · 9.51 KB
/
XsdTypeMapper.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
/*
* 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.xml;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.xml.XMLConstants;
import javax.xml.datatype.Duration;
import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.namespace.QName;
import com.evolveum.prism.xml.ns._public.types_3.ItemPathType;
import com.evolveum.prism.xml.ns._public.types_3.RawType;
import org.apache.commons.lang.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import com.evolveum.midpoint.prism.PrismConstants;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.prism.polystring.PolyString;
import com.evolveum.midpoint.util.DOMUtil;
import com.evolveum.midpoint.util.QNameUtil;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
/**
* Maintains mapping of XSD types (qnames) and Java types (classes)
*
* @author Radovan Semancik
*/
public class XsdTypeMapper {
public static final String BOOLEAN_XML_VALUE_TRUE = "true";
public static final String BOOLEAN_XML_VALUE_FALSE = "false";
private static final Map<Class, QName> javaToXsdTypeMap = new HashMap<>();
private static final Map<QName, Class> xsdToJavaTypeMap = new HashMap<>();
private static final Map<Class, QName> javaToXsdTypeMapExt = new HashMap<>();
private static final Map<QName, Class> xsdToJavaTypeMapExt = new HashMap<>();
private static final Trace LOGGER = TraceManager.getTrace(XsdTypeMapper.class);
private static final String MULTIPLICITY_UNBOUNDED = "unbounded";
private static void initTypeMap() throws IOException, ClassNotFoundException {
addMapping(String.class, DOMUtil.XSD_STRING, true);
addMapping(char.class, DOMUtil.XSD_STRING, false);
addMapping(File.class, DOMUtil.XSD_STRING, false);
addMapping(int.class, DOMUtil.XSD_INT, true);
addMapping(Integer.class, DOMUtil.XSD_INT, false);
addMapping(BigInteger.class, DOMUtil.XSD_INTEGER, true);
addMapping(BigDecimal.class, DOMUtil.XSD_DECIMAL, true);
addMapping(double.class, DOMUtil.XSD_DOUBLE, true);
addMapping(Double.class, DOMUtil.XSD_DOUBLE, false);
addMapping(float.class, DOMUtil.XSD_FLOAT, true);
addMapping(Float.class, DOMUtil.XSD_FLOAT, false);
//maybe this is not a great idea
addMapping(long.class, DOMUtil.XSD_LONG, true);
addMapping(Long.class, DOMUtil.XSD_LONG, false);
addMapping(short.class, DOMUtil.XSD_SHORT, true);
addMapping(Short.class, DOMUtil.XSD_SHORT, false);
addMapping(byte.class, DOMUtil.XSD_BYTE, true);
addMapping(Byte.class, DOMUtil.XSD_BYTE, false);
//great idea end
addMapping(boolean.class, DOMUtil.XSD_BOOLEAN, true);
addMapping(Boolean.class, DOMUtil.XSD_BOOLEAN, false);
addMapping(byte[].class, DOMUtil.XSD_BASE64BINARY, true);
addMapping(GregorianCalendar.class, DOMUtil.XSD_DATETIME, true);
addMapping(XMLGregorianCalendar.class, DOMUtil.XSD_DATETIME, true);
addMapping(Duration.class, DOMUtil.XSD_DURATION, true);
addMapping(ItemPathType.class, ItemPathType.COMPLEX_TYPE, true);
addMapping(ItemPath.class, ItemPathType.COMPLEX_TYPE, false);
addMapping(QName.class, DOMUtil.XSD_QNAME, true);
addMapping(PolyString.class, PrismConstants.POLYSTRING_TYPE_QNAME, true);
addMapping(RawType.class, DOMUtil.XSD_STRING, false);
addMappingExt(ItemPathType.class, ItemPathType.COMPLEX_TYPE, true); // TODO remove
xsdToJavaTypeMap.put(DOMUtil.XSD_ANYURI, String.class);
}
private static void addMapping(Class javaClass, QName xsdType, boolean both) {
LOGGER.trace("Adding XSD type mapping {} {} {} ", javaClass, both ? "<->" : " ->", xsdType);
javaToXsdTypeMap.put(javaClass, xsdType);
if (both) {
xsdToJavaTypeMap.put(xsdType, javaClass);
}
}
private static void addMappingExt(Class javaClass, QName xsdType, boolean both) {
LOGGER.trace("Adding 'ext' XSD type mapping {} {} {} ", javaClass, both ? "<->" : " ->", xsdType);
javaToXsdTypeMapExt.put(javaClass, xsdType);
if (both) {
xsdToJavaTypeMapExt.put(xsdType, javaClass);
}
}
public static QName toXsdType(Class javaClass) {
QName xsdType = getJavaToXsdMapping(javaClass);
if (xsdType == null) {
throw new IllegalArgumentException("No XSD mapping for Java type " + javaClass.getCanonicalName());
}
return xsdType;
}
public static QName getJavaToXsdMapping(Class<?> type) {
if (javaToXsdTypeMap.containsKey(type)) {
return javaToXsdTypeMap.get(type);
}
Class<?> superType = type.getSuperclass();
if (superType != null) {
return getJavaToXsdMapping(superType);
}
return null;
}
public static QName determineQNameWithNs(QName xsdType){
if (StringUtils.isNotBlank(xsdType.getNamespaceURI())){
return xsdType;
}
Set<QName> keys = xsdToJavaTypeMap.keySet();
for (Iterator<QName> iterator = keys.iterator(); iterator.hasNext();){
QName key = iterator.next();
if (QNameUtil.match(key, xsdType)){
return key;
}
}
return null;
}
public static <T> Class<T> getXsdToJavaMapping(QName xsdType) {
Class clazz = xsdToJavaTypeMap.get(xsdType);
if (clazz == null){
Set<Map.Entry<QName, Class>> entries = xsdToJavaTypeMap.entrySet();
for (Map.Entry<QName, Class> entry : entries) {
QName key = entry.getKey();
if (QNameUtil.match(key, xsdType)){
return entry.getValue();
}
}
}
return xsdToJavaTypeMap.get(xsdType);
}
/**
* Returns the class in the type mapping.
* The class supplied by the caller may be a subclass of what we have in the map.
* This returns the class that in the mapping.
*/
public static Class<?> getTypeFromClass(Class<?> clazz) {
if (javaToXsdTypeMap.containsKey(clazz)) {
return clazz;
}
Class<?> superClazz = clazz.getSuperclass();
if (superClazz != null) {
return getTypeFromClass(superClazz);
}
return null;
}
@Nullable
public static <T> Class<T> toJavaType(@NotNull QName xsdType) {
//noinspection ConstantConditions
return toJavaType(xsdToJavaTypeMap, xsdType, true);
}
@Nullable
public static <T> Class<T> toJavaTypeIfKnown(@NotNull QName xsdType) {
return toJavaType(xsdToJavaTypeMap, xsdType, false);
}
// experimental feature - covers all the classes
public static <T> Class<T> toJavaTypeIfKnownExt(@NotNull QName xsdType) {
Class<T> cls = toJavaType(xsdToJavaTypeMap, xsdType, false);
if (cls != null) {
return cls;
} else {
return toJavaType(xsdToJavaTypeMapExt, xsdType, false);
}
}
@Nullable
private static <T> Class<T> toJavaType(Map<QName, Class> map, @NotNull QName xsdType, boolean errorIfNoMapping) {
Class<?> javaType = map.get(xsdType);
if (javaType == null && StringUtils.isEmpty(xsdType.getNamespaceURI())) {
// TODO check uniqueness w.r.t. other types...
for (Map.Entry<QName,Class> entry : xsdToJavaTypeMap.entrySet()) {
if (QNameUtil.match(entry.getKey(), xsdType)) {
javaType = entry.getValue();
break;
}
}
}
if (javaType == null) {
if (errorIfNoMapping && xsdType.getNamespaceURI().equals(XMLConstants.W3C_XML_SCHEMA_NS_URI)) {
throw new IllegalArgumentException("No type mapping for XSD type " + xsdType);
} else {
return null;
}
}
@SuppressWarnings("unchecked")
Class<T> typedClass = (Class<T>) javaType;
return typedClass;
}
public static String multiplicityToString(Integer integer) {
if (integer == null) {
return null;
}
if (integer < 0) {
return MULTIPLICITY_UNBOUNDED;
}
return integer.toString();
}
public static Integer multiplicityToInteger(String string) {
if (string == null || StringUtils.isEmpty(string)) {
return null;
}
if (MULTIPLICITY_UNBOUNDED.equals(string)) {
return -1;
}
return Integer.valueOf(string);
}
public static boolean isMatchingMultiplicity(int number, int min, int max) {
if (min >= 0 && number < min) {
return false;
}
if (max >= 0 && number > max) {
return false;
}
return true;
}
static {
try {
initTypeMap();
} catch (Exception e) {
LOGGER.error("Cannot initialize XSD type mapping: " + e.getMessage(), e);
throw new IllegalStateException("Cannot initialize XSD type mapping: " + e.getMessage(), e);
}
}
}