Java Code Examples for org.eclipse.core.runtime.IPath#getDevice()

The following examples show how to use org.eclipse.core.runtime.IPath#getDevice() . 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
Source File: ResourceUtils.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Convert an {@link IPath} to a glob pattern.
 *
 * @param path
 *            the path to convert
 * @param recursive
 *            whether to end the glob with "/**"
 * @return a glob pattern prefixed with the path
 */
public static String toGlobPattern(IPath path, boolean recursive) {
	if (path == null) {
		return null;
	}

	String globPattern = path.toPortableString();
	if (path.getDevice() != null) {
		//This seems pretty hack-ish: need to remove device as it seems to break
		// file detection, at least on vscode
		globPattern = globPattern.replace(path.getDevice(), "**");
	}

	if (recursive) {
		if (!globPattern.endsWith("/")) {
			globPattern += "/";
		}
		globPattern += "**";
	}

	return globPattern;
}
 
Example 2
Source File: JavaSearchScope.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public boolean encloses(IJavaElement element) {
	if (this.elements != null) {
		for (int i = 0, length = this.elements.size(); i < length; i++) {
			IJavaElement scopeElement = (IJavaElement)this.elements.get(i);
			IJavaElement searchedElement = element;
			while (searchedElement != null) {
				if (searchedElement.equals(scopeElement))
					return true;
				searchedElement = searchedElement.getParent();
			}
		}
		return false;
	}
	IPackageFragmentRoot root = (IPackageFragmentRoot) element.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
	if (root != null && root.isArchive()) {
		// external or internal jar
		IPath rootPath = root.getPath();
		String rootPathToString = rootPath.getDevice() == null ? rootPath.toString() : rootPath.toOSString();
		IPath relativePath = getPath(element, true/*relative path*/);
		return indexOf(rootPathToString, relativePath.toString()) >= 0;
	}
	// resource in workspace
	String fullResourcePathString = getPath(element, false/*full path*/).toString();
	return indexOf(fullResourcePathString) >= 0;
}
 
Example 3
Source File: IndexManager.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Resets the index for a given path.
 * Returns true if the index was reset, false otherwise.
 */
public synchronized boolean resetIndex(IPath containerPath) {
	// only called to over write an existing cached index...
	String containerPathString = containerPath.getDevice() == null ? containerPath.toString() : containerPath.toOSString();
	try {
		// Path is already canonical
		IndexLocation indexLocation = computeIndexLocation(containerPath);
		Index index = getIndex(indexLocation);
		if (VERBOSE) {
			Util.verbose("-> reseting index: "+indexLocation+" for path: "+containerPathString); //$NON-NLS-1$ //$NON-NLS-2$
		}
		if (index == null) {
			// the index does not exist, try to recreate it
			return recreateIndex(containerPath) != null;
		}
		index.reset();
		return true;
	} catch (IOException e) {
		// The file could not be created. Possible reason: the project has been deleted.
		if (VERBOSE) {
			Util.verbose("-> failed to reset index for path: "+containerPathString); //$NON-NLS-1$
			e.printStackTrace();
		}
		return false;
	}
}
 
Example 4
Source File: IndexManager.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Recreates the index for a given path, keeping the same read-write monitor.
 * Returns the new empty index or null if it didn't exist before.
 * Warning: Does not check whether index is consistent (not being used)
 */
public synchronized Index recreateIndex(IPath containerPath) {
	// only called to over write an existing cached index...
	String containerPathString = containerPath.getDevice() == null ? containerPath.toString() : containerPath.toOSString();
	try {
		// Path is already canonical
		IndexLocation indexLocation = computeIndexLocation(containerPath);
		Index index = getIndex(indexLocation);
		ReadWriteMonitor monitor = index == null ? null : index.monitor;

		if (VERBOSE)
			Util.verbose("-> recreating index: "+indexLocation+" for path: "+containerPathString); //$NON-NLS-1$ //$NON-NLS-2$
		index = new Index(indexLocation, containerPathString, false /*do not reuse index file*/);
		this.indexes.put(indexLocation, index);
		index.monitor = monitor;
		return index;
	} catch (IOException e) {
		// The file could not be created. Possible reason: the project has been deleted.
		if (VERBOSE) {
			Util.verbose("-> failed to recreate index for path: "+containerPathString); //$NON-NLS-1$
			e.printStackTrace();
		}
		return null;
	}
}
 
Example 5
Source File: ExclusionInclusionEntryDialog.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
protected void checkIfPatternValid() {
	String pattern= fExclusionPatternDialog.getText().trim();
	if (pattern.length() == 0) {
		fExclusionPatternStatus.setError(NewWizardMessages.ExclusionInclusionEntryDialog_error_empty);
		return;
	}
	IPath path= new Path(pattern);
	if (path.isAbsolute() || path.getDevice() != null) {
		fExclusionPatternStatus.setError(NewWizardMessages.ExclusionInclusionEntryDialog_error_notrelative);
		return;
	}
	if (fExistingPatterns.contains(pattern)) {
		fExclusionPatternStatus.setError(NewWizardMessages.ExclusionInclusionEntryDialog_error_exists);
		return;
	}

	fExclusionPattern= pattern;
	fExclusionPatternStatus.setOK();
}
 
Example 6
Source File: EclipseProjectImporter.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private IPath fixDevice(IPath path) {
	if (path != null && path.getDevice() != null) {
		return path.setDevice(path.getDevice().toUpperCase());
	}
	if (Platform.OS_WIN32.equals(Platform.getOS()) && path != null && path.toString().startsWith("//")) {
		String server = path.segment(0);
		String pathStr = path.toString().replace(server, server.toUpperCase());
		return new Path(pathStr);
	}
	return path;
}
 
Example 7
Source File: JavaModel.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Helper method - returns the {@link IResource} corresponding to the provided {@link IPath},
 * or <code>null</code> if no such resource exists.
 */
public static IResource getWorkspaceTarget(IPath path) {
	if (path == null || path.getDevice() != null)
		return null;
	IWorkspace workspace = ResourcesPlugin.getWorkspace();
	if (workspace == null)
		return null;
	return workspace.getRoot().findMember(path);
}
 
Example 8
Source File: IndexBasedHierarchyBuilder.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
protected IBinaryType createInfoFromClassFileInJar(Openable classFile) {
	String filePath = (((ClassFile)classFile).getType().getFullyQualifiedName('$')).replace('.', '/') + SuffixConstants.SUFFIX_STRING_class;
	IPackageFragmentRoot root = classFile.getPackageFragmentRoot();
	IPath path = root.getPath();
	// take the OS path for external jars, and the forward slash path for internal jars
	String rootPath = path.getDevice() == null ? path.toString() : path.toOSString();
	String documentPath = rootPath + IJavaSearchScope.JAR_FILE_ENTRY_SEPARATOR + filePath;
	IBinaryType binaryType = (IBinaryType)this.binariesFromIndexMatches.get(documentPath);
	if (binaryType != null) {
		this.infoToHandle.put(binaryType, classFile);
		return binaryType;
	} else {
		return super.createInfoFromClassFileInJar(classFile);
	}
}
 
Example 9
Source File: AccessRuleEntryDialog.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
protected void checkIfPatternValid() {
	String pattern= fPatternDialog.getText().trim();
	if (pattern.length() == 0) {
		fPatternStatus.setError(NewWizardMessages.TypeRestrictionEntryDialog_error_empty);
		return;
	}
	IPath path= new Path(pattern);
	if (path.isAbsolute() || path.getDevice() != null) {
		fPatternStatus.setError(NewWizardMessages.TypeRestrictionEntryDialog_error_notrelative);
		return;
	}

	fPattern= pattern;
	fPatternStatus.setOK();
}
 
Example 10
Source File: SourceAttachmentBlock.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private boolean isWorkspacePath(IPath path) {
	if (path == null || path.getDevice() != null)
		return false;
	IWorkspace workspace= ResourcesPlugin.getWorkspace();
	if (workspace == null)
		return false;
	return workspace.getRoot().findMember(path) != null;
}
 
Example 11
Source File: JavaElementLabelComposer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void appendExternalArchiveLabel(IPackageFragmentRoot root, long flags) {
	IPath path;
	IClasspathEntry classpathEntry= null;
	try {
		classpathEntry= JavaModelUtil.getClasspathEntry(root);
		IPath rawPath= classpathEntry.getPath();
		if (classpathEntry.getEntryKind() != IClasspathEntry.CPE_CONTAINER && !rawPath.isAbsolute())
			path= rawPath;
		else
			path= root.getPath();
	} catch (JavaModelException e) {
		path= root.getPath();
	}
	if (getFlag(flags, JavaElementLabels.REFERENCED_ROOT_POST_QUALIFIED)) {
		int segements= path.segmentCount();
		if (segements > 0) {
			fBuffer.append(path.segment(segements - 1));
			int offset= fBuffer.length();
			if (segements > 1 || path.getDevice() != null) {
				fBuffer.append(JavaElementLabels.CONCAT_STRING);
				fBuffer.append(path.removeLastSegments(1).toOSString());
			}
			if (classpathEntry != null) {
				IClasspathEntry referencingEntry= classpathEntry.getReferencingEntry();
				if (referencingEntry != null) {
					fBuffer.append(Messages.format(JavaUIMessages.JavaElementLabels_onClassPathOf, new Object[] { Name.CLASS_PATH.toString(), referencingEntry.getPath().lastSegment() }));
				}
			}
			if (getFlag(flags, JavaElementLabels.COLORIZE)) {
				fBuffer.setStyle(offset, fBuffer.length() - offset, QUALIFIER_STYLE);
			}
		} else {
			fBuffer.append(path.toOSString());
		}
	} else {
		fBuffer.append(path.toOSString());
	}
}
 
Example 12
Source File: JavadocWriter.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private boolean hasSameDevice(IPath p1, IPath p2) {
	String dev= p1.getDevice();
	if (dev == null) {
		return p2.getDevice() == null;
	}
	return dev.equals(p2.getDevice());
}
 
Example 13
Source File: Index.java    From APICloud-Studio with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Index
 * 
 * @param containerURI
 * @param reuseExistingFile
 * @throws IOException
 */
protected Index(URI containerURI, boolean reuseExistingFile) throws IOException
{
	this.containerURI = containerURI;

	this.memoryIndex = new MemoryIndex();
	this.monitor = new ReentrantReadWriteLock();

	// Convert to a filename we can use for the actual index on disk
	IPath diskIndexPath = computeIndexLocation(containerURI);
	if (diskIndexPath == null)
	{
		return;
	}
	String diskIndexPathString = (diskIndexPath.getDevice() == null) ? diskIndexPath.toString() : diskIndexPath
			.toOSString();
	this.enterWrite();
	try
	{
		this.diskIndex = new DiskIndex(diskIndexPathString);
		this.diskIndex.initialize(reuseExistingFile);
	}
	finally
	{
		this.exitWrite();
	}
}
 
Example 14
Source File: JavaProject.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private boolean driveExists(IPath sourcePath, Map knownDrives) {	
	String drive = sourcePath.getDevice();
	if (drive == null) return true;
	Boolean good = (Boolean)knownDrives.get(drive);
	if (good == null) {
		if (new File(drive).exists()) {
			knownDrives.put(drive, Boolean.TRUE);
			return true;
		} else {
			knownDrives.put(drive, Boolean.FALSE);
			return false;
		}
	}
	return good.booleanValue();
}
 
Example 15
Source File: ResourceUtils.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns a resource for the given absolute or workspace-relative path.
 * <p>
 * If the path has a device (e.g. c:\ on Windows), it will be tried as an
 * absolute path. Otherwise, it is first tried as a workspace-relative path,
 * and failing that an absolute path.
 *
 * @param path the absolute or workspace-relative path to a resource
 * @return the resource, or null
 */
// TODO(tparker): Check against the internal implementation, which was tricky to get right.
public static IResource getResource(IPath path) {
  IResource res = null;
  if (path != null) {
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    if (path.getDevice() == null) {
      // try searching relative to the workspace first
      res = root.findMember(path);
    }

    if (res == null) {
      // look for files
      IFile[] files = root.findFilesForLocation(path);
      // Check for accessibility since for directories, the above will return
      // a non-accessible IFile
      if (files.length > 0 && files[0].isAccessible()) {
        res = files[0];
      }
    }

    if (res == null) {
      // look for folders
      IContainer[] containers = root.findContainersForLocation(path);
      if (containers.length > 0) {
        res = containers[0];
      }
    }
  }
  return res;
}
 
Example 16
Source File: ImportTraceWizardPage.java    From tracecompass with Eclipse Public License 2.0 5 votes vote down vote up
private static String getSourceDirectoryName(String sourceName) {
    IPath result = new Path(sourceName.trim());
    if (result.getDevice() != null && result.segmentCount() == 0) {
        result = result.addTrailingSeparator();
    } else {
        result = result.removeTrailingSeparator();
    }
    return result.toOSString();
}
 
Example 17
Source File: ProjectUtils.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public static IPath resolveGlobPath(IPath base, String glob) {
	IPath pattern = new org.eclipse.core.runtime.Path(glob);
	if (!pattern.isAbsolute()) { // Append cwd to relative path
		pattern = base.append(pattern);
	}
	if (pattern.getDevice() != null) { // VS Code only matches lower-case device
		pattern = pattern.setDevice(pattern.getDevice().toLowerCase());
	}
	return pattern;
}
 
Example 18
Source File: JavaElementLabelComposer.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private void appendExternalArchiveLabel(IPackageFragmentRoot root, long flags) {
	IPath path;
	IClasspathEntry classpathEntry= null;
	try {
		classpathEntry= JavaModelUtil.getClasspathEntry(root);
		IPath rawPath= classpathEntry.getPath();
		if (classpathEntry.getEntryKind() != IClasspathEntry.CPE_CONTAINER && !rawPath.isAbsolute()) {
			path= rawPath;
		} else {
			path= root.getPath();
		}
	} catch (JavaModelException e) {
		path= root.getPath();
	}
	if (getFlag(flags, JavaElementLabels.REFERENCED_ROOT_POST_QUALIFIED)) {
		int segments= path.segmentCount();
		if (segments > 0) {
			fBuilder.append(path.segment(segments - 1));
			if (segments > 1 || path.getDevice() != null) {
				fBuilder.append(JavaElementLabels.CONCAT_STRING);
				fBuilder.append(path.removeLastSegments(1).toOSString());
			}
			if (classpathEntry != null) {
				IClasspathEntry referencingEntry= classpathEntry.getReferencingEntry();
				if (referencingEntry != null) {
					fBuilder.append(NLS.bind(" (from {0} of {1})", new Object[] { Name.CLASS_PATH.toString(), referencingEntry.getPath().lastSegment() }));
				}
			}
		} else {
			fBuilder.append(path.toOSString());
		}
	} else {
		fBuilder.append(path.toOSString());
	}
}
 
Example 19
Source File: InvisibleProjectBuildSupport.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public boolean matchPattern(IPath base, String pattern, String path) {
	String glob = ProjectUtils.resolveGlobPath(base, pattern).toOSString();
	if (base.getDevice() != null) {
		return SelectorUtils.matchPath(glob, path, false); // Case insensitive match in Windows
	} else {
		return SelectorUtils.matchPath(glob, path); // Case sensitive match in *nix
	}
}
 
Example 20
Source File: JavaProject.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * @param path IPath
 * @return A handle to the package fragment root identified by the given path.
 * This method is handle-only and the element may or may not exist. Returns
 * <code>null</code> if unable to generate a handle from the path (for example,
 * an absolute path that has less than 1 segment. The path may be relative or
 * absolute.
 */
public IPackageFragmentRoot getPackageFragmentRoot(IPath path) {
	if (!path.isAbsolute()) {
		path = getPath().append(path);
	}
	int segmentCount = path.segmentCount();
	if (segmentCount == 0) {
		return null;
	}
	if (path.getDevice() != null || JavaModel.getExternalTarget(path, true/*check existence*/) != null) {
		// external path
		return getPackageFragmentRoot0(path);
	}
	IWorkspaceRoot workspaceRoot = this.project.getWorkspace().getRoot();
	IResource resource = workspaceRoot.findMember(path);
	if (resource == null) {
		// resource doesn't exist in workspace
		if (path.getFileExtension() != null) {
			if (!workspaceRoot.getProject(path.segment(0)).exists()) {
				// assume it is an external ZIP archive
				return getPackageFragmentRoot0(path);
			} else {
				// assume it is an internal ZIP archive
				resource = workspaceRoot.getFile(path);
			}
		} else if (segmentCount == 1) {
			// assume it is a project
			String projectName = path.segment(0);
			if (getElementName().equals(projectName)) { // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=75814
				// default root
				resource = this.project;
			} else {
				// lib being another project
				resource = workspaceRoot.getProject(projectName);
			}
		} else {
			// assume it is an internal folder
			resource = workspaceRoot.getFolder(path);
		}
	}
	return getPackageFragmentRoot(resource);
}