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

The following examples show how to use org.eclipse.core.runtime.IPath#isAbsolute() . 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: FixedFatJarExportPage.java    From sarl with 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 2
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 3
Source File: Arglets.java    From cmake4eclipse with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * @param cwd
 *          the current working directory of the compiler at its invocation
 * @see de.marw.cmake.cdt.lsp.IArglet#processArgument(IParseContext, IPath, String)
 */
protected final int processArgument(IParseContext parseContext, IPath cwd,
    String argsLine, NameOptionMatcher[] optionMatchers) {
  for (NameOptionMatcher oMatcher : optionMatchers) {
    final Matcher matcher = oMatcher.matcher;

    matcher.reset(argsLine);
    if (matcher.lookingAt()) {
      String name = matcher.group(oMatcher.nameGroup);
      IPath path = Path.fromOSString(name);
      if (!path.isAbsolute()) {
        // prepend CWD
        name = cwd.append(path).toOSString();
      }

      final ICLanguageSettingEntry entry = CDataUtil.createCMacroFileEntry(name,
          ICSettingEntry.READONLY);
      parseContext.addSettingEntry(entry);
      final int end = matcher.end();
      return end;
    }
  }
  return 0;// no input consumed
}
 
Example 4
Source File: Arglets.java    From cmake4eclipse with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * @param cwd
 *          the current working directory of the compiler at its invocation
 * @see de.marw.cmake.cdt.lsp.IArglet#processArgument(IParseContext, IPath, String)
 */
protected final int processArgument(IParseContext parseContext, IPath cwd,
    String argsLine, NameOptionMatcher[] optionMatchers) {
  for (NameOptionMatcher oMatcher : optionMatchers) {
    final Matcher matcher = oMatcher.matcher;

    matcher.reset(argsLine);
    if (matcher.lookingAt()) {
      String name = matcher.group(oMatcher.nameGroup);
      IPath path = Path.fromOSString(name);
      if (!path.isAbsolute()) {
        // prepend CWD
        name = cwd.append(path).toOSString();
      }

      final ICLanguageSettingEntry entry = CDataUtil.createCIncludeFileEntry(name,
          ICSettingEntry.READONLY);
      parseContext.addSettingEntry(entry);
      final int end = matcher.end();
      return end;
    }
  }
  return 0;// no input consumed
}
 
Example 5
Source File: Arglets.java    From cmake4eclipse with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * @param cwd
 *          the current working directory of the compiler at its invocation
 * @see de.marw.cmake.cdt.lsp.IArglet#processArgument(IParseContext, IPath, String)
 */
protected final int processArgument(IParseContext parseContext, IPath cwd,
    String argsLine, NameOptionMatcher[] optionMatchers) {
  for (NameOptionMatcher oMatcher : optionMatchers) {
    final Matcher matcher = oMatcher.matcher;

    matcher.reset(argsLine);
    if (matcher.lookingAt()) {
      String name = matcher.group(oMatcher.nameGroup);
      // workaround for relative path by cmake bug
      // https://gitlab.kitware.com/cmake/cmake/issues/13894 : prepend cwd
      IPath path = Path.fromOSString(name);
      if (!path.isAbsolute()) {
        // prepend CWD
        name = cwd.append(path).toOSString();
      }

      final ICLanguageSettingEntry entry = CDataUtil.createCIncludePathEntry(name,
          ICSettingEntry.READONLY);
      parseContext.addSettingEntry(entry);
      final int end = matcher.end();
      return end;
    }
  }
  return 0;// no input consumed
}
 
Example 6
Source File: PreferenceStoreHelper.java    From tlaplus with MIT License 6 votes vote down vote up
/**
    * Retrieves project root file name
    * @param project
    * @return
    */
public static IFile readProjectRootFile(IProject project) {
	final IEclipsePreferences projectPrefs = getProjectPreferences(project);
	if (projectPrefs != null) {
		String rootFileName = projectPrefs.get(IPreferenceConstants.P_PROJECT_ROOT_FILE,
				IPreferenceConstants.DEFAULT_NOT_SET);
		if (!IPreferenceConstants.DEFAULT_NOT_SET.equals(rootFileName)) {
			final IPath path = new Path(rootFileName);
			if (path.isAbsolute()) {
				// Convert a legacy (absolute) path to the new relative one
				// with the magic PARENT_PROJECT_LOC prefix.
				rootFileName = ResourceHelper.PARENT_ONE_PROJECT_LOC + path.lastSegment(); 
				convertAbsoluteToRelative(projectPrefs, rootFileName);
			}
			return ResourceHelper.getLinkedFile(project, rootFileName);
		}
	} else {
		Activator.getDefault().logInfo("projectPrefs is null");
	}
	return null;
}
 
Example 7
Source File: ManifestBasedSREInstall.java    From sarl with Apache License 2.0 6 votes vote down vote up
/** Path the given string for extracting a path.
 *
 * @param path the string representation of the path to parse.
 * @param defaultPath the default path.
 * @param rootPath the root path to use is the given path is not absolute.
 * @return the absolute path.
 */
private static IPath parsePath(String path, IPath defaultPath, IPath rootPath) {
	if (!Strings.isNullOrEmpty(path)) {
		try {
			final IPath pathObject = Path.fromPortableString(path);
			if (pathObject != null) {
				if (rootPath != null && !pathObject.isAbsolute()) {
					return rootPath.append(pathObject);
				}
				return pathObject;
			}
		} catch (Throwable exception) {
			//
		}
	}
	return defaultPath;
}
 
Example 8
Source File: PostGitCloneRequest.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
PostGitCloneRequest setFilePath(IPath filePath) throws JSONException {
	if (filePath != null && filePath.isAbsolute()) {
		//			assertEquals("file", filePath.segment(0));
		//			assertTrue(filePath.segmentCount() > 1);
		body.put(ProtocolConstants.KEY_PATH, filePath);
	}
	return this;
}
 
Example 9
Source File: ResponseFileArglets.java    From cmake4eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public int process(IParserHandler parserHandler, String argsLine) {
  for (NameOptionMatcher oMatcher : optionMatchers) {
    final Matcher matcher = oMatcher.matcher;

    matcher.reset(argsLine);
    if (matcher.lookingAt()) {
      String fname = matcher.group(oMatcher.nameGroup);
      final int consumed = matcher.end();
      if("<<".equals(fname)) {
        // see https://github.com/15knots/cmake4eclipse/issues/94
        // Handle '@<< compiler-args <<' syntax: The file '<<' does not exist, arguments come from argsline.
        // so we just do not open the non-existing file
        return consumed;
      }

      IPath path = Path.fromOSString(fname);
      if (!path.isAbsolute()) {
        // relative path, prepend CWD
        fname = parserHandler.getCompilerWorkingDirectory().append(path).toOSString();
      }

      // parse file
      java.nio.file.Path fpath = Paths.get(fname);
      try {
        String args2 = new String(Files.readAllBytes(fpath));
        parserHandler.parseArguments(args2);
      } catch (IOException e) {
        // swallow exception for now
        e.printStackTrace();
      }
      return consumed;
    }
  }
  return 0;// no input consumed
}
 
Example 10
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 11
Source File: ProcessDiagramEditorPlugin.java    From bonita-studio with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Respects images residing in any plug-in. If path is relative,
 * then this bundle is looked up for the image, otherwise, for absolute 
 * path, first segment is taken as id of plug-in with image
 *
 * @generated
 * @param path the path to image, either absolute (with plug-in id as first segment), or relative for bundled images
 * @return the image descriptor
 */
public static ImageDescriptor findImageDescriptor(String path) {
	final IPath p = new Path(path);
	if (p.isAbsolute() && p.segmentCount() > 1) {
		return AbstractUIPlugin.imageDescriptorFromPlugin(p.segment(0),
				p.removeFirstSegments(1).makeAbsolute().toString());
	} else {
		return getBundledImageDescriptor(p.makeAbsolute().toString());
	}
}
 
Example 12
Source File: RelativeFileFieldSetter.java    From google-cloud-eclipse with Apache License 2.0 5 votes vote down vote up
@Override
protected IPath preProcessInputPath(IPath inputPath) {
  if (!inputPath.isAbsolute()) {
    inputPath = basePath.append(inputPath);
  }
  return inputPath;
}
 
Example 13
Source File: FlexDeployCommandHandler.java    From google-cloud-eclipse with Apache License 2.0 5 votes vote down vote up
protected static IPath resolveFile(String path, IPath baseDirectory) throws CoreException {
  IPath fullPath = new Path(path);
  if (!fullPath.isAbsolute()) {
    fullPath = baseDirectory.append(path);
  }

  if (!fullPath.toFile().exists()) {
    throw new CoreException(
        StatusUtil.error(FlexDeployCommandHandler.class, fullPath + " does not exist."));
  }
  return fullPath;
}
 
Example 14
Source File: PathUtil.java    From google-cloud-eclipse with Apache License 2.0 5 votes vote down vote up
/**
 * Makes a relative path absolute based off of another path. If the initial path is not relative, than it will be
 * returned without changes.
 *
 * @param path the relative path
 * @param basePath the base path to which the relative path is appended 
 * @return the path appended to the base path if the path is relative, otherwise the original path
 */
public static IPath makePathAbsolute(IPath path, IPath basePath) {
  if (path == null) {
    return basePath;
  }
  if (path.isAbsolute() || basePath == null) {
    return path;
  } else {
    return basePath.append(path);
  }
}
 
Example 15
Source File: SelectLanguageConfigurationWizardPage.java    From tm4e with Eclipse Public License 1.0 5 votes vote down vote up
public ILanguageConfigurationDefinition getDefinition() {
	IPath p = new Path(fileText.getText());
	if (!p.isAbsolute()) {
		p = ResourcesPlugin.getWorkspace().getRoot().getFile(p).getLocation();
	}
	return new LanguageConfigurationDefinition(ContentTypeHelper.getContentTypeById(contentTypeText.getText()),
			p.toString());
}
 
Example 16
Source File: GitCloneTest.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Get list of clones for the given workspace or path. When <code>path</code> is
 * not <code>null</code> the result is narrowed to clones under the <code>path</code>.
 *
 * @param workspaceId the workspace ID. Must be null if path is provided.
 * @param path path under the workspace starting with project ID. Must be null if workspaceId is provided.
 * @return the request
 */
static WebRequest listGitClonesRequest(String workspaceId, IPath path) {
	assertTrue(workspaceId == null && path != null || workspaceId != null && path == null);
	String requestURI = SERVER_LOCATION + GIT_SERVLET_LOCATION + Clone.RESOURCE + '/';
	if (workspaceId != null) {
		requestURI += "workspace/" + workspaceId;
	} else if (path != null) {
		requestURI += "path" + (path.isAbsolute() ? path : path.makeAbsolute());
	}
	WebRequest request = new GetMethodWebRequest(requestURI);
	request.setHeaderField(ProtocolConstants.HEADER_ORION_VERSION, "1");
	setAuthentication(request);
	return request;
}
 
Example 17
Source File: LatexRunner.java    From texlipse with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Adds a problem marker
 * 
 * @param error The error or warning string
 * @param causingSourceFile name of the sourcefile
 * @param linenr where the error occurs
 * @param severity
 * @param resource
 * @param layout true, if this is a layout warning
 */
private void addProblemMarker(String error, String causingSourceFile,
        int linenr, int severity, IResource resource, boolean layout) {
    
    
	IProject project = resource.getProject();
    IContainer sourceDir = TexlipseProperties.getProjectSourceDir(project);
    
    IResource extResource = null;
    if (causingSourceFile != null) {
    	IPath p = new Path(causingSourceFile);
    	
    	if (p.isAbsolute()) {
    		//Make absolute path relative to source directory
    		//or to the directory of the resource
    		if (sourceDir.getLocation().isPrefixOf(p)) {
    			p = p.makeRelativeTo(sourceDir.getLocation());
    		}
    		else if (resource.getParent().getLocation().isPrefixOf(p)) {
    			p = p.makeRelativeTo(resource.getParent().getLocation());
    		}
    	}

    	extResource = sourceDir.findMember(p);
        if (extResource == null) {
            extResource = resource.getParent().findMember(p);
        }
    }
    if (extResource == null)
        createMarker(resource, null, error + (causingSourceFile != null ? " (Occurance: "
                + causingSourceFile + ")" : ""), severity);
    else {
        if (linenr >= 0) {
            if (layout)
                createLayoutMarker(extResource, new Integer(linenr), error);
            else
                createMarker(extResource, new Integer(linenr), error, severity);
        } else
            createMarker(extResource, null, error, severity);
    }
}
 
Example 18
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);
}
 
Example 19
Source File: BaseConnectionFileManager.java    From APICloud-Studio with GNU General Public License v3.0 4 votes vote down vote up
private ExtendedFileInfo resolveSymlink(IPath dirPath, String linkTarget, int options, IProgressMonitor monitor)
		throws CoreException, FileNotFoundException
{
	Set<IPath> visited = new HashSet<IPath>();
	visited.add(dirPath);
	while (linkTarget != null && linkTarget.length() > 0)
	{
		IPath targetPath = Path.fromPortableString(linkTarget);
		if (!targetPath.isAbsolute())
		{
			targetPath = dirPath.append(targetPath);
		}
		if (visited.contains(targetPath))
		{
			break;
		}
		visited.add(targetPath);
		ExtendedFileInfo targetFileInfo = getCachedFileInfo(targetPath);
		if (targetFileInfo == null)
		{
			Policy.checkCanceled(monitor);
			try
			{
				targetFileInfo = cache(targetPath,
						fetchFileInternal(targetPath, options, Policy.subMonitorFor(monitor, 1)));
			}
			catch (PermissionDeniedException e)
			{
				// permission denied is like file not found for the case of symlink resolving
				throw initFileNotFoundException(targetPath, e);
			}
		}
		cache(targetPath, targetFileInfo);
		if (targetFileInfo.getAttribute(EFS.ATTRIBUTE_SYMLINK))
		{
			linkTarget = targetFileInfo.getStringAttribute(EFS.ATTRIBUTE_LINK_TARGET);
			dirPath = targetPath.removeLastSegments(1);
			continue;
		}
		return targetFileInfo;
	}
	return new ExtendedFileInfo();
}
 
Example 20
Source File: JavaRefactoringDescriptorUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 3 votes vote down vote up
/**
 * Converts an input handle with the given prefix back to the corresponding
 * resource.
 *
 * @param project
 *            the project, or <code>null</code> for the workspace
 * @param handle
 *            the input handle
 *
 * @return the corresponding resource, or <code>null</code> if no such
 *         resource exists.
 *         Note: if the given handle is absolute, the project is not used to resolve.
 */
public static IResource handleToResource(final String project, final String handle) {
	final IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot();
	if ("".equals(handle)) //$NON-NLS-1$
		return null;
	final IPath path= Path.fromPortableString(handle);
	if (path == null)
		return null;
	if (project != null && !"".equals(project) && ! path.isAbsolute()) //$NON-NLS-1$
		return root.getProject(project).findMember(path);
	return root.findMember(path);
}