Skip to content

Commit 42e7ca9

Browse files
committed
fix bugs and document readme
1 parent cd7491c commit 42e7ca9

File tree

3 files changed

+31
-107
lines changed

3 files changed

+31
-107
lines changed

README-M3.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Milestone 3
2+
3+
For the Milestone 3 of SWE262P, the following new functions was added:
4+
5+
```java
6+
static JSONObject toJSONObject(Reader reader, Function func)
7+
```
8+
9+
This function takes in 3 parameters, a `Reader` object which contains some XML input and a `Function` object that includes a function for converting a `String` (expected type of input: a `String`, and is expected to return another `String`), and a `JSONObject` for replacement. And returns a new `JSONObject` object with tag names replaced, or throw an error if the `Reader` object gives an invalid XML.
10+
11+
The new function is placed in the `XML.java` file.
12+
13+
The test cases of the functions are placed under the `org.json.junit.milestone3.tests` package, and to run the test case, run the following command:
14+
15+
`mvn -Dtest=XMLKeyTransformerTest test`
16+
17+
By implementing the code in the original library code, the function is able to complete the task in one-pass, as the function is writing to a new `JSONObject` while parsing the input XML String. However, in milestone 1, the client code is only able to convert the whole XML String to a `JSONObject`, then convert the keys of this `JSONObject` object.
18+
19+
Thus, implementing this function inside the library code is able to reduce the execution time in half (one-pass vs two-pass), and also resulting in optimization of memory usage.

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

Lines changed: 11 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -483,9 +483,17 @@ private static boolean parse(XMLTokener x, JSONObject context, String name, XMLP
483483
}
484484
}
485485

486-
// overwritten method of parse which allows to pass a prefix tag
487-
/* Milestone3
488-
overwritten new method of parse
486+
/**
487+
* Compared to the original parse function, this function adds the function (String Convertor) as an input
488+
* @param x
489+
* @param context
490+
* @param name
491+
* @param config
492+
* @param currentNestingDepth
493+
* @param keyTransformer
494+
* The function which takes in a single String parameter, and returns another converted String
495+
* @return
496+
* @throws JSONException
489497
*/
490498
private static boolean parseMilestone3(XMLTokener x, JSONObject context, String name, XMLParserConfiguration config, int currentNestingDepth,Function<String, String> keyTransformer)
491499
throws JSONException {
@@ -604,29 +612,6 @@ private static boolean parseMilestone3(XMLTokener x, JSONObject context, String
604612
} else {
605613
jsonObject.accumulate(keyTransformer.apply(string), "");
606614
}
607-
/*} else if (!nilAttributeFound) {
608-
Object obj = stringToValue((String) token);
609-
if (obj instanceof Boolean) {
610-
jsonObject.accumulate(prefix + string,
611-
config.isKeepBooleanAsString()
612-
? ((String) token)
613-
: obj);
614-
} else if (obj instanceof Number) {
615-
jsonObject.accumulate(prefix + string,
616-
config.isKeepNumberAsString()
617-
? ((String) token)
618-
: obj);
619-
} else {
620-
jsonObject.accumulate(prefix + string, stringToValue((String) token));
621-
}
622-
}
623-
token = null;
624-
} else {
625-
jsonObject.accumulate(prefix + string, "");
626-
}
627-
*/
628-
629-
630615
} else if (token == SLASH) {
631616
// Empty tag <.../>
632617
if (x.nextToken() != GT) {
@@ -735,52 +720,6 @@ private static boolean parseMilestone3(XMLTokener x, JSONObject context, String
735720
}
736721
}
737722
}
738-
}
739-
740-
741-
// Nested element
742-
/*
743-
if (currentNestingDepth == config.getMaxNestingDepth()) {
744-
throw x.syntaxError("Maximum nesting depth of " + config.getMaxNestingDepth() + " reached");
745-
}
746-
747-
if (parse(x, jsonObject, tagName, prefix, config, currentNestingDepth + 1)) {
748-
if (config.getForceList().contains(tagName)) {
749-
// Force the value to be an array
750-
if (jsonObject.length() == 0) {
751-
context.put(tagName, new JSONArray());
752-
} else if (jsonObject.length() == 1
753-
&& jsonObject.opt(config.getcDataTagName()) != null) {
754-
context.append(prefix + tagName, jsonObject.opt(config.getcDataTagName()));
755-
} else {
756-
context.append(prefix + tagName, jsonObject);
757-
}
758-
} else {
759-
if (jsonObject.length() == 0) {
760-
context.accumulate(prefix + tagName, "");
761-
} else if (jsonObject.length() == 1
762-
&& jsonObject.opt(config.getcDataTagName()) != null) {
763-
context.accumulate(prefix + tagName, jsonObject.opt(config.getcDataTagName()));
764-
} else {
765-
if (!config.shouldTrimWhiteSpace()) {
766-
removeEmpty(jsonObject, config);
767-
}
768-
context.accumulate(prefix + tagName, jsonObject);
769-
}
770-
}
771-
772-
return false;
773-
}
774-
}
775-
}
776-
} else {
777-
throw x.syntaxError("Misshaped tag");
778-
}
779-
}
780-
}
781-
}
782-
783-
*/
784723
/**
785724
* This method removes any JSON entry which has the key set by XMLParserConfiguration.cDataTagName
786725
* and contains whitespace as this is caused by whitespace between tags. See test XMLTest.testNestedWithWhitespaceTrimmingDisabled.

src/test/java/org/json/junit/milestone3/tests/XMLKeyTransformerTest.java

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
public class XMLKeyTransformerTest {
1515
// define some customized functions for testing
1616
@Test
17-
public void keyTransformerSimpleTest() {
17+
public void keyTransformerAddPrefixTest() {
1818
String xml = "<book><title>Title</title><author>John</author></book>";
1919
Function<String, String> prefixer = key -> "swe262_" + key;
2020

@@ -39,37 +39,3 @@ public void nullReaderTest() {
3939
XML.toJSONObject(null, key -> "x_" + key);
4040
}
4141
}
42-
43-
/*
44-
@Test
45-
public void testXML01() {
46-
String xml = "<book><title><content>Old Title</content></title><author>John</author></book>";
47-
StringReader reader = new StringReader(xml);
48-
49-
JSONObject result = XML.toJSONObject(reader, "swe_262p");
50-
51-
System.out.println(result);
52-
}
53-
54-
@Test
55-
public void testXML02() {
56-
String xmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
57-
"<contact>\n"+
58-
" <nick>Crista </nick>\n"+
59-
" <name>Crista Lopes</name>\n" +
60-
" <address>\n" +
61-
" <street>Ave of Nowhere</street>\n" +
62-
" <zipcode>92614</zipcode>\n" +
63-
" </address>\n" +
64-
"</contact>";
65-
66-
try {
67-
JSONObject jobj = XML.toJSONObject(new StringReader(xmlString), "swe_262p");
68-
System.out.println(jobj);
69-
} catch (JSONException e) {
70-
System.out.println(e);
71-
}
72-
}
73-
}
74-
*/
75-

0 commit comments

Comments
 (0)