001/** 002 * GRANITE DATA SERVICES 003 * Copyright (C) 2006-2014 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 043/** 044 * @author Franck WOLFF 045 */ 046public class DefaultAs3TypeFactory implements As3TypeFactory { 047 048 /////////////////////////////////////////////////////////////////////////// 049 // Fields. 050 051 private final Map<Class<?>, As3Type> java2As3Type = new HashMap<Class<?>, As3Type>(); 052 053 /////////////////////////////////////////////////////////////////////////// 054 // Constructors. 055 056 public DefaultAs3TypeFactory() { 057 java2As3Type.put(Double.class, As3Type.NUMBER); 058 java2As3Type.put(Double.TYPE, As3Type.NUMBER); 059 java2As3Type.put(Float.class, As3Type.NUMBER); 060 java2As3Type.put(Float.TYPE, As3Type.NUMBER); 061 java2As3Type.put(Long.class, As3Type.NUMBER); 062 java2As3Type.put(Long.TYPE, As3Type.NUMBER); 063 java2As3Type.put(Integer.class, As3Type.NUMBER); 064 java2As3Type.put(Integer.TYPE, As3Type.INT); 065 java2As3Type.put(Short.class, As3Type.NUMBER); 066 java2As3Type.put(Short.TYPE, As3Type.INT); 067 java2As3Type.put(Byte.class, As3Type.NUMBER); 068 java2As3Type.put(Byte.TYPE, As3Type.INT); 069 070 java2As3Type.put(MathContext.class, As3Type.MATH_CONTEXT); 071 java2As3Type.put(RoundingMode.class, As3Type.ROUNDING_MODE); 072 073 java2As3Type.put(Boolean.class, As3Type.BOOLEAN); 074 java2As3Type.put(Boolean.TYPE, As3Type.BOOLEAN); 075 076 java2As3Type.put(String.class, As3Type.STRING); 077 java2As3Type.put(Character.class, As3Type.STRING); 078 java2As3Type.put(Character.TYPE, As3Type.STRING); 079 java2As3Type.put(Locale.class, As3Type.STRING); 080 java2As3Type.put(URL.class, As3Type.STRING); 081 java2As3Type.put(URI.class, As3Type.STRING); 082 083 java2As3Type.put(Object.class, As3Type.OBJECT); 084 java2As3Type.put(Serializable.class, As3Type.OBJECT); 085 086 java2As3Type.put(Enum.class, As3Type.ENUM); 087 } 088 089 /////////////////////////////////////////////////////////////////////////// 090 // Fields. 091 092 @Override 093 public void configure(boolean externalizeLong, boolean externalizeBigInteger, boolean externalizeBigDecimal) { 094 if (externalizeLong) { 095 java2As3Type.put(Long.class, As3Type.LONG); 096 java2As3Type.put(Long.TYPE, As3Type.LONG); 097 } 098 if (externalizeBigInteger) 099 java2As3Type.put(BigInteger.class, As3Type.BIG_INTEGER); 100 if (externalizeBigDecimal) 101 java2As3Type.put(BigDecimal.class, As3Type.BIG_DECIMAL); 102 } 103 104 @Override 105 public ClientType getClientType(Type jType, Class<?> declaringClass, ParameterizedType[] declaringTypes, PropertyType propertyType) { 106 return null; 107 } 108 109 @Override 110 public ClientType getAs3Type(Class<?> jType) { 111 if (jType.isAnnotation()) 112 return null; 113 114 As3Type as3Type = getFromCache(jType); 115 116 if (as3Type == null) { 117 if (Date.class.isAssignableFrom(jType) || Calendar.class.isAssignableFrom(jType)) { 118 as3Type = As3Type.DATE; 119 } 120 else if (Number.class.isAssignableFrom(jType)) { 121 as3Type = As3Type.NUMBER; 122 } 123 else if (Document.class.isAssignableFrom(jType)) { 124 as3Type = As3Type.XML; 125 } 126 else if (jType.isArray()) { 127 Class<?> componentType = jType.getComponentType(); 128 if (Byte.class.equals(componentType) || Byte.TYPE.equals(componentType)) 129 as3Type = As3Type.BYTE_ARRAY; 130 else if (Character.class.equals(componentType) || Character.TYPE.equals(componentType)) 131 as3Type = As3Type.STRING; 132 else 133 as3Type = As3Type.ARRAY; 134 } 135 else if (Collection.class.isAssignableFrom(jType)) { 136 as3Type = As3Type.LIST_COLLECTION_VIEW; 137 } 138 else if (Iterable.class.isAssignableFrom(jType)) { 139 as3Type = As3Type.ILIST; 140 } 141 else if (Map.class.isAssignableFrom(jType)) { 142 as3Type = As3Type.IMAP; 143 } 144 else if (jType.getName().equals("com.google.appengine.api.datastore.Key")) { 145 as3Type = As3Type.STRING; 146 } 147 else if (jType.getName().equals("org.springframework.data.domain.Page")) { 148 as3Type = As3Type.PAGE; 149 } 150 else if (jType.getName().equals("org.springframework.data.domain.Pageable")) { 151 as3Type = As3Type.PAGE_INFO; 152 } 153 else if (jType.getName().equals("org.springframework.data.domain.Sort")) { 154 as3Type = As3Type.SORT_INFO; 155 } 156 else { 157 as3Type = createAs3Type(jType); 158 } 159 160 putInCache(jType, as3Type); 161 } 162 163 return as3Type; 164 } 165 166 protected As3Type createAs3Type(Class<?> jType) { 167 String name = jType.getSimpleName(); 168 if (jType.isMemberClass()) 169 name = jType.getEnclosingClass().getSimpleName() + '$' + jType.getSimpleName(); 170 return new As3Type(ClassUtil.getPackageName(jType), name); 171 } 172 173 protected As3Type getFromCache(Class<?> jType) { 174 if (jType == null) 175 throw new NullPointerException("jType must be non null"); 176 return java2As3Type.get(jType); 177 } 178 179 protected void putInCache(Class<?> jType, As3Type as3Type) { 180 if (jType == null || as3Type == null) 181 throw new NullPointerException("jType and as3Type must be non null"); 182 java2As3Type.put(jType, as3Type); 183 } 184}