Skip to content

Commit f2d2098

Browse files
authored
Merge pull request stleary#832 from keatontaylor10/feature-disable-whitespace-trim
Add a config flag to disable whitespace trimming
2 parents 55b824d + 7915d85 commit f2d2098

File tree

5 files changed

+167
-6
lines changed

5 files changed

+167
-6
lines changed

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

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,9 @@ private static boolean parse(XMLTokener x, JSONObject context, String name, XMLP
431431
&& jsonObject.opt(config.getcDataTagName()) != null) {
432432
context.accumulate(tagName, jsonObject.opt(config.getcDataTagName()));
433433
} else {
434+
if (!config.shouldTrimWhiteSpace()) {
435+
removeEmpty(jsonObject, config);
436+
}
434437
context.accumulate(tagName, jsonObject);
435438
}
436439
}
@@ -445,6 +448,48 @@ private static boolean parse(XMLTokener x, JSONObject context, String name, XMLP
445448
}
446449
}
447450
}
451+
/**
452+
* This method removes any JSON entry which has the key set by XMLParserConfiguration.cDataTagName
453+
* and contains whitespace as this is caused by whitespace between tags. See test XMLTest.testNestedWithWhitespaceTrimmingDisabled.
454+
* @param jsonObject JSONObject which may require deletion
455+
* @param config The XMLParserConfiguration which includes the cDataTagName
456+
*/
457+
private static void removeEmpty(final JSONObject jsonObject, final XMLParserConfiguration config) {
458+
if (jsonObject.has(config.getcDataTagName())) {
459+
final Object s = jsonObject.get(config.getcDataTagName());
460+
if (s instanceof String) {
461+
if (isStringAllWhiteSpace(s.toString())) {
462+
jsonObject.remove(config.getcDataTagName());
463+
}
464+
}
465+
else if (s instanceof JSONArray) {
466+
final JSONArray sArray = (JSONArray) s;
467+
for (int k = sArray.length()-1; k >= 0; k--){
468+
final Object eachString = sArray.get(k);
469+
if (eachString instanceof String) {
470+
String s1 = (String) eachString;
471+
if (isStringAllWhiteSpace(s1)) {
472+
sArray.remove(k);
473+
}
474+
}
475+
}
476+
if (sArray.isEmpty()) {
477+
jsonObject.remove(config.getcDataTagName());
478+
}
479+
}
480+
}
481+
}
482+
483+
private static boolean isStringAllWhiteSpace(final String s) {
484+
for (int k = 0; k<s.length(); k++){
485+
final char eachChar = s.charAt(k);
486+
if (!Character.isWhitespace(eachChar)) {
487+
return false;
488+
}
489+
}
490+
return true;
491+
}
492+
448493

449494
/**
450495
* This method tries to convert the given string value to the target object
@@ -594,7 +639,7 @@ public static JSONObject toJSONObject(Reader reader, boolean keepStrings) throws
594639
*/
595640
public static JSONObject toJSONObject(Reader reader, XMLParserConfiguration config) throws JSONException {
596641
JSONObject jo = new JSONObject();
597-
XMLTokener x = new XMLTokener(reader);
642+
XMLTokener x = new XMLTokener(reader, config);
598643
while (x.more()) {
599644
x.skipPast("<");
600645
if(x.more()) {

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

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,26 @@ public class XMLParserConfiguration extends ParserConfiguration {
6161
*/
6262
private Set<String> forceList;
6363

64+
65+
/**
66+
* Flag to indicate whether white space should be trimmed when parsing XML.
67+
* The default behaviour is to trim white space. When this is set to false, inputting XML
68+
* with tags that are the same as the value of cDataTagName is unsupported. It is recommended to set cDataTagName
69+
* to a distinct value in this case.
70+
*/
71+
private boolean shouldTrimWhiteSpace;
72+
6473
/**
6574
* Default parser configuration. Does not keep strings (tries to implicitly convert
66-
* values), and the CDATA Tag Name is "content".
75+
* values), and the CDATA Tag Name is "content". Trims whitespace.
6776
*/
6877
public XMLParserConfiguration () {
6978
super();
7079
this.cDataTagName = "content";
7180
this.convertNilAttributeToNull = false;
7281
this.xsiTypeMap = Collections.emptyMap();
7382
this.forceList = Collections.emptySet();
83+
this.shouldTrimWhiteSpace = true;
7484
}
7585

7686
/**
@@ -172,7 +182,7 @@ protected XMLParserConfiguration clone() {
172182
// item, a new map instance should be created and if possible each value in the
173183
// map should be cloned as well. If the values of the map are known to also
174184
// be immutable, then a shallow clone of the map is acceptable.
175-
return new XMLParserConfiguration(
185+
final XMLParserConfiguration config = new XMLParserConfiguration(
176186
this.keepStrings,
177187
this.cDataTagName,
178188
this.convertNilAttributeToNull,
@@ -181,6 +191,8 @@ protected XMLParserConfiguration clone() {
181191
this.maxNestingDepth,
182192
this.closeEmptyTag
183193
);
194+
config.shouldTrimWhiteSpace = this.shouldTrimWhiteSpace;
195+
return config;
184196
}
185197

186198
/**
@@ -327,7 +339,23 @@ public XMLParserConfiguration withCloseEmptyTag(boolean closeEmptyTag){
327339
return clonedConfiguration;
328340
}
329341

342+
/**
343+
* Sets whether whitespace should be trimmed inside of tags. *NOTE* Do not use this if
344+
* you expect your XML tags to have names that are the same as cDataTagName as this is unsupported.
345+
* cDataTagName should be set to a distinct value in these cases.
346+
* @param shouldTrimWhiteSpace boolean to set trimming on or off. Off is default.
347+
* @return same instance of configuration with empty tag config updated
348+
*/
349+
public XMLParserConfiguration withShouldTrimWhitespace(boolean shouldTrimWhiteSpace){
350+
XMLParserConfiguration clonedConfiguration = this.clone();
351+
clonedConfiguration.shouldTrimWhiteSpace = shouldTrimWhiteSpace;
352+
return clonedConfiguration;
353+
}
354+
330355
public boolean isCloseEmptyTag() {
331356
return this.closeEmptyTag;
332357
}
358+
public boolean shouldTrimWhiteSpace() {
359+
return this.shouldTrimWhiteSpace;
360+
}
333361
}

src/main/java/org/json/XMLTokener.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ public class XMLTokener extends JSONTokener {
2020
*/
2121
public static final java.util.HashMap<String, Character> entity;
2222

23+
private XMLParserConfiguration configuration = XMLParserConfiguration.ORIGINAL;
24+
2325
static {
2426
entity = new java.util.HashMap<String, Character>(8);
2527
entity.put("amp", XML.AMP);
@@ -45,6 +47,16 @@ public XMLTokener(String s) {
4547
super(s);
4648
}
4749

50+
/**
51+
* Construct an XMLTokener from a Reader and an XMLParserConfiguration.
52+
* @param r A source reader.
53+
* @param configuration the configuration that can be used to set certain flags
54+
*/
55+
public XMLTokener(Reader r, XMLParserConfiguration configuration) {
56+
super(r);
57+
this.configuration = configuration;
58+
}
59+
4860
/**
4961
* Get the text in the CDATA block.
5062
* @return The string up to the <code>]]&gt;</code>.
@@ -83,7 +95,7 @@ public Object nextContent() throws JSONException {
8395
StringBuilder sb;
8496
do {
8597
c = next();
86-
} while (Character.isWhitespace(c));
98+
} while (Character.isWhitespace(c) && configuration.shouldTrimWhiteSpace());
8799
if (c == 0) {
88100
return null;
89101
}
@@ -97,7 +109,9 @@ public Object nextContent() throws JSONException {
97109
}
98110
if (c == '<') {
99111
back();
100-
return sb.toString().trim();
112+
if (configuration.shouldTrimWhiteSpace()) {
113+
return sb.toString().trim();
114+
} else return sb.toString();
101115
}
102116
if (c == '&') {
103117
sb.append(nextEntity(c));

src/test/java/org/json/junit/XMLConfigurationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1181,4 +1181,4 @@ private void compareFileToJSONObject(String xmlStr, String expectedStr) {
11811181
assertTrue("Error: " +e.getMessage(), false);
11821182
}
11831183
}
1184-
}
1184+
}

src/test/java/org/json/junit/XMLTest.java

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,6 +1323,80 @@ public void testMaxNestingDepthWithValidFittingXML() {
13231323
"parameter of the XMLParserConfiguration used");
13241324
}
13251325
}
1326+
@Test
1327+
public void testWithWhitespaceTrimmingDisabled() {
1328+
String originalXml = "<testXml> Test Whitespace String \t </testXml>";
1329+
1330+
JSONObject actualJson = XML.toJSONObject(originalXml, new XMLParserConfiguration().withShouldTrimWhitespace(false));
1331+
String expectedJsonString = "{\"testXml\":\" Test Whitespace String \t \"}";
1332+
JSONObject expectedJson = new JSONObject(expectedJsonString);
1333+
Util.compareActualVsExpectedJsonObjects(actualJson,expectedJson);
1334+
}
1335+
@Test
1336+
public void testNestedWithWhitespaceTrimmingDisabled() {
1337+
String originalXml =
1338+
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
1339+
"<addresses>\n"+
1340+
" <address>\n"+
1341+
" <name> Sherlock Holmes </name>\n"+
1342+
" </address>\n"+
1343+
"</addresses>";
1344+
1345+
JSONObject actualJson = XML.toJSONObject(originalXml, new XMLParserConfiguration().withShouldTrimWhitespace(false));
1346+
String expectedJsonString = "{\"addresses\":{\"address\":{\"name\":\" Sherlock Holmes \"}}}";
1347+
JSONObject expectedJson = new JSONObject(expectedJsonString);
1348+
Util.compareActualVsExpectedJsonObjects(actualJson,expectedJson);
1349+
}
1350+
@Test
1351+
public void shouldTrimWhitespaceDoesNotSupportTagsEqualingCDataTagName() {
1352+
// When using withShouldTrimWhitespace = true, input containing tags with same name as cDataTagName is unsupported and should not be used in conjunction
1353+
String originalXml =
1354+
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
1355+
"<addresses>\n"+
1356+
" <address>\n"+
1357+
" <content> Sherlock Holmes </content>\n"+
1358+
" </address>\n"+
1359+
"</addresses>";
1360+
1361+
JSONObject actualJson = XML.toJSONObject(originalXml, new XMLParserConfiguration().withShouldTrimWhitespace(false).withcDataTagName("content"));
1362+
String expectedJsonString = "{\"addresses\":{\"address\":[[\"\\n \",\" Sherlock Holmes \",\"\\n \"]]}}";
1363+
JSONObject expectedJson = new JSONObject(expectedJsonString);
1364+
Util.compareActualVsExpectedJsonObjects(actualJson,expectedJson);
1365+
}
1366+
@Test
1367+
public void shouldTrimWhitespaceEnabledDropsTagsEqualingCDataTagNameButValueRemains() {
1368+
String originalXml =
1369+
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
1370+
"<addresses>\n"+
1371+
" <address>\n"+
1372+
" <content> Sherlock Holmes </content>\n"+
1373+
" </address>\n"+
1374+
"</addresses>";
1375+
1376+
JSONObject actualJson = XML.toJSONObject(originalXml, new XMLParserConfiguration().withShouldTrimWhitespace(true).withcDataTagName("content"));
1377+
String expectedJsonString = "{\"addresses\":{\"address\":\"Sherlock Holmes\"}}";
1378+
JSONObject expectedJson = new JSONObject(expectedJsonString);
1379+
Util.compareActualVsExpectedJsonObjects(actualJson,expectedJson);
1380+
}
1381+
@Test
1382+
public void testWithWhitespaceTrimmingEnabled() {
1383+
String originalXml = "<testXml> Test Whitespace String \t </testXml>";
1384+
1385+
JSONObject actualJson = XML.toJSONObject(originalXml, new XMLParserConfiguration().withShouldTrimWhitespace(true));
1386+
String expectedJsonString = "{\"testXml\":\"Test Whitespace String\"}";
1387+
JSONObject expectedJson = new JSONObject(expectedJsonString);
1388+
Util.compareActualVsExpectedJsonObjects(actualJson,expectedJson);
1389+
}
1390+
@Test
1391+
public void testWithWhitespaceTrimmingEnabledByDefault() {
1392+
String originalXml = "<testXml> Test Whitespace String \t </testXml>";
1393+
1394+
JSONObject actualJson = XML.toJSONObject(originalXml, new XMLParserConfiguration());
1395+
String expectedJsonString = "{\"testXml\":\"Test Whitespace String\"}";
1396+
JSONObject expectedJson = new JSONObject(expectedJsonString);
1397+
Util.compareActualVsExpectedJsonObjects(actualJson,expectedJson);
1398+
}
1399+
13261400
}
13271401

13281402

0 commit comments

Comments
 (0)