@@ -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 )) {
0 commit comments