Mercurial Hosting > luan
comparison src/luan/modules/parsers/Xml.java @ 1468:35f3bfd4f51d
xml
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 14 Apr 2020 08:44:33 -0600 |
parents | 670b7d089699 |
children | f5b8c7e1ac82 |
comparison
equal
deleted
inserted
replaced
1467:509d49c493c0 | 1468:35f3bfd4f51d |
---|---|
14 | 14 |
15 | 15 |
16 public final class Xml { | 16 public final class Xml { |
17 | 17 |
18 public static String toString(LuanTable tbl) throws LuanException { | 18 public static String toString(LuanTable tbl) throws LuanException { |
19 if( tbl.rawSize() != 1 ) | 19 XmlElement[] elements = elements(tbl); |
20 throw new LuanException("XML most have 1 root element"); | 20 if( elements.length != 1 ) |
21 Map.Entry entry = tbl.iterator().next(); | 21 throw new LuanException("XML must have 1 root element"); |
22 Object key = entry.getKey(); | 22 return elements[0].toString(); |
23 if( !(key instanceof String) ) | |
24 throw new LuanException("XML key must be string"); | |
25 String name = (String)key; | |
26 Object value = entry.getValue(); | |
27 if( !(value instanceof LuanTable) ) | |
28 throw new LuanException("XML root value must be table"); | |
29 LuanTable t = (LuanTable)value; | |
30 Map<String,String> attributes = attributes(t); | |
31 XmlElement[] elements = elements(t); | |
32 XmlElement element = new XmlElement(name,attributes,elements); | |
33 return element.toString(); | |
34 } | 23 } |
35 | 24 |
36 private static final Integer ONE = new Integer(1); | 25 private static final Integer ONE = Integer.valueOf(1); |
26 private static final Integer TWO = Integer.valueOf(2); | |
27 | |
28 private static XmlElement[] elements(LuanTable tbl) throws LuanException { | |
29 List<XmlElement> list = new ArrayList<XmlElement>(); | |
30 for( Map.Entry entry : tbl.iterable() ) { | |
31 Object key = entry.getKey(); | |
32 if( key.equals(ONE) ) | |
33 continue; | |
34 if( !(key instanceof String) ) | |
35 throw new LuanException("XML key must be string"); | |
36 String name = (String)key; | |
37 Object value = entry.getValue(); | |
38 XmlElement element; | |
39 if( value instanceof String ) { | |
40 String s = (String)value; | |
41 element = new XmlElement(name,Collections.emptyMap(),s); | |
42 } else { | |
43 LuanTable t = (LuanTable)value; | |
44 Map<String,String> attributes = attributes(t); | |
45 String s = (String)t.get(TWO); | |
46 if( s != null ) { | |
47 element = new XmlElement(name,attributes,s); | |
48 } else { | |
49 XmlElement[] elements = elements(t); | |
50 if( elements.length==0 ) { | |
51 element = new XmlElement(name,attributes); | |
52 } else { | |
53 element = new XmlElement(name,attributes,elements); | |
54 } | |
55 } | |
56 } | |
57 list.add(element); | |
58 } | |
59 return list.toArray(new XmlElement[0]); | |
60 } | |
37 | 61 |
38 private static Map<String,String> attributes(LuanTable tbl) throws LuanException { | 62 private static Map<String,String> attributes(LuanTable tbl) throws LuanException { |
39 Object obj = tbl.get(ONE); | 63 Object obj = tbl.get(ONE); |
40 if( obj==null ) | 64 if( obj==null ) |
41 return Collections.emptyMap(); | 65 return Collections.emptyMap(); |
47 map.put(name,value); | 71 map.put(name,value); |
48 } | 72 } |
49 return map; | 73 return map; |
50 } | 74 } |
51 | 75 |
52 private static XmlElement[] elements(LuanTable tbl) throws LuanException { | |
53 List<XmlElement> list = new ArrayList<XmlElement>(); | |
54 for( Map.Entry entry : tbl.iterable() ) { | |
55 Object key = entry.getKey(); | |
56 if( key.equals(ONE) ) | |
57 continue; | |
58 String name = (String)key; | |
59 Object value = entry.getValue(); | |
60 XmlElement element; | |
61 if( value instanceof String ) { | |
62 String s = (String)value; | |
63 element = new XmlElement(name,Collections.emptyMap(),s); | |
64 } else { | |
65 LuanTable t = (LuanTable)value; | |
66 Map<String,String> attributes = attributes(t); | |
67 XmlElement[] elements = elements(t); | |
68 element = new XmlElement(name,attributes,elements); | |
69 } | |
70 list.add(element); | |
71 } | |
72 return list.toArray(new XmlElement[0]); | |
73 } | |
74 | |
75 | 76 |
76 public static LuanTable parse(Luan luan,String s) throws ParseException, LuanException { | 77 public static LuanTable parse(Luan luan,String s) throws ParseException, LuanException { |
77 XmlElement element = XmlParser.parse(s); | 78 XmlElement element = XmlParser.parse(s); |
78 return toTable(luan,new XmlElement[]{element}); | 79 LuanTable tbl = new LuanTable(luan); |
80 addElements(tbl,new XmlElement[]{element}); | |
81 return tbl; | |
79 } | 82 } |
80 | 83 |
81 private static LuanTable toTable(Luan luan,XmlElement[] elements) throws LuanException { | 84 private static LuanTable addElements(LuanTable tbl,XmlElement[] elements) throws LuanException { |
82 LuanTable tbl = new LuanTable(luan); | |
83 for( XmlElement element : elements ) { | 85 for( XmlElement element : elements ) { |
84 if( element.content instanceof String ) { | 86 LuanTable t = new LuanTable(tbl.luan()); |
85 tbl.put(element.name,element.content); | 87 if( !element.attributes.isEmpty() ) { |
88 LuanTable attrs = new LuanTable(tbl.luan()); | |
89 for( Map.Entry<String,String> entry : element.attributes.entrySet() ) { | |
90 attrs.put(entry.getKey(),entry.getValue()); | |
91 } | |
92 t.put( 1, attrs ); | |
93 } | |
94 if( element.content == null ) { | |
95 tbl.put(element.name,t); | |
96 } else if( element.content instanceof String ) { | |
97 if( t.isEmpty() ) { | |
98 tbl.put(element.name,element.content); | |
99 } else { | |
100 t.put( 2, element.content ); | |
101 tbl.put(element.name,t); | |
102 } | |
86 } else { | 103 } else { |
87 XmlElement[] els = (XmlElement[])element.content; | 104 XmlElement[] els = (XmlElement[])element.content; |
88 LuanTable t = toTable(luan,els); | 105 addElements(t,els); |
89 tbl.put(element.name,t); | 106 tbl.put(element.name,t); |
90 } | 107 } |
91 } | 108 } |
92 return tbl; | 109 return tbl; |
93 } | 110 } |