1 | /******************************************************************************* |
2 | * Copyright (c) 2007, 2009 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.pde.api.tools.internal.model; |
12 | |
13 | import java.io.File; |
14 | import java.io.FileFilter; |
15 | import java.io.FileInputStream; |
16 | import java.io.FileNotFoundException; |
17 | import java.io.IOException; |
18 | import java.io.InputStream; |
19 | import java.util.ArrayList; |
20 | import java.util.Arrays; |
21 | import java.util.Collections; |
22 | import java.util.HashMap; |
23 | import java.util.Iterator; |
24 | import java.util.List; |
25 | import java.util.Map; |
26 | |
27 | import org.eclipse.core.runtime.CoreException; |
28 | import org.eclipse.pde.api.tools.internal.provisional.ApiPlugin; |
29 | import org.eclipse.pde.api.tools.internal.provisional.model.ApiTypeContainerVisitor; |
30 | import org.eclipse.pde.api.tools.internal.provisional.model.IApiElement; |
31 | import org.eclipse.pde.api.tools.internal.provisional.model.IApiTypeContainer; |
32 | import org.eclipse.pde.api.tools.internal.provisional.model.IApiTypeRoot; |
33 | import org.eclipse.pde.api.tools.internal.util.Util; |
34 | |
35 | /** |
36 | * An {@link IApiTypeContainer} rooted at a directory in the file system. |
37 | * |
38 | * @since 1.0.0 |
39 | */ |
40 | public class DirectoryApiTypeContainer extends ApiElement implements IApiTypeContainer { |
41 | |
42 | /** |
43 | * Implementation of an {@link IApiTypeRoot} in the local file system. |
44 | */ |
45 | static class LocalApiTypeRoot extends AbstractApiTypeRoot implements Comparable { |
46 | |
47 | String fLocation = null; |
48 | |
49 | /** |
50 | * Constructs a class file on the given file |
51 | * @param directory the parent {@link IApiElement} directory |
52 | * @param location |
53 | * @param qualified type name |
54 | * @param component owning API component |
55 | */ |
56 | public LocalApiTypeRoot(DirectoryApiTypeContainer directory, String location, String typeName) { |
57 | super(directory, typeName); |
58 | fLocation = location; |
59 | } |
60 | |
61 | /* (non-Javadoc) |
62 | * @see org.eclipse.pde.api.tools.model.component.IClassFile#getTypeName() |
63 | */ |
64 | public String getTypeName() { |
65 | return getName(); |
66 | } |
67 | |
68 | /* (non-Javadoc) |
69 | * @see java.lang.Comparable#compareTo(java.lang.Object) |
70 | */ |
71 | public int compareTo(Object o) { |
72 | return getName().compareTo(((LocalApiTypeRoot)o).getName()); |
73 | } |
74 | |
75 | /* (non-Javadoc) |
76 | * @see java.lang.Object#equals(java.lang.Object) |
77 | */ |
78 | public boolean equals(Object obj) { |
79 | if (obj instanceof LocalApiTypeRoot) { |
80 | return ((LocalApiTypeRoot) obj).getName().equals(this.getName()); |
81 | } |
82 | return false; |
83 | } |
84 | |
85 | /* (non-Javadoc) |
86 | * @see java.lang.Object#hashCode() |
87 | */ |
88 | public int hashCode() { |
89 | return this.getName().hashCode(); |
90 | } |
91 | |
92 | /* (non-Javadoc) |
93 | * @see org.eclipse.pde.api.tools.internal.model.AbstractApiTypeRoot#getContents() |
94 | */ |
95 | public byte[] getContents() throws CoreException { |
96 | InputStream stream = null; |
97 | try { |
98 | stream = new FileInputStream(new File(fLocation)); |
99 | } catch (FileNotFoundException e) { |
100 | abort("File not found", e); //$NON-NLS-1$ |
101 | return null; |
102 | } |
103 | try { |
104 | return Util.getInputStreamAsByteArray(stream, -1); |
105 | } |
106 | catch(IOException ioe) { |
107 | abort("Unable to read class file: " + getTypeName(), ioe); //$NON-NLS-1$ |
108 | return null; |
109 | } |
110 | finally { |
111 | try { |
112 | stream.close(); |
113 | } catch (IOException e) { |
114 | ApiPlugin.log(e); |
115 | } |
116 | } |
117 | } |
118 | } |
119 | |
120 | /** |
121 | * Map of package names to associated directory (file) |
122 | */ |
123 | private Map fPackages; |
124 | |
125 | /** |
126 | * Cache of package names |
127 | */ |
128 | private String[] fPackageNames; |
129 | |
130 | /** |
131 | * Constructs an {@link IApiTypeContainer} rooted at the specified path. |
132 | * |
133 | * @param parent the parent {@link IApiElement} or <code>null</code> if none |
134 | * @param location absolute path in the local file system |
135 | */ |
136 | public DirectoryApiTypeContainer(IApiElement parent, String location) { |
137 | super(parent, IApiElement.API_TYPE_CONTAINER, location); |
138 | } |
139 | |
140 | /** |
141 | * @see org.eclipse.pde.api.tools.internal.provisional.IApiTypeContainer#accept(org.eclipse.pde.api.tools.internal.provisional.ApiTypeContainerVisitor) |
142 | */ |
143 | public void accept(ApiTypeContainerVisitor visitor) throws CoreException { |
144 | if(visitor.visit(this)) { |
145 | init(); |
146 | String[] packageNames = getPackageNames(); |
147 | for (int i = 0; i < packageNames.length; i++) { |
148 | String pkg = packageNames[i]; |
149 | if (visitor.visitPackage(pkg)) { |
150 | String location = (String) fPackages.get(pkg); |
151 | if(location == null) { |
152 | continue; |
153 | } |
154 | File dir = new File(location); |
155 | if(!dir.exists()) { |
156 | continue; |
157 | } |
158 | File[] files = dir.listFiles(new FileFilter() { |
159 | public boolean accept(File file) { |
160 | return file.isFile() && file.getName().endsWith(Util.DOT_CLASS_SUFFIX); |
161 | } |
162 | }); |
163 | if (files != null) { |
164 | List classFiles = new ArrayList(); |
165 | for (int j = 0; j < files.length; j++) { |
166 | String name = files[j].getName(); |
167 | String typeName = name.substring(0, name.length() - 6); |
168 | if (pkg.length() > 0) { |
169 | typeName = pkg + "." + typeName; //$NON-NLS-1$ |
170 | } |
171 | classFiles.add(new LocalApiTypeRoot(this, files[j].getAbsolutePath(), typeName)); |
172 | } |
173 | Collections.sort(classFiles); |
174 | Iterator cfIterator = classFiles.iterator(); |
175 | while (cfIterator.hasNext()) { |
176 | IApiTypeRoot classFile = (IApiTypeRoot) cfIterator.next(); |
177 | visitor.visit(pkg, classFile); |
178 | visitor.end(pkg, classFile); |
179 | } |
180 | } |
181 | } |
182 | visitor.endVisitPackage(pkg); |
183 | } |
184 | } |
185 | visitor.end(this); |
186 | } |
187 | |
188 | /** |
189 | * @see java.lang.Object#toString() |
190 | */ |
191 | public String toString() { |
192 | StringBuffer buff = new StringBuffer(); |
193 | buff.append("Directory Class File Container: "+getName()); //$NON-NLS-1$ |
194 | return buff.toString(); |
195 | } |
196 | |
197 | /* (non-Javadoc) |
198 | * @see org.eclipse.pde.api.tools.model.component.IClassFileContainer#close() |
199 | */ |
200 | public synchronized void close() throws CoreException { |
201 | fPackages = null; |
202 | fPackageNames = null; |
203 | } |
204 | |
205 | /* (non-Javadoc) |
206 | * @see org.eclipse.pde.api.tools.model.component.IClassFileContainer#findClassFile(java.lang.String) |
207 | */ |
208 | public IApiTypeRoot findTypeRoot(String qualifiedName) throws CoreException { |
209 | init(); |
210 | int index = qualifiedName.lastIndexOf('.'); |
211 | String cfName = qualifiedName; |
212 | String pkg = Util.DEFAULT_PACKAGE_NAME; |
213 | if (index > 0) { |
214 | pkg = qualifiedName.substring(0, index); |
215 | cfName = qualifiedName.substring(index + 1); |
216 | } |
217 | String location = (String) fPackages.get(pkg); |
218 | if(location != null) { |
219 | File file = new File(location, cfName + Util.DOT_CLASS_SUFFIX); |
220 | if (file.exists()) { |
221 | return new LocalApiTypeRoot(this, file.getAbsolutePath(), qualifiedName); |
222 | } |
223 | } |
224 | return null; |
225 | } |
226 | |
227 | |
228 | /* (non-Javadoc) |
229 | * @see org.eclipse.pde.api.tools.model.component.IClassFileContainer#getPackageNames() |
230 | */ |
231 | public String[] getPackageNames() throws CoreException { |
232 | init(); |
233 | if (fPackageNames == null) { |
234 | List names = new ArrayList(fPackages.keySet()); |
235 | String[] result = new String[names.size()]; |
236 | names.toArray(result); |
237 | Arrays.sort(result); |
238 | fPackageNames = result; |
239 | } |
240 | return fPackageNames; |
241 | } |
242 | |
243 | /** |
244 | * Builds cache of package names to directories |
245 | */ |
246 | private synchronized void init() { |
247 | if (fPackages == null) { |
248 | fPackages = new HashMap(); |
249 | processDirectory(Util.DEFAULT_PACKAGE_NAME, new File(getName())); |
250 | } |
251 | } |
252 | |
253 | /** |
254 | * Traverses a directory to determine if it has class files and |
255 | * then visits sub-directories. |
256 | * |
257 | * @param packageName package name of directory being visited |
258 | * @param dir directory being visited |
259 | */ |
260 | private void processDirectory(String packageName, File dir) { |
261 | File[] files = dir.listFiles(); |
262 | if (files != null) { |
263 | boolean hasClassFiles = false; |
264 | List dirs = new ArrayList(); |
265 | for (int i = 0; i < files.length; i++) { |
266 | File file = files[i]; |
267 | if (file.isDirectory()) { |
268 | dirs.add(file.getAbsoluteFile()); |
269 | } else if (!hasClassFiles) { |
270 | if (file.getName().endsWith(Util.DOT_CLASS_SUFFIX)) { |
271 | fPackages.put(packageName, dir.getAbsolutePath()); |
272 | hasClassFiles = true; |
273 | } |
274 | } |
275 | } |
276 | Iterator iterator = dirs.iterator(); |
277 | while (iterator.hasNext()) { |
278 | File child = (File)iterator.next(); |
279 | String nextName = null; |
280 | if (packageName.length() == 0) { |
281 | nextName = child.getName(); |
282 | } else { |
283 | nextName = packageName + "." + child.getName(); //$NON-NLS-1$ |
284 | } |
285 | processDirectory(nextName, child); |
286 | } |
287 | } |
288 | } |
289 | |
290 | /** |
291 | * @see org.eclipse.pde.api.tools.internal.provisional.IApiTypeContainer#findClassFile(java.lang.String, java.lang.String) |
292 | */ |
293 | public IApiTypeRoot findTypeRoot(String qualifiedName, String id) throws CoreException { |
294 | return findTypeRoot(qualifiedName); |
295 | } |
296 | |
297 | /* (non-Javadoc) |
298 | * @see org.eclipse.pde.api.tools.internal.provisional.model.IApiTypeContainer#getContainerType() |
299 | */ |
300 | public int getContainerType() { |
301 | return DIRECTORY; |
302 | } |
303 | } |