001/**
002 *   GRANITE DATA SERVICES
003 *   Copyright (C) 2006-2013 GRANITE DATA SERVICES S.A.S.
004 *
005 *   This file is part of the Granite Data Services Platform.
006 *
007 *   Granite Data Services is free software; you can redistribute it and/or
008 *   modify it under the terms of the GNU Lesser General Public
009 *   License as published by the Free Software Foundation; either
010 *   version 2.1 of the License, or (at your option) any later version.
011 *
012 *   Granite Data Services is distributed in the hope that it will be useful,
013 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
014 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
015 *   General Public License for more details.
016 *
017 *   You should have received a copy of the GNU Lesser General Public
018 *   License along with this library; if not, write to the Free Software
019 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
020 *   USA, or see <http://www.gnu.org/licenses/>.
021 */
022package org.granite.generator.as3;
023
024import java.io.Serializable;
025import java.lang.reflect.ParameterizedType;
026import java.lang.reflect.Type;
027import java.math.BigDecimal;
028import java.math.BigInteger;
029import java.math.MathContext;
030import java.math.RoundingMode;
031import java.net.URI;
032import java.net.URL;
033import java.util.Calendar;
034import java.util.Collection;
035import java.util.Date;
036import java.util.HashMap;
037import java.util.Locale;
038import java.util.Map;
039
040import org.granite.util.ClassUtil;
041import org.w3c.dom.Document;
042
043public class LCDSAs3TypeFactory implements As3TypeFactory {
044
045    ///////////////////////////////////////////////////////////////////////////
046    // Fields.
047
048    private final Map<Class<?>, As3Type> java2As3Type = new HashMap<Class<?>, As3Type>();
049
050    ///////////////////////////////////////////////////////////////////////////
051    // Constructors.
052
053    public LCDSAs3TypeFactory() {
054        java2As3Type.put(Double.class, As3Type.NUMBER);
055        java2As3Type.put(Double.TYPE, As3Type.NUMBER);
056        java2As3Type.put(Float.class, As3Type.NUMBER);
057        java2As3Type.put(Float.TYPE, As3Type.NUMBER);
058        java2As3Type.put(Long.class, As3Type.NUMBER);
059        java2As3Type.put(Long.TYPE, As3Type.NUMBER);
060        java2As3Type.put(Integer.class, As3Type.NUMBER);
061        java2As3Type.put(Integer.TYPE, As3Type.INT);
062        java2As3Type.put(Short.class, As3Type.NUMBER);
063        java2As3Type.put(Short.TYPE, As3Type.INT);
064        java2As3Type.put(Byte.class, As3Type.NUMBER);
065        java2As3Type.put(Byte.TYPE, As3Type.INT);
066
067        java2As3Type.put(Long.class, As3Type.NUMBER);
068        java2As3Type.put(Long.TYPE, As3Type.NUMBER);
069        java2As3Type.put(BigInteger.class, As3Type.NUMBER);
070        java2As3Type.put(BigDecimal.class, As3Type.NUMBER);
071        java2As3Type.put(MathContext.class, As3Type.NUMBER);
072        java2As3Type.put(RoundingMode.class, As3Type.NUMBER);
073
074        java2As3Type.put(Boolean.class, As3Type.BOOLEAN);
075        java2As3Type.put(Boolean.TYPE, As3Type.BOOLEAN);
076
077        java2As3Type.put(String.class, As3Type.STRING);
078        java2As3Type.put(Character.class, As3Type.STRING);
079        java2As3Type.put(Character.TYPE, As3Type.STRING);
080        java2As3Type.put(Locale.class, As3Type.STRING);
081        java2As3Type.put(URL.class, As3Type.STRING);
082        java2As3Type.put(URI.class, As3Type.STRING);
083
084        java2As3Type.put(Object.class, As3Type.OBJECT);
085        java2As3Type.put(Serializable.class, As3Type.OBJECT);
086
087        java2As3Type.put(Enum.class, As3Type.STRING);
088    }
089
090        @Override
091        public void configure(boolean externalizeLong, boolean externalizeBigInteger, boolean externalizeBigDecimal) {
092        }
093    
094    @Override
095        public ClientType getClientType(Type jType, Class<?> declaringClass, ParameterizedType[] declaringTypes, PropertyType propertyType) {
096        return null;
097    }
098
099        @Override
100        public ClientType getAs3Type(Class<?> jType) {
101        As3Type as3Type = getFromCache(jType);
102
103        if (as3Type == null) {
104            if (Date.class.isAssignableFrom(jType) || Calendar.class.isAssignableFrom(jType)) {
105                as3Type = As3Type.DATE;
106            }
107            else if (Number.class.isAssignableFrom(jType)) {
108                as3Type = As3Type.NUMBER;
109            }
110            else if (Document.class.isAssignableFrom(jType)) {
111                as3Type = As3Type.XML;
112            }
113            else if (jType.isEnum()) {
114                as3Type = As3Type.STRING;
115            }
116            else if ("javassist.bytecode.ByteArray".equals(jType.getName())) {
117                as3Type = As3Type.BYTE_ARRAY;
118            }
119            else if (jType.isArray()) {
120                Class<?> componentType = jType.getComponentType();
121                if (Byte.class.equals(componentType) || Byte.TYPE.equals(componentType))
122                    as3Type = As3Type.BYTE_ARRAY;
123                else if (Character.class.equals(componentType) || Character.TYPE.equals(componentType))
124                    as3Type = As3Type.STRING;
125                else
126                    as3Type = As3Type.ARRAY;
127            }
128            else if (Collection.class.isAssignableFrom(jType)) {
129                as3Type = As3Type.ARRAY_COLLECTION;
130            }
131            else if (Map.class.isAssignableFrom(jType)) {
132                as3Type = As3Type.OBJECT;
133            }
134            else {
135                as3Type = createAs3Type(jType);
136            }
137
138            putInCache(jType, as3Type);
139        }
140
141        return as3Type;
142        }
143
144    protected As3Type createAs3Type(Class<?> jType) {
145        String name = jType.getSimpleName();
146        if (jType.isMemberClass())
147                name = jType.getEnclosingClass().getSimpleName() + '$' + jType.getSimpleName();
148        return new As3Type(ClassUtil.getPackageName(jType), name);
149    }
150
151    protected As3Type getFromCache(Class<?> jType) {
152        if (jType == null)
153            throw new NullPointerException("jType must be non null");
154        return java2As3Type.get(jType);
155    }
156
157    protected void putInCache(Class<?> jType, As3Type as3Type) {
158        if (jType == null || as3Type == null)
159            throw new NullPointerException("jType and as3Type must be non null");
160        java2As3Type.put(jType, as3Type);
161    }
162}