1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package net.wangs.xtype.apps;
22
23 import java.util.*;
24 import org.w3c.dom.*;
25
26 import net.wangs.xtype.core.*;
27 import net.wangs.xmlutil.*;
28
29 /***
30 * Implementation of the XObject interface for all types of Maps.
31 *
32 * @author <a href="mailto:yuqingwang_99@yahoo.com">Yuqing Wang</a>
33 * @author <a href="mailto:christian.koelle@sdm.com">Dr. Christian K?lle</a>
34 *
35 */
36 public class XMap implements XObject {
37
38 /***
39 * Get a Collection from the XML element
40 * @param elem XML element with the XML representation of a Collection
41 * @param itemTagText Name of the item tag
42 * @return The Collection
43 * @throws XTypeException XML Element is not readable
44 */
45 public Object getObject(Element elem, String itemTagText) throws XTypeException {
46 try {
47
48
49 String classType = elem.getAttribute("type");
50
51 String keyType = elem.getAttribute("key-type");
52
53 if (keyType == null)
54 keyType = "java.lang.String";
55
56 String valueType = elem.getAttribute("value-type");
57
58 if (valueType == null)
59 valueType = "java.lang.String";
60
61 XMLUtil util = new XMLUtil(elem);
62 Vector v = util.getNodes("/*/" + itemTagText);
63
64 if (v == null) {
65 String msg = "Error retrieving data from XML element";
66 throw new XTypeException(msg);
67 }
68
69 return getMapObject(v, classType, keyType, valueType, itemTagText);
70 } catch (XMLUtilException e) {
71 throw new XTypeException(e.getMessage());
72 }
73
74 }
75
76 /***
77 * Constructs the Map from the XML data
78 * @param v Vector with all item nodes in XML
79 * @param classType Type of map
80 * @param valueType Data type of the values
81 * @param itemTagText Name of the item tag
82 * @return The XML data in a Map
83 * @throws XMLUtilException If any error occurs in examining the XML node
84 * @throws XTypeException If any error occurs in reading complex data types via XType itself
85 */
86 private Map getMapObject(Vector v, String classType, String keyType, String valueType, String itemTagText) throws XTypeException {
87 try {
88 Map map = (Map)(Class.forName(classType)).newInstance();
89
90 for (int i = 0; i < v.size(); i++) {
91 String localValueType = null;
92 Object key = null;
93 Object value = null;
94 XMLUtil util = new XMLUtil((Element) v.elementAt(i));
95
96
97 if (keyType.equals("java.lang.String")) {
98 key = util.getNamedAttrValue("/" + itemTagText, "name", 1);
99 }
100
101
102 try {
103 localValueType = util.getNamedAttrValue("/" + itemTagText, "type", 1);
104 } catch (XMLUtilException e) {
105 if (valueType == null) {
106 throw new XTypeException("No value type found!");
107 }
108 localValueType = valueType;
109 }
110
111 if (localValueType.equals("java.lang.String") || localValueType.equals("String") || localValueType.equals("string")) {
112 map.put(key, util.getNodeValue("/" + itemTagText, 1));
113 } else if (localValueType.equals("int")) {
114 map.put(key, new Integer(util.getNodeValue("/" + itemTagText, 1)));
115 }
116 }
117
118 return map;
119
120 } catch (Exception e) {
121 throw new XTypeException(e.getMessage());
122 }
123 }
124 }