changeset 1471:f5b8c7e1ac82

xml
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 14 Apr 2020 20:20:45 -0600
parents 580ee513a4b7
children 60f6741f000a
files src/luan/modules/parsers/Xml.java
diffstat 1 files changed, 45 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
diff -r 580ee513a4b7 -r f5b8c7e1ac82 src/luan/modules/parsers/Xml.java
--- a/src/luan/modules/parsers/Xml.java	Tue Apr 14 17:12:06 2020 -0600
+++ b/src/luan/modules/parsers/Xml.java	Tue Apr 14 20:20:45 2020 -0600
@@ -24,6 +24,7 @@
 
 	private static final Integer ONE = Integer.valueOf(1);
 	private static final Integer TWO = Integer.valueOf(2);
+	private static final String LIST = "$list";
 
 	private static XmlElement[] elements(LuanTable tbl) throws LuanException {
 		List<XmlElement> list = new ArrayList<XmlElement>();
@@ -35,30 +36,42 @@
 				throw new LuanException("XML key must be string");
 			String name = (String)key;
 			Object value = entry.getValue();
-			XmlElement element;
 			if( value instanceof String ) {
-				String s = (String)value;
-				element = new XmlElement(name,Collections.emptyMap(),s);
+				list.add( element(name,value) );
 			} else {
 				LuanTable t = (LuanTable)value;
-				Map<String,String> attributes = attributes(t);
-				String s = (String)t.get(TWO);
-				if( s != null ) {
-					element = new XmlElement(name,attributes,s);
+				LuanTable tList = (LuanTable)t.get(LIST);
+				if( tList == null ) {
+					list.add( element(name,t) );
 				} else {
-					XmlElement[] elements = elements(t);
-					if( elements.length==0 ) {
-						element = new XmlElement(name,attributes);
-					} else {
-						element = new XmlElement(name,attributes,elements);
+					for( Object obj : tList.asList() ) {
+						list.add( element(name,obj) );
 					}
 				}
 			}
-			list.add(element);
 		}
 		return list.toArray(new XmlElement[0]);
 	}
 
+	private static XmlElement element(String name,Object obj) throws LuanException {
+		if( obj instanceof String ) {
+			return new XmlElement( name, Collections.emptyMap(), (String)obj );
+		}
+		LuanTable t = (LuanTable)obj;
+		Map<String,String> attributes = attributes(t);
+		String s = (String)t.get(TWO);
+		if( s != null ) {
+			return new XmlElement(name,attributes,s);
+		} else {
+			XmlElement[] elements = elements(t);
+			if( elements.length==0 ) {
+				return new XmlElement(name,attributes);
+			} else {
+				return new XmlElement(name,attributes,elements);
+			}
+		}
+	}
+
 	private static Map<String,String> attributes(LuanTable tbl) throws LuanException {
 		Object obj = tbl.get(ONE);
 		if( obj==null )
@@ -91,19 +104,34 @@
 				}
 				t.put( 1, attrs );
 			}
+			Object value = t;
 			if( element.content == null ) {
-				tbl.put(element.name,t);
+				// nothing
 			} else if( element.content instanceof String ) {
 				if( t.isEmpty() ) {
-					tbl.put(element.name,element.content);
+					value = element.content;
 				} else {
 					t.put( 2, element.content );
-					tbl.put(element.name,t);
 				}
 			} else {
 				XmlElement[] els = (XmlElement[])element.content;
 				addElements(t,els);
-				tbl.put(element.name,t);
+			}
+			Object old = tbl.get(element.name);
+			if( old==null ) {
+				tbl.put(element.name,value);
+			} else {
+				LuanTable list = null;
+				if( old instanceof LuanTable )
+					list = (LuanTable)((LuanTable)old).get(LIST);
+				if( list==null ) {
+					list = new LuanTable(tbl.luan());
+					list.rawAdd(old);
+					LuanTable wrap = new LuanTable(tbl.luan());
+					wrap.put(LIST,list);
+					tbl.put(element.name,wrap);
+				}
+				list.rawAdd(value);
 			}
 		}
 		return tbl;