KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2003, 2006 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.core.sourcelookup.containers;
13
14 import java.net.URI JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.core.filesystem.EFS;
19 import org.eclipse.core.filesystem.IFileStore;
20 import org.eclipse.core.resources.IContainer;
21 import org.eclipse.core.resources.IFile;
22 import org.eclipse.core.resources.IFolder;
23 import org.eclipse.core.resources.IResource;
24 import org.eclipse.core.resources.IWorkspaceRoot;
25 import org.eclipse.core.resources.ResourcesPlugin;
26 import org.eclipse.core.runtime.CoreException;
27 import org.eclipse.core.runtime.IPath;
28 import org.eclipse.core.runtime.Path;
29 import org.eclipse.debug.core.sourcelookup.ISourceContainer;
30
31 /**
32  * A source container for a container in the workspace. Source elements are searched
33  * for within this container and optionally nested containers.
34  * <p>
35  * Names specified in <code>findSourceElements</code> method can
36  * be simple or qualified. When a name is qualified, a file will
37  * be searched for relative to this container, and optionally
38  * nested containers.
39  * </p>
40  * <p>
41  * This class is not intended to be subclassed or instantiated by clients.
42  * </p>
43  * @since 3.2
44  */

45 public abstract class ContainerSourceContainer extends CompositeSourceContainer {
46
47     private IContainer fContainer = null;
48     private boolean fSubfolders = false;
49     
50     private URI JavaDoc fRootURI = null;
51     private IFileStore 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         fRootURI = fContainer.getLocationURI();
65         if (fRootURI != null) {
66             try {
67                 fRootFile = EFS.getStore(fRootURI);
68             } catch (CoreException e) {
69             }
70             fRoot = ResourcesPlugin.getWorkspace().getRoot();
71         }
72     }
73     
74     /**
75      * Returns the workspace container this source container is
76      * rooted at.
77      *
78      * @return the workspace container this source container is
79      * rooted at
80      */

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

88     public Object JavaDoc[] findSourceElements(String JavaDoc name) throws CoreException {
89         ArrayList JavaDoc sources = new ArrayList JavaDoc();
90
91         // An IllegalArgumentException is thrown from the "getFile" method
92
// if the path created by appending the file name to the container
93
// path doesn't conform with Eclipse resource restrictions.
94
// To prevent the interruption of the search procedure we check
95
// if the path is valid before passing it to "getFile".
96
if ( validateFile(name) ) {
97             IFile file = fContainer.getFile(new Path(name));
98             if (file.exists()) {
99                 sources.add(file);
100             } else {
101                 // See bug 82627 - perform case insensitive source lookup
102
if (fRootURI == null) {
103                     return EMPTY;
104                 }
105                 // See bug 98090 - we need to handle relative path names
106
IFileStore target = fRootFile.getChild(new Path(name));
107                 if (target.fetchInfo().exists()) {
108                     // We no longer have to account for bug 95832, and URIs take care
109
// of canonical paths (fix to bug 95679 was removed).
110
IFile[] files = fRoot.findFilesForLocationURI(target.toURI());
111                     if (isFindDuplicates() && files.length > 1) {
112                         for (int i = 0; i < files.length; i++) {
113                             sources.add(files[i]);
114                         }
115                     } else if (files.length > 0) {
116                         sources.add(files[0]);
117                     }
118                 }
119             }
120         }
121
122         //check sub-folders
123
if ((isFindDuplicates() && fSubfolders) || (sources.isEmpty() && fSubfolders)) {
124             ISourceContainer[] containers = getSourceContainers();
125             for (int i=0; i < containers.length; i++) {
126                 Object JavaDoc[] objects = containers[i].findSourceElements(name);
127                 if (objects == null || objects.length == 0) {
128                     continue;
129                 }
130                 if (isFindDuplicates()) {
131                     for(int j=0; j < objects.length; j++)
132                         sources.add(objects[j]);
133                 } else {
134                     sources.add(objects[0]);
135                     break;
136                 }
137             }
138         }
139         
140         if(sources.isEmpty())
141             return EMPTY;
142         return sources.toArray();
143     }
144     
145     /* (non-Javadoc)
146      * @see org.eclipse.debug.internal.core.sourcelookup.ISourceContainer#getName()
147      */

148     public String JavaDoc getName() {
149         return getContainer().getName();
150     }
151
152     /* (non-Javadoc)
153      * @see java.lang.Object#equals(java.lang.Object)
154      */

155     public boolean equals(Object JavaDoc obj) {
156         if (obj != null && obj instanceof ContainerSourceContainer) {
157             ContainerSourceContainer loc = (ContainerSourceContainer) obj;
158             return loc.getContainer().equals(getContainer());
159         }
160         return false;
161     }
162     
163     /* (non-Javadoc)
164      * @see java.lang.Object#hashCode()
165      */

166     public int hashCode() {
167         return getContainer().hashCode();
168     }
169
170     /* (non-Javadoc)
171      * @see org.eclipse.debug.internal.core.sourcelookup.ISourceContainer#isComposite()
172      */

173     public boolean isComposite() {
174         return fSubfolders;
175     }
176
177     /* (non-Javadoc)
178      * @see org.eclipse.debug.internal.core.sourcelookup.containers.CompositeSourceContainer#createSourceContainers()
179      */

180     protected ISourceContainer[] createSourceContainers() throws CoreException {
181         if(fSubfolders) {
182             IResource[] resources = getContainer().members();
183             List JavaDoc list = new ArrayList JavaDoc(resources.length);
184             for (int i = 0; i < resources.length; i++) {
185                 IResource resource = resources[i];
186                 if (resource.getType() == IResource.FOLDER) {
187                     list.add(new FolderSourceContainer((IFolder)resource, fSubfolders));
188                 }
189             }
190             ISourceContainer[] containers = (ISourceContainer[]) list.toArray(new ISourceContainer[list.size()]);
191             for (int i = 0; i < containers.length; i++) {
192                 ISourceContainer container = containers[i];
193                 container.init(getDirector());
194             }
195             return containers;
196         }
197         return new ISourceContainer[0];
198     }
199
200     /**
201      * Validates the given string as a path for a file in this container.
202      *
203      * @param name path name
204      */

205     private boolean validateFile(String JavaDoc name) {
206         IContainer container = getContainer();
207         IPath path = container.getFullPath().append(name);
208         return ResourcesPlugin.getWorkspace().validatePath(path.toOSString(), IResource.FILE).isOK();
209     }
210
211 }
212
Popular Tags