KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > core > sourcelookup > containers > ContainerSourceContainer


1 /*******************************************************************************
2  * Copyright (c) 2003, 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  * QNX Software Systems - Mikhail Khodjaiants - Bug 80857
11  *******************************************************************************/

12 package org.eclipse.debug.internal.core.sourcelookup.containers;
13
14 import java.io.File JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.List JavaDoc;
18
19 import org.eclipse.core.resources.IContainer;
20 import org.eclipse.core.resources.IFile;
21 import org.eclipse.core.resources.IFolder;
22 import org.eclipse.core.resources.IResource;
23 import org.eclipse.core.resources.IWorkspaceRoot;
24 import org.eclipse.core.resources.ResourcesPlugin;
25 import org.eclipse.core.runtime.CoreException;
26 import org.eclipse.core.runtime.IPath;
27 import org.eclipse.core.runtime.Path;
28 import org.eclipse.debug.core.sourcelookup.ISourceContainer;
29 import org.eclipse.debug.core.sourcelookup.containers.CompositeSourceContainer;
30 import org.eclipse.debug.core.sourcelookup.containers.FolderSourceContainer;
31
32 /**
33  * A container in the workspace. Source elements are searched
34  * for within this container and optionally nested containers.
35  * <p>
36  * Names specified in <code>findSourceElements</code> method can
37  * be simple or qualified. When a name is qualified, a file will
38  * be searched for relative to this container, and optionally
39  * nested containers.
40  * </p>
41  *
42  * @since 3.0
43  */

44 public abstract class ContainerSourceContainer extends CompositeSourceContainer {
45
46     private IContainer fContainer = null;
47     private boolean fSubfolders = false;
48     
49     private IPath fRootPath = null;
50     private String JavaDoc[] fRootSegments = null;
51     private File JavaDoc fRootFile = null;
52     private IWorkspaceRoot fRoot = null;
53
54     /**
55      * Constructs a source container on the given workspace container.
56      *
57      * @param container the container to search for source in
58      * @param subfolders whether nested folders should be searched
59      * for source elements
60      */

61     public ContainerSourceContainer(IContainer container, boolean subfolders) {
62         fContainer = container;
63         fSubfolders = subfolders;
64         fRootPath = fContainer.getLocation();
65         if (fRootPath != null) {
66             fRootSegments = fRootPath.segments();
67             fRootFile = fRootPath.toFile();
68             fRoot = ResourcesPlugin.getWorkspace().getRoot();
69         }
70     }
71     
72     /**
73      * Returns the workspace container this source container is
74      * rooted at.
75      *
76      * @return the workspace container this source container is
77      * rooted at
78      */

79     public IContainer getContainer() {
80         return fContainer;
81     }
82     
83     /* (non-Javadoc)
84      * @see org.eclipse.debug.internal.core.sourcelookup.ISourceContainer#findSourceElements(java.lang.String)
85      */

86     public Object JavaDoc[] findSourceElements(String JavaDoc name) throws CoreException {
87         if (fRootPath == null) {
88             return EMPTY;
89         }
90         ArrayList JavaDoc sources = new ArrayList JavaDoc();
91
92         // An IllegalArgumentException is thrown from the "getFile" method
93
// if the path created by appending the file name to the container
94
// path doesn't conform with Eclipse resource restrictions.
95
// To prevent the interruption of the search procedure we check
96
// if the path is valid before passing it to "getFile".
97
if ( validateFile(name) ) {
98             IFile file = fContainer.getFile(new Path(name));
99             if (file.exists()) {
100                 sources.add(file);
101             } else {
102                 File JavaDoc osFile = new File JavaDoc(fRootFile, name);
103                 if (osFile.exists()) {
104                     try {
105                         // See bug 82627 and bug 95679 - we have to append the container path in the case
106
// that Eclipse thinks it is, with the file system case of the file in order to
107
// be successful when finding the IFile for a location.
108
// See bug 98090 - we need to handle relative path names
109
Path canonicalPath = new Path(osFile.getCanonicalPath());
110                         String JavaDoc[] canonicalSegments = canonicalPath.segments();
111                         IPath workspacePath = new Path(""); //$NON-NLS-1$
112
workspacePath = workspacePath.setDevice(canonicalPath.getDevice());
113                         for (int i = 0; i < canonicalSegments.length; i++) {
114                             String JavaDoc segment = canonicalSegments[i];
115                             if (i < fRootSegments.length) {
116                                 if (fRootSegments[i].equalsIgnoreCase(segment)) {
117                                     workspacePath = workspacePath.append(fRootSegments[i]);
118                                 } else {
119                                     workspacePath = workspacePath.append(segment);
120                                 }
121                             } else {
122                                 workspacePath = workspacePath.append(segment);
123                             }
124                         }
125                         IFile[] files = fRoot.findFilesForLocation(workspacePath);
126                         if (isFindDuplicates() && files.length > 1) {
127                             for (int i = 0; i < files.length; i++) {
128                                 sources.add(files[i]);
129                             }
130                         } else if (files.length > 0) {
131                             sources.add(files[0]);
132                         }
133                     } catch (IOException JavaDoc e) {
134                     }
135                 }
136             }
137         }
138
139         //check subfolders
140
if ((isFindDuplicates() && fSubfolders) || (sources.isEmpty() && fSubfolders)) {
141             ISourceContainer[] containers = getSourceContainers();
142             for (int i=0; i < containers.length; i++) {
143                 Object JavaDoc[] objects = containers[i].findSourceElements(name);
144                 if (objects == null || objects.length == 0) {
145                     continue;
146                 }
147                 if (isFindDuplicates()) {
148                     for(int j=0; j < objects.length; j++)
149                         sources.add(objects[j]);
150                 } else {
151                     sources.add(objects[0]);
152                     break;
153                 }
154             }
155         }
156         
157         if(sources.isEmpty())
158             return EMPTY;
159         return sources.toArray();
160     }
161     
162     /* (non-Javadoc)
163      * @see org.eclipse.debug.internal.core.sourcelookup.ISourceContainer#getName()
164      */

165     public String JavaDoc getName() {
166         return getContainer().getName();
167     }
168
169     /* (non-Javadoc)
170      * @see java.lang.Object#equals(java.lang.Object)
171      */

172     public boolean equals(Object JavaDoc obj) {
173         if (obj != null && obj instanceof ContainerSourceContainer) {
174             ContainerSourceContainer loc = (ContainerSourceContainer) obj;
175             return loc.getContainer().equals(getContainer());
176         }
177         return false;
178     }
179     
180     /* (non-Javadoc)
181      * @see java.lang.Object#hashCode()
182      */

183     public int hashCode() {
184         return getContainer().hashCode();
185     }
186
187     /* (non-Javadoc)
188      * @see org.eclipse.debug.internal.core.sourcelookup.ISourceContainer#isComposite()
189      */

190     public boolean isComposite() {
191         return fSubfolders;
192     }
193
194     /* (non-Javadoc)
195      * @see org.eclipse.debug.internal.core.sourcelookup.containers.CompositeSourceContainer#createSourceContainers()
196      */

197     protected ISourceContainer[] createSourceContainers() throws CoreException {
198         if(fSubfolders) {
199             IResource[] resources = getContainer().members();
200             List JavaDoc list = new ArrayList JavaDoc(resources.length);
201             for (int i = 0; i < resources.length; i++) {
202                 IResource resource = resources[i];
203                 if (resource.getType() == IResource.FOLDER) {
204                     list.add(new FolderSourceContainer((IFolder)resource, fSubfolders));
205                 }
206             }
207             ISourceContainer[] containers = (ISourceContainer[]) list.toArray(new ISourceContainer[list.size()]);
208             for (int i = 0; i < containers.length; i++) {
209                 ISourceContainer container = containers[i];
210                 container.init(getDirector());
211             }
212             return containers;
213         }
214         return new ISourceContainer[0];
215     }
216
217     /**
218      * Validates the given string as a path for a file in this container.
219      */

220     private boolean validateFile(String JavaDoc name) {
221         IContainer container = getContainer();
222         IPath path = container.getFullPath().append(name);
223         return ResourcesPlugin.getWorkspace().validatePath(path.toOSString(), IResource.FILE).isOK();
224     }
225
226 }
227
Popular Tags