Skip to content

Commit c04ced4

Browse files
authored
Add standalone="yes" support for xml.
1 parent 64cb928 commit c04ced4

File tree

2 files changed

+67
-5
lines changed

2 files changed

+67
-5
lines changed

src/main/java/com/github/underscore/lodash/Xml.java

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ public final class Xml {
3232
private static final String CDATA = "#cdata-section";
3333
private static final String COMMENT = "#comment";
3434
private static final String ENCODING = "#encoding";
35+
private static final String STANDALONE = "#standalone";
36+
private static final String YES = "yes";
3537
private static final String TEXT = "#text";
3638
private static final String NUMBER = "-number";
3739
private static final String ELEMENT = "<" + ELEMENT_TEXT + ">";
@@ -135,9 +137,10 @@ public String toString() {
135137
}
136138

137139
public static class XmlStringBuilderWithoutRoot extends XmlStringBuilder {
138-
public XmlStringBuilderWithoutRoot(XmlStringBuilder.Step identStep, String encoding) {
140+
public XmlStringBuilderWithoutRoot(XmlStringBuilder.Step identStep, String encoding,
141+
String standalone) {
139142
super(new StringBuilder("<?xml version=\"1.0\" encoding=\""
140-
+ XmlValue.escape(encoding).replace("\"", QUOT) + "\"?>"
143+
+ XmlValue.escape(encoding).replace("\"", QUOT) + "\"" + standalone + "?>"
141144
+ (identStep == Step.COMPACT ? "" : "\n")), identStep, 0);
142145
}
143146

@@ -839,7 +842,7 @@ public static Object getMapValue(Object map) {
839842
}
840843

841844
public static String toXml(Collection collection, XmlStringBuilder.Step identStep) {
842-
final XmlStringBuilder builder = new XmlStringBuilderWithoutRoot(identStep, UTF_8.name());
845+
final XmlStringBuilder builder = new XmlStringBuilderWithoutRoot(identStep, UTF_8.name(), "");
843846
builder.append("<root");
844847
if (collection != null && collection.isEmpty()) {
845848
builder.append(" empty-array=\"true\"");
@@ -863,11 +866,22 @@ public static String toXml(Map map, XmlStringBuilder.Step identStep) {
863866
final XmlStringBuilder builder;
864867
final Map localMap;
865868
if (map != null && map.containsKey(ENCODING)) {
866-
builder = new XmlStringBuilderWithoutRoot(identStep, String.valueOf(map.get(ENCODING)));
867869
localMap = (Map) U.clone(map);
868870
localMap.remove(ENCODING);
871+
if (map.containsKey(STANDALONE)) {
872+
builder = new XmlStringBuilderWithoutRoot(identStep, String.valueOf(map.get(ENCODING)),
873+
" standalone=\"" + (YES.equals(map.get(STANDALONE)) ? YES : "no") + "\"");
874+
localMap.remove(STANDALONE);
875+
} else {
876+
builder = new XmlStringBuilderWithoutRoot(identStep, String.valueOf(map.get(ENCODING)), "");
877+
}
878+
} else if (map != null && map.containsKey(STANDALONE)) {
879+
builder = new XmlStringBuilderWithoutRoot(identStep, UTF_8.name(), " standalone=\""
880+
+ (YES.equals(map.get(STANDALONE)) ? YES : "no") + "\"");
881+
localMap = (Map) U.clone(map);
882+
localMap.remove(STANDALONE);
869883
} else {
870-
builder = new XmlStringBuilderWithoutRoot(identStep, UTF_8.name());
884+
builder = new XmlStringBuilderWithoutRoot(identStep, UTF_8.name(), "");
871885
localMap = map;
872886
}
873887
if (localMap == null || localMap.size() != 1
@@ -1131,6 +1145,11 @@ public Object apply(Object object) {
11311145
}, Collections.<String, Object>emptyMap(), new int[] {1, 1, 1}, xml, new int[] {0});
11321146
if (document.getXmlEncoding() != null && !"UTF-8".equalsIgnoreCase(document.getXmlEncoding())) {
11331147
((Map) result).put(ENCODING, document.getXmlEncoding());
1148+
if (document.getXmlStandalone()) {
1149+
((Map) result).put(STANDALONE, YES);
1150+
}
1151+
} else if (document.getXmlStandalone()) {
1152+
((Map) result).put(STANDALONE, YES);
11341153
} else if (((Map.Entry) ((Map) result).entrySet().iterator().next()).getKey().equals("root")
11351154
&& (((Map.Entry) ((Map) result).entrySet().iterator().next()).getValue() instanceof List
11361155
|| ((Map.Entry) ((Map) result).entrySet().iterator().next()).getValue() instanceof Map)) {

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2014,6 +2014,49 @@ public void toJsonFromXml20() {
20142014
assertEquals(json6, U.toJson((Map<String, Object>) U.fromXml(xml6)));
20152015
}
20162016

2017+
@SuppressWarnings("unchecked")
2018+
@Test
2019+
public void toJsonFromXml21() {
2020+
final String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n<a/>";
2021+
final String json = "{\n"
2022+
+ " \"a\": {\n"
2023+
+ " \"-self-closing\": \"true\"\n"
2024+
+ " },\n"
2025+
+ " \"#standalone\": \"yes\"\n"
2026+
+ "}";
2027+
final String xml2 = "<?xml version=\"1.0\" standalone=\"yes\"?>\n<a/>";
2028+
assertEquals(json, U.toJson((Map<String, Object>) U.fromXml(xml)));
2029+
assertEquals(xml, U.toXml((Map<String, Object>) U.fromJson(json)));
2030+
assertEquals(json, U.toJson((Map<String, Object>) U.fromXml(xml2)));
2031+
final String xml3 = "<?xml version=\"1.0\" encoding=\"windows-1251\" standalone=\"yes\"?>\n<a/>";
2032+
final String json3 = "{\n"
2033+
+ " \"a\": {\n"
2034+
+ " \"-self-closing\": \"true\"\n"
2035+
+ " },\n"
2036+
+ " \"#encoding\": \"windows-1251\",\n"
2037+
+ " \"#standalone\": \"yes\"\n"
2038+
+ "}";
2039+
assertEquals(json3, U.toJson((Map<String, Object>) U.fromXml(xml3)));
2040+
assertEquals(xml3, U.toXml((Map<String, Object>) U.fromJson(json3)));
2041+
final String xml4 = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n<a/>";
2042+
final String json4 = "{\n"
2043+
+ " \"a\": {\n"
2044+
+ " \"-self-closing\": \"true\"\n"
2045+
+ " },\n"
2046+
+ " \"#standalone\": \"no\"\n"
2047+
+ "}";
2048+
assertEquals(xml4, U.toXml((Map<String, Object>) U.fromJson(json4)));
2049+
final String xml5 = "<?xml version=\"1.0\" encoding=\"windows-1251\" standalone=\"no\"?>\n<a/>";
2050+
final String json5 = "{\n"
2051+
+ " \"a\": {\n"
2052+
+ " \"-self-closing\": \"true\"\n"
2053+
+ " },\n"
2054+
+ " \"#encoding\": \"windows-1251\",\n"
2055+
+ " \"#standalone\": \"no\"\n"
2056+
+ "}";
2057+
assertEquals(xml5, U.toXml((Map<String, Object>) U.fromJson(json5)));
2058+
}
2059+
20172060
@SuppressWarnings("unchecked")
20182061
@Test
20192062
public void toXmlFromJson() {

0 commit comments

Comments
 (0)