1 | /******************************************************************************* |
2 | * Copyright (c) 2007, 2008 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.util.ArrayList; |
14 | import java.util.Arrays; |
15 | import java.util.Iterator; |
16 | import java.util.List; |
17 | |
18 | import org.eclipse.core.resources.IContainer; |
19 | import org.eclipse.core.resources.IFile; |
20 | import org.eclipse.core.resources.IFolder; |
21 | import org.eclipse.core.resources.IResource; |
22 | import org.eclipse.core.runtime.CoreException; |
23 | import org.eclipse.core.runtime.IPath; |
24 | import org.eclipse.core.runtime.Path; |
25 | import org.eclipse.pde.api.tools.internal.provisional.model.ApiTypeContainerVisitor; |
26 | import org.eclipse.pde.api.tools.internal.provisional.model.IApiElement; |
27 | import org.eclipse.pde.api.tools.internal.provisional.model.IApiTypeContainer; |
28 | import org.eclipse.pde.api.tools.internal.provisional.model.IApiTypeRoot; |
29 | import org.eclipse.pde.api.tools.internal.util.Util; |
30 | |
31 | /** |
32 | * An {@link IApiTypeRoot} rooted at a container in the workspace. |
33 | * |
34 | * @since 1.0.0 |
35 | */ |
36 | public class FolderApiTypeContainer extends ApiElement implements IApiTypeContainer { |
37 | |
38 | /** |
39 | * Root directory of the {@link IApiTypeContainer} |
40 | */ |
41 | private IContainer fRoot; |
42 | |
43 | /** |
44 | * Constructs an {@link IApiTypeContainer} rooted at the location. |
45 | * |
46 | * @param parent the {@link IApiElement} parent for this container |
47 | * @param container folder in the workspace |
48 | */ |
49 | public FolderApiTypeContainer(IApiElement parent, IContainer container) { |
50 | super(parent, IApiElement.API_TYPE_CONTAINER, container.getName()); |
51 | this.fRoot = container; |
52 | } |
53 | |
54 | /** |
55 | * @see org.eclipse.pde.api.tools.internal.AbstractApiTypeContainer#accept(org.eclipse.pde.api.tools.internal.provisional.ApiTypeContainerVisitor) |
56 | */ |
57 | public void accept(ApiTypeContainerVisitor visitor) throws CoreException { |
58 | if(visitor.visit(this)) { |
59 | doVisit(fRoot, Util.DEFAULT_PACKAGE_NAME, visitor); |
60 | } |
61 | visitor.end(this); |
62 | } |
63 | |
64 | /** |
65 | * @see org.eclipse.pde.api.tools.internal.provisional.IApiTypeContainer#close() |
66 | */ |
67 | public void close() throws CoreException {} |
68 | |
69 | /** |
70 | * @see java.lang.Object#toString() |
71 | */ |
72 | public String toString() { |
73 | StringBuffer buff = new StringBuffer(); |
74 | buff.append("Folder Class File Container: "+getName()); //$NON-NLS-1$ |
75 | return buff.toString(); |
76 | } |
77 | |
78 | /** |
79 | * Visits the given {@link IContainer} |
80 | * @param container |
81 | * @param pkgName |
82 | * @param visitor |
83 | * @throws CoreException |
84 | */ |
85 | private void doVisit(IContainer container, String pkgName, ApiTypeContainerVisitor visitor) throws CoreException { |
86 | IResource[] members = container.members(); |
87 | List dirs = new ArrayList(); |
88 | boolean visitPkg = visitor.visitPackage(pkgName); |
89 | for (int i = 0; i < members.length; i++) { |
90 | IResource file = members[i]; |
91 | switch (file.getType()) { |
92 | case IResource.FOLDER: |
93 | dirs.add(file); |
94 | break; |
95 | case IResource.FILE: |
96 | if (visitPkg && file.getName().endsWith(Util.DOT_CLASS_SUFFIX)) { |
97 | String name = file.getName(); |
98 | String typeName = name.substring(0, name.length() - 6); |
99 | if (pkgName.length() > 0) { |
100 | StringBuffer buf = new StringBuffer(pkgName); |
101 | buf.append('.'); |
102 | buf.append(typeName); |
103 | typeName = buf.toString(); |
104 | } |
105 | ResourceApiTypeRoot cf = new ResourceApiTypeRoot(this, (IFile) file, typeName); |
106 | visitor.visit(pkgName, cf); |
107 | visitor.end(pkgName, cf); |
108 | } |
109 | break; |
110 | } |
111 | } |
112 | visitor.endVisitPackage(pkgName); |
113 | Iterator iterator = dirs.iterator(); |
114 | while (iterator.hasNext()) { |
115 | IContainer child = (IContainer)iterator.next(); |
116 | String nextName = null; |
117 | if (pkgName.length() == 0) { |
118 | nextName = child.getName(); |
119 | } else { |
120 | StringBuffer buffer = new StringBuffer(pkgName); |
121 | buffer.append('.'); |
122 | buffer.append(child.getName()); |
123 | nextName = buffer.toString(); |
124 | } |
125 | doVisit(child, nextName, visitor); |
126 | } |
127 | } |
128 | |
129 | /** |
130 | * @see org.eclipse.pde.api.tools.internal.provisional.IApiTypeContainer#findTypeRoot(java.lang.String) |
131 | */ |
132 | public IApiTypeRoot findTypeRoot(String qualifiedName) throws CoreException { |
133 | int index = qualifiedName.lastIndexOf('.'); |
134 | String cfName = qualifiedName; |
135 | String pkg = Util.DEFAULT_PACKAGE_NAME; |
136 | if (index > 0) { |
137 | pkg = qualifiedName.substring(0, index); |
138 | cfName = qualifiedName.substring(index + 1); |
139 | } |
140 | IFolder folder = fRoot.getFolder(new Path(pkg.replace('.', IPath.SEPARATOR))); |
141 | if (folder.exists()) { |
142 | IFile file = folder.getFile(cfName + Util.DOT_CLASS_SUFFIX); |
143 | if (file.exists()) { |
144 | return new ResourceApiTypeRoot(this, file, qualifiedName); |
145 | } |
146 | } |
147 | return null; |
148 | } |
149 | |
150 | /** |
151 | * @see org.eclipse.pde.api.tools.internal.AbstractApiTypeContainer#getPackageNames() |
152 | */ |
153 | public String[] getPackageNames() throws CoreException { |
154 | List names = new ArrayList(); |
155 | collectPackageNames(names, Util.DEFAULT_PACKAGE_NAME, fRoot); |
156 | String[] result = new String[names.size()]; |
157 | names.toArray(result); |
158 | Arrays.sort(result); |
159 | return result; |
160 | } |
161 | |
162 | /** |
163 | * Traverses a directory to determine if it has {@link IApiTypeRoot}s and |
164 | * then visits sub-directories. |
165 | * |
166 | * @param packageName package name of directory being visited |
167 | * @param dir directory being visited |
168 | */ |
169 | private void collectPackageNames(List names, String packageName, IContainer dir) throws CoreException { |
170 | IResource[] members = dir.members(); |
171 | boolean hasClassFiles = false; |
172 | List dirs = new ArrayList(); |
173 | for (int i = 0; i < members.length; i++) { |
174 | IResource file = members[i]; |
175 | switch (file.getType()) { |
176 | case IResource.FOLDER: |
177 | dirs.add(file); |
178 | break; |
179 | case IResource.FILE: |
180 | if (!hasClassFiles && file.getName().endsWith(Util.DOT_CLASS_SUFFIX)) { |
181 | names.add(packageName); |
182 | hasClassFiles = true; |
183 | } |
184 | break; |
185 | } |
186 | } |
187 | Iterator iterator = dirs.iterator(); |
188 | while (iterator.hasNext()) { |
189 | IContainer child = (IContainer)iterator.next(); |
190 | String nextName = null; |
191 | if (packageName.length() == 0) { |
192 | nextName = child.getName(); |
193 | } else { |
194 | StringBuffer buffer = new StringBuffer(packageName); |
195 | buffer.append('.'); |
196 | buffer.append(child.getName()); |
197 | nextName = buffer.toString(); |
198 | } |
199 | collectPackageNames(names, nextName, child); |
200 | } |
201 | } |
202 | |
203 | /** |
204 | * @see org.eclipse.pde.api.tools.internal.provisional.IApiTypeContainer#findTypeRoot(java.lang.String, java.lang.String) |
205 | */ |
206 | public IApiTypeRoot findTypeRoot(String qualifiedName, String id) throws CoreException { |
207 | return findTypeRoot(qualifiedName); |
208 | } |
209 | |
210 | /* (non-Javadoc) |
211 | * @see org.eclipse.pde.api.tools.internal.provisional.model.IApiTypeContainer#getContainerType() |
212 | */ |
213 | public int getContainerType() { |
214 | return FOLDER; |
215 | } |
216 | } |