@@ -408,14 +408,14 @@ public static void writeXml(final Map map, final String name, final XmlStringBui
408408 final Map .Entry entry = entries .get (index );
409409 final boolean addNewLine = index < entries .size () - 1
410410 && !String .valueOf (entries .get (index + 1 ).getKey ()).startsWith (TEXT );
411- if (String .valueOf (entry .getKey ()).startsWith ("-" ) && ( entry .getValue () instanceof String ) ) {
411+ if (String .valueOf (entry .getKey ()).startsWith ("-" ) && entry .getValue () instanceof String ) {
412412 attrs .add (" " + XmlValue .escapeName (String .valueOf (entry .getKey ()).substring (1 ), namespaces )
413413 + "=\" " + XmlValue .escape (String .valueOf (entry .getValue ())).replace ("\" " , QUOT ) + "\" " );
414414 } else if (String .valueOf (entry .getKey ()).startsWith (TEXT )) {
415415 addText (entry , elems , identStep , ident , attrKeys , attrs );
416416 } else {
417- boolean localParentTextFound = ( !elems .isEmpty ()
418- && elems .get (elems .size () - 1 ) instanceof XmlStringBuilderText ) || parentTextFound ;
417+ boolean localParentTextFound = !elems .isEmpty ()
418+ && elems .get (elems .size () - 1 ) instanceof XmlStringBuilderText || parentTextFound ;
419419 processElements (entry , identStep , ident , addNewLine , elems , namespaces , localParentTextFound );
420420 }
421421 }
@@ -963,11 +963,12 @@ public static String toXml(Map map) {
963963 }
964964
965965 @ SuppressWarnings ("unchecked" )
966- private static Object getValue (final Object value ) {
966+ private static Object getValue (final Object value , final FromType fromType ) {
967967 final Object localValue ;
968968 if (value instanceof Map && ((Map <String , Object >) value ).entrySet ().size () == 1 ) {
969969 final Map .Entry <String , Object > entry = ((Map <String , Object >) value ).entrySet ().iterator ().next ();
970- if (TEXT .equals (entry .getKey ()) || entry .getKey ().equals (ELEMENT_TEXT )) {
970+ if (TEXT .equals (entry .getKey ())
971+ || (fromType == FromType .FOR_CONVERT && ELEMENT_TEXT .equals (entry .getKey ()))) {
971972 localValue = entry .getValue ();
972973 } else {
973974 localValue = value ;
@@ -1000,7 +1001,8 @@ private static Object stringToNumber(String number) {
10001001 private static Object createMap (final org .w3c .dom .Node node ,
10011002 final BiFunction <Object , Set <String >, String > elementMapper ,
10021003 final Function <Object , Object > nodeMapper , final Map <String , Object > attrMap ,
1003- final int [] uniqueIds , final String source , final int [] sourceIndex , final Set <String > namespaces ) {
1004+ final int [] uniqueIds , final String source , final int [] sourceIndex , final Set <String > namespaces ,
1005+ final FromType fromType ) {
10041006 final Map <String , Object > map = U .newLinkedHashMap ();
10051007 map .putAll (attrMap );
10061008 final org .w3c .dom .NodeList nodeList = node .getChildNodes ();
@@ -1016,7 +1018,7 @@ private static Object createMap(final org.w3c.dom.Node node,
10161018 if (currentNode .getNodeType () == org .w3c .dom .Node .ELEMENT_NODE ) {
10171019 sourceIndex [0 ] = source .indexOf ("<" + name , sourceIndex [0 ]) + name .length () + 1 ;
10181020 value = addElement (sourceIndex , source , elementMapper , nodeMapper ,
1019- uniqueIds , currentNode , namespaces );
1021+ uniqueIds , currentNode , namespaces , fromType );
10201022 } else {
10211023 if (COMMENT .equals (name )) {
10221024 sourceIndex [0 ] = source .indexOf ("-->" , sourceIndex [0 ]) + 3 ;
@@ -1030,7 +1032,7 @@ private static Object createMap(final org.w3c.dom.Node node,
10301032 || currentNode .getNodeType () == org .w3c .dom .Node .DOCUMENT_TYPE_NODE ) {
10311033 continue ;
10321034 }
1033- addNodeValue (map , name , value , elementMapper , nodeMapper , uniqueIds , namespaces );
1035+ addNodeValue (map , name , value , elementMapper , nodeMapper , uniqueIds , namespaces , fromType );
10341036 }
10351037 return checkNumberAndBoolean (map , node .getNodeName ());
10361038 }
@@ -1064,8 +1066,8 @@ private static Object checkArray(final Map<String, Object> map, final String nam
10641066 final Map <String , Object > localMap4 = (Map ) ((LinkedHashMap ) localMap ).clone ();
10651067 localMap4 .remove (ARRAY );
10661068 object = name .equals (XmlValue .getMapKey (localMap4 ))
1067- ? U .newArrayList (Arrays .asList (getValue (XmlValue .getMapValue (localMap4 ))))
1068- : U .newArrayList (Arrays .asList (getValue (localMap4 )));
1069+ ? U .newArrayList (Arrays .asList (getValue (XmlValue .getMapValue (localMap4 ), FromType . FOR_CONVERT )))
1070+ : U .newArrayList (Arrays .asList (getValue (localMap4 , FromType . FOR_CONVERT )));
10691071 } else {
10701072 object = localMap ;
10711073 }
@@ -1114,7 +1116,8 @@ private static Map<String, Object> checkNullAndString(final Map<String, Object>
11141116 private static Object addElement (final int [] sourceIndex , final String source ,
11151117 final BiFunction <Object , Set <String >, String > elementMapper ,
11161118 final Function <Object , Object > nodeMapper , final int [] uniqueIds ,
1117- final org .w3c .dom .Node currentNode , final Set <String > namespaces ) {
1119+ final org .w3c .dom .Node currentNode , final Set <String > namespaces ,
1120+ final FromType fromType ) {
11181121 final Map <String , Object > attrMapLocal = U .newLinkedHashMap ();
11191122 if (currentNode .getAttributes ().getLength () > 0 ) {
11201123 final java .util .regex .Matcher matcher = ATTRS .matcher (getAttributes (sourceIndex [0 ], source ));
@@ -1126,7 +1129,7 @@ private static Object addElement(final int[] sourceIndex, final String source,
11261129 matcher .reset ();
11271130 while (matcher .find ()) {
11281131 addNodeValue (attrMapLocal , '-' + matcher .group (1 ), matcher .group (2 ),
1129- elementMapper , nodeMapper , uniqueIds , namespaces );
1132+ elementMapper , nodeMapper , uniqueIds , namespaces , fromType );
11301133 }
11311134 }
11321135 if (getAttributes (sourceIndex [0 ], source ).endsWith ("/" )
@@ -1136,7 +1139,7 @@ private static Object addElement(final int[] sourceIndex, final String source,
11361139 attrMapLocal .put (SELF_CLOSING , TRUE );
11371140 }
11381141 return createMap (currentNode , elementMapper , nodeMapper , attrMapLocal , uniqueIds , source ,
1139- sourceIndex , namespaces );
1142+ sourceIndex , namespaces , fromType );
11401143 }
11411144
11421145 static String getAttributes (final int sourceIndex , final String source ) {
@@ -1209,39 +1212,39 @@ private static String unescapeName(final String name) {
12091212 @ SuppressWarnings ("unchecked" )
12101213 private static void addNodeValue (final Map <String , Object > map , final String name , final Object value ,
12111214 final BiFunction <Object , Set <String >, String > elementMapper , final Function <Object , Object > nodeMapper ,
1212- final int [] uniqueIds , final Set <String > namespaces ) {
1215+ final int [] uniqueIds , final Set <String > namespaces , final FromType fromType ) {
12131216 final String elementName = unescapeName (elementMapper .apply (name , namespaces ));
12141217 if (map .containsKey (elementName )) {
12151218 if (TEXT .equals (elementName )) {
1216- map .put (elementName + uniqueIds [0 ], nodeMapper .apply (getValue (value )));
1219+ map .put (elementName + uniqueIds [0 ], nodeMapper .apply (getValue (value , fromType )));
12171220 uniqueIds [0 ] += 1 ;
12181221 } else if (COMMENT .equals (elementName )) {
1219- map .put (elementName + uniqueIds [1 ], nodeMapper .apply (getValue (value )));
1222+ map .put (elementName + uniqueIds [1 ], nodeMapper .apply (getValue (value , fromType )));
12201223 uniqueIds [1 ] += 1 ;
12211224 } else if (CDATA .equals (elementName )) {
1222- map .put (elementName + uniqueIds [2 ], nodeMapper .apply (getValue (value )));
1225+ map .put (elementName + uniqueIds [2 ], nodeMapper .apply (getValue (value , fromType )));
12231226 uniqueIds [2 ] += 1 ;
12241227 } else {
12251228 final Object object = map .get (elementName );
12261229 if (object instanceof List ) {
1227- addText (map , elementName , (List <Object >) object , value );
1230+ addText (map , elementName , (List <Object >) object , value , fromType );
12281231 } else {
12291232 final List <Object > objects = U .newArrayList ();
12301233 objects .add (object );
1231- addText (map , elementName , objects , value );
1234+ addText (map , elementName , objects , value , fromType );
12321235 map .put (elementName , objects );
12331236 }
12341237 }
12351238 } else {
12361239 if (elementName != null ) {
1237- map .put (elementName , nodeMapper .apply (getValue (value )));
1240+ map .put (elementName , nodeMapper .apply (getValue (value , fromType )));
12381241 }
12391242 }
12401243 }
12411244
12421245 @ SuppressWarnings ("unchecked" )
12431246 private static void addText (final Map <String , Object > map , final String name , final List <Object > objects ,
1244- final Object value ) {
1247+ final Object value , final FromType fromType ) {
12451248 int lastIndex = map .size () - 1 ;
12461249 final int index = objects .size ();
12471250 while (true ) {
@@ -1256,7 +1259,7 @@ private static void addText(final Map<String, Object> map, final String name, fi
12561259 objects .add (index , item );
12571260 lastIndex -= 1 ;
12581261 }
1259- final Object newValue = getValue (value );
1262+ final Object newValue = getValue (value , fromType );
12601263 if (newValue instanceof List ) {
12611264 objects .add (((List ) newValue ).get (0 ));
12621265 } else {
@@ -1266,6 +1269,16 @@ private static void addText(final Map<String, Object> map, final String name, fi
12661269
12671270 @ SuppressWarnings ("unchecked" )
12681271 public static Object fromXml (final String xml ) {
1272+ return fromXml (xml , FromType .FOR_CONVERT );
1273+ }
1274+
1275+ public enum FromType {
1276+ FOR_CONVERT ,
1277+ FOR_FORMAT
1278+ }
1279+
1280+ @ SuppressWarnings ("unchecked" )
1281+ public static Object fromXml (final String xml , final FromType fromType ) {
12691282 if (xml == null ) {
12701283 return null ;
12711284 }
@@ -1280,8 +1293,8 @@ public Object apply(Object object) {
12801293 return object ;
12811294 }
12821295 }, Collections .<String , Object >emptyMap (), new int [] {1 , 1 , 1 }, xml , new int [] {0 },
1283- U .<String >newLinkedHashSet ());
1284- if (checkResult (xml , document , result )) {
1296+ U .<String >newLinkedHashSet (), fromType );
1297+ if (checkResult (xml , document , result , fromType )) {
12851298 return ((Map .Entry ) ((Map ) result ).entrySet ().iterator ().next ()).getValue ();
12861299 }
12871300 return result ;
@@ -1291,7 +1304,8 @@ public Object apply(Object object) {
12911304 }
12921305
12931306 @ SuppressWarnings ("unchecked" )
1294- private static boolean checkResult (final String xml , org .w3c .dom .Document document , final Object result ) {
1307+ private static boolean checkResult (final String xml , org .w3c .dom .Document document , final Object result ,
1308+ final FromType fromType ) {
12951309 final Map <String , String > headerAttributes = getHeaderAttributes (xml );
12961310 if (document .getXmlEncoding () != null && !"UTF-8" .equalsIgnoreCase (document .getXmlEncoding ())) {
12971311 ((Map ) result ).put (ENCODING , document .getXmlEncoding ());
@@ -1300,7 +1314,8 @@ private static boolean checkResult(final String xml, org.w3c.dom.Document docume
13001314 }
13011315 } else if (headerAttributes .containsKey (STANDALONE .substring (1 ))) {
13021316 ((Map ) result ).put (STANDALONE , headerAttributes .get (STANDALONE .substring (1 )));
1303- } else if (((Map .Entry ) ((Map ) result ).entrySet ().iterator ().next ()).getKey ().equals ("root" )
1317+ } else if (fromType == FromType .FOR_CONVERT
1318+ && ((Map .Entry ) ((Map ) result ).entrySet ().iterator ().next ()).getKey ().equals ("root" )
13041319 && (((Map .Entry ) ((Map ) result ).entrySet ().iterator ().next ()).getValue () instanceof List
13051320 || ((Map .Entry ) ((Map ) result ).entrySet ().iterator ().next ()).getValue () instanceof Map )) {
13061321 if (xml .startsWith (XML_HEADER )) {
@@ -1369,8 +1384,8 @@ public Object apply(Object object) {
13691384 return object instanceof List ? object : U .newArrayList (Arrays .asList (object ));
13701385 }
13711386 }, Collections .<String , Object >emptyMap (), new int [] {1 , 1 , 1 }, xml , new int [] {0 },
1372- U .<String >newLinkedHashSet ());
1373- if (checkResult (xml , document , result )) {
1387+ U .<String >newLinkedHashSet (), FromType . FOR_CONVERT );
1388+ if (checkResult (xml , document , result , FromType . FOR_CONVERT )) {
13741389 return ((Map .Entry ) ((Map ) result ).entrySet ().iterator ().next ()).getValue ();
13751390 }
13761391 return result ;
@@ -1388,8 +1403,8 @@ public Object apply(Object object) {
13881403 return object ;
13891404 }
13901405 }, Collections .<String , Object >emptyMap (), new int []{1 , 1 , 1 }, xml , new int []{0 },
1391- U .<String >newLinkedHashSet ());
1392- if (checkResult (xml , document , result )) {
1406+ U .<String >newLinkedHashSet (), FromType . FOR_CONVERT );
1407+ if (checkResult (xml , document , result , FromType . FOR_CONVERT )) {
13931408 return ((Map .Entry ) ((Map ) result ).entrySet ().iterator ().next ()).getValue ();
13941409 }
13951410 return result ;
@@ -1445,11 +1460,8 @@ public String apply(Object object, Set<String> namespaces) {
14451460
14461461 @ SuppressWarnings ("unchecked" )
14471462 public static String formatXml (String xml , XmlStringBuilder .Step identStep ) {
1448- Object result = fromXml (xml );
1449- if (result instanceof Map ) {
1450- return toXml ((Map ) result , identStep );
1451- }
1452- return toXml ((List ) result , identStep );
1463+ Object result = fromXml (xml , FromType .FOR_FORMAT );
1464+ return toXml ((Map ) result , identStep );
14531465 }
14541466
14551467 public static String formatXml (String xml ) {
0 commit comments