HierarchicalDataMetaData.java
上传用户:zhengdagz
上传日期:2014-03-06
资源大小:1956k
文件大小:4k
源码类别:

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: HierarchicalDataMetaData.java,v 1.2 2005/10/10 17:01:11 rbair Exp $
  3.  *
  4.  * Copyright 2005 Sun Microsystems, Inc., 4150 Network Circle,
  5.  * Santa Clara, California 95054, U.S.A. All rights reserved.
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  * 
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  * 
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  20.  */
  21. package org.jdesktop.binding.swingx.adapter;
  22. import java.util.HashMap;
  23. import org.jdesktop.binding.metadata.Converter;
  24. import org.jdesktop.binding.metadata.Converters;
  25. import org.w3c.dom.Element;
  26. import org.w3c.dom.Node;
  27. import org.w3c.dom.NodeList;
  28. /**
  29.  * @author Ramesh Gupta
  30.  */
  31. public class HierarchicalDataMetaData extends TabularDataMetaData {
  32.     private Element columns = null;
  33.     private static HashMap typesMap;
  34.     // Merge with realizer.attr.Decoder.typesMap()
  35.     static {
  36.         typesMap = new HashMap();
  37.         typesMap.put("boolean", java.lang.Boolean.class);
  38.         typesMap.put("date", java.util.Date.class);
  39.         typesMap.put("double", java.lang.Double.class);
  40.         typesMap.put("float", java.lang.Float.class);
  41.         typesMap.put("integer", java.lang.Integer.class);
  42.         typesMap.put("string", java.lang.String.class);
  43.     }
  44.     public HierarchicalDataMetaData(Element metaDataElement) {
  45.         super((metaDataElement == null) ?
  46.             0 : Integer.parseInt(metaDataElement.getAttribute("columnCount")));
  47.      columns = metaDataElement;
  48.         init();
  49.     }
  50.     protected void init() {
  51.         if (columns != null) {
  52.             NodeList list = ( (Element) columns).getChildNodes();
  53.             int i = 0, k = 0, max = list.getLength();
  54.             Node node;
  55.             Element elem;
  56.             while (i < max) {
  57.                 node = list.item(i++);
  58.                 if (node instanceof Element) {
  59.                     elem = (Element) node;
  60.                     if (elem.getLocalName().equals("columnMetaData")) {
  61.                         setColumnName(++k, elem.getAttribute("name"));
  62.                         String type = elem.getAttribute("type");
  63.                         if (type.length() > 0) {
  64.                             Class klass = decodeType(type);
  65.                             if (klass != null) {
  66.                                 setColumnClass(k, klass);
  67.                                 if (klass != String.class) {
  68.                                     Converter converter =
  69.                                         Converters.get(klass);
  70.                                     if (converter == null) {
  71.                                         System.err.println(
  72.                                             "warning: couldn't find converter for " +
  73.                                             klass.getName() +
  74.                                             ". Reseting class of column " +
  75.                                             k + "to String.class");
  76.                                         setColumnClass(k, String.class);
  77.                                     }
  78.                                     else {
  79.                                         setColumnConverter(k, converter); //stash it
  80.                                     }
  81.                                 }
  82.                             }
  83.                         }
  84.                     }
  85.                 }
  86.             }
  87.         }
  88.     }
  89.     // Merge with realizer.attr.Decoder.decodeType()
  90.     private Class decodeType(String value) {
  91.         Class klass = (Class)typesMap.get(value);
  92.         if (klass == null) {
  93.             try {
  94.                 klass = Class.forName(value);
  95.             } catch (ClassNotFoundException e) {
  96.                 System.out.println("Could not convert type: " + value + " to a java type or class");
  97.             }
  98.         }
  99.         return klass;
  100.     }
  101. }