/** * Return the IResource instance that matches the specified path. The path is the relative path in * the workspace. If no IResource instance is found that match this path a null is returned. * * @param name the path to the IResource * @return the IResource */ public static IResource findIResourceByPath(final IPath workspacePath) { if (workspacePath == null || workspacePath.isEmpty() || getWorkspace() == null) return null; // Collect all IResources within all IProjects final FileResourceCollectorVisitor visitor = new FileResourceCollectorVisitor(); if (getWorkspace() != null && getWorkspace().getRoot() != null) { final IProject[] projects = getWorkspace().getRoot().getProjects(); for (final IProject project : projects) try { project.accept(visitor); } catch (final CoreException e) { // do nothing } } final IFile[] fileResources = visitor.getFileResources(); for (final IFile fileResource : fileResources) if (fileResource != null) { final IPath path = fileResource.getFullPath(); // Do not process file names staring with '.' since these // are considered reserved for Eclipse specific files if (path.lastSegment().charAt(0) == '.') continue; if (path.equals(workspacePath)) return fileResource; } return null; }
/** * Return the IResource instance that matches the stringified UUID. If the stringified UUID is * null, empty, or does not have a UUID.PROTOCOL prefix then null is returned. If no IResource * instance is found that matches this UUID then null is returned. * * @param stringifiedUuid the stringified form of a UUID instance * @return the IResource */ public static IResource findIResourceByUUID(final String stringifiedUuid) { if (CoreStringUtil.isEmpty(stringifiedUuid) || !stringifiedUuid.startsWith(UUID.PROTOCOL) || getWorkspace() == null) return null; // Collect all IResources within all IProjects final FileResourceCollectorVisitor visitor = new FileResourceCollectorVisitor(); if (getWorkspace() != null && getWorkspace().getRoot() != null) { final IProject[] projects = getWorkspace().getRoot().getProjects(); for (final IProject project : projects) try { project.accept(visitor); } catch (final CoreException e) { // do nothing } } final IFile[] fileResources = visitor.getFileResources(); for (final IFile fileResource : fileResources) { final IFile iResource = fileResource; if (iResource != null && !ModelUtil.isXsdFile(iResource)) { final XMIHeader header = ModelUtil.getXmiHeader(iResource); if (header != null && stringifiedUuid.equals(header.getUUID())) return iResource; } } return null; }
private static IFile[] getAllProjectsFileResources() { // Collect all IResources within all IProjects final FileResourceCollectorVisitor visitor = new FileResourceCollectorVisitor(); final IWorkspace workSpace = getWorkspace(); if (workSpace != null && workSpace.getRoot() != null) { final IWorkspaceRoot wsRoot = workSpace.getRoot(); final IProject[] projects = wsRoot.getProjects(); for (final IProject project : projects) if (project.isOpen()) try { project.accept(visitor); } catch (final CoreException e) { // do nothing ModelerCore.Util.log(e); } } return visitor.getFileResources(); }
public static Collection getAllWorkspaceResources(final ResourceFilter filter) { // Collect all IResources within all IProjects final FileResourceCollectorVisitor visitor = new FileResourceCollectorVisitor(filter); final IProject[] projects = ModelerCore.getWorkspace().getRoot().getProjects(); for (final IProject project : projects) try { project.accept(visitor); } catch (final CoreException e) { // do nothing } final Collection fileResources = visitor.getFileResourcesCollection(); final Iterator itor = fileResources.iterator(); while (itor.hasNext()) { final IFile fileResource = (IFile) itor.next(); final IPath path = fileResource.getFullPath(); // Do not process file names starting with '.' since these // are considered reserved for Eclipse specific files if (path.lastSegment().charAt(0) == '.') itor.remove(); } // endwhile return fileResources; }
/** * Return the array of IResource instances that match the specified name. The name must consist of * only one path segment and may or may not have a file extension. If no IResource instances are * found that match this name an empty array is returned. * * @param name the name of the IResource * @return the IResource[] */ public static IResource[] findIResourceByName(final String name) { if (name == null || name.length() == 0 || getWorkspace() == null) return EMPTY_IRESOURCE_ARRAY; // Collect all IResources within all IProjects final FileResourceCollectorVisitor visitor = new FileResourceCollectorVisitor(); if (getWorkspace() != null && getWorkspace().getRoot() != null) { final IProject[] projects = getWorkspace().getRoot().getProjects(); for (final IProject project : projects) try { project.accept(visitor); } catch (final CoreException e) { // do nothing } } // Try to match the specified resource name with one of the IResource instances final boolean removeExtension = (name.indexOf('.') == -1); final IFile[] fileResources = visitor.getFileResources(); final ArrayList tmp = new ArrayList(); for (final IFile fileResource : fileResources) if (fileResource != null) { IPath path = fileResource.getFullPath(); // Do not process file names staring with '.' since these // are considered reserved for Eclipse specific files if (path.lastSegment().charAt(0) == '.') continue; if (removeExtension) path = path.removeFileExtension(); if (name.equalsIgnoreCase(path.lastSegment())) tmp.add(fileResource); } // If no matching resources are found return an empty array if (tmp.size() == 0) return EMPTY_IRESOURCE_ARRAY; final IResource[] result = new IResource[tmp.size()]; tmp.toArray(result); return result; }