XMLParserConfiguration.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. package com.usky.xml;
  2. /*
  3. Copyright (c) 2002 JSON.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. The Software shall be used for Good, not Evil.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  19. SOFTWARE.
  20. */
  21. import java.util.Collections;
  22. import java.util.HashMap;
  23. import java.util.Map;
  24. /**
  25. * Configuration object for the XML parser. The configuration is immutable.
  26. * @author AylwardJ
  27. */
  28. @SuppressWarnings({""})
  29. public class XMLParserConfiguration {
  30. /** Original Configuration of the XML Parser. */
  31. public static final XMLParserConfiguration ORIGINAL
  32. = new XMLParserConfiguration();
  33. /** Original configuration of the XML Parser except that values are kept as strings. */
  34. public static final XMLParserConfiguration KEEP_STRINGS
  35. = new XMLParserConfiguration().withKeepStrings(true);
  36. /**
  37. * When parsing the XML into JSON, specifies if values should be kept as strings (<code>true</code>), or if
  38. * they should try to be guessed into JSON values (numeric, boolean, string)
  39. */
  40. private boolean keepStrings;
  41. /**
  42. * The name of the key in a JSON Object that indicates a CDATA section. Historically this has
  43. * been the value "content" but can be changed. Use <code>null</code> to indicate no CDATA
  44. * processing.
  45. */
  46. private String cDataTagName;
  47. /**
  48. * When parsing the XML into JSON, specifies if values with attribute xsi:nil="true"
  49. * should be kept as attribute(<code>false</code>), or they should be converted to
  50. * <code>null</code>(<code>true</code>)
  51. */
  52. private boolean convertNilAttributeToNull;
  53. /**
  54. * This will allow type conversion for values in XML if xsi:type attribute is defined
  55. */
  56. private Map<String, XMLXsiTypeConverter<?>> xsiTypeMap;
  57. /**
  58. * Default parser configuration. Does not keep strings (tries to implicitly convert
  59. * values), and the CDATA Tag Name is "content".
  60. */
  61. public XMLParserConfiguration () {
  62. this.keepStrings = false;
  63. this.cDataTagName = "content";
  64. this.convertNilAttributeToNull = false;
  65. this.xsiTypeMap = Collections.emptyMap();
  66. }
  67. /**
  68. * Configure the parser string processing and use the default CDATA Tag Name as "content".
  69. * @param keepStrings <code>true</code> to parse all values as string.
  70. * <code>false</code> to try and convert XML string values into a JSON value.
  71. * @deprecated This constructor has been deprecated in favor of using the new builder
  72. * pattern for the configuration.
  73. * This constructor may be removed in a future release.
  74. */
  75. @Deprecated
  76. public XMLParserConfiguration (final boolean keepStrings) {
  77. this(keepStrings, "content", false);
  78. }
  79. /**
  80. * Configure the parser string processing to try and convert XML values to JSON values and
  81. * use the passed CDATA Tag Name the processing value. Pass <code>null</code> to
  82. * disable CDATA processing
  83. * @param cDataTagName<code>null</code> to disable CDATA processing. Any other value
  84. * to use that value as the JSONObject key name to process as CDATA.
  85. * @deprecated This constructor has been deprecated in favor of using the new builder
  86. * pattern for the configuration.
  87. * This constructor may be removed in a future release.
  88. */
  89. @Deprecated
  90. public XMLParserConfiguration (final String cDataTagName) {
  91. this(false, cDataTagName, false);
  92. }
  93. /**
  94. * Configure the parser to use custom settings.
  95. * @param keepStrings <code>true</code> to parse all values as string.
  96. * <code>false</code> to try and convert XML string values into a JSON value.
  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 boolean keepStrings, final String cDataTagName) {
  105. this.keepStrings = keepStrings;
  106. this.cDataTagName = cDataTagName;
  107. this.convertNilAttributeToNull = false;
  108. }
  109. /**
  110. * Configure the parser to use custom settings.
  111. * @param keepStrings <code>true</code> to parse all values as string.
  112. * <code>false</code> to try and convert XML string values into a JSON value.
  113. * @param cDataTagName <code>null</code> to disable CDATA processing. Any other value
  114. * to use that value as the JSONObject key name to process as CDATA.
  115. * @param convertNilAttributeToNull <code>true</code> to parse values with attribute xsi:nil="true" as null.
  116. * <code>false</code> to parse values with attribute xsi:nil="true" as {"xsi:nil":true}.
  117. * @deprecated This constructor has been deprecated in favor of using the new builder
  118. * pattern for the configuration.
  119. * This constructor may be removed or marked private in a future release.
  120. */
  121. @Deprecated
  122. public XMLParserConfiguration (final boolean keepStrings, final String cDataTagName, final boolean convertNilAttributeToNull) {
  123. this.keepStrings = keepStrings;
  124. this.cDataTagName = cDataTagName;
  125. this.convertNilAttributeToNull = convertNilAttributeToNull;
  126. }
  127. /**
  128. * Configure the parser to use custom settings.
  129. * @param keepStrings <code>true</code> to parse all values as string.
  130. * <code>false</code> to try and convert XML string values into a JSON value.
  131. * @param cDataTagName <code>null</code> to disable CDATA processing. Any other value
  132. * to use that value as the JSONObject key name to process as CDATA.
  133. * @param convertNilAttributeToNull <code>true</code> to parse values with attribute xsi:nil="true" as null.
  134. * <code>false</code> to parse values with attribute xsi:nil="true" as {"xsi:nil":true}.
  135. * @param xsiTypeMap <code>new HashMap<String, XMLXsiTypeConverter<?>>()</code> to parse values with attribute
  136. * xsi:type="integer" as integer, xsi:type="string" as string
  137. */
  138. private XMLParserConfiguration (final boolean keepStrings, final String cDataTagName,
  139. final boolean convertNilAttributeToNull, final Map<String, XMLXsiTypeConverter<?>> xsiTypeMap ) {
  140. this.keepStrings = keepStrings;
  141. this.cDataTagName = cDataTagName;
  142. this.convertNilAttributeToNull = convertNilAttributeToNull;
  143. this.xsiTypeMap = Collections.unmodifiableMap(xsiTypeMap);
  144. }
  145. /**
  146. * Provides a new instance of the same configuration.
  147. */
  148. @Override
  149. protected XMLParserConfiguration clone() {
  150. // future modifications to this method should always ensure a "deep"
  151. // clone in the case of collections. i.e. if a Map is added as a configuration
  152. // item, a new map instance should be created and if possible each value in the
  153. // map should be cloned as well. If the values of the map are known to also
  154. // be immutable, then a shallow clone of the map is acceptable.
  155. return new XMLParserConfiguration(
  156. this.keepStrings,
  157. this.cDataTagName,
  158. this.convertNilAttributeToNull,
  159. this.xsiTypeMap
  160. );
  161. }
  162. /**
  163. * When parsing the XML into JSON, specifies if values should be kept as strings (<code>true</code>), or if
  164. * they should try to be guessed into JSON values (numeric, boolean, string)
  165. *
  166. * @return The {@link #keepStrings} configuration value.
  167. */
  168. public boolean isKeepStrings() {
  169. return this.keepStrings;
  170. }
  171. /**
  172. * When parsing the XML into JSON, specifies if values should be kept as strings (<code>true</code>), or if
  173. * they should try to be guessed into JSON values (numeric, boolean, string)
  174. *
  175. * @param newVal
  176. * new value to use for the {@link #keepStrings} configuration option.
  177. *
  178. * @return The existing configuration will not be modified. A new configuration is returned.
  179. */
  180. public XMLParserConfiguration withKeepStrings(final boolean newVal) {
  181. XMLParserConfiguration newConfig = this.clone();
  182. newConfig.keepStrings = newVal;
  183. return newConfig;
  184. }
  185. /**
  186. * The name of the key in a JSON Object that indicates a CDATA section. Historically this has
  187. * been the value "content" but can be changed. Use <code>null</code> to indicate no CDATA
  188. * processing.
  189. *
  190. * @return The {@link #cDataTagName} configuration value.
  191. */
  192. public String getcDataTagName() {
  193. return this.cDataTagName;
  194. }
  195. /**
  196. * The name of the key in a JSON Object that indicates a CDATA section. Historically this has
  197. * been the value "content" but can be changed. Use <code>null</code> to indicate no CDATA
  198. * processing.
  199. *
  200. * @param newVal
  201. * new value to use for the {@link #cDataTagName} configuration option.
  202. *
  203. * @return The existing configuration will not be modified. A new configuration is returned.
  204. */
  205. public XMLParserConfiguration withcDataTagName(final String newVal) {
  206. XMLParserConfiguration newConfig = this.clone();
  207. newConfig.cDataTagName = newVal;
  208. return newConfig;
  209. }
  210. /**
  211. * When parsing the XML into JSON, specifies if values with attribute xsi:nil="true"
  212. * should be kept as attribute(<code>false</code>), or they should be converted to
  213. * <code>null</code>(<code>true</code>)
  214. *
  215. * @return The {@link #convertNilAttributeToNull} configuration value.
  216. */
  217. public boolean isConvertNilAttributeToNull() {
  218. return this.convertNilAttributeToNull;
  219. }
  220. /**
  221. * When parsing the XML into JSON, specifies if values with attribute xsi:nil="true"
  222. * should be kept as attribute(<code>false</code>), or they should be converted to
  223. * <code>null</code>(<code>true</code>)
  224. *
  225. * @param newVal
  226. * new value to use for the {@link #convertNilAttributeToNull} configuration option.
  227. *
  228. * @return The existing configuration will not be modified. A new configuration is returned.
  229. */
  230. public XMLParserConfiguration withConvertNilAttributeToNull(final boolean newVal) {
  231. XMLParserConfiguration newConfig = this.clone();
  232. newConfig.convertNilAttributeToNull = newVal;
  233. return newConfig;
  234. }
  235. /**
  236. * When parsing the XML into JSON, specifies that the values with attribute xsi:type
  237. * will be converted to target type defined to client in this configuration
  238. * {@code Map<String, XMLXsiTypeConverter<?>>} to parse values with attribute
  239. * xsi:type="integer" as integer, xsi:type="string" as string
  240. * @return {@link #xsiTypeMap} unmodifiable configuration map.
  241. */
  242. public Map<String, XMLXsiTypeConverter<?>> getXsiTypeMap() {
  243. return this.xsiTypeMap;
  244. }
  245. /**
  246. * When parsing the XML into JSON, specifies that the values with attribute xsi:type
  247. * will be converted to target type defined to client in this configuration
  248. * {@code Map<String, XMLXsiTypeConverter<?>>} to parse values with attribute
  249. * xsi:type="integer" as integer, xsi:type="string" as string
  250. * @param xsiTypeMap {@code new HashMap<String, XMLXsiTypeConverter<?>>()} to parse values with attribute
  251. * xsi:type="integer" as integer, xsi:type="string" as string
  252. * @return The existing configuration will not be modified. A new configuration is returned.
  253. */
  254. public XMLParserConfiguration withXsiTypeMap(final Map<String, XMLXsiTypeConverter<?>> xsiTypeMap) {
  255. XMLParserConfiguration newConfig = this.clone();
  256. Map<String, XMLXsiTypeConverter<?>> cloneXsiTypeMap = new HashMap<String, XMLXsiTypeConverter<?>>(xsiTypeMap);
  257. newConfig.xsiTypeMap = Collections.unmodifiableMap(cloneXsiTypeMap);
  258. return newConfig;
  259. }
  260. }