KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > util > TypeInfoFactory


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.corext.util;
12
13 import java.util.Arrays JavaDoc;
14 import java.util.Comparator JavaDoc;
15
16 import org.eclipse.core.resources.ResourcesPlugin;
17
18 import org.eclipse.jdt.core.IJavaModel;
19 import org.eclipse.jdt.core.IJavaProject;
20 import org.eclipse.jdt.core.JavaCore;
21 import org.eclipse.jdt.core.JavaModelException;
22 import org.eclipse.jdt.core.search.IJavaSearchScope;
23
24
25 public class TypeInfoFactory {
26     
27     private String JavaDoc[] fProjects;
28     private TypeInfo fLast;
29     private char[] fBuffer;
30
31     private static final String JavaDoc CLASS= "class"; //$NON-NLS-1$
32
private static final String JavaDoc JAVA= "java"; //$NON-NLS-1$
33

34     public TypeInfoFactory() {
35         super();
36         fProjects= getProjectList();
37         fLast= null;
38         fBuffer= new char[512];
39     }
40
41     public TypeInfo create(char[] packageName, char[] typeName, char[][] enclosingName, int modifiers, String JavaDoc path) {
42         String JavaDoc pn= getPackageName(packageName);
43         String JavaDoc tn= new String JavaDoc(typeName);
44         TypeInfo result= null;
45         int index= path.indexOf(IJavaSearchScope.JAR_FILE_ENTRY_SEPARATOR);
46         if (index != -1) {
47             result= createJarFileEntryTypeInfo(pn, tn, enclosingName, modifiers, path, getJarFileEntryTypeInfo(fLast), index);
48         } else {
49             String JavaDoc project= getProject(path);
50             if (project != null) {
51                 result= createIFileTypeInfo(pn, tn, enclosingName, modifiers, path, getIFileTypeInfo(fLast), project);
52             }
53         }
54         if (result == null) {
55             result= new UnresolvableTypeInfo(pn, tn, enclosingName, modifiers, path);
56         } else {
57             fLast= result;
58         }
59         return result;
60     }
61     
62     private static JarFileEntryTypeInfo getJarFileEntryTypeInfo(TypeInfo info) {
63         if (info == null || info.getElementType() != TypeInfo.JAR_FILE_ENTRY_TYPE_INFO)
64             return null;
65         return (JarFileEntryTypeInfo)info;
66     }
67     
68     private static IFileTypeInfo getIFileTypeInfo(TypeInfo info) {
69         if (info == null || info.getElementType() != TypeInfo.IFILE_TYPE_INFO)
70             return null;
71         return (IFileTypeInfo)info;
72     }
73     
74     private TypeInfo createJarFileEntryTypeInfo(String JavaDoc packageName, String JavaDoc typeName, char[][] enclosingName, int modifiers, String JavaDoc path, JarFileEntryTypeInfo last, int index) {
75         String JavaDoc jar= path.substring(0, index);
76         String JavaDoc rest= path.substring(index + 1);
77         index= rest.lastIndexOf(TypeInfo.SEPARATOR);
78         if (index != -1) {
79             rest= rest.substring(index + 1);
80         }
81         String JavaDoc file= null;
82         String JavaDoc extension= null;
83         index= rest.lastIndexOf(TypeInfo.EXTENSION_SEPARATOR);
84         if (index != -1) {
85             file= rest.substring(0, index);
86             extension= rest.substring(index + 1);
87         } else {
88             return null;
89         }
90         if (last != null) {
91             if (last.getJar().equals(jar)) {
92                 jar= last.getJar();
93             } else {
94                 // System.out.println("Creating new jar: " + jar);
95
jar= createString(jar);
96             }
97         } else {
98             jar= createString(jar);
99         }
100         if (typeName.equals(file)) {
101             file= typeName;
102         } else {
103             file= createString(file);
104         }
105         if (CLASS.equals(extension))
106             extension= CLASS;
107         else if (JAVA.equals(extension))
108             extension= JAVA;
109         else
110             extension= createString(extension);
111         return new JarFileEntryTypeInfo(packageName, typeName, enclosingName, modifiers, jar, file, extension);
112     }
113     
114     private TypeInfo createIFileTypeInfo(String JavaDoc packageName, String JavaDoc typeName, char[][] enclosingName, int modifiers, String JavaDoc path, IFileTypeInfo last, String JavaDoc project) {
115         String JavaDoc rest= path.substring(project.length() + 1); // the first slashes.
116
int index= rest.lastIndexOf(TypeInfo.SEPARATOR);
117         if (index == -1)
118             return null;
119         String JavaDoc middle= rest.substring(0, index);
120         rest= rest.substring(index + 1);
121         index= rest.lastIndexOf(TypeInfo.EXTENSION_SEPARATOR);
122         String JavaDoc file= null;
123         String JavaDoc extension= null;
124         if (index != -1) {
125             file= rest.substring(0, index);
126             extension= rest.substring(index + 1);
127         } else {
128             return null;
129         }
130         String JavaDoc SRC= null;
131         int ml= middle.length();
132         int pl= packageName.length();
133         // if we have a source or package then we have to substract the leading '/'
134
if (ml > 0 && ml - 1 > pl) {
135              // If we have a package then we have to substract the '/' between src and package
136
SRC= middle.substring(1, ml - pl - (pl > 0 ? 1 : 0));
137         }
138         if (last != null) {
139             if (src != null && src.equals(last.getFolder()))
140                 src= last.getFolder();
141         }
142         if (typeName.equals(file)) {
143             file= typeName;
144         } else {
145             file= createString(file);
146         }
147         if (CLASS.equals(extension))
148             extension= CLASS;
149         else if (JAVA.equals(extension))
150             extension= JAVA;
151         else
152             extension= createString(extension);
153         
154         return new IFileTypeInfo(packageName, typeName, enclosingName, modifiers, project, src, file, extension);
155     }
156     
157     private String JavaDoc getPackageName(char[] packageName) {
158         if (fLast == null)
159             return new String JavaDoc(packageName);
160         String JavaDoc lastPackageName= fLast.getPackageName();
161         if (Strings.equals(lastPackageName, packageName))
162             return lastPackageName;
163         return new String JavaDoc(packageName);
164     }
165     
166     private String JavaDoc getProject(String JavaDoc path) {
167         for (int i= 0; i < fProjects.length; i++) {
168             String JavaDoc project= fProjects[i];
169             if (path.startsWith(project, 1))
170                 return project;
171         }
172         return null;
173     }
174     
175     private String JavaDoc createString(String JavaDoc s) {
176         if (s == null)
177             return null;
178         int length= s.length();
179         if (length > fBuffer.length)
180             fBuffer= new char[length];
181         s.getChars(0, length, fBuffer, 0);
182         return new String JavaDoc(fBuffer, 0, length);
183     }
184     
185     private static String JavaDoc[] getProjectList() {
186         IJavaModel model= JavaCore.create(ResourcesPlugin.getWorkspace().getRoot());
187         String JavaDoc[] result;
188         try {
189             IJavaProject[] projects= model.getJavaProjects();
190             result= new String JavaDoc[projects.length];
191             for (int i= 0; i < projects.length; i++) {
192                 result[i]= projects[i].getElementName();
193             }
194         } catch (JavaModelException e) {
195             result= new String JavaDoc[0];
196         }
197         // We have to sort the list of project names to make sure that we cut of the longest
198
// project from the path, if two projects with the same prefix exist. For example
199
// org.eclipse.jdt.ui and org.eclipse.jdt.ui.tests.
200
Arrays.sort(result, new Comparator JavaDoc() {
201             public int compare(Object JavaDoc o1, Object JavaDoc o2) {
202                 int l1= ((String JavaDoc)o1).length();
203                 int l2= ((String JavaDoc)o2).length();
204                 if (l1 < l2)
205                     return 1;
206                 if (l2 < l1)
207                     return -1;
208                 return 0;
209             }
210             public boolean equals(Object JavaDoc obj) {
211                 return super.equals(obj);
212             }
213         });
214         return result;
215     }
216 }
217
Popular Tags