Skip to content

Commit f7c1458

Browse files
authored
Add root support for the $.fromXml() method.
1 parent 61d2664 commit f7c1458

File tree

5 files changed

+177
-145
lines changed

5 files changed

+177
-145
lines changed

lodash-plugin/src/main/java/com/github/underscore/lodash/$.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2534,7 +2534,7 @@ public static Object fromXml(final String xml) {
25342534
javax.xml.parsers.DocumentBuilderFactory.newInstance();
25352535
factory.setNamespaceAware(true);
25362536
final org.w3c.dom.Document document = factory.newDocumentBuilder().parse(stream);
2537-
return createMap(document.getDocumentElement());
2537+
return createMap(document);
25382538
} catch (Exception ex) {
25392539
throw new IllegalArgumentException(ex);
25402540
}

lodash-plugin/src/test/java/com/github/underscore/lodash/LodashTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ public void fetchGetWithTimeouts() {
490490
@Test
491491
public void fetchGetXml() {
492492
$.FetchResponse result = $.fetch("http://www.dragonsofmugloar.com/weather/api/report/7614759");
493-
assertEquals("NMR", (String) $.get((Map<String, Object>) result.xml(), "code"));
493+
assertEquals("NMR", (String) $.get((Map<String, Object>) result.xml(), "report.code"));
494494
}
495495

496496
@Test(expected = UnsupportedOperationException.class)
@@ -782,7 +782,7 @@ public void stackoverflow4() {
782782
+ " <Gelco type=\"int\">26</Gelco>"
783783
+ "</root>";
784784

785-
assertEquals("{Durapipe=1, EXPLAIN=2, woods=2, hanging=3, hastily=2, localized=1, "
786-
+ "Schuster=5, regularize=1, LASR=1, LAST=22, Gelch=2, Gelco=26}", $.fromXml(xml).toString());
785+
assertEquals("{root={Durapipe=1, EXPLAIN=2, woods=2, hanging=3, hastily=2, localized=1, "
786+
+ "Schuster=5, regularize=1, LASR=1, LAST=22, Gelch=2, Gelco=26}}", $.fromXml(xml).toString());
787787
}
788788
}

lodash-plugin/src/test/java/com/github/underscore/lodash/StringTest.java

Lines changed: 85 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,62 +1330,6 @@ public void toXml() {
13301330
@SuppressWarnings("unchecked")
13311331
@Test
13321332
public void fromXml() {
1333-
String string =
1334-
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
1335-
+ "\n<root>"
1336-
+ "\n <glossary>"
1337-
+ "\n <title>example glossary</title>"
1338-
+ "\n <GlossDiv>"
1339-
+ "\n <title>S</title>"
1340-
+ "\n <GlossList>"
1341-
+ "\n <GlossEntry>"
1342-
+ "\n <ID>SGML</ID>"
1343-
+ "\n <SortAs>SGML</SortAs>"
1344-
+ "\n <GlossTerm>Standard Generalized Markup Language</GlossTerm>"
1345-
+ "\n <Acronym>SGML</Acronym>"
1346-
+ "\n <Abbrev>ISO 8879:1986</Abbrev>"
1347-
+ "\n <GlossDef>"
1348-
+ "\n <para>A meta-markup language, used to create markup languages such as DocBook.</para>"
1349-
+ "\n <GlossSeeAlso>"
1350-
+ "\n <element>GML</element>"
1351-
+ "\n <element>XML</element>"
1352-
+ "\n </GlossSeeAlso>"
1353-
+ "\n </GlossDef>"
1354-
+ "\n <GlossSee>markup</GlossSee>"
1355-
+ "\n </GlossEntry>"
1356-
+ "\n </GlossList>"
1357-
+ "\n </GlossDiv>"
1358-
+ "\n </glossary>"
1359-
+ "\n</root>";
1360-
assertEquals("{\n \"glossary\": {\n \"title\": \"example glossary\",\n \"GlossDiv\": {\n \"title\":"
1361-
+ " \"S\",\n \"GlossList\": {\n \"GlossEntry\": {\n \"ID\": \"SGML\",\n"
1362-
+ " \"SortAs\": \"SGML\",\n \"GlossTerm\": \"Standard Generalized Markup Language\",\n"
1363-
+ " \"Acronym\": \"SGML\",\n \"Abbrev\": \"ISO 8879:1986\",\n \"GlossDef\": {\n"
1364-
+ " \"para\": \"A meta-markup language, used to create markup languages such as DocBook.\",\n"
1365-
+ " \"GlossSeeAlso\": [\n \"GML\",\n \"XML\"\n ]\n"
1366-
+ " },\n \"GlossSee\": \"markup\"\n }\n }\n }\n }\n}",
1367-
$.toJson((Map<String, Object>) $.fromXml(string)));
1368-
assertEquals("{\n \"glossary\": {\n \"title\": \"example glossary\",\n \"GlossDiv\": {\n \"title\":"
1369-
+ " \"S\",\n \"GlossList\": {\n \"GlossEntry\": {\n \"ID\": \"SGML\",\n"
1370-
+ " \"SortAs\": \"SGML\",\n \"GlossTerm\": \"Standard Generalized Markup Language\",\n"
1371-
+ " \"Acronym\": \"SGML\",\n \"Abbrev\": \"ISO 8879:1986\",\n \"GlossDef\": {\n"
1372-
+ " \"para\": \"A meta-markup language, used to create markup languages such as DocBook.\",\n"
1373-
+ " \"GlossSeeAlso\": [\n \"GML\",\n \"XML\"\n ]\n"
1374-
+ " },\n \"GlossSee\": \"markup\"\n }\n }\n }\n }\n}",
1375-
$.toJson((Map<String, Object>) new $(string).fromXml()));
1376-
assertEquals("{\n \"glossary\": {\n \"title\": \"example glossary\",\n \"GlossDiv\": {\n \"title\":"
1377-
+ " \"S\",\n \"GlossList\": {\n \"GlossEntry\": {\n \"ID\": \"SGML\",\n"
1378-
+ " \"SortAs\": \"SGML\",\n \"GlossTerm\": \"Standard Generalized Markup Language\",\n"
1379-
+ " \"Acronym\": \"SGML\",\n \"Abbrev\": \"ISO 8879:1986\",\n \"GlossDef\": {\n"
1380-
+ " \"para\": \"A meta-markup language, used to create markup languages such as DocBook.\",\n"
1381-
+ " \"GlossSeeAlso\": [\n \"GML\",\n \"XML\"\n ]\n"
1382-
+ " },\n \"GlossSee\": \"markup\"\n }\n }\n }\n }\n}",
1383-
$.toJson((Map<String, Object>) $.chain(string).fromXml().item()));
1384-
}
1385-
1386-
@SuppressWarnings("unchecked")
1387-
@Test
1388-
public void fromXml2() {
13891333
String string =
13901334
"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
13911335
+ "\n"
@@ -1410,23 +1354,95 @@ public void fromXml2() {
14101354
+ "\n</root>";
14111355
assertEquals(
14121356
"{"
1413-
+ "\n \"Details\": {"
1414-
+ "\n \"detail-a\": {"
1415-
+ "\n \"detail\": ["
1416-
+ "\n \" attribute 1 of detail a \","
1417-
+ "\n \" attribute 2 of detail a \","
1418-
+ "\n \" attribute 3 of detail a \""
1419-
+ "\n ]"
1420-
+ "\n },"
1421-
+ "\n \"detail-b\": {"
1422-
+ "\n \"detail\": ["
1423-
+ "\n \" attribute 1 of detail b \","
1424-
+ "\n \" attribute 2 of detail b \""
1425-
+ "\n ]"
1357+
+ "\n \"root\": {"
1358+
+ "\n \"Details\": {"
1359+
+ "\n \"detail-a\": {"
1360+
+ "\n \"detail\": ["
1361+
+ "\n \" attribute 1 of detail a \","
1362+
+ "\n \" attribute 2 of detail a \","
1363+
+ "\n \" attribute 3 of detail a \""
1364+
+ "\n ]"
1365+
+ "\n },"
1366+
+ "\n \"detail-b\": {"
1367+
+ "\n \"detail\": ["
1368+
+ "\n \" attribute 1 of detail b \","
1369+
+ "\n \" attribute 2 of detail b \""
1370+
+ "\n ]"
1371+
+ "\n }"
14261372
+ "\n }"
14271373
+ "\n }"
14281374
+ "\n}",
14291375
$.toJson((Map<String, Object>) $.fromXml(string)));
1376+
assertEquals(
1377+
"{"
1378+
+ "\n \"root\": {"
1379+
+ "\n \"Details\": {"
1380+
+ "\n \"detail-a\": {"
1381+
+ "\n \"detail\": ["
1382+
+ "\n \" attribute 1 of detail a \","
1383+
+ "\n \" attribute 2 of detail a \","
1384+
+ "\n \" attribute 3 of detail a \""
1385+
+ "\n ]"
1386+
+ "\n },"
1387+
+ "\n \"detail-b\": {"
1388+
+ "\n \"detail\": ["
1389+
+ "\n \" attribute 1 of detail b \","
1390+
+ "\n \" attribute 2 of detail b \""
1391+
+ "\n ]"
1392+
+ "\n }"
1393+
+ "\n }"
1394+
+ "\n }"
1395+
+ "\n}",
1396+
$.toJson((Map<String, Object>) new $(string).fromXml()));
1397+
assertEquals(
1398+
"{"
1399+
+ "\n \"root\": {"
1400+
+ "\n \"Details\": {"
1401+
+ "\n \"detail-a\": {"
1402+
+ "\n \"detail\": ["
1403+
+ "\n \" attribute 1 of detail a \","
1404+
+ "\n \" attribute 2 of detail a \","
1405+
+ "\n \" attribute 3 of detail a \""
1406+
+ "\n ]"
1407+
+ "\n },"
1408+
+ "\n \"detail-b\": {"
1409+
+ "\n \"detail\": ["
1410+
+ "\n \" attribute 1 of detail b \","
1411+
+ "\n \" attribute 2 of detail b \""
1412+
+ "\n ]"
1413+
+ "\n }"
1414+
+ "\n }"
1415+
+ "\n }"
1416+
+ "\n}",
1417+
$.toJson((Map<String, Object>) $.chain(string).fromXml().item()));
1418+
String stringXml =
1419+
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
1420+
+ "\n<root>"
1421+
+ "\n <glossary>"
1422+
+ "\n <title>example glossary</title>"
1423+
+ "\n <GlossDiv>"
1424+
+ "\n <title>S</title>"
1425+
+ "\n <GlossList>"
1426+
+ "\n <GlossEntry>"
1427+
+ "\n <ID>SGML</ID>"
1428+
+ "\n <SortAs>SGML</SortAs>"
1429+
+ "\n <GlossTerm>Standard Generalized Markup Language</GlossTerm>"
1430+
+ "\n <Acronym>SGML</Acronym>"
1431+
+ "\n <Abbrev>ISO 8879:1986</Abbrev>"
1432+
+ "\n <GlossDef>"
1433+
+ "\n <para>A meta-markup language, used to create markup languages such as DocBook.</para>"
1434+
+ "\n <GlossSeeAlso>"
1435+
+ "\n <element>GML</element>"
1436+
+ "\n <element>XML</element>"
1437+
+ "\n </GlossSeeAlso>"
1438+
+ "\n </GlossDef>"
1439+
+ "\n <GlossSee>markup</GlossSee>"
1440+
+ "\n </GlossEntry>"
1441+
+ "\n </GlossList>"
1442+
+ "\n </GlossDiv>"
1443+
+ "\n </glossary>"
1444+
+ "\n</root>";
1445+
$.fromXml(stringXml);
14301446
}
14311447

14321448
@Test(expected = IllegalArgumentException.class)

string-plugin/src/main/java/com/github/underscore/string/$.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* The MIT License (MIT)
33
*
4-
* Copyright 2016 Valentyn Kolesnikov
4+
* Copyright 2016, 2017 Valentyn Kolesnikov
55
*
66
* Permission is hereby granted, free of charge, to any person obtaining a copy
77
* of this software and associated documentation files (the "Software"), to deal
@@ -2054,7 +2054,7 @@ public static Object fromXml(final String xml) {
20542054
javax.xml.parsers.DocumentBuilderFactory.newInstance();
20552055
factory.setNamespaceAware(true);
20562056
final org.w3c.dom.Document document = factory.newDocumentBuilder().parse(stream);
2057-
return createMap(document.getDocumentElement());
2057+
return createMap(document);
20582058
} catch (Exception ex) {
20592059
throw new IllegalArgumentException(ex);
20602060
}

string-plugin/src/test/java/com/github/underscore/string/StringTest.java

Lines changed: 86 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,62 +1332,6 @@ public void toXml() {
13321332
@SuppressWarnings("unchecked")
13331333
@Test
13341334
public void fromXml() {
1335-
String string =
1336-
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
1337-
+ "\n<root>"
1338-
+ "\n <glossary>"
1339-
+ "\n <title>example glossary</title>"
1340-
+ "\n <GlossDiv>"
1341-
+ "\n <title>S</title>"
1342-
+ "\n <GlossList>"
1343-
+ "\n <GlossEntry>"
1344-
+ "\n <ID>SGML</ID>"
1345-
+ "\n <SortAs>SGML</SortAs>"
1346-
+ "\n <GlossTerm>Standard Generalized Markup Language</GlossTerm>"
1347-
+ "\n <Acronym>SGML</Acronym>"
1348-
+ "\n <Abbrev>ISO 8879:1986</Abbrev>"
1349-
+ "\n <GlossDef>"
1350-
+ "\n <para>A meta-markup language, used to create markup languages such as DocBook.</para>"
1351-
+ "\n <GlossSeeAlso>"
1352-
+ "\n <element>GML</element>"
1353-
+ "\n <element>XML</element>"
1354-
+ "\n </GlossSeeAlso>"
1355-
+ "\n </GlossDef>"
1356-
+ "\n <GlossSee>markup</GlossSee>"
1357-
+ "\n </GlossEntry>"
1358-
+ "\n </GlossList>"
1359-
+ "\n </GlossDiv>"
1360-
+ "\n </glossary>"
1361-
+ "\n</root>";
1362-
assertEquals("{\n \"glossary\": {\n \"title\": \"example glossary\",\n \"GlossDiv\": {\n \"title\":"
1363-
+ " \"S\",\n \"GlossList\": {\n \"GlossEntry\": {\n \"ID\": \"SGML\",\n"
1364-
+ " \"SortAs\": \"SGML\",\n \"GlossTerm\": \"Standard Generalized Markup Language\",\n"
1365-
+ " \"Acronym\": \"SGML\",\n \"Abbrev\": \"ISO 8879:1986\",\n \"GlossDef\": {\n"
1366-
+ " \"para\": \"A meta-markup language, used to create markup languages such as DocBook.\",\n"
1367-
+ " \"GlossSeeAlso\": [\n \"GML\",\n \"XML\"\n ]\n"
1368-
+ " },\n \"GlossSee\": \"markup\"\n }\n }\n }\n }\n}",
1369-
$.toJson((Map<String, Object>) $.fromXml(string)));
1370-
assertEquals("{\n \"glossary\": {\n \"title\": \"example glossary\",\n \"GlossDiv\": {\n \"title\":"
1371-
+ " \"S\",\n \"GlossList\": {\n \"GlossEntry\": {\n \"ID\": \"SGML\",\n"
1372-
+ " \"SortAs\": \"SGML\",\n \"GlossTerm\": \"Standard Generalized Markup Language\",\n"
1373-
+ " \"Acronym\": \"SGML\",\n \"Abbrev\": \"ISO 8879:1986\",\n \"GlossDef\": {\n"
1374-
+ " \"para\": \"A meta-markup language, used to create markup languages such as DocBook.\",\n"
1375-
+ " \"GlossSeeAlso\": [\n \"GML\",\n \"XML\"\n ]\n"
1376-
+ " },\n \"GlossSee\": \"markup\"\n }\n }\n }\n }\n}",
1377-
$.toJson((Map<String, Object>) new $(string).fromXml()));
1378-
assertEquals("{\n \"glossary\": {\n \"title\": \"example glossary\",\n \"GlossDiv\": {\n \"title\":"
1379-
+ " \"S\",\n \"GlossList\": {\n \"GlossEntry\": {\n \"ID\": \"SGML\",\n"
1380-
+ " \"SortAs\": \"SGML\",\n \"GlossTerm\": \"Standard Generalized Markup Language\",\n"
1381-
+ " \"Acronym\": \"SGML\",\n \"Abbrev\": \"ISO 8879:1986\",\n \"GlossDef\": {\n"
1382-
+ " \"para\": \"A meta-markup language, used to create markup languages such as DocBook.\",\n"
1383-
+ " \"GlossSeeAlso\": [\n \"GML\",\n \"XML\"\n ]\n"
1384-
+ " },\n \"GlossSee\": \"markup\"\n }\n }\n }\n }\n}",
1385-
$.toJson((Map<String, Object>) $.chain(string).fromXml().item()));
1386-
}
1387-
1388-
@SuppressWarnings("unchecked")
1389-
@Test
1390-
public void fromXml2() {
13911335
String string =
13921336
"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
13931337
+ "\n"
@@ -1412,23 +1356,95 @@ public void fromXml2() {
14121356
+ "\n</root>";
14131357
assertEquals(
14141358
"{"
1415-
+ "\n \"Details\": {"
1416-
+ "\n \"detail-a\": {"
1417-
+ "\n \"detail\": ["
1418-
+ "\n \" attribute 1 of detail a \","
1419-
+ "\n \" attribute 2 of detail a \","
1420-
+ "\n \" attribute 3 of detail a \""
1421-
+ "\n ]"
1422-
+ "\n },"
1423-
+ "\n \"detail-b\": {"
1424-
+ "\n \"detail\": ["
1425-
+ "\n \" attribute 1 of detail b \","
1426-
+ "\n \" attribute 2 of detail b \""
1427-
+ "\n ]"
1359+
+ "\n \"root\": {"
1360+
+ "\n \"Details\": {"
1361+
+ "\n \"detail-a\": {"
1362+
+ "\n \"detail\": ["
1363+
+ "\n \" attribute 1 of detail a \","
1364+
+ "\n \" attribute 2 of detail a \","
1365+
+ "\n \" attribute 3 of detail a \""
1366+
+ "\n ]"
1367+
+ "\n },"
1368+
+ "\n \"detail-b\": {"
1369+
+ "\n \"detail\": ["
1370+
+ "\n \" attribute 1 of detail b \","
1371+
+ "\n \" attribute 2 of detail b \""
1372+
+ "\n ]"
1373+
+ "\n }"
14281374
+ "\n }"
14291375
+ "\n }"
14301376
+ "\n}",
14311377
$.toJson((Map<String, Object>) $.fromXml(string)));
1378+
assertEquals(
1379+
"{"
1380+
+ "\n \"root\": {"
1381+
+ "\n \"Details\": {"
1382+
+ "\n \"detail-a\": {"
1383+
+ "\n \"detail\": ["
1384+
+ "\n \" attribute 1 of detail a \","
1385+
+ "\n \" attribute 2 of detail a \","
1386+
+ "\n \" attribute 3 of detail a \""
1387+
+ "\n ]"
1388+
+ "\n },"
1389+
+ "\n \"detail-b\": {"
1390+
+ "\n \"detail\": ["
1391+
+ "\n \" attribute 1 of detail b \","
1392+
+ "\n \" attribute 2 of detail b \""
1393+
+ "\n ]"
1394+
+ "\n }"
1395+
+ "\n }"
1396+
+ "\n }"
1397+
+ "\n}",
1398+
$.toJson((Map<String, Object>) new $(string).fromXml()));
1399+
assertEquals(
1400+
"{"
1401+
+ "\n \"root\": {"
1402+
+ "\n \"Details\": {"
1403+
+ "\n \"detail-a\": {"
1404+
+ "\n \"detail\": ["
1405+
+ "\n \" attribute 1 of detail a \","
1406+
+ "\n \" attribute 2 of detail a \","
1407+
+ "\n \" attribute 3 of detail a \""
1408+
+ "\n ]"
1409+
+ "\n },"
1410+
+ "\n \"detail-b\": {"
1411+
+ "\n \"detail\": ["
1412+
+ "\n \" attribute 1 of detail b \","
1413+
+ "\n \" attribute 2 of detail b \""
1414+
+ "\n ]"
1415+
+ "\n }"
1416+
+ "\n }"
1417+
+ "\n }"
1418+
+ "\n}",
1419+
$.toJson((Map<String, Object>) $.chain(string).fromXml().item()));
1420+
String stringXml =
1421+
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
1422+
+ "\n<root>"
1423+
+ "\n <glossary>"
1424+
+ "\n <title>example glossary</title>"
1425+
+ "\n <GlossDiv>"
1426+
+ "\n <title>S</title>"
1427+
+ "\n <GlossList>"
1428+
+ "\n <GlossEntry>"
1429+
+ "\n <ID>SGML</ID>"
1430+
+ "\n <SortAs>SGML</SortAs>"
1431+
+ "\n <GlossTerm>Standard Generalized Markup Language</GlossTerm>"
1432+
+ "\n <Acronym>SGML</Acronym>"
1433+
+ "\n <Abbrev>ISO 8879:1986</Abbrev>"
1434+
+ "\n <GlossDef>"
1435+
+ "\n <para>A meta-markup language, used to create markup languages such as DocBook.</para>"
1436+
+ "\n <GlossSeeAlso>"
1437+
+ "\n <element>GML</element>"
1438+
+ "\n <element>XML</element>"
1439+
+ "\n </GlossSeeAlso>"
1440+
+ "\n </GlossDef>"
1441+
+ "\n <GlossSee>markup</GlossSee>"
1442+
+ "\n </GlossEntry>"
1443+
+ "\n </GlossList>"
1444+
+ "\n </GlossDiv>"
1445+
+ "\n </glossary>"
1446+
+ "\n</root>";
1447+
$.fromXml(stringXml);
14321448
}
14331449

14341450
@SuppressWarnings("unchecked")
@@ -1438,7 +1454,7 @@ public void fetchGetXml() {
14381454
+ "</coords><code>NMR</code><message>Another day of everyday normal regular weather, business as usual, unless "
14391455
+ "it’s going to be like the time of the Great Paprika Mayonnaise Incident of 2014, that was some pretty nasty "
14401456
+ "stuff.</message><varX-Rating>8</varX-Rating></report>";
1441-
assertEquals("NMR", (String) ((Map<String, Object>) $.fromXml(xml)).get("code"));
1457+
assertEquals("NMR", (String) ((Map<String, Object>) ((Map<String, Object>) $.fromXml(xml)).get("report")).get("code"));
14421458
}
14431459

14441460
@Test(expected = IllegalArgumentException.class)

0 commit comments

Comments
 (0)