Skip to content

Commit acce15c

Browse files
committed
Added overloaded method toJSONObject(Reader, Function) for Milestone 3 that transforms keys given by a transformation given by user..
Added a TestCase to XMLTest_SWE262 called testMethodThree_replaceKay
1 parent 75582f2 commit acce15c

4 files changed

Lines changed: 219 additions & 2 deletions

File tree

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@
118118
<artifactId>maven-compiler-plugin</artifactId>
119119
<version>2.3.2</version>
120120
<configuration>
121-
<source>1.6</source>
122-
<target>1.6</target>
121+
<source>8</source>
122+
<target>8</target>
123123
</configuration>
124124
</plugin>
125125
<plugin>

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

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ of this software and associated documentation files (the "Software"), to deal
3030
import java.math.BigDecimal;
3131
import java.math.BigInteger;
3232
import java.util.Iterator;
33+
import java.util.function.Function;
3334

3435

3536
/**
@@ -967,6 +968,81 @@ public static JSONObject toJSONObject(Reader reader, JSONPointer path, JSONObjec
967968
return query;
968969
}
969970

971+
//[RJ_ADDED] This overloaded static method replces all keys with output
972+
//of keyTransformer output.
973+
public static JSONObject toJSONObject(Reader reader, Function<String, String> keyTransformer)
974+
{
975+
976+
JSONObject jo = new JSONObject();
977+
XMLParserConfiguration config = new XMLParserConfiguration();
978+
XMLTokener x = new XMLTokener(reader);
979+
980+
981+
//rebuild the sub xml
982+
String rebuildXML = "";
983+
boolean cycle = false;
984+
985+
boolean exitLoop = false;
986+
987+
String currTag ="";
988+
989+
//String startString = "<" + parseTag;
990+
//String exitString = "</" + parseTag;
991+
992+
while (x.more()) {
993+
x.skipPast("<");
994+
currTag = "<" + x.nextContent();
995+
//System.out.println(currTag);
996+
997+
//Start recording after start string reached.
998+
999+
1000+
//Current tag holds the tag <> with possible info afterwards.
1001+
//1. Extract the first word after opening tag openTag = <book id=...
1002+
// we want book.
1003+
1004+
//Skip the XML information
1005+
if(!currTag.contains("xml version"))
1006+
{
1007+
//Handle open <tag case...
1008+
//Get what's in the tag only
1009+
String[] tag = currTag.split(">",0);
1010+
//Get the first word after < in the tag
1011+
tag = tag[0].split(" ",0);
1012+
1013+
//Handle close </tag case
1014+
if(tag[0].contains("/"))
1015+
{
1016+
//remove the /
1017+
tag[0] = tag[0].replace("/", "");
1018+
}
1019+
1020+
//This should contain what's in the current line tag
1021+
String oldTag = tag[0].substring(tag[0].indexOf("<") + +1);
1022+
1023+
1024+
//2. Feed the openTag to keyTransformer function. Set it to new variable newTag.
1025+
String newTag = keyTransformer.apply(oldTag);
1026+
1027+
//3. Replace openTag with newTag in currTag.
1028+
currTag = currTag.replaceAll(oldTag,newTag);; //+ something
1029+
}
1030+
1031+
1032+
rebuildXML += currTag;
1033+
//System.out.println("Pre If: " + currTag);
1034+
}
1035+
1036+
//System.out.println(rebuildXML);
1037+
JSONObject query = XML.toJSONObject(rebuildXML);
1038+
//System.out.println(query.toString());
1039+
1040+
1041+
//System.out.println("PrintingJSON");
1042+
1043+
return query;
1044+
//return null;
1045+
}
9701046
/**
9711047
* Convert a JSONObject into a well-formed, element-normal XML string.
9721048
*

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

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.io.*;
1010
import java.util.HashMap;
1111
import java.util.Map;
12+
import java.util.function.Function;
1213

1314
import org.json.*;
1415
import org.junit.Rule;
@@ -132,4 +133,144 @@ public void testMethodTwo_replaceAll()
132133
}
133134
}
134135

136+
@Test
137+
public void testMethodThree_replaceKey()
138+
{
139+
System.out.println("\nIn Test Method three");
140+
String expectedStr = "<?xml version=\"1.0\"?>\n" +
141+
"<RJ_catalog>\n" +
142+
" <RJ_book id=\"bk101\">\n" +
143+
" <RJ_author>Gambardella, Matthew</RJ_author>\n" +
144+
" <RJ_title>XML Developer's Guide</RJ_title>\n" +
145+
" <RJ_genre>Computer</RJ_genre>\n" +
146+
" <RJ_price>44.95</RJ_price>\n" +
147+
" <RJ_publish_date>2000-10-01</RJ_publish_date>\n" +
148+
" <RJ_description>An in-depth look at creating applications\n" +
149+
" with XML.</RJ_description>\n" +
150+
" </RJ_book>\n" +
151+
" <RJ_book id=\"bk102\">\n" +
152+
" <RJ_author>Ralls, Kim</RJ_author>\n" +
153+
" <RJ_title>Midnight Rain</RJ_title>\n" +
154+
" <RJ_genre>Fantasy</RJ_genre>\n" +
155+
" <RJ_price>5.95</RJ_price>\n" +
156+
" <RJ_publish_date>2000-12-16</RJ_publish_date>\n" +
157+
" <RJ_description>A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.</RJ_description>\n" +
158+
" </RJ_book>\n" +
159+
" <RJ_book id=\"bk103\">\n" +
160+
" <RJ_author>Corets, Eva</RJ_author>\n" +
161+
" <RJ_title>Maeve Ascendant</RJ_title>\n" +
162+
" <RJ_genre>Fantasy</RJ_genre>\n" +
163+
" <RJ_price>5.95</RJ_price>\n" +
164+
" <RJ_publish_date>2000-11-17</RJ_publish_date>\n" +
165+
" <RJ_description>After the collapse of a nanotechnology\n" +
166+
" society in England, the young survivors lay the\n" +
167+
" foundation for a new society.</RJ_description>\n" +
168+
" </RJ_book>\n" +
169+
" <RJ_book id=\"bk104\">\n" +
170+
" <RJ_author>Corets, Eva</RJ_author>\n" +
171+
" <RJ_title>Oberon's Legacy</RJ_title>\n" +
172+
" <RJ_genre>Fantasy</RJ_genre>\n" +
173+
" <RJ_price>5.95</RJ_price>\n" +
174+
" <RJ_publish_date>2001-03-10</RJ_publish_date>\n" +
175+
" <RJ_description>In post-apocalypse England, the mysterious\n" +
176+
" agent known only as Oberon helps to create a new life\n" +
177+
" for the inhabitants of London. Sequel to Maeve\n" +
178+
" Ascendant.</RJ_description>\n" +
179+
" </RJ_book>\n" +
180+
" <RJ_book id=\"bk105\">\n" +
181+
" <RJ_author>Corets, Eva</RJ_author>\n" +
182+
" <RJ_title>The Sundered Grail</RJ_title>\n" +
183+
" <RJ_genre>Fantasy</RJ_genre>\n" +
184+
" <RJ_price>5.95</RJ_price>\n" +
185+
" <RJ_publish_date>2001-09-10</RJ_publish_date>\n" +
186+
" <RJ_description>The two daughters of Maeve, half-sisters,\n" +
187+
" battle one another for control of England. Sequel to\n" +
188+
" Oberon's Legacy.</RJ_description>\n" +
189+
" </RJ_book>\n" +
190+
" <RJ_book id=\"bk106\">\n" +
191+
" <RJ_author>Randall, Cynthia</RJ_author>\n" +
192+
" <RJ_title>Lover Birds</RJ_title>\n" +
193+
" <RJ_genre>Romance</RJ_genre>\n" +
194+
" <RJ_price>4.95</RJ_price>\n" +
195+
" <RJ_publish_date>2000-09-02</RJ_publish_date>\n" +
196+
" <RJ_description>When Carla meets Paul at an ornithology\n" +
197+
" conference, tempers fly as feathers get ruffled.</RJ_description>\n" +
198+
" </RJ_book>\n" +
199+
" <RJ_book id=\"bk107\">\n" +
200+
" <RJ_author>Thurman, Paula</RJ_author>\n" +
201+
" <RJ_title>Splish Splash</RJ_title>\n" +
202+
" <RJ_genre>Romance</RJ_genre>\n" +
203+
" <RJ_price>4.95</RJ_price>\n" +
204+
" <RJ_publish_date>2000-11-02</RJ_publish_date>\n" +
205+
" <RJ_description>A deep sea diver finds true love twenty\n" +
206+
" thousand leagues beneath the sea.</RJ_description>\n" +
207+
" </RJ_book>\n" +
208+
" <RJ_book id=\"bk108\">\n" +
209+
" <RJ_author>Knorr, Stefan</RJ_author>\n" +
210+
" <RJ_title>Creepy Crawlies</RJ_title>\n" +
211+
" <RJ_genre>Horror</RJ_genre>\n" +
212+
" <RJ_price>4.95</RJ_price>\n" +
213+
" <RJ_publish_date>2000-12-06</RJ_publish_date>\n" +
214+
" <RJ_description>An anthology of horror stories about roaches,\n" +
215+
" centipedes, scorpions and other insects.</RJ_description>\n" +
216+
" </RJ_book>\n" +
217+
" <RJ_book id=\"bk109\">\n" +
218+
" <RJ_author>Kress, Peter</RJ_author>\n" +
219+
" <RJ_title>Paradox Lost</RJ_title>\n" +
220+
" <RJ_genre>Science Fiction</RJ_genre>\n" +
221+
" <RJ_price>6.95</RJ_price>\n" +
222+
" <RJ_publish_date>2000-11-02</RJ_publish_date>\n" +
223+
" <RJ_description>After an inadvertant trip through a Heisenberg\n" +
224+
" Uncertainty Device, James Salway discovers the problems\n" +
225+
" of being quantum.</RJ_description>\n" +
226+
" </RJ_book>\n" +
227+
" <RJ_book id=\"bk110\">\n" +
228+
" <RJ_author>O'Brien, Tim</RJ_author>\n" +
229+
" <RJ_title>Microsoft .NET: The Programming Bible</RJ_title>\n" +
230+
" <RJ_genre>Computer</RJ_genre>\n" +
231+
" <RJ_price>36.95</RJ_price>\n" +
232+
" <RJ_publish_date>2000-12-09</RJ_publish_date>\n" +
233+
" <RJ_description>Microsoft's .NET initiative is explored in\n" +
234+
" detail in this deep programmer's reference.</RJ_description>\n" +
235+
" </RJ_book>\n" +
236+
" <RJ_book id=\"bk111\">\n" +
237+
" <RJ_author>O'Brien, Tim</RJ_author>\n" +
238+
" <RJ_title>MSXML3: A Comprehensive Guide</RJ_title>\n" +
239+
" <RJ_genre>Computer</RJ_genre>\n" +
240+
" <RJ_price>36.95</RJ_price>\n" +
241+
" <RJ_publish_date>2000-12-01</RJ_publish_date>\n" +
242+
" <RJ_description>The Microsoft MSXML3 parser is covered in\n" +
243+
" detail, with attention to XML DOM interfaces, XSLT processing,\n" +
244+
" SAX and more.</RJ_description>\n" +
245+
" </RJ_book>\n" +
246+
" <RJ_book id=\"bk112\">\n" +
247+
" <RJ_author>Galos, Mike</RJ_author>\n" +
248+
" <RJ_title>Visual Studio 7: A Comprehensive Guide</RJ_title>\n" +
249+
" <RJ_genre>Computer</RJ_genre>\n" +
250+
" <RJ_price>49.95</RJ_price>\n" +
251+
" <RJ_publish_date>2001-04-16</RJ_publish_date>\n" +
252+
" <RJ_description>Microsoft Visual Studio 7 is explored in depth,\n" +
253+
" looking at how Visual Basic, Visual C++, C#, and ASP+ are\n" +
254+
" integrated into a comprehensive development\n" +
255+
" environment.</RJ_description>\n" +
256+
" </RJ_book>\n" +
257+
"</RJ_catalog>";
258+
259+
expectedStr = XML.toJSONObject(expectedStr).toString();
260+
try {
261+
//Define the function
262+
Function<String, String> keyTransformer= (x) -> ("RJ_"+x);
263+
FileReader filereader = new FileReader("src/test/resources/Catalog.xml");
264+
JSONObject jo = XML.toJSONObject(filereader, keyTransformer);
265+
266+
String actualStr = jo.toString();
267+
//System.out.println(actualStr);
268+
assertEquals(expectedStr, actualStr);
269+
270+
} catch (FileNotFoundException e) {
271+
System.out.println("File not found");
272+
e.printStackTrace();
273+
}
274+
}
275+
135276
}

src/test/resources/TransformerTest.xml

Whitespace-only changes.

0 commit comments

Comments
 (0)