1 /* |
1 /* |
2 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
5 * This code is free software; you can redistribute it and/or modify it |
6 * under the terms of the GNU General Public License version 2 only, as |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. Oracle designates this |
7 * published by the Free Software Foundation. Oracle designates this |
58 * |
58 * |
59 * @since 2.0 |
59 * @since 2.0 |
60 * @author Kohsuke Kawaguchi |
60 * @author Kohsuke Kawaguchi |
61 */ |
61 */ |
62 public class ContextFactory { |
62 public class ContextFactory { |
|
63 |
63 /** |
64 /** |
64 * The API will invoke this method via reflection |
65 * The API will invoke this method via reflection |
65 */ |
66 */ |
66 public static JAXBContext createContext( Class[] classes, Map<String,Object> properties ) throws JAXBException { |
67 public static JAXBContext createContext(Class[] classes, Map<String,Object> properties ) throws JAXBException { |
67 // fool-proof check, and copy the map to make it easier to find unrecognized properties. |
68 // fool-proof check, and copy the map to make it easier to find unrecognized properties. |
68 if(properties==null) |
69 if(properties==null) |
69 properties = Collections.emptyMap(); |
70 properties = Collections.emptyMap(); |
70 else |
71 else |
71 properties = new HashMap<String,Object>(properties); |
72 properties = new HashMap<String,Object>(properties); |
74 |
75 |
75 Boolean c14nSupport = getPropertyValue(properties,JAXBRIContext.CANONICALIZATION_SUPPORT,Boolean.class); |
76 Boolean c14nSupport = getPropertyValue(properties,JAXBRIContext.CANONICALIZATION_SUPPORT,Boolean.class); |
76 if(c14nSupport==null) |
77 if(c14nSupport==null) |
77 c14nSupport = false; |
78 c14nSupport = false; |
78 |
79 |
|
80 Boolean disablesecurityProcessing = getPropertyValue(properties, JAXBRIContext.DISABLE_XML_SECURITY, Boolean.class); |
|
81 if (disablesecurityProcessing==null) |
|
82 disablesecurityProcessing = false; |
|
83 |
79 Boolean allNillable = getPropertyValue(properties,JAXBRIContext.TREAT_EVERYTHING_NILLABLE,Boolean.class); |
84 Boolean allNillable = getPropertyValue(properties,JAXBRIContext.TREAT_EVERYTHING_NILLABLE,Boolean.class); |
80 if(allNillable==null) |
85 if(allNillable==null) |
81 allNillable = false; |
86 allNillable = false; |
82 |
87 |
83 Boolean retainPropertyInfo = getPropertyValue(properties, JAXBRIContext.RETAIN_REFERENCE_TO_INFO, Boolean.class); |
88 Boolean retainPropertyInfo = getPropertyValue(properties, JAXBRIContext.RETAIN_REFERENCE_TO_INFO, Boolean.class); |
87 Boolean supressAccessorWarnings = getPropertyValue(properties, JAXBRIContext.SUPRESS_ACCESSOR_WARNINGS, Boolean.class); |
92 Boolean supressAccessorWarnings = getPropertyValue(properties, JAXBRIContext.SUPRESS_ACCESSOR_WARNINGS, Boolean.class); |
88 if(supressAccessorWarnings==null) |
93 if(supressAccessorWarnings==null) |
89 supressAccessorWarnings = false; |
94 supressAccessorWarnings = false; |
90 |
95 |
91 Boolean improvedXsiTypeHandling = getPropertyValue(properties, JAXBRIContext.IMPROVED_XSI_TYPE_HANDLING, Boolean.class); |
96 Boolean improvedXsiTypeHandling = getPropertyValue(properties, JAXBRIContext.IMPROVED_XSI_TYPE_HANDLING, Boolean.class); |
92 if(improvedXsiTypeHandling == null) |
97 if (improvedXsiTypeHandling == null) { |
93 improvedXsiTypeHandling = true; |
98 String improvedXsiSystemProperty = Util.getSystemProperty(JAXBRIContext.IMPROVED_XSI_TYPE_HANDLING); |
|
99 if (improvedXsiSystemProperty == null) { |
|
100 improvedXsiTypeHandling = true; |
|
101 } else { |
|
102 improvedXsiTypeHandling = Boolean.valueOf(improvedXsiSystemProperty); |
|
103 } |
|
104 } |
94 |
105 |
95 Boolean xmlAccessorFactorySupport = getPropertyValue(properties, |
106 Boolean xmlAccessorFactorySupport = getPropertyValue(properties, |
96 JAXBRIContext.XMLACCESSORFACTORY_SUPPORT,Boolean.class); |
107 JAXBRIContext.XMLACCESSORFACTORY_SUPPORT,Boolean.class); |
97 if(xmlAccessorFactorySupport==null){ |
108 if(xmlAccessorFactorySupport==null){ |
98 xmlAccessorFactorySupport = false; |
109 xmlAccessorFactorySupport = false; |
101 "is not active. Using JAXB's implementation"); |
112 "is not active. Using JAXB's implementation"); |
102 } |
113 } |
103 |
114 |
104 RuntimeAnnotationReader ar = getPropertyValue(properties,JAXBRIContext.ANNOTATION_READER,RuntimeAnnotationReader.class); |
115 RuntimeAnnotationReader ar = getPropertyValue(properties,JAXBRIContext.ANNOTATION_READER,RuntimeAnnotationReader.class); |
105 |
116 |
|
117 Collection<TypeReference> tr = getPropertyValue(properties, JAXBRIContext.TYPE_REFERENCES, Collection.class); |
|
118 if (tr == null) { |
|
119 tr = Collections.<TypeReference>emptyList(); |
|
120 } |
|
121 |
106 Map<Class,Class> subclassReplacements; |
122 Map<Class,Class> subclassReplacements; |
107 try { |
123 try { |
108 subclassReplacements = TypeCast.checkedCast( |
124 subclassReplacements = TypeCast.checkedCast( |
109 getPropertyValue(properties, JAXBRIContext.SUBCLASS_REPLACEMENTS, Map.class), Class.class, Class.class); |
125 getPropertyValue(properties, JAXBRIContext.SUBCLASS_REPLACEMENTS, Map.class), Class.class, Class.class); |
110 } catch (ClassCastException e) { |
126 } catch (ClassCastException e) { |
115 throw new JAXBException(Messages.UNSUPPORTED_PROPERTY.format(properties.keySet().iterator().next())); |
131 throw new JAXBException(Messages.UNSUPPORTED_PROPERTY.format(properties.keySet().iterator().next())); |
116 } |
132 } |
117 |
133 |
118 JAXBContextImpl.JAXBContextBuilder builder = new JAXBContextImpl.JAXBContextBuilder(); |
134 JAXBContextImpl.JAXBContextBuilder builder = new JAXBContextImpl.JAXBContextBuilder(); |
119 builder.setClasses(classes); |
135 builder.setClasses(classes); |
120 builder.setTypeRefs(Collections.<TypeReference>emptyList()); |
136 builder.setTypeRefs(tr); |
121 builder.setSubclassReplacements(subclassReplacements); |
137 builder.setSubclassReplacements(subclassReplacements); |
122 builder.setDefaultNsUri(defaultNsUri); |
138 builder.setDefaultNsUri(defaultNsUri); |
123 builder.setC14NSupport(c14nSupport); |
139 builder.setC14NSupport(c14nSupport); |
124 builder.setAnnotationReader(ar); |
140 builder.setAnnotationReader(ar); |
125 builder.setXmlAccessorFactorySupport(xmlAccessorFactorySupport); |
141 builder.setXmlAccessorFactorySupport(xmlAccessorFactorySupport); |
126 builder.setAllNillable(allNillable); |
142 builder.setAllNillable(allNillable); |
127 builder.setRetainPropertyInfo(retainPropertyInfo); |
143 builder.setRetainPropertyInfo(retainPropertyInfo); |
128 builder.setSupressAccessorWarnings(supressAccessorWarnings); |
144 builder.setSupressAccessorWarnings(supressAccessorWarnings); |
129 builder.setImprovedXsiTypeHandling(improvedXsiTypeHandling); |
145 builder.setImprovedXsiTypeHandling(improvedXsiTypeHandling); |
|
146 builder.setDisableSecurityProcessing(disablesecurityProcessing); |
130 return builder.build(); |
147 return builder.build(); |
131 } |
148 } |
132 |
149 |
133 /** |
150 /** |
134 * If a key is present in the map, remove the value and return it. |
151 * If a key is present in the map, remove the value and return it. |
142 throw new JAXBException(Messages.INVALID_PROPERTY_VALUE.format(keyName,o)); |
159 throw new JAXBException(Messages.INVALID_PROPERTY_VALUE.format(keyName,o)); |
143 else |
160 else |
144 return type.cast(o); |
161 return type.cast(o); |
145 } |
162 } |
146 |
163 |
|
164 /** |
|
165 * |
|
166 * @param classes |
|
167 * @param typeRefs |
|
168 * @param subclassReplacements |
|
169 * @param defaultNsUri |
|
170 * @param c14nSupport |
|
171 * @param ar |
|
172 * @param xmlAccessorFactorySupport |
|
173 * @param allNillable |
|
174 * @param retainPropertyInfo |
|
175 * @return |
|
176 * @throws JAXBException |
|
177 * @deprecated use createContext(Class[] classes, Map<String,Object> properties) method instead |
|
178 */ |
|
179 @Deprecated |
147 public static JAXBRIContext createContext( Class[] classes, |
180 public static JAXBRIContext createContext( Class[] classes, |
148 Collection<TypeReference> typeRefs, Map<Class,Class> subclassReplacements, |
181 Collection<TypeReference> typeRefs, Map<Class,Class> subclassReplacements, |
149 String defaultNsUri, boolean c14nSupport, RuntimeAnnotationReader ar, |
182 String defaultNsUri, boolean c14nSupport, RuntimeAnnotationReader ar, |
150 boolean xmlAccessorFactorySupport, boolean allNillable, boolean retainPropertyInfo) throws JAXBException { |
183 boolean xmlAccessorFactorySupport, boolean allNillable, boolean retainPropertyInfo) throws JAXBException { |
151 |
184 |
152 return createContext(classes, typeRefs, subclassReplacements, |
185 return createContext(classes, typeRefs, subclassReplacements, |
153 defaultNsUri, c14nSupport, ar, xmlAccessorFactorySupport, |
186 defaultNsUri, c14nSupport, ar, xmlAccessorFactorySupport, |
154 allNillable, retainPropertyInfo, false); |
187 allNillable, retainPropertyInfo, false); |
155 } |
188 } |
156 |
189 |
|
190 /** |
|
191 * |
|
192 * @param classes |
|
193 * @param typeRefs |
|
194 * @param subclassReplacements |
|
195 * @param defaultNsUri |
|
196 * @param c14nSupport |
|
197 * @param ar |
|
198 * @param xmlAccessorFactorySupport |
|
199 * @param allNillable |
|
200 * @param retainPropertyInfo |
|
201 * @param improvedXsiTypeHandling |
|
202 * @return |
|
203 * @throws JAXBException |
|
204 * @deprecated use createContext( Class[] classes, Map<String,Object> properties) method instead |
|
205 */ |
|
206 @Deprecated |
157 public static JAXBRIContext createContext( Class[] classes, |
207 public static JAXBRIContext createContext( Class[] classes, |
158 Collection<TypeReference> typeRefs, Map<Class,Class> subclassReplacements, |
208 Collection<TypeReference> typeRefs, Map<Class,Class> subclassReplacements, |
159 String defaultNsUri, boolean c14nSupport, RuntimeAnnotationReader ar, |
209 String defaultNsUri, boolean c14nSupport, RuntimeAnnotationReader ar, |
160 boolean xmlAccessorFactorySupport, boolean allNillable, boolean retainPropertyInfo, boolean improvedXsiTypeHandling) throws JAXBException { |
210 boolean xmlAccessorFactorySupport, boolean allNillable, boolean retainPropertyInfo, boolean improvedXsiTypeHandling) throws JAXBException { |
161 |
211 |