1 17 package org.eclipse.emf.codegen.util; 18 19 import java.util.Collection ; 20 import java.util.HashMap ; 21 import java.util.HashSet ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 import java.util.SortedSet ; 25 import java.util.TreeSet ; 26 27 import org.eclipse.core.runtime.NullProgressMonitor; 28 import org.eclipse.jdt.core.dom.AST; 29 import org.eclipse.jdt.core.dom.ASTParser; 30 import org.eclipse.jdt.core.dom.CompilationUnit; 31 import org.eclipse.jdt.core.dom.ImportDeclaration; 32 33 public class ImportManager 34 { 35 protected SortedSet imports = new TreeSet (); 36 protected HashMap shortNameToImportMap = new HashMap (); 37 protected HashSet javaLangImports = null; 38 protected HashSet importedPackages; 39 40 public ImportManager(String compilationUnitPackage) 41 { 42 importedPackages = new HashSet (); 43 importedPackages.add(compilationUnitPackage); 44 } 45 46 public Collection getImports() 47 { 48 return imports; 49 } 50 51 public String getImportedName(String qualifiedName) 52 { 53 String indices = ""; 54 int firstBracket = qualifiedName.indexOf("["); 55 if (firstBracket != -1) 56 { 57 indices = qualifiedName.substring(firstBracket); 58 qualifiedName = qualifiedName.substring(0, firstBracket); 59 } 60 61 String baseName = qualifiedName.substring(qualifiedName.lastIndexOf(".") + 1); 62 63 String shortName = baseName; 64 int firstDollar = shortName.indexOf("$"); 65 if (firstDollar != -1) 66 { 67 shortName = shortName.substring(0, firstDollar); 68 } 69 70 String registeredName = (String )shortNameToImportMap.get(shortName); 71 if (registeredName == null) 72 { 73 registeredName = "java.lang." + shortName; 74 if (qualifiedName.equals(registeredName)) 75 { 76 if (javaLangImports != null && javaLangImports.contains(shortName)) 77 { 78 imports.add(qualifiedName); 79 } 80 return shortName + indices; 81 } 82 else 83 { 84 return qualifiedName + indices; 85 } 86 } 87 else 88 { 89 if (qualifiedName.startsWith(registeredName)) 90 { 91 if (qualifiedName.length () == registeredName.length()) 92 { 93 return baseName.replace('$', '.') + indices; 94 } 95 else 96 { 97 char character = qualifiedName.charAt(registeredName.length()); 98 if (character == '.' || character == '$') 99 { 100 return baseName.replace('$', '.') + indices; 101 } 102 } 103 } 104 return qualifiedName.replace('$', '.') + indices; 105 } 106 } 107 108 public void addImport(String packageName, String shortName) 109 { 110 int firstBracket = shortName.indexOf("["); 111 if (firstBracket != -1) shortName = shortName.substring(0, firstBracket); 112 basicAdd(packageName, shortName, packageName + "." + shortName); 113 } 114 115 public void addImport(String qualifiedName) 116 { 117 int firstBracket = qualifiedName.indexOf("["); 118 if (firstBracket != -1) qualifiedName = qualifiedName.substring(0, firstBracket); 119 120 int lastDot = qualifiedName.lastIndexOf("."); 121 String shortName = qualifiedName.substring(lastDot + 1); 122 int firstDollar = shortName.indexOf("$"); 123 if (firstDollar != -1) 124 { 125 shortName = shortName.substring(0, firstDollar); 126 } 127 128 String packageName = lastDot == -1 ? null : qualifiedName.substring(0, lastDot); 129 basicAdd(packageName, shortName, qualifiedName); 130 } 131 132 public void addMasterImport(String packageName, String shortName) 133 { 134 shortNameToImportMap.put(shortName, packageName + "." + shortName); 135 } 136 137 public void addJavaLangImports(List javaLangClassNames) 138 { 139 if (!javaLangClassNames.isEmpty()) 140 { 141 javaLangImports = new HashSet (); 142 javaLangImports.addAll(javaLangClassNames); 143 } 144 } 145 146 public boolean hasImport(String shortName) 147 { 148 return shortNameToImportMap.containsKey(shortName); 149 } 150 151 public void addCompilationUnitImports(String compilationUnitContents) 152 { 153 ASTParser parser = ASTParser.newParser(AST.JLS3); 154 parser.setSource(compilationUnitContents.toCharArray()); 155 CompilationUnit compilationUnit = (CompilationUnit)parser.createAST(new NullProgressMonitor()); 156 for (Iterator i = compilationUnit.imports().iterator(); i.hasNext();) 157 { 158 ImportDeclaration importDeclaration = (ImportDeclaration)i.next(); 159 String qualifiedName = importDeclaration.getName().getFullyQualifiedName(); 160 int lastDot = qualifiedName.lastIndexOf("."); 161 String shortName = qualifiedName.substring(lastDot + 1); 162 if (shortName.equals("*")) 163 { 164 String packageName = qualifiedName.substring(0, lastDot); 165 importedPackages.add(packageName); 166 } 167 else 168 { 169 shortNameToImportMap.put(shortName, qualifiedName); 170 } 171 } 172 } 173 174 public void addPseudoImport(String qualifiedName) 175 { 176 int lastDot = qualifiedName.lastIndexOf("."); 177 String shortName = qualifiedName.substring(lastDot + 1); 178 if (shortName.equals("*")) 179 { 180 String packageName = qualifiedName.substring(0, lastDot); 181 importedPackages.add(packageName); 182 } 183 else 184 { 185 shortNameToImportMap.put(shortName, qualifiedName); 186 } 187 } 188 189 private void basicAdd(String packageName, String shortName, String qualifiedName) 190 { 191 if (shortName.equals("*")) 192 { 193 importedPackages.add(packageName); 194 imports.add(qualifiedName); 195 } 196 else if (!shortNameToImportMap.containsKey(shortName) && (!CodeGenUtil.isJavaDefaultType(shortName))) 197 { 198 shortNameToImportMap.put(shortName, qualifiedName); 199 200 if (!importedPackages.contains(packageName)) 201 { 202 imports.add(qualifiedName); 203 } 204 } 205 } 206 207 public String computeSortedImports() 208 { 209 String NL = System.getProperties().getProperty("line.separator"); 210 StringBuffer imports = new StringBuffer (); 211 212 String previousPackageName = null; 213 for (Iterator iter = getImports().iterator(); iter.hasNext(); ) 214 { 215 String importName = (String )iter.next(); 216 int index = importName.lastIndexOf("."); 217 if (index != -1) 218 { 219 String packageName = importName.substring(0, index); 220 if (previousPackageName != null && !previousPackageName.equals(packageName)) 221 { 222 imports.append(NL); 223 } 224 previousPackageName = packageName; 225 } 226 imports.append(NL + "import " + importName + ";"); 227 } 228 229 return imports.toString(); 230 } 231 } | Popular Tags |