1 package org.json;
2 /*
3 Copyright (c) 2002 JSON.org
4
5 Permission is hereby granted, free of charge, to any person obtaining a copy
6 of this software and associated documentation files (the "Software"), to deal
7 in the Software without restriction, including without limitation the rights
8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 copies of the Software, and to permit persons to whom the Software is
10 furnished to do so, subject to the following conditions:
11
12 The above copyright notice and this permission notice shall be included in all
13 copies or substantial portions of the Software.
14
15 The Software shall be used for Good, not Evil.
16
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 SOFTWARE.
24 */

25
26 import java.util.Collections;
27 import java.util.HashMap;
28 import java.util.Map;
29
30
31 /**
32  * Configuration object for the XML parser. The configuration is immutable.
33  * @author AylwardJ
34  */

35 @SuppressWarnings({""})
36 public class XMLParserConfiguration {
37     /** Original Configuration of the XML Parser. */
38     public static final XMLParserConfiguration ORIGINAL
39         = new XMLParserConfiguration();
40     /** Original configuration of the XML Parser except that values are kept as strings. */
41     public static final XMLParserConfiguration KEEP_STRINGS
42         = new XMLParserConfiguration().withKeepStrings(true);
43
44     /**
45      * When parsing the XML into JSON, specifies if values should be kept as strings (<code>true</code>), or if
46      * they should try to be guessed into JSON values (numeric, boolean, string)
47      */

48     private boolean keepStrings;
49     
50     /**
51      * The name of the key in a JSON Object that indicates a CDATA section. Historically this has
52      * been the value "content" but can be changed. Use <code>null</code> to indicate no CDATA
53      * processing.
54      */

55     private String cDataTagName;
56     
57     /**
58      * When parsing the XML into JSON, specifies if values with attribute xsi:nil="true"
59      * should be kept as attribute(<code>false</code>), or they should be converted to
60      * <code>null</code>(<code>true</code>)
61      */

62     private boolean convertNilAttributeToNull;
63
64     /**
65      * This will allow type conversion for values in XML if xsi:type attribute is defined
66      */

67     private Map<String, XMLXsiTypeConverter<?>> xsiTypeMap;
68
69     /**
70      * Default parser configuration. Does not keep strings (tries to implicitly convert
71      * values), and the CDATA Tag Name is "content".
72      */

73     public XMLParserConfiguration () {
74         this.keepStrings = false;
75         this.cDataTagName = "content";
76         this.convertNilAttributeToNull = false;
77         this.xsiTypeMap = Collections.emptyMap();
78     }
79
80     /**
81      * Configure the parser string processing and use the default CDATA Tag Name as "content".
82      * @param keepStrings <code>true</code> to parse all values as string.
83      *      <code>false</code> to try and convert XML string values into a JSON value.
84      * @deprecated This constructor has been deprecated in favor of using the new builder
85      *      pattern for the configuration.
86      *      This constructor may be removed in a future release.
87      */

88     @Deprecated
89     public XMLParserConfiguration (final boolean keepStrings) {
90         this(keepStrings, "content"false);
91     }
92
93     /**
94      * Configure the parser string processing to try and convert XML values to JSON values and
95      * use the passed CDATA Tag Name the processing value. Pass <code>null</code> to
96      * disable CDATA processing
97      * @param cDataTagName<code>null</code> to disable CDATA processing. Any other value
98      *      to use that value as the JSONObject key name to process as CDATA.
99      * @deprecated This constructor has been deprecated in favor of using the new builder
100      *      pattern for the configuration.
101      *      This constructor may be removed in a future release.
102      */

103     @Deprecated
104     public XMLParserConfiguration (final String cDataTagName) {
105         this(false, cDataTagName, false);
106     }
107
108     /**
109      * Configure the parser to use custom settings.
110      * @param keepStrings <code>true</code> to parse all values as string.
111      *      <code>false</code> to try and convert XML string values into a JSON value.
112      * @param cDataTagName<code>null</code> to disable CDATA processing. Any other value
113      *      to use that value as the JSONObject key name to process as CDATA.
114      * @deprecated This constructor has been deprecated in favor of using the new builder
115      *      pattern for the configuration.
116      *      This constructor may be removed in a future release.
117      */

118     @Deprecated
119     public XMLParserConfiguration (final boolean keepStrings, final String cDataTagName) {
120         this.keepStrings = keepStrings;
121         this.cDataTagName = cDataTagName;
122         this.convertNilAttributeToNull = false;
123     }
124
125     /**
126      * Configure the parser to use custom settings.
127      * @param keepStrings <code>true</code> to parse all values as string.
128      *      <code>false</code> to try and convert XML string values into a JSON value.
129      * @param cDataTagName <code>null</code> to disable CDATA processing. Any other value
130      *      to use that value as the JSONObject key name to process as CDATA.
131      * @param convertNilAttributeToNull <code>true</code> to parse values with attribute xsi:nil="true" as null.
132      *                                  <code>false</code> to parse values with attribute xsi:nil="true" as {"xsi:nil":true}.
133      * @deprecated This constructor has been deprecated in favor of using the new builder
134      *      pattern for the configuration.
135      *      This constructor may be removed or marked private in a future release.
136      */

137     @Deprecated
138     public XMLParserConfiguration (final boolean keepStrings, final String cDataTagName, final boolean convertNilAttributeToNull) {
139         this.keepStrings = keepStrings;
140         this.cDataTagName = cDataTagName;
141         this.convertNilAttributeToNull = convertNilAttributeToNull;
142     }
143
144     /**
145      * Configure the parser to use custom settings.
146      * @param keepStrings <code>true</code> to parse all values as string.
147      *      <code>false</code> to try and convert XML string values into a JSON value.
148      * @param cDataTagName <code>null</code> to disable CDATA processing. Any other value
149      *      to use that value as the JSONObject key name to process as CDATA.
150      * @param convertNilAttributeToNull <code>true</code> to parse values with attribute xsi:nil="true" as null.
151      *                                  <code>false</code> to parse values with attribute xsi:nil="true" as {"xsi:nil":true}.
152      * @param xsiTypeMap  <code>new HashMap<String, XMLXsiTypeConverter<?>>()</code> to parse values with attribute
153      *                   xsi:type="integer" as integer,  xsi:type="string" as string
154      */

155     private XMLParserConfiguration (final boolean keepStrings, final String cDataTagName,
156             final boolean convertNilAttributeToNull, final Map<String, XMLXsiTypeConverter<?>> xsiTypeMap ) {
157         this.keepStrings = keepStrings;
158         this.cDataTagName = cDataTagName;
159         this.convertNilAttributeToNull = convertNilAttributeToNull;
160         this.xsiTypeMap = Collections.unmodifiableMap(xsiTypeMap);
161     }
162
163     /**
164      * Provides a new instance of the same configuration.
165      */

166     @Override
167     protected XMLParserConfiguration clone() {
168         // future modifications to this method should always ensure a "deep"
169         // clone in the case of collections. i.e. if a Map is added as a configuration
170         // item, a new map instance should be created and if possible each value in the
171         // map should be cloned as well. If the values of the map are known to also
172         // be immutable, then a shallow clone of the map is acceptable.
173         return new XMLParserConfiguration(
174                 this.keepStrings,
175                 this.cDataTagName,
176                 this.convertNilAttributeToNull,
177                 this.xsiTypeMap
178         );
179     }
180     
181     /**
182      * When parsing the XML into JSON, specifies if values should be kept as strings (<code>true</code>), or if
183      * they should try to be guessed into JSON values (numeric, boolean, string)
184      * 
185      * @return The {@link #keepStrings} configuration value.
186      */

187     public boolean isKeepStrings() {
188         return this.keepStrings;
189     }
190
191     /**
192      * When parsing the XML into JSON, specifies if values should be kept as strings (<code>true</code>), or if
193      * they should try to be guessed into JSON values (numeric, boolean, string)
194      * 
195      * @param newVal
196      *      new value to use for the {@link #keepStrings} configuration option.
197      * 
198      * @return The existing configuration will not be modified. A new configuration is returned.
199      */

200     public XMLParserConfiguration withKeepStrings(final boolean newVal) {
201         XMLParserConfiguration newConfig = this.clone();
202         newConfig.keepStrings = newVal;
203         return newConfig;
204     }
205
206     /**
207      * The name of the key in a JSON Object that indicates a CDATA section. Historically this has
208      * been the value "content" but can be changed. Use <code>null</code> to indicate no CDATA
209      * processing.
210      * 
211      * @return The {@link #cDataTagName} configuration value.
212      */

213     public String getcDataTagName() {
214         return this.cDataTagName;
215     }
216
217     /**
218      * The name of the key in a JSON Object that indicates a CDATA section. Historically this has
219      * been the value "content" but can be changed. Use <code>null</code> to indicate no CDATA
220      * processing.
221      * 
222      * @param newVal
223      *      new value to use for the {@link #cDataTagName} configuration option.
224      * 
225      * @return The existing configuration will not be modified. A new configuration is returned.
226      */

227     public XMLParserConfiguration withcDataTagName(final String newVal) {
228         XMLParserConfiguration newConfig = this.clone();
229         newConfig.cDataTagName = newVal;
230         return newConfig;
231     }
232
233     /**
234      * When parsing the XML into JSON, specifies if values with attribute xsi:nil="true"
235      * should be kept as attribute(<code>false</code>), or they should be converted to
236      * <code>null</code>(<code>true</code>)
237      * 
238      * @return The {@link #convertNilAttributeToNull} configuration value.
239      */

240     public boolean isConvertNilAttributeToNull() {
241         return this.convertNilAttributeToNull;
242     }
243
244     /**
245      * When parsing the XML into JSON, specifies if values with attribute xsi:nil="true"
246      * should be kept as attribute(<code>false</code>), or they should be converted to
247      * <code>null</code>(<code>true</code>)
248      * 
249      * @param newVal
250      *      new value to use for the {@link #convertNilAttributeToNull} configuration option.
251      * 
252      * @return The existing configuration will not be modified. A new configuration is returned.
253      */

254     public XMLParserConfiguration withConvertNilAttributeToNull(final boolean newVal) {
255         XMLParserConfiguration newConfig = this.clone();
256         newConfig.convertNilAttributeToNull = newVal;
257         return newConfig;
258     }
259
260     /**
261      * When parsing the XML into JSON, specifies that the values with attribute xsi:type
262      * will be converted to target type defined to client in this configuration
263      * {@code Map<String, XMLXsiTypeConverter<?>>} to parse values with attribute
264      * xsi:type="integer" as integer,  xsi:type="string" as string
265      * @return {@link #xsiTypeMap} unmodifiable configuration map.
266      */

267     public Map<String, XMLXsiTypeConverter<?>> getXsiTypeMap() {
268         return this.xsiTypeMap;
269     }
270
271     /**
272      * When parsing the XML into JSON, specifies that the values with attribute xsi:type
273      * will be converted to target type defined to client in this configuration
274      * {@code Map<String, XMLXsiTypeConverter<?>>} to parse values with attribute
275      * xsi:type="integer" as integer,  xsi:type="string" as string
276      * @param xsiTypeMap  {@code new HashMap<String, XMLXsiTypeConverter<?>>()} to parse values with attribute
277      *                   xsi:type="integer" as integer,  xsi:type="string" as string
278      * @return The existing configuration will not be modified. A new configuration is returned.
279      */

280     public XMLParserConfiguration withXsiTypeMap(final Map<String, XMLXsiTypeConverter<?>> xsiTypeMap) {
281         XMLParserConfiguration newConfig = this.clone();
282         Map<String, XMLXsiTypeConverter<?>> cloneXsiTypeMap = new HashMap<String, XMLXsiTypeConverter<?>>(xsiTypeMap);
283         newConfig.xsiTypeMap = Collections.unmodifiableMap(cloneXsiTypeMap);
284         return newConfig;
285     }
286 }
287