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

The following examples show how to use org.eclipse.core.runtime.IPath#matchingFirstSegments() . 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: CheckConfigurationFactory.java    From eclipse-cs with GNU Lesser General Public License v2.1 6 votes vote down vote up
private static IPath getTargetStateLocation(IPath newWorkspaceRoot) {

    IPath currentWorkspaceRoot = Platform.getLocation();
    IPath currentStateLocation = CheckstylePlugin.getDefault().getStateLocation();

    if (currentStateLocation == null) {
      return null;
    }
    int segmentsToRemove = currentStateLocation.matchingFirstSegments(currentWorkspaceRoot);

    // Strip it down to the extension
    currentStateLocation = currentStateLocation.removeFirstSegments(segmentsToRemove);

    // Now add to the target workspace root
    IPath targetStateLocation = newWorkspaceRoot.append(currentStateLocation);
    return targetStateLocation;

  }
 
Example 2
Source File: ViewerManager.java    From texlipse with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Resolves a relative path from one directory to another.
 * The path is returned as an OS-specific string with
 * a terminating separator.
 * 
 * @param sourcePath a directory to start from 
 * @param outputPath a directory to end up to
 * @return a relative path from sourcePath to outputPath
 */
public static String resolveRelativePath(IPath sourcePath, IPath outputPath) {

    int same = sourcePath.matchingFirstSegments(outputPath);
    if (same == sourcePath.segmentCount()
            && same == outputPath.segmentCount()) {
        return "";
    }
        
    outputPath = outputPath.removeFirstSegments(same);
    sourcePath = sourcePath.removeFirstSegments(same);
    
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < sourcePath.segmentCount(); i++) {
        sb.append("..");
        sb.append(File.separatorChar);
    }
    
    for (int i = 0; i < outputPath.segmentCount(); i++) {
        sb.append(outputPath.segment(i));
        sb.append(File.separatorChar);
    }
    return sb.toString();
}
 
Example 3
Source File: WorkItem.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
private static void addClassesForFile(IFile file, Map<IPath, IPath> outLocations, Project fbProject) {
    IPath path = file.getLocation();
    IPath srcRoot = getMatchingSourceRoot(path, outLocations);
    if (srcRoot == null) {
        return;
    }
    IPath outputRoot = outLocations.get(srcRoot);
    int firstSegments = path.matchingFirstSegments(srcRoot);
    // add relative path to the output path
    IPath out = outputRoot.append(path.removeFirstSegments(firstSegments));
    String fileName = path.removeFileExtension().lastSegment();
    String namePattern = fileName + "\\.class|" + fileName + "\\$.*\\.class";
    namePattern = addSecondaryTypesToPattern(file, fileName, namePattern);
    File directory = out.removeLastSegments(1).toFile();

    // add parent folder and regexp for file names
    Pattern classNamesPattern = Pattern.compile(namePattern);
    ResourceUtils.addFiles(fbProject, directory, classNamesPattern);
}
 
Example 4
Source File: WorkItem.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * @param srcPath may be null
 * @param outLocations
 *            key is the source root, value is output folder
 * @return source root folder matching (parent of) given path, or null
 */
private static @Nullable IPath getMatchingSourceRoot(@Nullable IPath srcPath, Map<IPath, IPath> outLocations) {
    if (srcPath == null) {
        return null;
    }
    Set<Entry<IPath, IPath>> outEntries = outLocations.entrySet();
    IPath result = null;
    int maxSegments = 0;
    for (Entry<IPath, IPath> entry : outEntries) {
        IPath srcRoot = entry.getKey();
        int firstSegments = srcPath.matchingFirstSegments(srcRoot);
        if (firstSegments > maxSegments && firstSegments == srcRoot.segmentCount()) {
            maxSegments = firstSegments;
            result = srcRoot;
        }
    }
    return result;
}
 
Example 5
Source File: NewHostPageWizard.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 6 votes vote down vote up
private static String getRelativeUrl(IPath fromFile, IPath refFile) {
  IPath fromContainer = fromFile.removeLastSegments(1);
  IPath refFileContainer = refFile.removeLastSegments(1);

  // Find the paths' common ancestor
  IPath commonAncestor = new Path("");
  int matchingSegments = fromContainer.matchingFirstSegments(refFileContainer);
  for (int i = 0; i < matchingSegments; i++) {
    commonAncestor = commonAncestor.append(fromContainer.segment(i));
  }

  // Walk up until we get to the common ancestor
  StringBuilder sb = new StringBuilder();
  int fromLevelsFromCommon = fromContainer.segmentCount() - matchingSegments;
  for (int i = 0; i < fromLevelsFromCommon; i++) {
    sb.append("../");
  }

  // Now add on the path to the referenced file, relative to the ancestor
  IPath refFilePathRelativeToCommon = getPathRelativeToAncestor(refFile,
      commonAncestor);
  sb.append(refFilePathRelativeToCommon.toString());

  return sb.toString();
}
 
Example 6
Source File: WizardFileSystemResourceExportPage2.java    From translationstudio8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the name of a container with a location that encompasses targetDirectory. Returns null if there is no
 * conflict.
 * @param targetDirectory
 *            the path of the directory to check.
 * @return the conflicting container name or <code>null</code>
 */
protected String getConflictingContainerNameFor(String targetDirectory) {

	IPath rootPath = ResourcesPlugin.getWorkspace().getRoot().getLocation();
	IPath testPath = new Path(targetDirectory);
	// cannot export into workspace root
	if (testPath.equals(rootPath))
		return rootPath.lastSegment();

	// Are they the same?
	if (testPath.matchingFirstSegments(rootPath) == rootPath.segmentCount()) {
		String firstSegment = testPath.removeFirstSegments(rootPath.segmentCount()).segment(0);
		if (!Character.isLetterOrDigit(firstSegment.charAt(0)))
			return firstSegment;
	}

	return null;

}
 
Example 7
Source File: NewGraniteProjectWizard.java    From aem-eclipse-developer-tools with Apache License 2.0 6 votes vote down vote up
private String calculateRelativePath(IProject from, IProject to) {
	IPath fromPath = from.getRawLocation();
	IPath toPath = to.getRawLocation();
	toPath.setDevice(null);
	fromPath.setDevice(null);
	int ssc = fromPath.matchingFirstSegments(toPath);
	fromPath = fromPath.removeFirstSegments(ssc);
	toPath = toPath.removeFirstSegments(ssc);
	StringBuffer relPath = new StringBuffer();
	for(int i=0; i<fromPath.segmentCount(); i++) {
		if (relPath.length()!=0) {
			relPath.append("/");
		}
		relPath.append("..");
	}
	if (relPath.length()!=0) {
		relPath.append("/");
	}
	relPath.append(toPath.toString());
	return relPath.toString();
}
 
Example 8
Source File: WizardFileSystemResourceExportPage2.java    From tmxeditor8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the name of a container with a location that encompasses targetDirectory. Returns null if there is no
 * conflict.
 * @param targetDirectory
 *            the path of the directory to check.
 * @return the conflicting container name or <code>null</code>
 */
protected String getConflictingContainerNameFor(String targetDirectory) {

	IPath rootPath = ResourcesPlugin.getWorkspace().getRoot().getLocation();
	IPath testPath = new Path(targetDirectory);
	// cannot export into workspace root
	if (testPath.equals(rootPath))
		return rootPath.lastSegment();

	// Are they the same?
	if (testPath.matchingFirstSegments(rootPath) == rootPath.segmentCount()) {
		String firstSegment = testPath.removeFirstSegments(rootPath.segmentCount()).segment(0);
		if (!Character.isLetterOrDigit(firstSegment.charAt(0)))
			return firstSegment;
	}

	return null;

}
 
Example 9
Source File: WorkItem.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * @return false if no classes was added
 */
private static boolean addClassesForFolder(IFolder folder, Map<IPath, IPath> outLocations, Project fbProject) {
    IPath path = folder.getLocation();
    IPath srcRoot = getMatchingSourceRoot(path, outLocations);
    if (srcRoot == null) {
        return false;
    }
    IPath outputRoot = outLocations.get(srcRoot);
    int firstSegments = path.matchingFirstSegments(srcRoot);
    // add relative path to the output path
    IPath out = outputRoot.append(path.removeFirstSegments(firstSegments));
    File directory = out.toFile();
    return fbProject.addFile(directory.getAbsolutePath());
    // TODO child directories too. Should add preference???
}
 
Example 10
Source File: AbstractJarDestinationWizardPage.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
@Override
protected boolean validateDestinationGroup() {
	if (fDestinationNamesCombo.getText().length() == 0) {
		// Clear error
		if (getErrorMessage() != null)
			setErrorMessage(null);
		if (getMessage() != null)
			setMessage(null);
		return false;
	}
	if (fJarPackage.getAbsoluteJarLocation().toString().endsWith("/")) { //$NON-NLS-1$
		setErrorMessage(JarPackagerMessages.JarPackageWizardPage_error_exportDestinationMustNotBeDirectory);
		fDestinationNamesCombo.setFocus();
		return false;
	}
	// Check if the Jar is put into the workspace and conflicts with the containers
	// exported. If the workspace isn't on the local files system we are fine since
	// the Jar is always created in the local file system
	IPath workspaceLocation= ResourcesPlugin.getWorkspace().getRoot().getLocation();
	if (workspaceLocation != null && workspaceLocation.isPrefixOf(fJarPackage.getAbsoluteJarLocation())) {
		int segments= workspaceLocation.matchingFirstSegments(fJarPackage.getAbsoluteJarLocation());
		IPath path= fJarPackage.getAbsoluteJarLocation().removeFirstSegments(segments);
		IResource resource= ResourcesPlugin.getWorkspace().getRoot().findMember(path);
		if (resource != null && resource.getType() == IResource.FILE) {
			// test if included
			if (JarPackagerUtil.contains(JarPackagerUtil.asResources(fJarPackage.getElements()), (IFile) resource)) {
				setErrorMessage(JarPackagerMessages.JarPackageWizardPage_error_cantExportJARIntoItself);
				return false;
			}
		}
	}
	// Inform user about relative directory
	String currentMessage= getMessage();
	if (!(Path.fromOSString(fDestinationNamesCombo.getText()).isAbsolute())) {
		if (currentMessage == null)
			setMessage(JarPackagerMessages.JarPackageWizardPage_info_relativeExportDestination, IMessageProvider.INFORMATION);
	} else {
		if (currentMessage != null)
			setMessage(null);
	}
	return ensureTargetFileIsValid(fJarPackage.getAbsoluteJarLocation().toFile());
}