Skip to content

Commit 25868d9

Browse files
committed
added some method headers
1 parent 4528728 commit 25868d9

2 files changed

Lines changed: 71 additions & 22 deletions

File tree

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

Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,8 @@ private static boolean isPresent(String[] str, String tagName) {
496496
}
497497

498498
/**
499+
* parse method for METHOD 1 MILESTONE 2
500+
*
499501
* Scan the content following the named tag, attaching it to the context.
500502
*
501503
* @param x
@@ -540,6 +542,17 @@ private static boolean parse(XMLTokener x, JSONObject context, String name, XMLP
540542
// <=
541543
// <<
542544

545+
546+
/**
547+
* this lets us jump from the closing tag of the thing we wanted
548+
* all the way to the end of the XML document we are parsing. This
549+
* is done by checking every closing tag with the target tagName we
550+
* wish to replace. We utilize a stack to keep track of all tags as they
551+
* come in. If the closing tag is equal to the target, we have found what
552+
* we wanted, already extracted the subObject and therefore no longer need
553+
* to be in the method. We skip past the remaining tags and get out of the
554+
* method much faster
555+
*/
543556
token = x.nextToken();
544557

545558
if (!(token == BANG || token == QUEST)) {
@@ -684,6 +697,7 @@ private static boolean parse(XMLTokener x, JSONObject context, String name, XMLP
684697
context.accumulate(tagName, "");
685698
}
686699
}
700+
// exiting the recursive method early in parsing
687701
return false;
688702

689703
} else if (token == GT) {
@@ -718,6 +732,13 @@ private static boolean parse(XMLTokener x, JSONObject context, String name, XMLP
718732
"Maximum nesting depth of " + config.getMaxNestingDepth() + " reached");
719733
}
720734

735+
/**
736+
* this part of the method is where we enter the recursive call.
737+
* before we do that, we want to adjust the path. we saved the
738+
* name of the tag currently being processed, and check it with
739+
* our array path. if it matches, we decrement our path by the first
740+
* element to keep track of the method's recursive call.
741+
*/
721742
String[] newP = path;
722743

723744
if (path.length > 0 && tagName.equals(path[0])) {
@@ -743,7 +764,11 @@ private static boolean parse(XMLTokener x, JSONObject context, String name, XMLP
743764
} else if (jsonObject.length() == 1
744765
&& jsonObject.opt(config.getcDataTagName()) != null) {
745766
context.accumulate(tagName, jsonObject.opt(config.getcDataTagName()));
746-
767+
/**
768+
* if the path arr is empty, we know that we
769+
* can extract the subObject and save it to a
770+
* method variable
771+
*/
747772
if (path.length == 0) {
748773
requiredObj.accumulate(tagName, jsonObject.opt(config.getcDataTagName()));
749774
}
@@ -1066,7 +1091,8 @@ private static boolean parsed2(XMLTokener x, JSONObject context, String name, XM
10661091

10671092
token = x.nextToken();
10681093

1069-
1094+
// tag is equal to target, we replace its tag
1095+
// with the subObject we made's top tag
10701096
if (token.equals((Object)LAST_PATH)) {
10711097
token = NewTagName;
10721098
}
@@ -1093,7 +1119,11 @@ private static boolean parsed2(XMLTokener x, JSONObject context, String name, XM
10931119
}
10941120

10951121
else {
1096-
1122+
/**
1123+
* open tag is found and processed here.
1124+
* we replace the curr subObjects opening tag
1125+
* with the opening tag of the subObject
1126+
*/
10971127
tagName = (String) token;
10981128
if(tagName.equals(LAST_PATH)){
10991129
openTagFound.set(true);
@@ -1203,36 +1233,34 @@ else if (token instanceof String) {
12031233
throw x.syntaxError(
12041234
"Maximum nesting depth of " + config.getMaxNestingDepth() + " reached");
12051235
}
1206-
1236+
/**
1237+
* adjust path as we go deeper into object
1238+
*/
12071239
if (path_tracker.length > 0 && tagName.equals(path_tracker[0])) {
12081240
String[] newArray = path_tracker.length > 1
12091241
? Arrays.copyOfRange(path_tracker, 1, path_tracker.length)
12101242
: new String[0];
12111243
path_tracker = newArray;
12121244
}
1245+
/**
1246+
* we have come to the tag of the object we want to replace
1247+
*/
12131248
if (path_tracker.length == 1) {
12141249

12151250
LAST_PATH = path_tracker[0].trim();
12161251
}
12171252

12181253
if (parsed2(x, jsonObject, tagName, config, currentNestingDepth + 1, path_tracker, replacement,
12191254
openTagFound, LAST_PATH, NewTagName)) {
1220-
// if (openTagFound.get()) {
1221-
1222-
// context.put(NewTagName, replacement);
1223-
// return false;
1224-
// }
1225-
// if(tagName == LAST_PATH){
1226-
// return true;
1227-
// }
1255+
1256+
/**
1257+
* check to see if we have found the replacement tags
1258+
* we inserted. if so, we replace the object with our
1259+
* own object
1260+
*/
12281261
if(tagName.equals(NewTagName)){
12291262

1230-
1231-
1232-
12331263
for (String key : replacement.keySet()) {
1234-
System.out.println(key);
1235-
System.out.println(replacement.get(key));
12361264
jsonObject.put(key, replacement.get(key));
12371265
}
12381266

@@ -1359,6 +1387,7 @@ public static JSONObject toJSONObject(String string) throws JSONException {
13591387
return toJSONObject(string, XMLParserConfiguration.ORIGINAL);
13601388
}
13611389
/**
1390+
* MILESTONE 5
13621391
* Milestone V Method
13631392
* This method takes in a Reader Object as well as consumer functions that operate on the
13641393
* jsonObject after the executor thread has properly parsed the reader xml to JSON. This works
@@ -1523,6 +1552,15 @@ public static JSONObject toJSONObject(String string, XMLParserConfiguration conf
15231552
}
15241553

15251554
/**
1555+
* METHOD 1 MILESTONE 2
1556+
*
1557+
* essentially use an array to parse through the different levels of the JSON structure
1558+
* as we get deeper. once we find the deepest level associated with the pointer, we can
1559+
* extract that subobject that is passed through an overloaded parse method along with
1560+
* every method call. We essentialluy add an empty json Object and once we find the subObject
1561+
* we are looking for, we save it to an empty JSONObject and return that value the second we find
1562+
* it thereby speeding up the process of finding a subObject without parsing the whole XML File
1563+
*
15261564
*
15271565
* @param reader - The reader object for the xml string
15281566
* @param path - The path that is needed to be found
@@ -1562,7 +1600,7 @@ public static JSONObject toJSONObject(Reader reader, Function<String, String> ke
15621600
}
15631601

15641602
/**
1565-
* SECOND METHOD INPLEMENTATION FOR MILESTONE 2
1603+
* METHOD 2 MILESTONE 2
15661604
* @param reader - reads the XML String
15671605
* @param path - path to SubObject that we wish to replace
15681606
* @param replacement - the actual JSON Object that we want to use to replace the object at end of path

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,7 +1322,7 @@ public void testMaxNestingDepthWithValidFittingXML() {
13221322
}
13231323

13241324
/**
1325-
* This method tests the efficacy of the milestone 2 method 2
1325+
* This method tests the efficacy of the MILESTONE 2 METHOD 2
13261326
* As you can see the initial xml object is taken and a replacement is
13271327
* named. The method is sucessfully able to replace the subobject (the entire
13281328
* adress body including nick, name, street, and zipcode) with the new
@@ -1366,7 +1366,7 @@ public void testReplacement() {
13661366
}
13671367

13681368
/**
1369-
* Further test efficacy of milestone 2 method 2 implementation by checking a
1369+
* Further test efficacy of MILESTONE 2 METHOD 2 implementation by checking a
13701370
* different
13711371
* XMLString for a subObject. The structure of this one is different and
13721372
* therefore allows
@@ -1413,6 +1413,8 @@ public void testReplacement2() {
14131413
}
14141414

14151415
/**
1416+
* MILESTONE 2 METHOD 2
1417+
*
14161418
* What happens if we try to use a pointer to a subObject that doesn't exist?
14171419
* We would expect that nothing happens and the same object is returned
14181420
*/
@@ -1452,6 +1454,9 @@ public void testReplacementNull() {
14521454
}
14531455
}
14541456

1457+
/**
1458+
* MILESTONE 2 METHOD 1
1459+
*/
14551460
@Test
14561461
public void customXmlPathExample1() {
14571462
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
@@ -1470,7 +1475,10 @@ public void customXmlPathExample1() {
14701475

14711476
assertTrue(expected.similar(actual));
14721477
}
1473-
1478+
1479+
/**
1480+
* MILESTONE 2 METHOD 1
1481+
*/
14741482
@Test
14751483
public void customXmlPathExample2() {
14761484
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
@@ -1492,7 +1500,10 @@ public void customXmlPathExample2() {
14921500
assertTrue(expected.similar(actual));
14931501

14941502
}
1495-
1503+
1504+
/**
1505+
* MILESTONE 2 METHOD 1
1506+
*/
14961507
@Test
14971508
public void customXmlPathExample3() {
14981509
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +

0 commit comments

Comments
 (0)