Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 100807 | Differences between
and this patch

Collapse All | Expand All

(-)core/org/eclipse/debug/internal/core/sourcelookup/containers/ContainerSourceContainer.java (-180 / +202 lines)
Lines 30-221 Link Here
30
import org.eclipse.debug.core.sourcelookup.containers.FolderSourceContainer;
30
import org.eclipse.debug.core.sourcelookup.containers.FolderSourceContainer;
31
31
32
/**
32
/**
33
 * A container in the workspace. Source elements are searched
33
 * A container in the workspace. Source elements are searched for within this
34
 * for within this container and optionally nested containers.
34
 * container and optionally nested containers.
35
 * <p>
35
 * <p>
36
 * Names specified in <code>findSourceElements</code> method can
36
 * Names specified in <code>findSourceElements</code> method can be simple or
37
 * be simple or qualified. When a name is qualified, a file will
37
 * qualified. When a name is qualified, a file will be searched for relative to
38
 * be searched for relative to this container, and optionally
38
 * this container, and optionally nested containers.
39
 * nested containers.
40
 * </p>
39
 * </p>
41
 * 
40
 * 
42
 * @since 3.0
41
 * @since 3.0
43
 */
42
 */
44
public abstract class ContainerSourceContainer extends CompositeSourceContainer {
43
public abstract class ContainerSourceContainer extends CompositeSourceContainer {
45
44
46
	private IContainer fContainer = null;
45
    private IContainer fContainer = null;
47
	private boolean fSubfolders = false;
46
48
	
47
    private boolean fSubfolders = false;
49
	private IPath fRootPath = null;
48
50
	private String[] fRootSegments = null;
49
    private IPath fRootPath = null;
51
	private File fRootFile = null;
50
52
	private IWorkspaceRoot fRoot = null;
51
    private String[] fRootSegments = null;
53
52
54
	/**
53
    private File fRootFile = null;
55
	 * Constructs a source container on the given workspace container. 
54
56
	 * 
55
    private IWorkspaceRoot fRoot = null;
57
	 * @param container the container to search for source in
56
58
	 * @param subfolders whether nested folders should be searched
57
    /**
59
	 *  for source elements
58
     * Constructs a source container on the given workspace container.
60
	 */
59
     * 
61
	public ContainerSourceContainer(IContainer container, boolean subfolders) {
60
     * @param container
62
		fContainer = container;
61
     *            the container to search for source in
63
		fSubfolders = subfolders;
62
     * @param subfolders
64
		fRootPath = fContainer.getLocation();
63
     *            whether nested folders should be searched for source elements
65
		if (fRootPath != null) {
64
     */
66
			fRootSegments = fRootPath.segments();
65
    public ContainerSourceContainer(IContainer container, boolean subfolders) {
67
			fRootFile = fRootPath.toFile();
66
        fContainer = container;
68
			fRoot = ResourcesPlugin.getWorkspace().getRoot();
67
        fSubfolders = subfolders;
69
		}
68
        fRootPath = fContainer.getLocation();
70
	}
69
        if (fRootPath != null) {
71
	
70
            fRootSegments = fRootPath.segments();
72
	/**
71
            fRootFile = fRootPath.toFile();
73
	 * Returns the workspace container this source container is
72
            fRoot = ResourcesPlugin.getWorkspace().getRoot();
74
	 * rooted at.
73
        }
75
	 *  
74
    }
76
	 * @return the workspace container this source container is
75
77
	 * rooted at
76
    /**
78
	 */
77
     * Returns the workspace container this source container is rooted at.
79
	public IContainer getContainer() {
78
     * 
80
		return fContainer;
79
     * @return the workspace container this source container is rooted at
81
	}
80
     */
82
	
81
    public IContainer getContainer() {
83
	/* (non-Javadoc)
82
        return fContainer;
84
	 * @see org.eclipse.debug.internal.core.sourcelookup.ISourceContainer#findSourceElements(java.lang.String)
83
    }
85
	 */
84
86
	public Object[] findSourceElements(String name) throws CoreException {
85
    /*
87
		if (fRootPath == null) {
86
     * (non-Javadoc)
88
			return EMPTY;
87
     * 
89
		}
88
     * @see org.eclipse.debug.internal.core.sourcelookup.ISourceContainer#findSourceElements(java.lang.String)
90
		ArrayList sources = new ArrayList();
89
     */
91
90
    public Object[] findSourceElements(String name) throws CoreException {
92
		// An IllegalArgumentException is thrown from the "getFile" method 
91
        if (fRootPath == null) {
93
		// if the path created by appending the file name to the container 
92
            return EMPTY;
94
		// path doesn't conform with Eclipse resource restrictions.
93
        }
95
		// To prevent the interruption of the search procedure we check 
94
        ArrayList sources = new ArrayList();
96
		// if the path is valid before passing it to "getFile".		
95
97
		if ( validateFile(name) ) {
96
        // An IllegalArgumentException is thrown from the "getFile" method
98
			File osFile = new File(fRootFile, name);
97
        // if the path created by appending the file name to the container
99
			if (osFile.exists()) {
98
        // path doesn't conform with Eclipse resource restrictions.
100
				try {
99
        // To prevent the interruption of the search procedure we check
101
					// See bug 82627 and bug 95679 - we have to append the container path in the case
100
        // if the path is valid before passing it to "getFile".
102
					// that Eclipse thinks it is, with the file system case of the file in order to
101
        if (validateFile(name)) {
103
					// be successful when finding the IFile for a location.
102
            IFile file = fContainer.getFile(new Path(name));
104
					// See bug 98090 - we need to handle relative path names
103
            if (file.exists()) {
105
					Path canonicalPath = new Path(osFile.getCanonicalPath());
104
                sources.add(file);
106
					String[] canonicalSegments = canonicalPath.segments();
105
            } else {
107
					IPath workspacePath = new Path(""); //$NON-NLS-1$
106
                File osFile = new File(fRootFile, name);
108
					workspacePath = workspacePath.setDevice(canonicalPath.getDevice());
107
                if (osFile.exists()) {
109
					for (int i = 0; i < canonicalSegments.length; i++) {
108
                    try {
110
						String segment = canonicalSegments[i];
109
                        // See bug 82627 and bug 95679 - we have to append the
111
						if (i < fRootSegments.length) {
110
                        // container path in the case
112
							if (fRootSegments[i].equalsIgnoreCase(segment)) {
111
                        // that Eclipse thinks it is, with the file system case
113
								workspacePath = workspacePath.append(fRootSegments[i]);
112
                        // of
114
							} else {
113
                        // the file in order to
115
								workspacePath = workspacePath.append(segment);
114
                        // be successful when finding the IFile for a location.
116
							}
115
                        // See bug 98090 - we need to handle relative path names
117
						} else {
116
                        Path canonicalPath = new Path(osFile.getCanonicalPath());
118
							workspacePath = workspacePath.append(segment);
117
                        String[] canonicalSegments = canonicalPath.segments();
119
						}
118
                        IPath workspacePath = new Path(""); //$NON-NLS-1$
120
					}
119
                        workspacePath = workspacePath.setDevice(canonicalPath.getDevice());
121
					IFile[] files = fRoot.findFilesForLocation(workspacePath);
120
                        for (int i = 0; i < canonicalSegments.length; i++) {
122
					if (isFindDuplicates() && files.length > 1) {
121
                            String segment = canonicalSegments[i];
123
						for (int i = 0; i < files.length; i++) {
122
                            if (i < fRootSegments.length) {
124
							sources.add(files[i]);
123
                                if (fRootSegments[i].equalsIgnoreCase(segment)) {
125
						}
124
                                    workspacePath = workspacePath.append(fRootSegments[i]);
126
					} else if (files.length > 0) {
125
                                } else {
127
						sources.add(files[0]);
126
                                    workspacePath = workspacePath.append(segment);
128
					}					
127
                                }
129
				} catch (IOException e) {
128
                            } else {
130
				}
129
                                workspacePath = workspacePath.append(segment);
131
			}
130
                            }
132
		}
131
                        }
133
132
                        IFile[] files = fRoot.findFilesForLocation(workspacePath);
134
		//check subfolders		
133
                        if (isFindDuplicates() && files.length > 1) {
135
		if ((isFindDuplicates() && fSubfolders) || (sources.isEmpty() && fSubfolders)) {
134
                            for (int i = 0; i < files.length; i++) {
136
			ISourceContainer[] containers = getSourceContainers();
135
                                sources.add(files[i]);
137
			for (int i=0; i < containers.length; i++) {
136
                            }
138
				Object[] objects = containers[i].findSourceElements(name);
137
                        } else if (files.length > 0) {
139
				if (objects == null || objects.length == 0) {
138
                            sources.add(files[0]);
140
					continue;
139
                        }
141
				}
140
                    } catch (IOException e) {
142
				if (isFindDuplicates()) {
141
                    }
143
					for(int j=0; j < objects.length; j++)
142
                }
144
						sources.add(objects[j]);
143
            }
145
				} else {
144
        }
146
					sources.add(objects[0]);
145
147
					break;
146
        // check subfolders
148
				}
147
        if ((isFindDuplicates() && fSubfolders) || (sources.isEmpty() && fSubfolders)) {
149
			}
148
            ISourceContainer[] containers = getSourceContainers();
150
		}			
149
            for (int i = 0; i < containers.length; i++) {
151
		
150
                Object[] objects = containers[i].findSourceElements(name);
152
		if(sources.isEmpty())
151
                if (objects == null || objects.length == 0) {
153
			return EMPTY;
152
                    continue;
154
		return sources.toArray();
153
                }
155
	}
154
                if (isFindDuplicates()) {
156
	
155
                    for (int j = 0; j < objects.length; j++)
157
	/* (non-Javadoc)
156
                        sources.add(objects[j]);
158
	 * @see org.eclipse.debug.internal.core.sourcelookup.ISourceContainer#getName()
157
                } else {
159
	 */
158
                    sources.add(objects[0]);
160
	public String getName() {		
159
                    break;
161
		return getContainer().getName(); 
160
                }
162
	}
161
            }
163
162
        }
164
	/* (non-Javadoc)
163
165
	 * @see java.lang.Object#equals(java.lang.Object)
164
        if (sources.isEmpty())
166
	 */
165
            return EMPTY;
167
	public boolean equals(Object obj) {
166
        return sources.toArray();
168
		if (obj != null && obj instanceof ContainerSourceContainer) {
167
    }
169
			ContainerSourceContainer loc = (ContainerSourceContainer) obj;
168
170
			return loc.getContainer().equals(getContainer());
169
    /*
171
		}	
170
     * (non-Javadoc)
172
		return false;
171
     * 
173
	}	
172
     * @see org.eclipse.debug.internal.core.sourcelookup.ISourceContainer#getName()
174
	
173
     */
175
	/* (non-Javadoc)
174
    public String getName() {
176
	 * @see java.lang.Object#hashCode()
175
        return getContainer().getName();
177
	 */
176
    }
178
	public int hashCode() {
177
179
		return getContainer().hashCode();
178
    /*
180
	}
179
     * (non-Javadoc)
181
180
     * 
182
	/* (non-Javadoc)
181
     * @see java.lang.Object#equals(java.lang.Object)
183
	 * @see org.eclipse.debug.internal.core.sourcelookup.ISourceContainer#isComposite()
182
     */
184
	 */
183
    public boolean equals(Object obj) {
185
	public boolean isComposite() {	
184
        if (obj != null && obj instanceof ContainerSourceContainer) {
186
		return fSubfolders;
185
            ContainerSourceContainer loc = (ContainerSourceContainer) obj;
187
	}
186
            return loc.getContainer().equals(getContainer());
188
187
        }
189
	/* (non-Javadoc)
188
        return false;
190
	 * @see org.eclipse.debug.internal.core.sourcelookup.containers.CompositeSourceContainer#createSourceContainers()
189
    }
191
	 */
190
192
	protected ISourceContainer[] createSourceContainers() throws CoreException {
191
    /*
193
		if(fSubfolders) {
192
     * (non-Javadoc)
194
			IResource[] resources = getContainer().members();
193
     * 
195
			List list = new ArrayList(resources.length);
194
     * @see java.lang.Object#hashCode()
196
			for (int i = 0; i < resources.length; i++) {
195
     */
197
				IResource resource = resources[i];
196
    public int hashCode() {
198
				if (resource.getType() == IResource.FOLDER) {
197
        return getContainer().hashCode();
199
					list.add(new FolderSourceContainer((IFolder)resource, fSubfolders));
198
    }
200
				}
199
201
			}
200
    /*
202
			ISourceContainer[] containers = (ISourceContainer[]) list.toArray(new ISourceContainer[list.size()]);
201
     * (non-Javadoc)
203
			for (int i = 0; i < containers.length; i++) {
202
     * 
204
				ISourceContainer container = containers[i];
203
     * @see org.eclipse.debug.internal.core.sourcelookup.ISourceContainer#isComposite()
205
				container.init(getDirector());
204
     */
206
			}			
205
    public boolean isComposite() {
207
			return containers;
206
        return fSubfolders;
208
		}
207
    }
209
		return new ISourceContainer[0];
208
210
	}
209
    /*
211
210
     * (non-Javadoc)
212
	/**
211
     * 
213
	 * Validates the given string as a path for a file in this container. 
212
     * @see org.eclipse.debug.internal.core.sourcelookup.containers.CompositeSourceContainer#createSourceContainers()
214
	 */
213
     */
215
	private boolean validateFile(String name) {
214
    protected ISourceContainer[] createSourceContainers() throws CoreException {
216
		IContainer container = getContainer();
215
        if (fSubfolders) {
217
		IPath path = container.getFullPath().append(name);
216
            IResource[] resources = getContainer().members();
218
		return ResourcesPlugin.getWorkspace().validatePath(path.toOSString(), IResource.FILE).isOK();
217
            List list = new ArrayList(resources.length);
219
	}
218
            for (int i = 0; i < resources.length; i++) {
219
                IResource resource = resources[i];
220
                if (resource.getType() == IResource.FOLDER) {
221
                    list.add(new FolderSourceContainer((IFolder) resource, fSubfolders));
222
                }
223
            }
224
            ISourceContainer[] containers = (ISourceContainer[]) list.toArray(new ISourceContainer[list.size()]);
225
            for (int i = 0; i < containers.length; i++) {
226
                ISourceContainer container = containers[i];
227
                container.init(getDirector());
228
            }
229
            return containers;
230
        }
231
        return new ISourceContainer[0];
232
    }
233
234
    /**
235
     * Validates the given string as a path for a file in this container.
236
     */
237
    private boolean validateFile(String name) {
238
        IContainer container = getContainer();
239
        IPath path = container.getFullPath().append(name);
240
        return ResourcesPlugin.getWorkspace().validatePath(path.toOSString(), IResource.FILE).isOK();
241
    }
220
242
221
}
243
}

Return to bug 100807