Here you can find the source of makeRelativePath(IPath path, IPath relativeTo)
public static IPath makeRelativePath(IPath path, IPath relativeTo)
//package com.java2s; /******************************************************************************* * Copyright (c) 2004, 2014 QNX Software Systems and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors://from ww w . ja va 2s.co m * QNX Software Systems - initial API and implementation * Markus Schorn (Wind River Systems) * Ed Swartz (Nokia) * Sergey Prigogin (Google) * James Blackburn (Broadcom Corp.) *******************************************************************************/ import org.eclipse.core.runtime.Assert; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.Path; public class Main { /** Detect whether we're running on Windows (as IPath does) */ private static final boolean WINDOWS = java.io.File.separatorChar == '\\'; public static IPath makeRelativePath(IPath path, IPath relativeTo) { int segments = matchingFirstSegments(relativeTo, path); if (segments > 0) { IPath prefix = relativeTo.removeFirstSegments(segments); IPath suffix = path.removeFirstSegments(segments); IPath relativePath = new Path(""); //$NON-NLS-1$ for (int i = 0; i < prefix.segmentCount(); ++i) { relativePath = relativePath.append(".." + IPath.SEPARATOR); //$NON-NLS-1$ } return relativePath.append(suffix); } return null; } /** * Returns the number of segments which match in path1 and path2 * (device ids are ignored), comparing in increasing segment number order. * <p> * Similar to IPath.matchingFirstSegments(IPath anotherPath), but takes case sensitivity * of the file system into account. * * @return the number of matching segments * @since 5.1 */ public static int matchingFirstSegments(IPath path1, IPath path2) { Assert.isNotNull(path1); Assert.isNotNull(path2); int len1 = path1.segmentCount(); int len2 = path2.segmentCount(); int max = Math.min(len1, len2); int count = 0; boolean caseSensitive = !isWindowsFileSystem(); for (int i = 0; i < max; i++) { if (!(caseSensitive ? path1.segment(i).equals(path2.segment(i)) : path1.segment(i).equalsIgnoreCase(path2.segment(i)))) { return count; } count++; } return count; } public static boolean isWindowsFileSystem() { return WINDOWS; } }