001 /*
002 GRANITE DATA SERVICES
003 Copyright (C) 2011 GRANITE DATA SERVICES S.A.S.
004
005 This file is part of Granite Data Services.
006
007 Granite Data Services is free software; you can redistribute it and/or modify
008 it under the terms of the GNU Library General Public License as published by
009 the Free Software Foundation; either version 2 of the License, or (at your
010 option) any later version.
011
012 Granite Data Services is distributed in the hope that it will be useful, but
013 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015 for more details.
016
017 You should have received a copy of the GNU Library General Public License
018 along with this library; if not, see <http://www.gnu.org/licenses/>.
019 */
020
021 package org.granite.generator.as3;
022
023 import java.io.Serializable;
024 import java.math.BigDecimal;
025 import java.math.BigInteger;
026 import java.math.MathContext;
027 import java.math.RoundingMode;
028 import java.net.URI;
029 import java.net.URL;
030 import java.util.Calendar;
031 import java.util.Collection;
032 import java.util.Date;
033 import java.util.HashMap;
034 import java.util.Locale;
035 import java.util.Map;
036
037 import org.granite.util.ClassUtil;
038 import org.w3c.dom.Document;
039
040 /**
041 * @author Franck WOLFF
042 */
043 public class DefaultAs3TypeFactory 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 DefaultAs3TypeFactory() {
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(MathContext.class, As3Type.MATH_CONTEXT);
068 java2As3Type.put(RoundingMode.class, As3Type.ROUNDING_MODE);
069
070 java2As3Type.put(Boolean.class, As3Type.BOOLEAN);
071 java2As3Type.put(Boolean.TYPE, As3Type.BOOLEAN);
072
073 java2As3Type.put(String.class, As3Type.STRING);
074 java2As3Type.put(Character.class, As3Type.STRING);
075 java2As3Type.put(Character.TYPE, As3Type.STRING);
076 java2As3Type.put(Locale.class, As3Type.STRING);
077 java2As3Type.put(URL.class, As3Type.STRING);
078 java2As3Type.put(URI.class, As3Type.STRING);
079
080 java2As3Type.put(Object.class, As3Type.OBJECT);
081 java2As3Type.put(Serializable.class, As3Type.OBJECT);
082
083 java2As3Type.put(Enum.class, As3Type.ENUM);
084 }
085
086 ///////////////////////////////////////////////////////////////////////////
087 // Fields.
088
089 public void configure(boolean externalizeLong, boolean externalizeBigInteger, boolean externalizeBigDecimal) {
090 if (externalizeLong) {
091 java2As3Type.put(Long.class, As3Type.LONG);
092 java2As3Type.put(Long.TYPE, As3Type.LONG);
093 }
094 if (externalizeBigInteger)
095 java2As3Type.put(BigInteger.class, As3Type.BIG_INTEGER);
096 if (externalizeBigDecimal)
097 java2As3Type.put(BigDecimal.class, As3Type.BIG_DECIMAL);
098 }
099
100 public As3Type 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.isArray()) {
114 Class<?> componentType = jType.getComponentType();
115 if (Byte.class.equals(componentType) || Byte.TYPE.equals(componentType))
116 as3Type = As3Type.BYTE_ARRAY;
117 else if (Character.class.equals(componentType) || Character.TYPE.equals(componentType))
118 as3Type = As3Type.STRING;
119 else
120 as3Type = As3Type.ARRAY;
121 }
122 else if (Collection.class.isAssignableFrom(jType)) {
123 as3Type = As3Type.ARRAY_COLLECTION;
124 }
125 else if (Map.class.isAssignableFrom(jType)) {
126 as3Type = As3Type.IMAP;
127 }
128 else if (jType.getName().equals("com.google.appengine.api.datastore.Key")) {
129 as3Type = As3Type.STRING;
130 }
131 else {
132 as3Type = createAs3Type(jType);
133 }
134
135 putInCache(jType, as3Type);
136 }
137
138 return as3Type;
139 }
140
141 protected As3Type createAs3Type(Class<?> jType) {
142 String name = jType.getSimpleName();
143 if (jType.isMemberClass())
144 name = jType.getEnclosingClass().getSimpleName() + '$' + jType.getSimpleName();
145 return new As3Type(ClassUtil.getPackageName(jType), name);
146 }
147
148 protected As3Type getFromCache(Class<?> jType) {
149 if (jType == null)
150 throw new NullPointerException("jType must be non null");
151 return java2As3Type.get(jType);
152 }
153
154 protected void putInCache(Class<?> jType, As3Type as3Type) {
155 if (jType == null || as3Type == null)
156 throw new NullPointerException("jType and as3Type must be non null");
157 java2As3Type.put(jType, as3Type);
158 }
159 }