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           2003/11/28  ckoelle         Bug fix: Nodes have to read as java.util.Map/hashItem
14  * 1.6           2004/04/19  ckoelle         Nodes are now item as before for consistency reasons, Hashtables 
15  *                                           as items are no longer allowed.
16  * 1.7           2004/04/21  ckoelle         Allow different tag texts for the items
17  *                                           Enable simple syntax for items
18  * 1.8           2004/04/23  ckoelle         Change Atrribute "class-type" to "type"
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        // XMLUtil or other engine should provide ability to validate elem against DTD/Schema.
48  
49        String classType = elem.getAttribute("type");
50  
51        String keyType = elem.getAttribute("key-type");
52        // default to "String" type
53        if (keyType == null)
54          keyType = "java.lang.String";
55  
56        String valueType = elem.getAttribute("value-type");
57        // default to "String" type
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            // Read the key
97            if (keyType.equals("java.lang.String")) {
98                key = util.getNamedAttrValue("/" + itemTagText, "name", 1);
99            } 
100             
101           // Read the value
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 }