KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > codegen > util > ImportManager


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2005 IBM Corporation and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * IBM - Initial API and implementation
12  *
13  * </copyright>
14  *
15  * ImportManager.java,v 1.1 2005/05/16 18:37:55 marcelop Exp
16  */

17 package org.eclipse.emf.codegen.util;
18
19 import java.util.Collection JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.HashSet JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.SortedSet JavaDoc;
25 import java.util.TreeSet JavaDoc;
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 JavaDoc imports = new TreeSet JavaDoc();
36   protected HashMap JavaDoc shortNameToImportMap = new HashMap JavaDoc();
37   protected HashSet JavaDoc javaLangImports = null;
38   protected HashSet JavaDoc importedPackages;
39
40   public ImportManager(String JavaDoc compilationUnitPackage)
41   {
42     importedPackages = new HashSet JavaDoc();
43     importedPackages.add(compilationUnitPackage);
44   }
45   
46   public Collection JavaDoc getImports()
47   {
48     return imports;
49   }
50
51   public String JavaDoc getImportedName(String JavaDoc qualifiedName)
52   {
53     String JavaDoc 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 JavaDoc baseName = qualifiedName.substring(qualifiedName.lastIndexOf(".") + 1);
62
63     String JavaDoc shortName = baseName;
64     int firstDollar = shortName.indexOf("$");
65     if (firstDollar != -1)
66     {
67       shortName = shortName.substring(0, firstDollar);
68     }
69
70     String JavaDoc registeredName = (String JavaDoc)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 JavaDoc packageName, String JavaDoc 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 JavaDoc qualifiedName)
116   {
117     int firstBracket = qualifiedName.indexOf("[");
118     if (firstBracket != -1) qualifiedName = qualifiedName.substring(0, firstBracket);
119
120     int lastDot = qualifiedName.lastIndexOf(".");
121     String JavaDoc 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 JavaDoc packageName = lastDot == -1 ? null : qualifiedName.substring(0, lastDot);
129     basicAdd(packageName, shortName, qualifiedName);
130   }
131
132   public void addMasterImport(String JavaDoc packageName, String JavaDoc shortName)
133   {
134     shortNameToImportMap.put(shortName, packageName + "." + shortName);
135   }
136
137   public void addJavaLangImports(List JavaDoc javaLangClassNames)
138   {
139     if (!javaLangClassNames.isEmpty())
140     {
141       javaLangImports = new HashSet JavaDoc();
142       javaLangImports.addAll(javaLangClassNames);
143     }
144   }
145
146   public boolean hasImport(String JavaDoc shortName)
147   {
148     return shortNameToImportMap.containsKey(shortName);
149   }
150
151   public void addCompilationUnitImports(String JavaDoc 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 JavaDoc i = compilationUnit.imports().iterator(); i.hasNext();)
157     {
158       ImportDeclaration importDeclaration = (ImportDeclaration)i.next();
159       String JavaDoc qualifiedName = importDeclaration.getName().getFullyQualifiedName();
160       int lastDot = qualifiedName.lastIndexOf(".");
161       String JavaDoc shortName = qualifiedName.substring(lastDot + 1);
162       if (shortName.equals("*"))
163       {
164         String JavaDoc 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 JavaDoc qualifiedName)
175   {
176     int lastDot = qualifiedName.lastIndexOf(".");
177     String JavaDoc shortName = qualifiedName.substring(lastDot + 1);
178     if (shortName.equals("*"))
179     {
180       String JavaDoc 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 JavaDoc packageName, String JavaDoc shortName, String JavaDoc 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 JavaDoc computeSortedImports()
208   {
209     String JavaDoc NL = System.getProperties().getProperty("line.separator");
210     StringBuffer JavaDoc imports = new StringBuffer JavaDoc();
211
212     String JavaDoc previousPackageName = null;
213     for (Iterator JavaDoc iter = getImports().iterator(); iter.hasNext(); )
214     {
215       String JavaDoc importName = (String JavaDoc)iter.next();
216       int index = importName.lastIndexOf(".");
217       if (index != -1)
218       {
219         String JavaDoc 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