001    /* ========================================================================
002     * JCommon : a free general purpose class library for the Java(tm) platform
003     * ========================================================================
004     *
005     * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
006     * 
007     * Project Info:  http://www.jfree.org/jcommon/index.html
008     *
009     * This library is free software; you can redistribute it and/or modify it 
010     * under the terms of the GNU Lesser General Public License as published by 
011     * the Free Software Foundation; either version 2.1 of the License, or 
012     * (at your option) any later version.
013     *
014     * This library is distributed in the hope that it will be useful, but 
015     * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
016     * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
017     * License for more details.
018     *
019     * You should have received a copy of the GNU Lesser General Public
020     * License along with this library; if not, write to the Free Software
021     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
022     * USA.  
023     *
024     * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
025     * in the United States and other countries.]
026     * 
027     * ---------------------
028     * BasicTypeSupport.java
029     * ---------------------
030     * (C)opyright 2003-2005, by Thomas Morgner and Contributors.
031     *
032     * Original Author:  Thomas Morgner;
033     * Contributor(s):   David Gilbert (for Object Refinery Limited);
034     *
035     * $Id: BasicTypeSupport.java,v 1.4 2005/10/18 13:33:53 mungady Exp $
036     *
037     * Changes 
038     * -------
039     * 12-Nov-2003 : Initial version (TM);
040     * 26-Nov-2003 : Updated header and Javadocs (DG);
041     *  
042     */
043    
044    package org.jfree.xml.util;
045    
046    import org.jfree.xml.attributehandlers.BooleanAttributeHandler;
047    import org.jfree.xml.attributehandlers.ByteAttributeHandler;
048    import org.jfree.xml.attributehandlers.DoubleAttributeHandler;
049    import org.jfree.xml.attributehandlers.FloatAttributeHandler;
050    import org.jfree.xml.attributehandlers.IntegerAttributeHandler;
051    import org.jfree.xml.attributehandlers.LongAttributeHandler;
052    import org.jfree.xml.attributehandlers.ShortAttributeHandler;
053    import org.jfree.xml.attributehandlers.StringAttributeHandler;
054    
055    /**
056     * A class that contains information about some basic types.
057     */
058    public class BasicTypeSupport {
059    
060      private BasicTypeSupport ()
061      {
062      }
063    
064        /**
065         * Returns the fully qualified class name for the attribute handler for a property of 
066         * the specified class.
067         * 
068         * @param c  the property class.
069         * 
070         * @return the attribute handler class name.
071         */
072        public static String getHandlerClass(final Class c) {
073            if (c.equals(Integer.class) || c.equals(Integer.TYPE)) {
074                return IntegerAttributeHandler.class.getName();
075            }
076            if (c.equals(Short.class) || c.equals(Short.TYPE)) {
077                return ShortAttributeHandler.class.getName();
078            }
079            if (c.equals(Long.class) || c.equals(Long.TYPE)) {
080                return LongAttributeHandler.class.getName();
081            }
082            if (c.equals(Boolean.class) || c.equals(Boolean.TYPE)) {
083                return BooleanAttributeHandler.class.getName();
084            }
085            if (c.equals(Float.class) || c.equals(Float.TYPE)) {
086                return FloatAttributeHandler.class.getName();
087            }
088            if (c.equals(Double.class) || c.equals(Double.TYPE)) {
089                return DoubleAttributeHandler.class.getName();
090            }
091            if (c.equals(Byte.class) || c.equals(Byte.TYPE)) {
092                return ByteAttributeHandler.class.getName();
093            }
094            // string can also be handled directly ...
095            if (c.equals(String.class)) {
096                return StringAttributeHandler.class.getName();
097            }
098            throw new IllegalArgumentException("BasicTypeSupport.getHandlerClass(Class): "
099                + "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    }