Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2009 itemis AG (http://www.itemis.eu) 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 /
  
  package org.eclipse.xtext.ui.util;
  
 
 import  org.eclipse.core.resources.IContainer;
 import  org.eclipse.core.resources.IFile;
 import  org.eclipse.core.resources.IProject;
 import  org.eclipse.core.resources.ResourcesPlugin;
 import  org.eclipse.core.runtime.IPath;
 import  org.eclipse.core.runtime.Path;
 
Copied from org.eclipse.emf.workspace.util.WorkspaceSynchronizer to avoid dependencies to EMFT.

Author(s):
Jan Koehnlein - Initial contribution and API
 
 public class ResourceUtil {

Returns:
the handle to the member container of the given project or the project itself
Since:
2.4
 
 	public static IContainer getContainer(IProject projectString path) {
 		Preconditions.checkNotNull(project"parameter 'project' must not be null");
 		Preconditions.checkNotNull(path"parameter 'path' must not be null");
 		if (".".equals(path) || "./".equals(path) || "".equals(path)) {
 			return project;
 		}
 		return project.getFolder(path);
 	}
Obtains the workspace file corresponding to the specified resource, if it has a platform-resource URI. Note that the resulting file, if not null, may nonetheless not actually exist (as the file is just a handle).

Note that, if the resource is in an archive (such as a ZIP file) then it does not map to a workspace file. In this case, however, the workspace file (if any) corresponding to the containing archive can be obtained via the getUnderlyingFile(Resource) method.

Parameters:
resource an EMF resource
Returns:
the corresponding workspace file, or null if the resource's URI is not a platform-resource URI
See also:
getUnderlyingFile(Resource)
 
 	public static IFile getFile(Resource resource) {
 		ResourceSet rset = resource.getResourceSet();
 
 		return getFile(resource.getURI(), (rset != null) ? rset.getURIConverter() : nullfalse);
 	}

Obtains the workspace file underlying the specified resource. If the resource has an archive scheme, the authority is considered instead. If the URI has a file scheme, it's looked up in the workspace, just as in the getFile(Resource) method. Otherwise, a platform scheme is assumed.

Note that the resulting file, if not null, may nonetheless not actually exist (as the file is just a handle).

Parameters:
resource an EMF resource
Returns:
the underlying workspace file, or null if the resource's URI is not a platform-resource URI
Since:
1.2
See also:
getFile(Resource)
 
 	public static IFile getUnderlyingFile(Resource resource) {
 		ResourceSet rset = resource.getResourceSet();
 
 		return getFile(resource.getURI(), (rset != null) ? rset.getURIConverter() : nulltrue);
 	}

Finds the file corresponding to the specified URI, using a URI converter if necessary (and provided) to normalize it.

Parameters:
uri a URI
converter an optional URI converter (may be null)
Returns:
the file, if available in the workspace
	private static IFile getFile(URI uriURIConverter converterboolean considerArchives) {
		IFile result = null;
		if (considerArchives && uri.isArchive()) {
					super(url);
				}
				public String getNestedURI() {
					try {
						return getNestedURL();
catch (IOException exception) {
						return ""//$NON-NLS-1$
					}
				}
			}
			MyArchiveURLConnection archiveURLConnection = new MyArchiveURLConnection(uri.toString());
			result = getFile(URI.createURI(archiveURLConnection.getNestedURI()), converterconsiderArchives);
else if (uri.isPlatformResource()) {
			IPath path = new Path(uri.toPlatformString(true));
			result = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
else if (uri.isFile() && !uri.isRelative()) {
			result = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(new Path(uri.toFileString()));
else {
			// normalize, to see whether we can resolve it this time
			if (converter != null) {
				URI normalized = converter.normalize(uri);
				if (!uri.equals(normalized)) {
					// recurse on the new URI
					result = getFile(normalizedconverterconsiderArchives);
				}
			}
		}
		if ((result == null) && !uri.isRelative()) {
			try {
				IFile[] files = ResourcesPlugin.getWorkspace().getRoot().findFilesForLocationURI(
						new java.net.URI(uri.toString()));
				if (files.length > 0) {
					// set the result to be the first file found
					result = files[0];
				}
catch (URISyntaxException e) {
				// won't get this because EMF provides a well-formed URI
			}
		}
		return result;
	}
New to GrepCode? Check out our FAQ X