Skip to content

Commit 2391d24

Browse files
fix: amend XMLParserConfiguration.clone() to include the new maxNestingDepth param.
Amend Javadoc for XML and XMLParserConfiguration classes.
1 parent 401495a commit 2391d24

File tree

2 files changed

+40
-32
lines changed

2 files changed

+40
-32
lines changed

src/main/java/org/json/XML.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public void remove() {
9898
/**
9999
* Replace special characters with XML escapes:
100100
*
101-
* <pre>{@code
101+
* <pre>{@code
102102
* &amp; (ampersand) is replaced by &amp;amp;
103103
* &lt; (less than) is replaced by &amp;lt;
104104
* &gt; (greater than) is replaced by &amp;gt;
@@ -229,8 +229,12 @@ public static void noSpace(String string) throws JSONException {
229229
* The JSONObject that will include the new material.
230230
* @param name
231231
* The tag name.
232+
* @param config
233+
* The XML parser configuration.
234+
* @param currentNestingDepth
235+
* The current nesting depth.
232236
* @return true if the close tag is processed.
233-
* @throws JSONException
237+
* @throws JSONException Thrown if any parsing error occurs.
234238
*/
235239
private static boolean parse(XMLTokener x, JSONObject context, String name, XMLParserConfiguration config, int currentNestingDepth)
236240
throws JSONException {
@@ -427,7 +431,7 @@ private static boolean parse(XMLTokener x, JSONObject context, String name, XMLP
427431
context.accumulate(tagName, jsonObject);
428432
}
429433
}
430-
434+
431435
return false;
432436
}
433437
}
@@ -491,7 +495,7 @@ public static Object stringToValue(String string) {
491495
}
492496
return string;
493497
}
494-
498+
495499
/**
496500
* direct copy of {@link JSONObject#stringToNumber(String)} to maintain Android support.
497501
*/
@@ -538,7 +542,7 @@ private static Number stringToNumber(final String val) throws NumberFormatExcept
538542
// integer representation.
539543
// This will narrow any values to the smallest reasonable Object representation
540544
// (Integer, Long, or BigInteger)
541-
545+
542546
// BigInteger down conversion: We use a similar bitLength compare as
543547
// BigInteger#intValueExact uses. Increases GC, but objects hold
544548
// only what they need. i.e. Less runtime overhead if the value is
@@ -554,7 +558,7 @@ private static Number stringToNumber(final String val) throws NumberFormatExcept
554558
}
555559
throw new NumberFormatException("val ["+val+"] is not a valid number.");
556560
}
557-
561+
558562
/**
559563
* direct copy of {@link JSONObject#isDecimalNotation(String)} to maintain Android support.
560564
*/
@@ -572,7 +576,7 @@ private static boolean isDecimalNotation(final String val) {
572576
* name/value pairs and arrays of values. JSON does not does not like to
573577
* distinguish between elements and attributes. Sequences of similar
574578
* elements are represented as JSONArrays. Content text may be placed in a
575-
* "content" member. Comments, prologs, DTDs, and <pre>{@code
579+
* "content" member. Comments, prologs, DTDs, and <pre>{@code
576580
* &lt;[ [ ]]>}</pre>
577581
* are ignored.
578582
*
@@ -593,7 +597,7 @@ public static JSONObject toJSONObject(String string) throws JSONException {
593597
* name/value pairs and arrays of values. JSON does not does not like to
594598
* distinguish between elements and attributes. Sequences of similar
595599
* elements are represented as JSONArrays. Content text may be placed in a
596-
* "content" member. Comments, prologs, DTDs, and <pre>{@code
600+
* "content" member. Comments, prologs, DTDs, and <pre>{@code
597601
* &lt;[ [ ]]>}</pre>
598602
* are ignored.
599603
*
@@ -673,7 +677,7 @@ public static JSONObject toJSONObject(Reader reader, XMLParserConfiguration conf
673677
* name/value pairs and arrays of values. JSON does not does not like to
674678
* distinguish between elements and attributes. Sequences of similar
675679
* elements are represented as JSONArrays. Content text may be placed in a
676-
* "content" member. Comments, prologs, DTDs, and <pre>{@code
680+
* "content" member. Comments, prologs, DTDs, and <pre>{@code
677681
* &lt;[ [ ]]>}</pre>
678682
* are ignored.
679683
*
@@ -699,7 +703,7 @@ public static JSONObject toJSONObject(String string, boolean keepStrings) throws
699703
* name/value pairs and arrays of values. JSON does not does not like to
700704
* distinguish between elements and attributes. Sequences of similar
701705
* elements are represented as JSONArrays. Content text may be placed in a
702-
* "content" member. Comments, prologs, DTDs, and <pre>{@code
706+
* "content" member. Comments, prologs, DTDs, and <pre>{@code
703707
* &lt;[ [ ]]>}</pre>
704708
* are ignored.
705709
*

src/main/java/org/json/XMLParserConfiguration.java

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ public class XMLParserConfiguration {
3939
* they should try to be guessed into JSON values (numeric, boolean, string)
4040
*/
4141
private boolean keepStrings;
42-
42+
4343
/**
4444
* The name of the key in a JSON Object that indicates a CDATA section. Historically this has
4545
* been the value "content" but can be changed. Use <code>null</code> to indicate no CDATA
4646
* processing.
4747
*/
4848
private String cDataTagName;
49-
49+
5050
/**
5151
* When parsing the XML into JSON, specifies if values with attribute xsi:nil="true"
5252
* should be kept as attribute(<code>false</code>), or they should be converted to
@@ -66,8 +66,7 @@ public class XMLParserConfiguration {
6666
private Set<String> forceList;
6767

6868
/**
69-
* When parsing the XML into JSON, specifies the tags whose values should be converted
70-
* to arrays
69+
* The maximum nesting depth when parsing a XML document to JSON.
7170
*/
7271
private int maxNestingDepth = DEFAULT_MAXIMUM_NESTING_DEPTH;
7372

@@ -157,15 +156,18 @@ public XMLParserConfiguration (final boolean keepStrings, final String cDataTagN
157156
* <code>false</code> to parse values with attribute xsi:nil="true" as {"xsi:nil":true}.
158157
* @param xsiTypeMap <code>new HashMap<String, XMLXsiTypeConverter<?>>()</code> to parse values with attribute
159158
* xsi:type="integer" as integer, xsi:type="string" as string
160-
* @param forceList <code>new HashSet<String>()</code> to parse the provided tags' values as arrays
159+
* @param forceList <code>new HashSet<String>()</code> to parse the provided tags' values as arrays
160+
* @param maxNestingDepth <code>int</code> to limit the nesting depth
161161
*/
162162
private XMLParserConfiguration (final boolean keepStrings, final String cDataTagName,
163-
final boolean convertNilAttributeToNull, final Map<String, XMLXsiTypeConverter<?>> xsiTypeMap, final Set<String> forceList ) {
163+
final boolean convertNilAttributeToNull, final Map<String, XMLXsiTypeConverter<?>> xsiTypeMap, final Set<String> forceList,
164+
final int maxNestingDepth) {
164165
this.keepStrings = keepStrings;
165166
this.cDataTagName = cDataTagName;
166167
this.convertNilAttributeToNull = convertNilAttributeToNull;
167168
this.xsiTypeMap = Collections.unmodifiableMap(xsiTypeMap);
168169
this.forceList = Collections.unmodifiableSet(forceList);
170+
this.maxNestingDepth = maxNestingDepth;
169171
}
170172

171173
/**
@@ -183,14 +185,15 @@ protected XMLParserConfiguration clone() {
183185
this.cDataTagName,
184186
this.convertNilAttributeToNull,
185187
this.xsiTypeMap,
186-
this.forceList
188+
this.forceList,
189+
this.maxNestingDepth
187190
);
188191
}
189-
192+
190193
/**
191194
* When parsing the XML into JSON, specifies if values should be kept as strings (<code>true</code>), or if
192195
* they should try to be guessed into JSON values (numeric, boolean, string)
193-
*
196+
*
194197
* @return The <code>keepStrings</code> configuration value.
195198
*/
196199
public boolean isKeepStrings() {
@@ -200,10 +203,10 @@ public boolean isKeepStrings() {
200203
/**
201204
* When parsing the XML into JSON, specifies if values should be kept as strings (<code>true</code>), or if
202205
* they should try to be guessed into JSON values (numeric, boolean, string)
203-
*
206+
*
204207
* @param newVal
205208
* new value to use for the <code>keepStrings</code> configuration option.
206-
*
209+
*
207210
* @return The existing configuration will not be modified. A new configuration is returned.
208211
*/
209212
public XMLParserConfiguration withKeepStrings(final boolean newVal) {
@@ -216,7 +219,7 @@ public XMLParserConfiguration withKeepStrings(final boolean newVal) {
216219
* The name of the key in a JSON Object that indicates a CDATA section. Historically this has
217220
* been the value "content" but can be changed. Use <code>null</code> to indicate no CDATA
218221
* processing.
219-
*
222+
*
220223
* @return The <code>cDataTagName</code> configuration value.
221224
*/
222225
public String getcDataTagName() {
@@ -227,10 +230,10 @@ public String getcDataTagName() {
227230
* The name of the key in a JSON Object that indicates a CDATA section. Historically this has
228231
* been the value "content" but can be changed. Use <code>null</code> to indicate no CDATA
229232
* processing.
230-
*
233+
*
231234
* @param newVal
232235
* new value to use for the <code>cDataTagName</code> configuration option.
233-
*
236+
*
234237
* @return The existing configuration will not be modified. A new configuration is returned.
235238
*/
236239
public XMLParserConfiguration withcDataTagName(final String newVal) {
@@ -243,7 +246,7 @@ public XMLParserConfiguration withcDataTagName(final String newVal) {
243246
* When parsing the XML into JSON, specifies if values with attribute xsi:nil="true"
244247
* should be kept as attribute(<code>false</code>), or they should be converted to
245248
* <code>null</code>(<code>true</code>)
246-
*
249+
*
247250
* @return The <code>convertNilAttributeToNull</code> configuration value.
248251
*/
249252
public boolean isConvertNilAttributeToNull() {
@@ -254,10 +257,10 @@ public boolean isConvertNilAttributeToNull() {
254257
* When parsing the XML into JSON, specifies if values with attribute xsi:nil="true"
255258
* should be kept as attribute(<code>false</code>), or they should be converted to
256259
* <code>null</code>(<code>true</code>)
257-
*
260+
*
258261
* @param newVal
259262
* new value to use for the <code>convertNilAttributeToNull</code> configuration option.
260-
*
263+
*
261264
* @return The existing configuration will not be modified. A new configuration is returned.
262265
*/
263266
public XMLParserConfiguration withConvertNilAttributeToNull(final boolean newVal) {
@@ -295,7 +298,7 @@ public XMLParserConfiguration withXsiTypeMap(final Map<String, XMLXsiTypeConvert
295298

296299
/**
297300
* When parsing the XML into JSON, specifies that tags that will be converted to arrays
298-
* in this configuration {@code Set<String>} to parse the provided tags' values as arrays
301+
* in this configuration {@code Set<String>} to parse the provided tags' values as arrays
299302
* @return <code>forceList</code> unmodifiable configuration set.
300303
*/
301304
public Set<String> getForceList() {
@@ -304,8 +307,8 @@ public Set<String> getForceList() {
304307

305308
/**
306309
* When parsing the XML into JSON, specifies that tags that will be converted to arrays
307-
* in this configuration {@code Set<String>} to parse the provided tags' values as arrays
308-
* @param forceList {@code new HashSet<String>()} to parse the provided tags' values as arrays
310+
* in this configuration {@code Set<String>} to parse the provided tags' values as arrays
311+
* @param forceList {@code new HashSet<String>()} to parse the provided tags' values as arrays
309312
* @return The existing configuration will not be modified. A new configuration is returned.
310313
*/
311314
public XMLParserConfiguration withForceList(final Set<String> forceList) {
@@ -327,8 +330,9 @@ public int getMaxNestingDepth() {
327330
/**
328331
* Defines the maximum nesting depth that the parser will descend before throwing an exception
329332
* 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.
333+
* will throw a JsonException if the maximum depth is reached.
334+
* Using any negative value as a parameter is equivalent to setting no limit to the nesting depth,
335+
* which means the parses will go as deep as the maximum call stack size allows.
332336
* @param maxNestingDepth the maximum nesting depth allowed to the XML parser
333337
* @return The existing configuration will not be modified. A new configuration is returned.
334338
*/

0 commit comments

Comments
 (0)