Java Path Relative Get getRelativePath(String targetPath, String basePath, String pathSeparator)

Here you can find the source of getRelativePath(String targetPath, String basePath, String pathSeparator)

Description

get Relative Path

License

Apache License

Declaration

public static String getRelativePath(String targetPath, String basePath, String pathSeparator) 

Method Source Code


//package com.java2s;
/*//from  w w  w . jav a2s. com
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.    
 */

import java.io.File;

import java.util.regex.Pattern;

public class Main {
    public static String getRelativePath(String targetPath, String basePath, String pathSeparator) {

        File f = new File(targetPath);
        boolean isDir = f.isDirectory();
        //  We need the -1 argument to split to make sure we get a trailing 
        //  "" token if the base ends in the path separator and is therefore
        //  a directory. We require directory paths to end in the path
        //  separator -- otherwise they are indistinguishable from files.
        String[] base = basePath.split(Pattern.quote(pathSeparator), -1);
        String[] target = targetPath.split(Pattern.quote(pathSeparator), 0);

        //  First get all the common elements. Store them as a string,
        //  and also count how many of them there are. 
        String common = "";
        int commonIndex = 0;
        for (int i = 0; i < target.length && i < base.length; i++) {
            if (target[i].equals(base[i])) {
                common += target[i] + pathSeparator;
                commonIndex++;
            } else
                break;
        }

        if (commonIndex == 0) {
            //  not even a single common path element. This most
            //  likely indicates differing drive letters, like C: and D:. 
            //  These paths cannot be relativized. Return the target path.
            return targetPath;
        }

        String relative = "";
        if (base.length == commonIndex) {
            //  Comment this out if you prefer that a relative path not start with ./
            relative = "." + pathSeparator;
        } else {
            int numDirsUp = base.length - commonIndex - (isDir ? 0 : 1); /* only subtract 1 if it  is a file. */
            //  The number of directories we have to backtrack is the length of 
            //  the base path MINUS the number of common path elements, minus
            //  one because the last element in the path isn't a directory.
            for (int i = 1; i <= (numDirsUp); i++) {
                relative += ".." + pathSeparator;
            }
        }
        //if we are comparing directories 
        if (targetPath.length() > common.length()) {
            //it's OK, it isn't a directory
            relative += targetPath.substring(common.length());
        }

        return relative;
    }
}

Related

  1. getRelativePath(String root, String path)
  2. getRelativePath(String rootpath, String path)
  3. getRelativePath(String source, String target)
  4. getRelativePath(String target, String base)
  5. getRelativePath(String targetPath, String basePath, String pathSeparator)
  6. getRelativePathOfZipEntry(final File fileToZip, final File base)
  7. getRelativePathRecursive(File file, File baseDir, String prefix)
  8. getRelativePathToBase(File path, File basePath)
  9. getRelativePathToTestModule()