1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.onemind.commons.java.datastructure;
22
23 import java.sql.Types;
24 import java.util.HashMap;
25 import java.util.Map;
26 import org.onemind.commons.java.lang.reflect.ReflectUtils;
27 import org.onemind.commons.java.xml.digest.*;
28 import org.xml.sax.Attributes;
29 import org.xml.sax.SAXException;
30 /***
31 * A Sax elemenbt handler that can handle parsing properties specified in an xml file
32 * This syntax of the xml is something like
33 * <Property name="defaultvalue" value="default"/>
34 <Property name="shortvalue" type="short" value="1"/>
35 <Property name="intvalue" type="int" value="1"/>
36 <Property name="longvalue" type="long" value="1"/>
37 <Property name="floatvalue" type="float" value="1"/>
38 <Property name="doublevalue" type="double" value="1"/>
39 <Property name="charvalue" type="char" value="c"/>
40 <Property name="stringvalue" type="string" value="string"/>
41 * @author TiongHiang Lee (thlee@onemindsoft.org)
42 */
43 public class XmlPropertyElementDigester extends DefaultDigester implements ElementDigester
44 {
45
46 private static final Map _typeMap;
47 static
48 {
49 _typeMap = new HashMap();
50 _typeMap.put("short", new Integer(Types.SMALLINT));
51 _typeMap.put("int", new Integer(Types.INTEGER));
52 _typeMap.put("long", new Integer(Types.BIGINT));
53 _typeMap.put("float", new Integer(Types.FLOAT));
54 _typeMap.put("double", new Integer(Types.DOUBLE));
55 _typeMap.put("boolean", new Integer(Types.BOOLEAN));
56 _typeMap.put("char", new Integer(Types.CHAR));
57 _typeMap.put("string", new Integer(Types.VARCHAR));
58 }
59
60 private final Map _prop;
61
62 public XmlPropertyElementDigester(String elementName, Map prop)
63 {
64 super(elementName);
65 _prop = prop;
66 }
67
68 /***
69 * {@inheritDoc}
70 */
71 public void startDigest(SaxDigesterHandler handler, Attributes attrs) throws SAXException
72 {
73 String name = attrs.getValue("name");
74 String type = attrs.getValue("type");
75 String value = attrs.getValue("value");
76 String clazz = attrs.getValue("class");
77 if (name == null)
78 {
79 throw new SAXException("name attribute must exists on property");
80 }
81 if (type != null && clazz != null)
82 {
83 throw new SAXException("Both type and class cannot be specified on same property");
84 }
85 if (clazz != null)
86 {
87 try
88 {
89 Class c = ReflectUtils.getClass(clazz);
90 Object obj = (Object) ReflectUtils.newInstance(c, null);
91 String digest = attrs.getValue("digest");
92 if (digest != null && digest.equalsIgnoreCase("true"))
93 {
94 if (clazz != null)
95 {
96 if (!ElementDigester.class.isAssignableFrom(obj.getClass()))
97 {
98 throw new SAXException("Class " + clazz + " is not a subclass of ElementDigester");
99 }
100 ElementDigester dig = (ElementDigester) obj;
101 handler.addSubDigester(dig);
102 _prop.put(name, dig);
103 }
104 } else
105 {
106 _prop.put(name, obj);
107 }
108 } catch (Exception e)
109 {
110 throw new SAXException(e.getMessage(), e);
111 }
112 } else if (type != null)
113 {
114 Object v = null;
115 Integer typeInt = (Integer) _typeMap.get(type);
116 if (typeInt == null)
117 {
118 throw new SAXException("Unrecognized property type " + type);
119 } else
120 {
121 switch (typeInt.intValue())
122 {
123 case Types.SMALLINT :
124 v = Short.valueOf(value);
125 break;
126 case Types.INTEGER :
127 v = Integer.valueOf(value);
128 break;
129 case Types.BIGINT :
130 v = Long.valueOf(value);
131 break;
132 case Types.FLOAT :
133 v = Float.valueOf(value);
134 break;
135 case Types.DOUBLE :
136 v = Double.valueOf(value);
137 break;
138 case Types.BOOLEAN :
139 v = Boolean.valueOf(value);
140 break;
141 case Types.CHAR :
142 v = new Character(value.charAt(0));
143 break;
144 case Types.VARCHAR :
145 v = value;
146 break;
147 default :
148 throw new IllegalStateException("Unrecognized property type " + type);
149 }
150 }
151 _prop.put(name, v);
152 } else
153 {
154 _prop.put(name, value);
155 }
156 }
157
158 /***
159 * {@inheritDoc}
160 */
161 public void endDigest(SaxDigesterHandler handler) throws SAXException
162 {
163
164 }
165
166 /***
167 * {@inheritDoc}
168 */
169 public void characters(SaxDigesterHandler handler, char[] chars, int offset, int length) throws SAXException
170 {
171
172 }
173 }