|
16 | 16 | */ |
17 | 17 | @SuppressWarnings({""}) |
18 | 18 | public class XMLParserConfiguration { |
| 19 | + /** |
| 20 | + * Used to indicate there's no defined limit to the maximum nesting depth when parsing a XML |
| 21 | + * document to JSON. |
| 22 | + */ |
| 23 | + public static final int UNDEFINED_MAXIMUM_NESTING_DEPTH = -1; |
| 24 | + |
| 25 | + /** |
| 26 | + * The default maximum nesting depth when parsing a XML document to JSON. |
| 27 | + */ |
| 28 | + public static final int DEFAULT_MAXIMUM_NESTING_DEPTH = 512; |
| 29 | + |
19 | 30 | /** Original Configuration of the XML Parser. */ |
20 | 31 | public static final XMLParserConfiguration ORIGINAL |
21 | 32 | = new XMLParserConfiguration(); |
@@ -54,6 +65,12 @@ public class XMLParserConfiguration { |
54 | 65 | */ |
55 | 66 | private Set<String> forceList; |
56 | 67 |
|
| 68 | + /** |
| 69 | + * When parsing the XML into JSON, specifies the tags whose values should be converted |
| 70 | + * to arrays |
| 71 | + */ |
| 72 | + private int maxNestingDepth = DEFAULT_MAXIMUM_NESTING_DEPTH; |
| 73 | + |
57 | 74 | /** |
58 | 75 | * Default parser configuration. Does not keep strings (tries to implicitly convert |
59 | 76 | * values), and the CDATA Tag Name is "content". |
@@ -297,4 +314,33 @@ public XMLParserConfiguration withForceList(final Set<String> forceList) { |
297 | 314 | newConfig.forceList = Collections.unmodifiableSet(cloneForceList); |
298 | 315 | return newConfig; |
299 | 316 | } |
| 317 | + |
| 318 | + /** |
| 319 | + * The maximum nesting depth that the parser will descend before throwing an exception |
| 320 | + * when parsing the XML into JSON. |
| 321 | + * @return the maximum nesting depth set for this configuration |
| 322 | + */ |
| 323 | + public int getMaxNestingDepth() { |
| 324 | + return maxNestingDepth; |
| 325 | + } |
| 326 | + |
| 327 | + /** |
| 328 | + * Defines the maximum nesting depth that the parser will descend before throwing an exception |
| 329 | + * when parsing the XML into JSON. The default max nesting depth is 512, which means the parser |
| 330 | + * will go as deep as the maximum call stack size allows. Using any negative value as a |
| 331 | + * parameter is equivalent to setting no limit to the nesting depth. |
| 332 | + * @param maxNestingDepth the maximum nesting depth allowed to the XML parser |
| 333 | + * @return The existing configuration will not be modified. A new configuration is returned. |
| 334 | + */ |
| 335 | + public XMLParserConfiguration withMaxNestingDepth(int maxNestingDepth) { |
| 336 | + XMLParserConfiguration newConfig = this.clone(); |
| 337 | + |
| 338 | + if (maxNestingDepth > UNDEFINED_MAXIMUM_NESTING_DEPTH) { |
| 339 | + newConfig.maxNestingDepth = maxNestingDepth; |
| 340 | + } else { |
| 341 | + newConfig.maxNestingDepth = UNDEFINED_MAXIMUM_NESTING_DEPTH; |
| 342 | + } |
| 343 | + |
| 344 | + return newConfig; |
| 345 | + } |
300 | 346 | } |
0 commit comments