Source for org.jfree.xml.util.BasicTypeSupport

   1: /* ========================================================================
   2:  * JCommon : a free general purpose class library for the Java(tm) platform
   3:  * ========================================================================
   4:  *
   5:  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
   6:  * 
   7:  * Project Info:  http://www.jfree.org/jcommon/index.html
   8:  *
   9:  * This library is free software; you can redistribute it and/or modify it 
  10:  * under the terms of the GNU Lesser General Public License as published by 
  11:  * the Free Software Foundation; either version 2.1 of the License, or 
  12:  * (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but 
  15:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
  16:  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
  17:  * License for more details.
  18:  *
  19:  * You should have received a copy of the GNU Lesser General Public
  20:  * License along with this library; if not, write to the Free Software
  21:  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
  22:  * USA.  
  23:  *
  24:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
  25:  * in the United States and other countries.]
  26:  * 
  27:  * ---------------------
  28:  * BasicTypeSupport.java
  29:  * ---------------------
  30:  * (C)opyright 2003-2005, by Thomas Morgner and Contributors.
  31:  *
  32:  * Original Author:  Thomas Morgner;
  33:  * Contributor(s):   David Gilbert (for Object Refinery Limited);
  34:  *
  35:  * $Id: BasicTypeSupport.java,v 1.4 2005/10/18 13:33:53 mungady Exp $
  36:  *
  37:  * Changes 
  38:  * -------
  39:  * 12-Nov-2003 : Initial version (TM);
  40:  * 26-Nov-2003 : Updated header and Javadocs (DG);
  41:  *  
  42:  */
  43: 
  44: package org.jfree.xml.util;
  45: 
  46: import org.jfree.xml.attributehandlers.BooleanAttributeHandler;
  47: import org.jfree.xml.attributehandlers.ByteAttributeHandler;
  48: import org.jfree.xml.attributehandlers.DoubleAttributeHandler;
  49: import org.jfree.xml.attributehandlers.FloatAttributeHandler;
  50: import org.jfree.xml.attributehandlers.IntegerAttributeHandler;
  51: import org.jfree.xml.attributehandlers.LongAttributeHandler;
  52: import org.jfree.xml.attributehandlers.ShortAttributeHandler;
  53: import org.jfree.xml.attributehandlers.StringAttributeHandler;
  54: 
  55: /**
  56:  * A class that contains information about some basic types.
  57:  */
  58: public class BasicTypeSupport {
  59: 
  60:   private BasicTypeSupport ()
  61:   {
  62:   }
  63: 
  64:     /**
  65:      * Returns the fully qualified class name for the attribute handler for a property of 
  66:      * the specified class.
  67:      * 
  68:      * @param c  the property class.
  69:      * 
  70:      * @return the attribute handler class name.
  71:      */
  72:     public static String getHandlerClass(final Class c) {
  73:         if (c.equals(Integer.class) || c.equals(Integer.TYPE)) {
  74:             return IntegerAttributeHandler.class.getName();
  75:         }
  76:         if (c.equals(Short.class) || c.equals(Short.TYPE)) {
  77:             return ShortAttributeHandler.class.getName();
  78:         }
  79:         if (c.equals(Long.class) || c.equals(Long.TYPE)) {
  80:             return LongAttributeHandler.class.getName();
  81:         }
  82:         if (c.equals(Boolean.class) || c.equals(Boolean.TYPE)) {
  83:             return BooleanAttributeHandler.class.getName();
  84:         }
  85:         if (c.equals(Float.class) || c.equals(Float.TYPE)) {
  86:             return FloatAttributeHandler.class.getName();
  87:         }
  88:         if (c.equals(Double.class) || c.equals(Double.TYPE)) {
  89:             return DoubleAttributeHandler.class.getName();
  90:         }
  91:         if (c.equals(Byte.class) || c.equals(Byte.TYPE)) {
  92:             return ByteAttributeHandler.class.getName();
  93:         }
  94:         // string can also be handled directly ...
  95:         if (c.equals(String.class)) {
  96:             return StringAttributeHandler.class.getName();
  97:         }
  98:         throw new IllegalArgumentException("BasicTypeSupport.getHandlerClass(Class): "
  99:             + "this is no attribute type.");
 100:     }
 101: 
 102:     /**
 103:      * Returns <code>true</code> if the specified class is a "basic" type, and <code>false</code>
 104:      * otherwise.  Basic types are written as attributes (rather than elements) in XML output.
 105:      * 
 106:      * @param c  the class.
 107:      * 
 108:      * @return a boolean.
 109:      */
 110:     public static boolean isBasicDataType (final Class c) {
 111:         if (c.equals(Integer.class) || c.equals(Integer.TYPE)) {
 112:             return true;
 113:         }
 114:         if (c.equals(Short.class) || c.equals(Short.TYPE)) {
 115:             return true;
 116:         }
 117:         if (c.equals(Long.class) || c.equals(Long.TYPE)) {
 118:             return true;
 119:         }
 120:         if (c.equals(Boolean.class) || c.equals(Boolean.TYPE)) {
 121:             return true;
 122:         }
 123:         if (c.equals(Float.class) || c.equals(Float.TYPE)) {
 124:             return true;
 125:         }
 126:         if (c.equals(Double.class) || c.equals(Double.TYPE)) {
 127:             return true;
 128:         }
 129:         if (c.equals(Byte.class) || c.equals(Byte.TYPE)) {
 130:             return true;
 131:         }
 132:         // string can also be handled directly ...
 133:         if (c.equals(String.class)) {
 134:             return true;
 135:         }
 136:         return false;
 137:     }
 138: 
 139:     /**
 140:      * Returns the class for a given primitive class type.
 141:      * 
 142:      * @param className  the primitive class name.
 143:      * 
 144:      * @return a class.
 145:      */
 146:     public static Class getClassRepresentation(final String className) {
 147:         if (className.equals("::double")) {
 148:             return Double.TYPE;
 149:         }
 150:         if (className.equals("::boolean")) {
 151:             return Boolean.TYPE;
 152:         }
 153:         if (className.equals("::int")) {
 154:             return Integer.TYPE;
 155:         }
 156:         if (className.equals("::short")) {
 157:             return Short.TYPE;
 158:         }
 159:         if (className.equals("::long")) {
 160:             return Long.TYPE;
 161:         }
 162:         if (className.equals("::byte")) {
 163:             return Byte.TYPE;
 164:         }
 165:         throw new IllegalArgumentException("This is none of my primitives.");
 166:     }
 167: 
 168: }