Java Canonical Path Create getCanonicalSubPath(File inBasepathDir, File inFile)

Here you can find the source of getCanonicalSubPath(File inBasepathDir, File inFile)

Description

get Canonical Sub Path

License

Open Source License

Declaration

public static String getCanonicalSubPath(File inBasepathDir, File inFile) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.File;

public class Main {
    public static String getCanonicalSubPath(File inBasepathDir, File inFile) {
        return getCanonicalSubPath(inBasepathDir, inFile, false);
    }//from www.j a  v  a 2s . c  o  m

    /**
     * 
     * @param theBasepath
     * @param inFile
     * @return the path part which does not start with the basepath
     */
    public static String getCanonicalSubPath(File inBasepathDir, File inFile, boolean inQuiet) {

        inBasepathDir = getCanonicalPathQuietly(inBasepathDir);
        inFile = getCanonicalPathQuietly(inFile);
        if (!isDirectory(inBasepathDir) && inQuiet) {
            return null;
        } else if (!isDirectory(inBasepathDir)) {
            throw new IllegalArgumentException("basepath is no directory");
        } else if (inFile == null) {
            return null;
        } else if (!inFile.getAbsolutePath().startsWith(inBasepathDir.getAbsolutePath()) && inQuiet) {
            return null;
        } else if (!inFile.getAbsolutePath().startsWith(inBasepathDir.getAbsolutePath())) {
            throw new IllegalArgumentException("file is no sub file of basepath");
        }

        return inFile.getAbsolutePath().substring(inBasepathDir.getAbsolutePath().length());
    }

    public static File getCanonicalPathQuietly(String inPath) {

        return getCanonicalPathQuietly(inPath, false);
    }

    public static File getCanonicalPathQuietly(String inPath, boolean inReturnAbsolutPathInstead) {

        return inPath == null ? null : getCanonicalPathQuietly(new File(inPath), inReturnAbsolutPathInstead);
    }

    public static File getCanonicalPathQuietly(File inFile) {
        return getCanonicalPathQuietly(inFile, false);
    }

    public static File getCanonicalPathQuietly(File inFile, boolean inReturnAbsolutPathInstead) {

        try {

            return inFile.getCanonicalFile();

        } catch (final Exception e) {

            try {

                return inReturnAbsolutPathInstead ? inFile.getAbsoluteFile() : null;

            } catch (final Exception e2) {
            }
        }

        return null;
    }

    public static boolean isDirectory(String inPath) {

        return isFolder(inPath == null ? null : new File(inPath));
    }

    public static boolean isDirectory(File inPath) {

        return inPath != null && inPath.isDirectory();
    }

    /**
     * NPE safe {@link String} startsWith
     * 
     * @param inStart
     * @param inValue
     * @return
     */
    public static boolean startsWith(String inStart, String inValue) {

        if (inStart == null && inValue == null) {

            return true;

        } else if (inStart == null || inValue == null) {

            return false;
        }

        return inValue.startsWith(inStart);
    }

    public static boolean isFolder(String inPath) {

        return isFolder(inPath == null ? null : new File(inPath));
    }

    public static boolean isFolder(File inPath) {

        return inPath != null && inPath.isDirectory();
    }
}

Related

  1. getCanonicalPath(String s)
  2. getCanonicalPathIfPossible(java.io.File file)
  3. getCanonicalPathQuietly(String inPath)
  4. getCanonicalPaths(List filenames)
  5. getCanonicalPathWithoutFilename(String filename)