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