1 56 57 package org.objectstyle.cayenne.gen; 58 59 import java.util.ArrayList ; 60 import java.util.Collections ; 61 import java.util.Comparator ; 62 import java.util.HashMap ; 63 import java.util.Iterator ; 64 import java.util.List ; 65 import java.util.Map ; 66 67 72 public class ImportUtils { 73 74 public static final String importOrdering[] = new String [] { 75 "java.", "javax.", "org.", "com." }; 76 77 80 protected Map importTypesMap = new HashMap (); 81 82 protected String packageName; 83 84 public ImportUtils() 85 { 86 super(); 87 } 88 89 public void addType(String typeName) 90 { 91 if (null == typeName) return; 93 94 StringUtils stringUtils = StringUtils.getInstance(); 95 String typeClassName = stringUtils.stripPackageName(typeName); 96 String typePackageName = stringUtils.stripClass(typeName); 97 98 if (typePackageName.length() == 0) return; if ("java.lang".equals(typePackageName)) return; 100 if (packageName.equals(typePackageName)) return; 101 102 if (importTypesMap.containsKey(typeClassName)) return; 104 105 importTypesMap.put(typeClassName, typeName); 106 } 107 108 public void setPackage(String packageName) 109 { 110 this.packageName = packageName; 111 } 112 113 116 public String formatJavaType(String typeName) { 117 if (typeName != null) { 118 StringUtils stringUtils = StringUtils.getInstance(); 119 String typeClassName = stringUtils.stripPackageName(typeName); 120 121 if (importTypesMap.containsKey(typeClassName)) 122 { 123 if (typeName.equals(importTypesMap.get(typeClassName))) return typeClassName; 124 } 125 126 String typePackageName = stringUtils.stripClass(typeName); 127 if ("java.lang".equals(typePackageName)) return typeClassName; 128 if (packageName.equals(typePackageName)) return typeClassName; 129 } 130 131 return typeName; 132 } 133 134 public String generate() 135 { 136 StringBuffer outputBuffer = new StringBuffer (); 137 138 if (null != packageName) 139 { 140 outputBuffer.append("package "); 141 outputBuffer.append(packageName); 142 outputBuffer.append(';'); 143 outputBuffer.append(System.getProperty("line.separator")); 144 outputBuffer.append(System.getProperty("line.separator")); 145 } 146 147 List typesList = new ArrayList (importTypesMap.values()); 148 Collections.sort(typesList, new Comparator () { 149 150 public boolean equals(Object obj) { 151 return this.equals(obj); 152 } 153 154 public int compare(Object o1, Object o2) { 155 156 String s1 = (String )o1; 157 String s2 = (String )o2; 158 159 for (int index = 0; index < importOrdering.length; index++) { 160 String ordering = importOrdering[index]; 161 162 if ( (s1.startsWith(ordering)) && (!s2.startsWith(ordering)) ) 163 return -1; 164 if ( (!s1.startsWith(ordering)) && (s2.startsWith(ordering)) ) 165 return 1; 166 } 167 168 return s1.compareTo(s2); 169 } 170 }); 171 172 String lastStringPrefix = null; 173 Iterator typesIterator = typesList.iterator(); 174 while (typesIterator.hasNext()) { 175 String typeName = (String )typesIterator.next(); 176 177 String thisStringPrefix = typeName; 180 int dotIndex = typeName.indexOf('.'); 181 if (-1 != dotIndex) 182 { 183 thisStringPrefix = typeName.substring(0, dotIndex); 184 } 185 if (null != lastStringPrefix) 187 { 188 if (false == thisStringPrefix.equals(lastStringPrefix)) 190 { 191 outputBuffer.append(System.getProperty("line.separator")); 193 } 194 } 195 lastStringPrefix = thisStringPrefix; 196 197 outputBuffer.append("import "); 198 outputBuffer.append(typeName); 199 outputBuffer.append(';'); 200 if (typesIterator.hasNext()) 201 { 202 outputBuffer.append(System.getProperty("line.separator")); 203 } 204 } 205 206 return outputBuffer.toString(); 207 } 208 } | Popular Tags |