Skip to main content
summaryrefslogtreecommitdiffstats
blob: 6c5dcba5d78ce19c912e169e088b28e3e034d72b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*******************************************************************************
 * Copyright (c) 2006 Sybase, Inc. and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Sybase, Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.jsf.common.ui.internal.utils;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRunnable;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.jst.jsf.common.ui.IFileFolderConstants;
import org.eclipse.jst.jsf.common.ui.JSFUICommonPlugin;
import org.eclipse.jst.jsf.common.ui.internal.logging.Logger;

/**
 * TODO: This class should be abstract to common utility.
 * 
 * This class implements management of resource in a workspace.
 */
public final class WorkspaceUtil {
	/** log instance */
	private static final Logger log = JSFUICommonPlugin
			.getLogger(WorkspaceUtil.class);

	private WorkspaceUtil() {
	    // no external instantiation
	}

	/**
	 * Create the given file in the workspace resource info tree.
	 * @param resource 
	 * @param contents 
	 */
	public static void ensureExistsInWorkspace(final IFile resource,
			final InputStream contents) {
		if (resource == null) {
			return;
		}
		IWorkspaceRunnable body = new IWorkspaceRunnable() {
			public void run(IProgressMonitor monitor) throws CoreException {
				if (resource.exists()) {
					resource.setContents(contents, true, false, null);
				} else {
					ensureExistsInWorkspace(resource.getParent(), true);
					resource.create(contents, true, null);
				}
			}
		};
		try {
			ResourcesPlugin.getWorkspace().run(body, null);
		} catch (CoreException e) {
			// Test.EclipseWorkspaceTest.Error.FileCreationInWorkspace = Fail in
			// creating file:{0} in the workspace resource info tree.
			log
					.error(
							"Test.EclipseWorkspaceTest.Error.FileCreationInWorkspace", resource.getName(), e);//$NON-NLS-1$
		}
	}

	/**
	 * Create the given file in the workspace resource info tree.
	 * @param resource 
	 * @param contents 
	 */
	public static void ensureExistsInWorkspace(IFile resource, String contents) {
		// FIXME: We'll need some way for handing file encoding.
		ensureExistsInWorkspace(resource, new ByteArrayInputStream(contents
				.getBytes()));
	}

	/**
	 * Create the given resource in the workspace resource info tree.
	 * @param resource 
	 * @param local 
	 */
	public static void ensureExistsInWorkspace(final IResource resource,
			final boolean local) {
		IWorkspaceRunnable body = new IWorkspaceRunnable() {
			public void run(IProgressMonitor monitor) throws CoreException {
				create(resource, local);
			}
		};
		try {
			ResourcesPlugin.getWorkspace().run(body, null);
		} catch (CoreException e) {
			// Test.EclipseWorkspaceTest.Error.ResourceCreationInWorkspace =
			// Fail in creating resource:{0} in the workspace resource info
			// tree.
			log
					.error(
							"Test.EclipseWorkspaceTest.Error.ResourceCreationInWorkspace", resource.getName(), e);//$NON-NLS-1$
		}
	}

	/**
	 * crate the resource if the resource is not existed, create a new one.
	 * 
	 * @param resource -
	 *            resource instance
	 * @param local -
	 *            a flag controlling whether or not the folder will be local
	 *            after the creation
	 * @throws CoreException
	 */
	protected static void create(final IResource resource, boolean local)
			throws CoreException {
		if (resource == null || resource.exists()) {
			return;
		}
		if (!resource.getParent().exists()) {
			create(resource.getParent(), local);
		}
		switch (resource.getType()) {
		case IResource.FILE:
			((IFile) resource).create(local ? new ByteArrayInputStream(
					new byte[0]) : null, true, getMonitor());
			break;
		case IResource.FOLDER:
			((IFolder) resource).create(true, local, getMonitor());
			break;
		case IResource.PROJECT:
			((IProject) resource).create(getMonitor());
			((IProject) resource).open(getMonitor());
			break;
		}
	}

	/**
	 * create and return a NullProgressMonitor
	 * 
	 * @return - NullProgressMonitor
	 */
	public static IProgressMonitor getMonitor() {
		return new NullProgressMonitor();
	}

	/**
	 * Get the project reference for a given path
	 * 
	 * @param path -
	 *            the path
	 * @return IProject - the project reference
	 */
	public static IProject getProjectFor(IPath path) {
		String[] segs = path.segments();
		String projectPath = new String();
		IProject[] projects = ResourcesPlugin.getWorkspace().getRoot()
				.getProjects();
		IProject project = null;
		for (int p = 0; p < projects.length; p++) {
			if (projects[p].isOpen()) {
				for (int s = 0; s < segs.length; s++) {
					if (segs[s].equalsIgnoreCase(projects[p].getName())) {
						// Once we have a match on the project name, then
						// the remainder of the segments equals the project path
						for (int s2 = s + 1; s2 < segs.length; s2++) {
							projectPath = projectPath
									+ IFileFolderConstants.PATH_SEPARATOR
									+ segs[s2];
						}
						project = projects[p];
						break;
					}
				}
			}
		}
		if (project == null) {
			return null;
		}

		// TODO: still don't understand why this refreshLocal is necessary
		// for now, going to only allow it if this method is called 
		// when the tree isn't locked.  This shouldn't cause a regression, since
		// when the call fails currently things keep on going due to the catch
		if (!project.getWorkspace().isTreeLocked())
		{
    		try {
    			project.refreshLocal(IResource.DEPTH_INFINITE, null);
    		} catch (CoreException e) {
                // TODO C.B.:pushing this down to a warning because it creates really
                // spurious output.  Don't know why we are calling refreshLocal at all.
                JSFUICommonPlugin.getLogger(WorkspaceUtil.class).info("Error.RefreshingLocal", e); //$NON-NLS-1$
    		}
		}
		
		IResource res = project.findMember(new Path(projectPath));
		if ((res != null) && (res.exists())) {
			return project;
		}
		return null;
	}

	/**
	 * Get the project reference for a given file
	 * 
	 * @param file -
	 *            the IFile file reference
	 * @return IProject - the project reference
	 */
	public static IProject getProjectFor(IFile file) {
		IPath testPath = new Path(file.getFullPath().toOSString());
		return getProjectFor(testPath);
	}

	/**
	 * Get the project reference for a given file
	 * 
	 * @param file -
	 *            the File file reference
	 * @return IProject - the project reference
	 */
	public static IProject getProjectFor(File file) {
		IPath testPath = new Path(file.getAbsolutePath());
		return getProjectFor(testPath);
	}

	/**
	 * Get the project-relative resource reference for a given path
	 * 
	 * @param path -
	 *            the path
	 * @return IResource - the project-relative resource
	 */
	public static IResource getProjectRelativeResource(IPath path) {
		String[] segs = path.segments();
		String projectPath = new String();
		IProject[] projects = ResourcesPlugin.getWorkspace().getRoot()
				.getProjects();
		IProject project = null;
		for (int p = 0; p < projects.length; p++) {
			if (projects[p].isOpen()) {
				for (int s = 0; s < segs.length; s++) {
					if (segs[s].equalsIgnoreCase(projects[p].getName())) {
						// Once we have a match on the project name, then
						// the remainder of the segments equals the project path
						for (int s2 = s + 1; s2 < segs.length; s2++) {
							projectPath = projectPath
									+ IFileFolderConstants.PATH_SEPARATOR
									+ segs[s2];
						}
						project = projects[p];
						break;
					}
				}
			}
		}
		if (project == null) {
			return null;
		}

		return project.getFile(projectPath);
	}

	/**
	 * Get the project-relative resource reference for a given file
	 * 
	 * @param file -
	 *            the File file reference
	 * @return IResource - the project-relative resource
	 */
	public static IResource getProjectRelativeResource(File file) {
		IPath testPath = new Path(file.getAbsolutePath());
		return getProjectRelativeResource(testPath);
	}
}

Back to the top