1 19 20 21 package org.apache.cayenne.gen; 22 23 import java.util.ArrayList ; 24 import java.util.Collections ; 25 import java.util.Comparator ; 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.Map ; 30 31 import org.apache.cayenne.util.Util; 32 33 38 public class ImportUtils { 39 40 public static final String importOrdering[] = new String [] { 41 "java.", "javax.", "org.", "com." }; 42 43 static final String primitives[] = new String [] { 44 "long", "double", "byte", "boolean", "float", "short", "int" 45 }; 46 47 static final String primitiveClasses[] = new String [] { 48 Long .class.getName(), Double .class.getName(), Byte .class.getName(), 49 Boolean .class.getName(), Float .class.getName(), Short .class.getName(), 50 Integer .class.getName() 51 }; 52 53 static Map classesForPrimitives = Util.toMap(primitives, primitiveClasses); 54 static Map primitivesForClasses = Util.toMap(primitiveClasses, primitives); 55 56 protected Map importTypesMap = new HashMap (); 57 protected Map reservedImportTypesMap = new HashMap (); 59 protected String packageName; 60 61 public ImportUtils() 62 { 63 super(); 64 } 65 66 protected boolean canRegisterType(String typeName) 67 { 68 if (null == typeName) return false; 70 71 StringUtils stringUtils = StringUtils.getInstance(); 72 String typeClassName = stringUtils.stripPackageName(typeName); 73 String typePackageName = stringUtils.stripClass(typeName); 74 75 if (typePackageName.length() == 0) return false; if ("java.lang".equals(typePackageName)) return false; 77 78 if (reservedImportTypesMap.containsKey(typeClassName)) return false; 80 if (importTypesMap.containsKey(typeClassName)) return false; 81 82 return true; 83 } 84 85 91 public void addReservedType(String typeName) 92 { 93 if (! canRegisterType(typeName)) return; 94 95 StringUtils stringUtils = StringUtils.getInstance(); 96 String typeClassName = stringUtils.stripPackageName(typeName); 97 98 reservedImportTypesMap.put(typeClassName, typeName); 99 } 100 101 106 public void addType(String typeName) 107 { 108 if (! canRegisterType(typeName)) return; 109 110 StringUtils stringUtils = StringUtils.getInstance(); 111 String typePackageName = stringUtils.stripClass(typeName); 112 String typeClassName = stringUtils.stripPackageName(typeName); 113 114 if (typePackageName.equals(packageName)) return; 115 116 importTypesMap.put(typeClassName, typeName); 117 } 118 119 123 public void setPackage(String packageName) 124 { 125 this.packageName = packageName; 126 } 127 128 134 public String formatJavaType(String typeName, boolean usePrimitives) { 135 if (usePrimitives) { 136 String primitive = (String ) primitivesForClasses.get(typeName); 137 return (primitive != null) ? primitive : formatJavaType(typeName); 138 } 139 else { 140 String primitiveClass = (String ) classesForPrimitives.get(typeName); 141 return (primitiveClass != null) 142 ? formatJavaType(primitiveClass) 143 : formatJavaType(typeName); 144 } 145 } 146 147 150 public String formatJavaType(String typeName) { 151 if (typeName != null) { 152 StringUtils stringUtils = StringUtils.getInstance(); 153 String typeClassName = stringUtils.stripPackageName(typeName); 154 155 if (! reservedImportTypesMap.containsKey(typeClassName)) 156 { 157 if (importTypesMap.containsKey(typeClassName)) 158 { 159 if (typeName.equals(importTypesMap.get(typeClassName))) return typeClassName; 160 } 161 } 162 163 String typePackageName = stringUtils.stripClass(typeName); 164 if ("java.lang".equals(typePackageName)) return typeClassName; 165 if ((null != packageName) && (packageName.equals(typePackageName))) 166 return typeClassName; 167 } 168 169 return typeName; 170 } 171 172 175 public String generate() 176 { 177 StringBuffer outputBuffer = new StringBuffer (); 178 179 if (null != packageName) 180 { 181 outputBuffer.append("package "); 182 outputBuffer.append(packageName); 183 outputBuffer.append(';'); 184 outputBuffer.append(System.getProperty("line.separator")); 185 outputBuffer.append(System.getProperty("line.separator")); 186 } 187 188 List typesList = new ArrayList (importTypesMap.values()); 189 Collections.sort(typesList, new Comparator () { 190 191 public int compare(Object o1, Object o2) { 192 193 String s1 = (String )o1; 194 String s2 = (String )o2; 195 196 for (int index = 0; index < importOrdering.length; index++) { 197 String ordering = importOrdering[index]; 198 199 if ( (s1.startsWith(ordering)) && (!s2.startsWith(ordering)) ) 200 return -1; 201 if ( (!s1.startsWith(ordering)) && (s2.startsWith(ordering)) ) 202 return 1; 203 } 204 205 return s1.compareTo(s2); 206 } 207 }); 208 209 String lastStringPrefix = null; 210 Iterator typesIterator = typesList.iterator(); 211 while (typesIterator.hasNext()) { 212 String typeName = (String )typesIterator.next(); 213 214 String thisStringPrefix = typeName; 217 int dotIndex = typeName.indexOf('.'); 218 if (-1 != dotIndex) 219 { 220 thisStringPrefix = typeName.substring(0, dotIndex); 221 } 222 if (null != lastStringPrefix) 224 { 225 if (false == thisStringPrefix.equals(lastStringPrefix)) 227 { 228 outputBuffer.append(System.getProperty("line.separator")); 230 } 231 } 232 lastStringPrefix = thisStringPrefix; 233 234 outputBuffer.append("import "); 235 outputBuffer.append(typeName); 236 outputBuffer.append(';'); 237 if (typesIterator.hasNext()) 238 { 239 outputBuffer.append(System.getProperty("line.separator")); 240 } 241 } 242 243 return outputBuffer.toString(); 244 } 245 } 246 | Popular Tags |