changeset 1495:2e8a5df45d56

better xml
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 05 May 2020 11:03:48 -0600
parents 91c167099462
children 6c830be6be98
files src/luan/LuanTable.java src/luan/modules/parsers/Xml.java
diffstat 2 files changed, 24 insertions(+), 36 deletions(-) [+]
line wrap: on
line diff
diff -r 91c167099462 -r 2e8a5df45d56 src/luan/LuanTable.java
--- a/src/luan/LuanTable.java	Sun May 03 11:51:31 2020 -0600
+++ b/src/luan/LuanTable.java	Tue May 05 11:03:48 2020 -0600
@@ -124,7 +124,7 @@
 		return map==null || map.isEmpty();
 	}
 
-	boolean isMap() {
+	public boolean isMap() {
 		return map!=null || list==null;
 	}
 
diff -r 91c167099462 -r 2e8a5df45d56 src/luan/modules/parsers/Xml.java
--- a/src/luan/modules/parsers/Xml.java	Sun May 03 11:51:31 2020 -0600
+++ b/src/luan/modules/parsers/Xml.java	Tue May 05 11:03:48 2020 -0600
@@ -11,6 +11,7 @@
 import luan.Luan;
 import luan.LuanTable;
 import luan.LuanException;
+import luan.modules.BasicLuan;
 
 
 public final class Xml {
@@ -22,31 +23,27 @@
 		return elements[0].toString();
 	}
 
-	private static final Integer ONE = Integer.valueOf(1);
-	private static final Integer TWO = Integer.valueOf(2);
-	private static final String LIST = "$list";
+	private static final String ATTRIBUTES = "xml_attributes";
+	private static final String TEXT = "xml_text";
 
 	private static XmlElement[] elements(LuanTable tbl) throws LuanException {
 		List<XmlElement> list = new ArrayList<XmlElement>();
 		for( Map.Entry entry : tbl.iterable() ) {
 			Object key = entry.getKey();
-			if( key.equals(ONE) )
-				continue;
 			if( !(key instanceof String) )
 				throw new LuanException("XML key must be string");
 			String name = (String)key;
 			Object value = entry.getValue();
-			if( value instanceof String ) {
-				list.add( element(name,value) );
+			if( name.equals(ATTRIBUTES) )
+				continue;
+			if( name.equals(TEXT) )
+				throw new LuanException("Can't mix text and elements");
+			LuanTable t = (LuanTable)value;
+			if( t.isMap() ) {
+				list.add( element(name,t) );
 			} else {
-				LuanTable t = (LuanTable)value;
-				LuanTable tList = (LuanTable)t.get(LIST);
-				if( tList == null ) {
-					list.add( element(name,t) );
-				} else {
-					for( Object obj : tList.asList() ) {
-						list.add( element(name,obj) );
-					}
+				for( Object obj : t.asList() ) {
+					list.add( element(name,obj) );
 				}
 			}
 		}
@@ -59,7 +56,7 @@
 		}
 		LuanTable t = (LuanTable)obj;
 		Map<String,String> attributes = attributes(t);
-		String s = (String)t.get(TWO);
+		String s = (String)t.get(TEXT);
 		if( s != null ) {
 			return new XmlElement(name,attributes,s);
 		} else {
@@ -73,7 +70,7 @@
 	}
 
 	private static Map<String,String> attributes(LuanTable tbl) throws LuanException {
-		Object obj = tbl.get(ONE);
+		Object obj = tbl.get(ATTRIBUTES);
 		if( obj==null )
 			return Collections.emptyMap();
 		LuanTable t = (LuanTable)obj;
@@ -102,36 +99,27 @@
 				for( Map.Entry<String,String> entry : element.attributes.entrySet() ) {
 					attrs.put(entry.getKey(),entry.getValue());
 				}
-				t.put( 1, attrs );
+				t.put( ATTRIBUTES, attrs );
 			}
-			Object value = t;
 			if( element.content == null ) {
 				// nothing
 			} else if( element.content instanceof String ) {
-				if( t.isEmpty() ) {
-					value = element.content;
-				} else {
-					t.put( 2, element.content );
-				}
+				t.put( TEXT, element.content );
 			} else {
 				XmlElement[] els = (XmlElement[])element.content;
 				addElements(t,els);
 			}
-			Object old = tbl.get(element.name);
+			LuanTable old = (LuanTable)tbl.get(element.name);
 			if( old==null ) {
-				tbl.put(element.name,value);
+				tbl.put(element.name,t);
 			} else {
-				LuanTable list = null;
-				if( old instanceof LuanTable )
-					list = (LuanTable)((LuanTable)old).get(LIST);
-				if( list==null ) {
-					list = new LuanTable(tbl.luan());
+				if( !old.isList() ) {
+					LuanTable list = new LuanTable(tbl.luan());
 					list.rawAdd(old);
-					LuanTable wrap = new LuanTable(tbl.luan());
-					wrap.put(LIST,list);
-					tbl.put(element.name,wrap);
+					old = list;
+					tbl.put(element.name,old);
 				}
-				list.rawAdd(value);
+				old.rawAdd(t);
 			}
 		}
 		return tbl;