Skip to content

Commit 45bcba5

Browse files
authored
Merge pull request #729 from TamasPergerDWP/f-parserconfig
Refactor ParserConfiguration class hierarchy
2 parents 47fb49b + 2409349 commit 45bcba5

File tree

4 files changed

+162
-126
lines changed

4 files changed

+162
-126
lines changed

src/main/java/org/json/JSONMLParserConfiguration.java

Lines changed: 11 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,12 @@
77
* Configuration object for the XML to JSONML parser. The configuration is immutable.
88
*/
99
@SuppressWarnings({""})
10-
public class JSONMLParserConfiguration {
11-
/**
12-
* Used to indicate there's no defined limit to the maximum nesting depth when parsing a XML
13-
* document to JSONML.
14-
*/
15-
public static final int UNDEFINED_MAXIMUM_NESTING_DEPTH = -1;
10+
public class JSONMLParserConfiguration extends ParserConfiguration {
1611

1712
/**
18-
* The default maximum nesting depth when parsing a XML document to JSONML.
13+
* We can override the default maximum nesting depth if needed.
1914
*/
20-
public static final int DEFAULT_MAXIMUM_NESTING_DEPTH = 512;
15+
public static final int DEFAULT_MAXIMUM_NESTING_DEPTH = ParserConfiguration.DEFAULT_MAXIMUM_NESTING_DEPTH;
2116

2217
/** Original Configuration of the XML to JSONML Parser. */
2318
public static final JSONMLParserConfiguration ORIGINAL
@@ -26,22 +21,12 @@ public class JSONMLParserConfiguration {
2621
public static final JSONMLParserConfiguration KEEP_STRINGS
2722
= new JSONMLParserConfiguration().withKeepStrings(true);
2823

29-
/**
30-
* When parsing the XML into JSONML, specifies if values should be kept as strings (<code>true</code>), or if
31-
* they should try to be guessed into JSON values (numeric, boolean, string)
32-
*/
33-
private boolean keepStrings;
34-
35-
/**
36-
* The maximum nesting depth when parsing a XML document to JSONML.
37-
*/
38-
private int maxNestingDepth = DEFAULT_MAXIMUM_NESTING_DEPTH;
39-
4024
/**
4125
* Default parser configuration. Does not keep strings (tries to implicitly convert values).
4226
*/
4327
public JSONMLParserConfiguration() {
44-
this.keepStrings = false;
28+
super();
29+
this.maxNestingDepth = DEFAULT_MAXIMUM_NESTING_DEPTH;
4530
}
4631

4732
/**
@@ -50,9 +35,8 @@ public JSONMLParserConfiguration() {
5035
* <code>false</code> to try and convert XML string values into a JSON value.
5136
* @param maxNestingDepth <code>int</code> to limit the nesting depth
5237
*/
53-
private JSONMLParserConfiguration(final boolean keepStrings, final int maxNestingDepth) {
54-
this.keepStrings = keepStrings;
55-
this.maxNestingDepth = maxNestingDepth;
38+
protected JSONMLParserConfiguration(final boolean keepStrings, final int maxNestingDepth) {
39+
super(keepStrings, maxNestingDepth);
5640
}
5741

5842
/**
@@ -71,58 +55,13 @@ protected JSONMLParserConfiguration clone() {
7155
);
7256
}
7357

74-
/**
75-
* When parsing the XML into JSONML, specifies if values should be kept as strings (<code>true</code>), or if
76-
* they should try to be guessed into JSON values (numeric, boolean, string)
77-
*
78-
* @return The <code>keepStrings</code> configuration value.
79-
*/
80-
public boolean isKeepStrings() {
81-
return this.keepStrings;
82-
}
83-
84-
/**
85-
* When parsing the XML into JSONML, specifies if values should be kept as strings (<code>true</code>), or if
86-
* they should try to be guessed into JSON values (numeric, boolean, string)
87-
*
88-
* @param newVal
89-
* new value to use for the <code>keepStrings</code> configuration option.
90-
*
91-
* @return The existing configuration will not be modified. A new configuration is returned.
92-
*/
58+
@Override
9359
public JSONMLParserConfiguration withKeepStrings(final boolean newVal) {
94-
JSONMLParserConfiguration newConfig = this.clone();
95-
newConfig.keepStrings = newVal;
96-
return newConfig;
97-
}
98-
99-
/**
100-
* The maximum nesting depth that the parser will descend before throwing an exception
101-
* when parsing the XML into JSONML.
102-
* @return the maximum nesting depth set for this configuration
103-
*/
104-
public int getMaxNestingDepth() {
105-
return maxNestingDepth;
60+
return super.withKeepStrings(newVal);
10661
}
10762

108-
/**
109-
* Defines the maximum nesting depth that the parser will descend before throwing an exception
110-
* when parsing the XML into JSONML. The default max nesting depth is 512, which means the parser
111-
* will throw a JsonException if the maximum depth is reached.
112-
* Using any negative value as a parameter is equivalent to setting no limit to the nesting depth,
113-
* which means the parses will go as deep as the maximum call stack size allows.
114-
* @param maxNestingDepth the maximum nesting depth allowed to the XML parser
115-
* @return The existing configuration will not be modified. A new configuration is returned.
116-
*/
63+
@Override
11764
public JSONMLParserConfiguration withMaxNestingDepth(int maxNestingDepth) {
118-
JSONMLParserConfiguration newConfig = this.clone();
119-
120-
if (maxNestingDepth > UNDEFINED_MAXIMUM_NESTING_DEPTH) {
121-
newConfig.maxNestingDepth = maxNestingDepth;
122-
} else {
123-
newConfig.maxNestingDepth = UNDEFINED_MAXIMUM_NESTING_DEPTH;
124-
}
125-
126-
return newConfig;
65+
return super.withMaxNestingDepth(maxNestingDepth);
12766
}
12867
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package org.json;
2+
/*
3+
Public Domain.
4+
*/
5+
6+
/**
7+
* Configuration base object for parsers. The configuration is immutable.
8+
*/
9+
@SuppressWarnings({""})
10+
public class ParserConfiguration {
11+
/**
12+
* Used to indicate there's no defined limit to the maximum nesting depth when parsing a document.
13+
*/
14+
public static final int UNDEFINED_MAXIMUM_NESTING_DEPTH = -1;
15+
16+
/**
17+
* The default maximum nesting depth when parsing a document.
18+
*/
19+
public static final int DEFAULT_MAXIMUM_NESTING_DEPTH = 512;
20+
21+
/**
22+
* Specifies if values should be kept as strings (<code>true</code>), or if
23+
* they should try to be guessed into JSON values (numeric, boolean, string)
24+
*/
25+
protected boolean keepStrings;
26+
27+
/**
28+
* The maximum nesting depth when parsing a document.
29+
*/
30+
protected int maxNestingDepth;
31+
32+
public ParserConfiguration() {
33+
this.keepStrings = false;
34+
this.maxNestingDepth = DEFAULT_MAXIMUM_NESTING_DEPTH;
35+
}
36+
37+
protected ParserConfiguration(final boolean keepStrings, final int maxNestingDepth) {
38+
this.keepStrings = keepStrings;
39+
this.maxNestingDepth = maxNestingDepth;
40+
}
41+
42+
/**
43+
* Provides a new instance of the same configuration.
44+
*/
45+
@Override
46+
protected ParserConfiguration clone() {
47+
// future modifications to this method should always ensure a "deep"
48+
// clone in the case of collections. i.e. if a Map is added as a configuration
49+
// item, a new map instance should be created and if possible each value in the
50+
// map should be cloned as well. If the values of the map are known to also
51+
// be immutable, then a shallow clone of the map is acceptable.
52+
return new ParserConfiguration(
53+
this.keepStrings,
54+
this.maxNestingDepth
55+
);
56+
}
57+
58+
/**
59+
* When parsing the XML into JSONML, specifies if values should be kept as strings (<code>true</code>), or if
60+
* they should try to be guessed into JSON values (numeric, boolean, string)
61+
*
62+
* @return The <code>keepStrings</code> configuration value.
63+
*/
64+
public boolean isKeepStrings() {
65+
return this.keepStrings;
66+
}
67+
68+
/**
69+
* When parsing the XML into JSONML, specifies if values should be kept as strings (<code>true</code>), or if
70+
* they should try to be guessed into JSON values (numeric, boolean, string)
71+
*
72+
* @param newVal
73+
* new value to use for the <code>keepStrings</code> configuration option.
74+
*
75+
* @return The existing configuration will not be modified. A new configuration is returned.
76+
*/
77+
public <T extends ParserConfiguration> T withKeepStrings(final boolean newVal) {
78+
T newConfig = (T)this.clone();
79+
newConfig.keepStrings = newVal;
80+
return newConfig;
81+
}
82+
83+
/**
84+
* The maximum nesting depth that the parser will descend before throwing an exception
85+
* when parsing the XML into JSONML.
86+
* @return the maximum nesting depth set for this configuration
87+
*/
88+
public int getMaxNestingDepth() {
89+
return maxNestingDepth;
90+
}
91+
92+
/**
93+
* Defines the maximum nesting depth that the parser will descend before throwing an exception
94+
* when parsing the XML into JSONML. The default max nesting depth is 512, which means the parser
95+
* will throw a JsonException if the maximum depth is reached.
96+
* Using any negative value as a parameter is equivalent to setting no limit to the nesting depth,
97+
* which means the parses will go as deep as the maximum call stack size allows.
98+
* @param maxNestingDepth the maximum nesting depth allowed to the XML parser
99+
* @return The existing configuration will not be modified. A new configuration is returned.
100+
*/
101+
public <T extends ParserConfiguration> T withMaxNestingDepth(int maxNestingDepth) {
102+
T newConfig = (T)this.clone();
103+
104+
if (maxNestingDepth > UNDEFINED_MAXIMUM_NESTING_DEPTH) {
105+
newConfig.maxNestingDepth = maxNestingDepth;
106+
} else {
107+
newConfig.maxNestingDepth = UNDEFINED_MAXIMUM_NESTING_DEPTH;
108+
}
109+
110+
return newConfig;
111+
}
112+
}

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

Lines changed: 10 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,12 @@
1515
* @author AylwardJ
1616
*/
1717
@SuppressWarnings({""})
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;
18+
public class XMLParserConfiguration extends ParserConfiguration {
2419

2520
/**
2621
* The default maximum nesting depth when parsing a XML document to JSON.
2722
*/
28-
public static final int DEFAULT_MAXIMUM_NESTING_DEPTH = 512;
23+
// public static final int DEFAULT_MAXIMUM_NESTING_DEPTH = 512; // We could override
2924

3025
/** Original Configuration of the XML Parser. */
3126
public static final XMLParserConfiguration ORIGINAL
@@ -34,12 +29,6 @@ public class XMLParserConfiguration {
3429
public static final XMLParserConfiguration KEEP_STRINGS
3530
= new XMLParserConfiguration().withKeepStrings(true);
3631

37-
/**
38-
* When parsing the XML into JSON, specifies if values should be kept as strings (<code>true</code>), or if
39-
* they should try to be guessed into JSON values (numeric, boolean, string)
40-
*/
41-
private boolean keepStrings;
42-
4332
/**
4433
* The name of the key in a JSON Object that indicates a CDATA section. Historically this has
4534
* been the value "content" but can be changed. Use <code>null</code> to indicate no CDATA
@@ -65,17 +54,12 @@ public class XMLParserConfiguration {
6554
*/
6655
private Set<String> forceList;
6756

68-
/**
69-
* The maximum nesting depth when parsing a XML document to JSON.
70-
*/
71-
private int maxNestingDepth = DEFAULT_MAXIMUM_NESTING_DEPTH;
72-
7357
/**
7458
* Default parser configuration. Does not keep strings (tries to implicitly convert
7559
* values), and the CDATA Tag Name is "content".
7660
*/
7761
public XMLParserConfiguration () {
78-
this.keepStrings = false;
62+
super();
7963
this.cDataTagName = "content";
8064
this.convertNilAttributeToNull = false;
8165
this.xsiTypeMap = Collections.emptyMap();
@@ -122,7 +106,7 @@ public XMLParserConfiguration (final String cDataTagName) {
122106
*/
123107
@Deprecated
124108
public XMLParserConfiguration (final boolean keepStrings, final String cDataTagName) {
125-
this.keepStrings = keepStrings;
109+
super(keepStrings, DEFAULT_MAXIMUM_NESTING_DEPTH);
126110
this.cDataTagName = cDataTagName;
127111
this.convertNilAttributeToNull = false;
128112
}
@@ -141,7 +125,7 @@ public XMLParserConfiguration (final boolean keepStrings, final String cDataTagN
141125
*/
142126
@Deprecated
143127
public XMLParserConfiguration (final boolean keepStrings, final String cDataTagName, final boolean convertNilAttributeToNull) {
144-
this.keepStrings = keepStrings;
128+
super(keepStrings, DEFAULT_MAXIMUM_NESTING_DEPTH);
145129
this.cDataTagName = cDataTagName;
146130
this.convertNilAttributeToNull = convertNilAttributeToNull;
147131
}
@@ -162,12 +146,11 @@ public XMLParserConfiguration (final boolean keepStrings, final String cDataTagN
162146
private XMLParserConfiguration (final boolean keepStrings, final String cDataTagName,
163147
final boolean convertNilAttributeToNull, final Map<String, XMLXsiTypeConverter<?>> xsiTypeMap, final Set<String> forceList,
164148
final int maxNestingDepth) {
165-
this.keepStrings = keepStrings;
149+
super(keepStrings, maxNestingDepth);
166150
this.cDataTagName = cDataTagName;
167151
this.convertNilAttributeToNull = convertNilAttributeToNull;
168152
this.xsiTypeMap = Collections.unmodifiableMap(xsiTypeMap);
169153
this.forceList = Collections.unmodifiableSet(forceList);
170-
this.maxNestingDepth = maxNestingDepth;
171154
}
172155

173156
/**
@@ -190,16 +173,6 @@ protected XMLParserConfiguration clone() {
190173
);
191174
}
192175

193-
/**
194-
* When parsing the XML into JSON, specifies if values should be kept as strings (<code>true</code>), or if
195-
* they should try to be guessed into JSON values (numeric, boolean, string)
196-
*
197-
* @return The <code>keepStrings</code> configuration value.
198-
*/
199-
public boolean isKeepStrings() {
200-
return this.keepStrings;
201-
}
202-
203176
/**
204177
* When parsing the XML into JSON, specifies if values should be kept as strings (<code>true</code>), or if
205178
* they should try to be guessed into JSON values (numeric, boolean, string)
@@ -209,10 +182,9 @@ public boolean isKeepStrings() {
209182
*
210183
* @return The existing configuration will not be modified. A new configuration is returned.
211184
*/
185+
@Override
212186
public XMLParserConfiguration withKeepStrings(final boolean newVal) {
213-
XMLParserConfiguration newConfig = this.clone();
214-
newConfig.keepStrings = newVal;
215-
return newConfig;
187+
return super.withKeepStrings(newVal);
216188
}
217189

218190
/**
@@ -318,15 +290,6 @@ public XMLParserConfiguration withForceList(final Set<String> forceList) {
318290
return newConfig;
319291
}
320292

321-
/**
322-
* The maximum nesting depth that the parser will descend before throwing an exception
323-
* when parsing the XML into JSON.
324-
* @return the maximum nesting depth set for this configuration
325-
*/
326-
public int getMaxNestingDepth() {
327-
return maxNestingDepth;
328-
}
329-
330293
/**
331294
* Defines the maximum nesting depth that the parser will descend before throwing an exception
332295
* when parsing the XML into JSON. The default max nesting depth is 512, which means the parser
@@ -336,15 +299,8 @@ public int getMaxNestingDepth() {
336299
* @param maxNestingDepth the maximum nesting depth allowed to the XML parser
337300
* @return The existing configuration will not be modified. A new configuration is returned.
338301
*/
302+
@Override
339303
public XMLParserConfiguration withMaxNestingDepth(int maxNestingDepth) {
340-
XMLParserConfiguration newConfig = this.clone();
341-
342-
if (maxNestingDepth > UNDEFINED_MAXIMUM_NESTING_DEPTH) {
343-
newConfig.maxNestingDepth = maxNestingDepth;
344-
} else {
345-
newConfig.maxNestingDepth = UNDEFINED_MAXIMUM_NESTING_DEPTH;
346-
}
347-
348-
return newConfig;
304+
return super.withMaxNestingDepth(maxNestingDepth);
349305
}
350306
}

0 commit comments

Comments
 (0)