Java Path Relative makeRelativePathToIncludes(IPath fullPath, String[] includePaths)

Here you can find the source of makeRelativePathToIncludes(IPath fullPath, String[] includePaths)

Description

make Relative Path To Includes

License

Open Source License

Declaration

public static IPath makeRelativePathToIncludes(IPath fullPath,
            String[] includePaths) 

Method Source Code

//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:/*  ww  w .j av  a 2 s.c o  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.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 makeRelativePathToIncludes(IPath fullPath,
            String[] includePaths) {
        IPath relativePath = null;
        int mostSegments = 0;
        for (int i = 0; i < includePaths.length; ++i) {
            IPath includePath = new Path(includePaths[i]);
            if (isPrefix(includePath, fullPath)) {
                int segments = includePath.segmentCount();
                if (segments > mostSegments) {
                    relativePath = fullPath.removeFirstSegments(segments)
                            .setDevice(null);
                    mostSegments = segments;
                }
            }
        }
        return relativePath;
    }

    /**
     * Checks whether path1 is a prefix of path2. To be a prefix, path1's segments
     * must appear in path1 in the same order, and their device ids must match.
     * <p>
     * An empty path is a prefix of all paths with the same device; a root path is a prefix of
     * all absolute paths with the same device.
     * <p>
     * Similar to IPath.isPrefixOf(IPath anotherPath), but takes case sensitivity of the file system
     * into account.
     *
     * @return {@code true} if path1 is a prefix of path2, and {@code false} otherwise
     * @since 5.1
     */
    public static boolean isPrefix(IPath path1, IPath path2) {
        if (path1.getDevice() == null) {
            if (path2.getDevice() != null) {
                return false;
            }
        } else {
            if (!path1.getDevice().equalsIgnoreCase(path2.getDevice())) {
                return false;
            }
        }
        if (path1.isEmpty() || (path1.isRoot() && path2.isAbsolute())) {
            return true;
        }
        int len1 = path1.segmentCount();
        if (len1 > path2.segmentCount()) {
            return false;
        }
        boolean caseSensitive = !isWindowsFileSystem();
        for (int i = 0; i < len1; i++) {
            if (!(caseSensitive ? path1.segment(i).equals(path2.segment(i))
                    : path1.segment(i).equalsIgnoreCase(path2.segment(i)))) {
                return false;
            }
        }
        return true;
    }

    public static boolean isWindowsFileSystem() {
        return WINDOWS;
    }
}

Related

  1. makeRelativePath(File from, File to)
  2. makeRelativePath(File parent, File file)
  3. makeRelativePath(IPath path, IPath relativeTo)
  4. makeRelativePathAbsolute(String relativePath)
  5. makeRelativePaths(String classDir, List files)