View Javadoc

1   /*
2   * This program is licensed under Common Public License Version 0.5.
3   *
4   * For License Information and conditions of use, see "LICENSE" in packaged
5   * 
6   * Change History (based on CVS versions):
7   * 
8   * Version:      Date:       Author:         Description:
9   * 1.1           2002/09/24  yuqing_wang     Initial revision
10  * 1.2           2002/09/25  yuqing_wang     .
11  * 1.3           2002/09/25  yuqing_wang     .
12  * 1.4           2003/10/30  ckoelle         Hashtable as value type introduced
13  * 1.5           2004/04/21  ckoelle         Allow different tag texts for the items
14  *                                           Allow mixed data types (only simple ones)
15  * 1.6           2004/04/23  ckoelle         Change Atrribute "class-type" to "type" 
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              // XMLUtil or other engine should provide ability to validate elem against DTD/Schema.
45              String classType = elem.getAttribute("type");
46              // default to "Vector" type
47              if (classType == null)
48              classType = "java.util.Vector";
49  
50              String valueType = elem.getAttribute("value-type");
51              // default to "String" type
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 }