Java Code Examples for org.eclipse.core.resources.IFile#getLocation()

The following examples show how to use org.eclipse.core.resources.IFile#getLocation() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
/**
* @generated
*/
private long computeModificationStamp(ResourceSetInfo info) {
	int result = 0;
	for (Iterator<Resource> it = info.getLoadedResourcesIterator(); it.hasNext();) {
		Resource nextResource = it.next();
		IFile file = WorkspaceSynchronizer.getFile(nextResource);
		if (file != null) {
			if (file.getLocation() != null) {
				result += file.getLocation().toFile().lastModified();
			} else {
				result += file.getModificationStamp();
			}
		}
	}
	return result;
}
 
Example 2
Source Project: CppStyle   File: ClangFormatFormatter.java    License: MIT License 6 votes vote down vote up
private static IPath getSourceFilePathFromEditorInput(IEditorInput editorInput) {
	if (editorInput instanceof IURIEditorInput) {
		URI uri = ((IURIEditorInput) editorInput).getURI();
		if (uri != null) {
			IPath path = URIUtil.toPath(uri);
			if (path != null) {
				  return path;
			}
		}
	}

	if (editorInput instanceof IFileEditorInput) {
		IFile file = ((IFileEditorInput) editorInput).getFile();
		if (file != null) {
			return file.getLocation();
		}
	}

	if (editorInput instanceof ILocationProvider) {
		return ((ILocationProvider) editorInput).getPath(editorInput);
	}

	return null;
}
 
Example 3
Source Project: ice   File: Item.java    License: Eclipse Public License 1.0 6 votes vote down vote up
/**
 * This operation instantiates the output file.
 */
protected void setupOutputFile() {

	// Setup the output file handle name
	String outputFilename = form.getName().replaceAll("\\s+", "_") + "_"
			+ getId() + "_processOutput.txt";
	// Get the file handle from the project space. Note that it may not
	// actually exist.
	if (project != null) {
		// Get the file
		IFile outputFileHandle = project.getFile(outputFilename);
		IPath location = outputFileHandle.getLocation();
		outputFile = location.toFile();
		// Create a new file if it does not already exist
		try {
			outputFile.createNewFile();
		} catch (Exception fileFailException) {
			logger.info("Item Message: Unable to create output "
					+ "file in workspace. Aborting.");
			fileFailException.printStackTrace();
			return;
		}
	}

	return;
}
 
Example 4
/**
 * @param lastSegment
 * @param modelFolder
 * @return
 */
private IFile createUniqueFileFrom(final IFile originalFile, final IFolder modelFolder) {
	IFile file = originalFile;
	final Pattern p = Pattern.compile("(.*?)(\\d+)?(\\..*)?");
	while (file.exists()) {
		final IPath path = file.getLocation();
		String fName = path.lastSegment();
		final Matcher m = p.matcher(fName);
		if ( m.matches() ) {// group 1 is the prefix, group 2 is the number, group 3 is the suffix
			fName = m.group(1) + (m.group(2) == null ? 1 : Integer.parseInt(m.group(2)) + 1) +
				(m.group(3) == null ? "" : m.group(3));
		}
		file = modelFolder.getFile(fName);
	}
	return file;

}
 
Example 5
/**
 * Writes the passed resource to the current archive.
 *
 * @param resource			the file to be written
 * @param destinationPath	the path for the file inside the archive
 * @throws	CoreException	to signal any other unusual termination.
 * 								This can also be used to return information
 * 								in the status object.
 */
public void write(IFile resource, IPath destinationPath) throws CoreException {
	try {
		IPath fileLocation= resource.getLocation();
		File file= null;
		if (fileLocation != null) {
			file= fileLocation.toFile();
		}
		if (fJarPackage.areDirectoryEntriesIncluded())
			addDirectories(destinationPath, file);
		addFile(resource, destinationPath, file);
	} catch (IOException ex) {
		// Ensure full path is visible
		String message= null;
		if (ex.getLocalizedMessage() != null)
			message= Messages.format(JarPackagerMessages.JarWriter_writeProblemWithMessage, new Object[] {BasicElementLabels.getPathLabel(resource.getFullPath(), false), ex.getLocalizedMessage()});
		else
			message= Messages.format(JarPackagerMessages.JarWriter_writeProblem, BasicElementLabels.getPathLabel(resource.getFullPath(), false));
		throw JarPackagerUtil.createCoreException(message, ex);
	}
}
 
Example 6
/**
 * Gets the absolute location of the JAR file.
 * This path is normally external to the workspace.
 *
 * @return the absolute path representing the location of the JAR file
 *
 * @since 3.0
 */
public IPath getAbsoluteJarLocation() {
	if (!fJarLocation.isAbsolute()) {
		IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot();
		if (fJarLocation.segmentCount() >= 2 && !"..".equals(fJarLocation.segment(0))) { //$NON-NLS-1$
			// reverse of AbstractJarDestinationWizardPage#handleDestinationBrowseButtonPressed()
			IFile file= root.getFile(fJarLocation);
			IPath absolutePath= file.getLocation();
			if (absolutePath != null) {
				return absolutePath;
			}
		}
		// The path does not exist in the workspace (e.g. because there's no such project).
		// Fallback is to just append the path to the workspace root.
		return root.getLocation().append(fJarLocation);
	}
	return fJarLocation;
}
 
Example 7
/**
* @generated
*/
private long computeModificationStamp(ResourceSetInfo info) {
	int result = 0;
	for (Iterator<Resource> it = info.getLoadedResourcesIterator(); it.hasNext();) {
		Resource nextResource = it.next();
		IFile file = WorkspaceSynchronizer.getFile(nextResource);
		if (file != null) {
			if (file.getLocation() != null) {
				result += file.getLocation().toFile().lastModified();
			} else {
				result += file.getModificationStamp();
			}
		}
	}
	return result;
}
 
Example 8
Source Project: sarl   File: SARLRuntime.java    License: Apache License 2.0 6 votes vote down vote up
/** Replies if the given directory contains a SRE.
 *
 * @param directory the directory.
 * @return <code>true</code> if the given directory contains a SRE. Otherwise <code>false</code>.
 * @see #isPackedSRE(File)
 */
public static boolean isUnpackedSRE(IPath directory) {
	try {
		final IFile location = ResourcesPlugin.getWorkspace().getRoot().getFile(directory);
		if (location != null) {
			final IPath path = location.getLocation();
			if (path != null) {
				final File file = path.toFile();
				if (file.exists()) {
					if (file.isDirectory()) {
						return isUnpackedSRE(file);
					}
					return false;
				}
			}
		}
		return isUnpackedSRE(directory.makeAbsolute().toFile());
	} catch (Exception exception) {
		return false;
	}
}
 
Example 9
Source Project: sarl   File: SARLRuntime.java    License: Apache License 2.0 6 votes vote down vote up
/** Replies if the given JAR file contains a SRE.
 *
 * <p>The SRE detection is based on the content of the manifest.
 *
 * @param jarFile the JAR file to test.
 * @return <code>true</code> if the given directory contains a SRE. Otherwise <code>false</code>.
 * @see #isUnpackedSRE(File)
 */
public static boolean isPackedSRE(IPath jarFile) {
	try {
		final IFile location = ResourcesPlugin.getWorkspace().getRoot().getFile(jarFile);
		if (location != null) {
			final IPath path = location.getLocation();
			if (path != null) {
				final File file = path.toFile();
				if (file.exists()) {
					if (file.isFile()) {
						return isPackedSRE(file);
					}
					return false;
				}
			}
		}
		return isPackedSRE(jarFile.makeAbsolute().toFile());
	} catch (Exception exception) {
		return false;
	}
}
 
Example 10
Source Project: sarl   File: SARLRuntime.java    License: Apache License 2.0 6 votes vote down vote up
/** Replies if the given directory contains a SRE bootstrap.
 *
 * <p>The SRE bootstrap detection is based on the service definition within META-INF folder.
 *
 * @param directory the directory.
 * @return <code>true</code> if the given directory contains a SRE. Otherwise <code>false</code>.
 * @since 0.7
 * @see #containsPackedBootstrap(File)
 * @see #getDeclaredBootstrap(IPath)
 */
public static boolean containsUnpackedBootstrap(IPath directory) {
	final IFile location = ResourcesPlugin.getWorkspace().getRoot().getFile(directory);
	if (location != null) {
		final IPath path = location.getLocation();
		if (path != null) {
			final File file = path.toFile();
			if (file.exists()) {
				if (file.isDirectory()) {
					return containsUnpackedBootstrap(file);
				}
				return false;
			}
		}
	}
	return containsUnpackedBootstrap(directory.makeAbsolute().toFile());
}
 
Example 11
Source Project: sarl   File: SARLRuntime.java    License: Apache License 2.0 6 votes vote down vote up
/** Replies if the given JAR file contains a SRE bootstrap.
 *
 * <p>The SRE bootstrap detection is based on the service definition within META-INF folder.
 *
 * @param jarFile the JAR file to test.
 * @return <code>true</code> if the given directory contains a SRE. Otherwise <code>false</code>.
 * @since 0.7
 * @see #containsUnpackedBootstrap(File)
 * @see #getDeclaredBootstrap(IPath)
 */
public static boolean containsPackedBootstrap(IPath jarFile) {
	try {
		final IFile location = ResourcesPlugin.getWorkspace().getRoot().getFile(jarFile);
		if (location != null) {
			final IPath path = location.getLocation();
			if (path != null) {
				final File file = path.toFile();
				if (file.exists()) {
					if (file.isFile()) {
						return containsPackedBootstrap(file);
					}
					return false;
				}
			}
		}
		return containsPackedBootstrap(jarFile.makeAbsolute().toFile());
	} catch (Exception exception) {
		return false;
	}
}
 
Example 12
Source Project: sarl   File: FixedFatJarExportPage.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Gets the absolute location relative to the workspace.
 * 
 * @param location the location
 * @return the absolute path for the location of the file
 * 
 * @since 3.8
 */
private IPath getAbsoluteLocation(IPath location) {
	if (location.isAbsolute())
		return location;

	IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot();
	if (location.segmentCount() >= 2 && !"..".equals(location.segment(0))) { //$NON-NLS-1$
		IFile file= root.getFile(location);
		IPath absolutePath= file.getLocation();
		if (absolutePath != null) {
			return absolutePath;
		}
	}
	// The path does not exist in the workspace (e.g. because there's no such project).
	// Fallback is to just append the path to the workspace root.
	return root.getLocation().append(location);

}
 
Example 13
/**
* @generated
*/
public boolean isDeleted(Object element) {
	IDiagramDocument document = getDiagramDocument(element);
	if (document != null) {
		Resource diagramResource = document.getDiagram().eResource();
		if (diagramResource != null) {
			IFile file = WorkspaceSynchronizer.getFile(diagramResource);
			return file == null || file.getLocation() == null || !file.getLocation().toFile().exists();
		}
	}
	return super.isDeleted(element);
}
 
Example 14
private boolean isPlatformInstalled(String platformName) {
	if (platformName == null) {
		return false;
	}
	try {
		IFolder platformHome = getPlatformHomeFolder(platformName);
		if (platformHome != null) {
			IFile platformJson = platformHome.getFile(platformName + ".json");
			return platformJson.getLocation() != null && platformJson.getLocation().toFile().exists();
		}
	} catch (CoreException e) {
		// ignore to return false
	}
	return false;
}
 
Example 15
@Override
public JSONObject getContent() throws ReadFileStoreException {
    if (!getResource().exists()) {
        throw new ReadFileStoreException(getResource().getLocation() + " does not exists.");
    }
    final IFile jsonFile = getJSONIFile();
    if (!jsonFile.exists()) {
        throw new ReadFileStoreException(jsonFile.getLocation() + " does not exists.");
    }
    return toJSONObject(jsonFile);
}
 
Example 16
IFile createUnclassifiedModelsProjectAndAdd(final IPath location) {
	IFile iFile = null;
	try {
		final IFolder modelFolder = createUnclassifiedModelsProject(location);
		iFile = modelFolder.getFile(location.lastSegment());
		if ( iFile.exists() ) {
			if ( iFile.isLinked() ) {
				final IPath path = iFile.getLocation();
				if ( path.equals(location) ) {
					// First case, this is a linked resource to the same location. In that case, we simply return
					// its name.
					return iFile;
				} else {
					// Second case, this resource is a link to another location. We create a filename that is
					// guaranteed not to exist and change iFile accordingly.
					iFile = createUniqueFileFrom(iFile, modelFolder);
				}
			} else {
				// Third case, this resource is local and we do not want to overwrite it. We create a filename that
				// is guaranteed not to exist and change iFile accordingly.
				iFile = createUniqueFileFrom(iFile, modelFolder);
			}
		}
		iFile.createLink(location, IResource.NONE, null);
		// RefreshHandler.run();
		return iFile;
	} catch (final CoreException e) {
		e.printStackTrace();
		MessageDialog.openInformation(Display.getDefault().getActiveShell(), "Error in creation",
			"The file " + (iFile == null ? location.lastSegment() : iFile.getFullPath().lastSegment()) +
				" cannot be created because of the following exception " + e.getMessage());
		return null;
	}
}
 
Example 17
public static String getModelPathOf(final Resource r) {
	// Likely in a headless scenario (w/o workspace)
	if (r.getURI().isFile()) {
		return new Path(r.getURI().toFileString()).toOSString();
	} else {
		final IPath path = getPathOf(r);
		final IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
		final IPath fullPath = file.getLocation();
		return fullPath == null ? "" : fullPath.toOSString();
	}
}
 
Example 18
private IPath createFileInProject(String filename) throws CoreException {
  IFile file = projectCreator.getProject().getFile(filename);
  file.create(new ByteArrayInputStream(new byte[0]), true, null);
  return file.getLocation();
}
 
Example 19
Source Project: typescript.java   File: TypeScriptResourceUtil.java    License: MIT License 4 votes vote down vote up
public static IDocument getDocument(IFile file) {
	IPath location = file.getLocation();
	return getDocument(location);
}
 
Example 20
/**
 * IFile 构建函数
 * @param file
 */
public FileConversionItem(IFile file) {
	super(file.getLocation());
	this.resource = file;
}