Example usage for java.io File getCanonicalFile

List of usage examples for java.io File getCanonicalFile

Introduction

In this page you can find the example usage for java.io File getCanonicalFile.

Prototype

public File getCanonicalFile() throws IOException 

Source Link

Document

Returns the canonical form of this abstract pathname.

Usage

From source file:org.apache.maven.plugin.javadoc.AbstractJavadocMojo.java

/**
 * Get the path of the Javadoc tool executable depending the user entry or try to find it depending the OS
 * or the <code>java.home</code> system property or the <code>JAVA_HOME</code> environment variable.
 *
 * @return the path of the Javadoc tool/*from  w  w w .ja  va  2  s  . c o  m*/
 * @throws IOException if not found
 */
private String getJavadocExecutable() throws IOException {
    Toolchain tc = getToolchain();

    if (tc != null) {
        getLog().info("Toolchain in javadoc-plugin: " + tc);
        if (javadocExecutable != null) {
            getLog().warn(
                    "Toolchains are ignored, 'javadocExecutable' parameter is set to " + javadocExecutable);
        } else {
            javadocExecutable = tc.findTool("javadoc");
        }
    }

    String javadocCommand = "javadoc" + (SystemUtils.IS_OS_WINDOWS ? ".exe" : "");

    File javadocExe;

    // ----------------------------------------------------------------------
    // The javadoc executable is defined by the user
    // ----------------------------------------------------------------------
    if (StringUtils.isNotEmpty(javadocExecutable)) {
        javadocExe = new File(javadocExecutable);

        if (javadocExe.isDirectory()) {
            javadocExe = new File(javadocExe, javadocCommand);
        }

        if (SystemUtils.IS_OS_WINDOWS && javadocExe.getName().indexOf('.') < 0) {
            javadocExe = new File(javadocExe.getPath() + ".exe");
        }

        if (!javadocExe.isFile()) {
            throw new IOException("The javadoc executable '" + javadocExe
                    + "' doesn't exist or is not a file. Verify the <javadocExecutable/> parameter.");
        }

        return javadocExe.getAbsolutePath();
    }

    // ----------------------------------------------------------------------
    // Try to find javadocExe from System.getProperty( "java.home" )
    // By default, System.getProperty( "java.home" ) = JRE_HOME and JRE_HOME
    // should be in the JDK_HOME
    // ----------------------------------------------------------------------
    // For IBM's JDK 1.2
    if (SystemUtils.IS_OS_AIX) {
        javadocExe = new File(SystemUtils.getJavaHome() + File.separator + ".." + File.separator + "sh",
                javadocCommand);
    } else if (SystemUtils.IS_OS_MAC_OSX) {
        javadocExe = new File(SystemUtils.getJavaHome() + File.separator + "bin", javadocCommand);
    } else {
        javadocExe = new File(SystemUtils.getJavaHome() + File.separator + ".." + File.separator + "bin",
                javadocCommand);
    }

    // ----------------------------------------------------------------------
    // Try to find javadocExe from JAVA_HOME environment variable
    // ----------------------------------------------------------------------
    if (!javadocExe.exists() || !javadocExe.isFile()) {
        Properties env = CommandLineUtils.getSystemEnvVars();
        String javaHome = env.getProperty("JAVA_HOME");
        if (StringUtils.isEmpty(javaHome)) {
            throw new IOException("The environment variable JAVA_HOME is not correctly set.");
        }
        if ((!new File(javaHome).getCanonicalFile().exists())
                || (new File(javaHome).getCanonicalFile().isFile())) {
            throw new IOException("The environment variable JAVA_HOME=" + javaHome
                    + " doesn't exist or is not a valid directory.");
        }

        javadocExe = new File(javaHome + File.separator + "bin", javadocCommand);
    }

    if (!javadocExe.getCanonicalFile().exists() || !javadocExe.getCanonicalFile().isFile()) {
        throw new IOException("The javadoc executable '" + javadocExe
                + "' doesn't exist or is not a file. Verify the JAVA_HOME environment variable.");
    }

    return javadocExe.getAbsolutePath();
}

From source file:com.clark.func.Functions.java

/**
 * Determines whether the specified file is a Symbolic Link rather than an
 * actual file.// w w w  .j  a  va  2s  . c o m
 * <p>
 * Will not return true if there is a Symbolic Link anywhere in the path,
 * only if the specific file is.
 * 
 * @param file
 *            the file to check
 * @return true if the file is a Symbolic Link
 * @throws IOException
 *             if an IO error occurs while checking the file
 * @since Commons IO 2.0
 */
public static boolean isSymlink(File file) throws IOException {
    if (file == null) {
        throw new NullPointerException("File must not be null");
    }
    if (isSystemWindows()) {
        return false;
    }
    File fileInCanonicalDir = null;
    if (file.getParent() == null) {
        fileInCanonicalDir = file;
    } else {
        File canonicalDir = file.getParentFile().getCanonicalFile();
        fileInCanonicalDir = new File(canonicalDir, file.getName());
    }

    if (fileInCanonicalDir.getCanonicalFile().equals(fileInCanonicalDir.getAbsoluteFile())) {
        return false;
    } else {
        return true;
    }
}

From source file:com.clark.func.Functions.java

/**
 * Compares the contents of two files to determine if they are equal or not.
 * <p>//from  w  ww .  j a v a 2 s  .c  o m
 * This method checks to see if the two files are different lengths or if
 * they point to the same file, before resorting to byte-by-byte comparison
 * of the contents.
 * <p>
 * Code origin: Avalon
 * 
 * @param file1
 *            the first file
 * @param file2
 *            the second file
 * @return true if the content of the files are equal or they both don't
 *         exist, false otherwise
 * @throws IOException
 *             in case of an I/O error
 */
public static boolean contentEquals(File file1, File file2) throws IOException {
    boolean file1Exists = file1.exists();
    if (file1Exists != file2.exists()) {
        return false;
    }

    if (!file1Exists) {
        // two not existing files are equal
        return true;
    }

    if (file1.isDirectory() || file2.isDirectory()) {
        // don't want to compare directory contents
        throw new IOException("Can't compare directories, only files");
    }

    if (file1.length() != file2.length()) {
        // lengths differ, cannot be equal
        return false;
    }

    if (file1.getCanonicalFile().equals(file2.getCanonicalFile())) {
        // same file
        return true;
    }

    InputStream input1 = null;
    InputStream input2 = null;
    try {
        input1 = new FileInputStream(file1);
        input2 = new FileInputStream(file2);
        return contentEquals(input1, input2);

    } finally {
        closeQuietly(input1);
        closeQuietly(input2);
    }
}