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