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.axis2.description.AxisService.java

private static String getDocumentURI(String currentURI) {
    try {//from w w w .java 2  s  .  co m
        File file = new File(currentURI);
        return file.getCanonicalFile().toURI().toString();
    } catch (IOException e) {
        return null;
    }
}

From source file:com.google.ratel.deps.io.FileUtils.java

/**
 * Determines whether the specified file is a Symbolic Link rather than an actual file.
 * <p>/*from  w  ww  .  j av a  2 s  .  co m*/
 * Will not return true if there is a Symbolic Link anywhere in the path,
 * only if the specific file is.
 * <p>
 * <b>Note:</b> the current implementation always returns {@code false} if the system
 * is detected as Windows using {@link FilenameUtils#isSystemWindows()}
 * 
 * @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 2.0
 */
public static boolean isSymlink(File file) throws IOException {
    if (file == null) {
        throw new NullPointerException("File must not be null");
    }
    if (FilenameUtils.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:org.apache.jackrabbit.core.query.lucene.SearchIndex.java

/**
 * Creates a file system resource to the synonym provider configuration.
 *
 * @return a file system resource or <code>null</code> if no path was
 *         configured.//from w ww.  j a  v  a2 s. co  m
 * @throws FileSystemException if an exception occurs accessing the file
 *                             system.
 */
protected FileSystemResource createSynonymProviderConfigResource() throws FileSystemException, IOException {
    if (synonymProviderConfigPath != null) {
        FileSystemResource fsr;
        // simple sanity check
        if (synonymProviderConfigPath.endsWith(FileSystem.SEPARATOR)) {
            throw new FileSystemException("Invalid synonymProviderConfigPath: " + synonymProviderConfigPath);
        }
        FileSystem fs = getContext().getFileSystem();
        if (fs == null) {
            fs = new LocalFileSystem();
            int lastSeparator = synonymProviderConfigPath.lastIndexOf(FileSystem.SEPARATOR_CHAR);
            if (lastSeparator != -1) {
                File root = new File(path, synonymProviderConfigPath.substring(0, lastSeparator));
                ((LocalFileSystem) fs).setRoot(root.getCanonicalFile());
                fs.init();
                fsr = new FileSystemResource(fs, synonymProviderConfigPath.substring(lastSeparator + 1));
            } else {
                ((LocalFileSystem) fs).setPath(path);
                fs.init();
                fsr = new FileSystemResource(fs, synonymProviderConfigPath);
            }
            synonymProviderConfigFs = fs;
        } else {
            fsr = new FileSystemResource(fs, synonymProviderConfigPath);
        }
        return fsr;
    } else {
        // path not configured
        return null;
    }
}

From source file:com.example.util.FileUtils.java

/**
 * Determines whether the specified file is a Symbolic Link rather than an actual file.
 * <p>//from  w  w  w .ja  va  2 s . com
 * Will not return true if there is a Symbolic Link anywhere in the path,
 * only if the specific file is.
 * <p>
 * <b>Note:</b> the current implementation always returns {@code false} if the system
 * is detected as Windows using {@link FilenameUtils#isSystemWindows()}
 * 
 * @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 2.0
 */
public static boolean isSymlink(File file) throws IOException {
    if (file == null) {
        throw new NullPointerException("File must not be null");
    }
    // modify by sdhuang 20150210
    /*if (FilenameUtils.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.matteoveroni.model.copy.FileUtils.java

/**
 * Determines whether the specified file is a Symbolic Link rather than an actual file.
 * <p>/* ww w .  j  av a 2s .co  m*/
 * Will not return true if there is a Symbolic Link anywhere in the path,
 * only if the specific file is.
 * <p>
 * <b>Note:</b> the current implementation always returns {@code false} if the system
 * is detected as Windows using {@link FilenameUtils#isSystemWindows()}
 * 
 * @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 2.0
 */
public static boolean isSymlink(File file) throws IOException {
    if (file == null) {
        throw new NullPointerException("File must not be null");
    }
    if (com.matteoveroni.model.copy.FilenameUtils.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:org.apache.axis2.description.AxisService.java

private static String getBaseURI(String currentURI) {
    try {//from w w w . ja  va  2  s .  com
        File file = new File(currentURI);
        if (file.exists()) {
            return file.getCanonicalFile().getParentFile().toURI().toString();
        }
        String uriFragment = currentURI.substring(0, currentURI.lastIndexOf("/"));
        return uriFragment + (uriFragment.endsWith("/") ? "" : "/");
    } catch (IOException e) {
        return null;
    }
}

From source file:org.apache.openejb.config.DeploymentLoader.java

/**
 * Finds all faces configuration files and stores them in the WebModule
 *
 * @param webModule WebModule// w  w  w .java  2  s . com
 * @throws OpenEJBException
 */
private void addFacesConfigs(final WebModule webModule) throws OpenEJBException {
    //*************************IMPORTANT*******************************************
    // TODO : kmalhi :: Add support to scrape META-INF/faces-config.xml in jar files
    // look at section 10.4.2 of the JSF v1.2 spec, bullet 1 for details
    final Set<URL> facesConfigLocations = new HashSet<URL>();

    // web.xml contains faces config locations in the context parameter javax.faces.CONFIG_FILES
    final File warFile = new File(webModule.getJarLocation());
    final WebApp webApp = webModule.getWebApp();
    if (webApp != null) {
        final String foundContextParam = webApp.contextParamsAsMap().get("javax.faces.CONFIG_FILES");
        if (foundContextParam != null) {
            // the value is a comma separated list of config files
            final String commaDelimitedListOfFiles = foundContextParam.trim();
            final String[] configFiles = commaDelimitedListOfFiles.split(",");
            // trim any extra spaces in each file
            final String[] trimmedConfigFiles = new String[configFiles.length];
            for (int i = 0; i < configFiles.length; i++) {
                trimmedConfigFiles[i] = configFiles[i].trim();
            }
            // convert each file to a URL and add it to facesConfigLocations
            for (final String location : trimmedConfigFiles) {
                if (!location.startsWith("/")) {
                    logger.error(
                            "A faces configuration file should be context relative when specified in web.xml. Please fix the value of context parameter javax.faces.CONFIG_FILES for the file "
                                    + location);
                }
                try {
                    final File file = new File(warFile, location).getCanonicalFile().getAbsoluteFile();
                    final URL url = file.toURI().toURL();
                    facesConfigLocations.add(url);

                } catch (final IOException e) {
                    logger.error("Faces configuration file location bad: " + location, e);
                }
            }
        } else {
            logger.debug("faces config file is null");
        }
    }

    // Search for WEB-INF/faces-config.xml
    final File webInf = new File(warFile, "WEB-INF");
    if (webInf.isDirectory()) {
        File facesConfigFile = new File(webInf, "faces-config.xml");
        if (facesConfigFile.exists()) {
            try {
                facesConfigFile = facesConfigFile.getCanonicalFile().getAbsoluteFile();
                final URL url = facesConfigFile.toURI().toURL();
                facesConfigLocations.add(url);
            } catch (final IOException e) {
                // TODO: kmalhi:: Remove the printStackTrace after testing
                e.printStackTrace();
            }
        }
    }
    // load the faces configuration files
    // TODO:kmalhi:: Its good to have separate FacesConfig objects for multiple configuration files, but what if there is a conflict where the same
    // managebean is declared in two different files, which one wins? -- check the jsf spec, Hopefully JSF should be able to check for this and
    // flag an error and not allow the application to be deployed.
    for (final URL location : facesConfigLocations) {
        final FacesConfig facesConfig = ReadDescriptors.readFacesConfig(location);
        webModule.getFacesConfigs().add(facesConfig);
        if ("file".equals(location.getProtocol())) {
            webModule.getWatchedResources().add(URLs.toFilePath(location));
        }
    }
}

From source file:org.xmlsh.sh.shell.Shell.java

public File getExplicitFile(File dir, String name, boolean mustExist) throws IOException {

    File file = null;
    file = new File(dir, name);

    try {//from ww w . j a v  a 2 s  .  co  m
        file = file.getCanonicalFile();
    } catch (IOException e) {
        getLogger().info("Exception translating file to canonical file", e);
        // try to still use file
    }
    if (mustExist && !file.exists())
        return null;

    return file;
}

From source file:org.exoplatform.services.jcr.ext.backup.AbstractBackupUseCasesTest.java

public void testRelativeBackupDir() throws Exception {
    // prepare stage #1
    ManageableRepository repository = helper.createRepository(container, DatabaseStructureType.MULTI, null);
    addConent(repository, repository.getConfiguration().getSystemWorkspaceName());

    // backup/*  w  w w  . j av  a2 s .  com*/
    File backDir = new File("target");

    RepositoryBackupConfig config = new RepositoryBackupConfig();
    config.setRepository(repository.getConfiguration().getName());
    config.setBackupType(BackupManager.FULL_BACKUP_ONLY);
    config.setBackupDir(backDir);

    RepositoryBackupChain bch = backup.startBackup(config);
    waitEndOfBackup(bch);
    backup.stopBackup(bch);

    // prepare stage #2
    String repositoryBackupChainLogPath = bch.getLogFilePath();

    String relativePrefixBackupDir = backDir.getCanonicalFile().getParent() + File.separator;

    String newBackupDir = bch.getBackupConfig().getBackupDir().getCanonicalPath()
            .replace(relativePrefixBackupDir, "");
    String newBackupDirForWrite = newBackupDir;
    if (File.separator.equals("\\")) {
        newBackupDirForWrite = newBackupDir.replaceAll("\\\\", "\\\\\\\\");
    }

    File dest = new File(repositoryBackupChainLogPath + ".xml");
    dest.createNewFile();

    RepositoryBackupChainLog newRepositoryBackupChainLog = null;

    String sConfig = setNewBackupDirInRepositoryBackupChainLog(new File(repositoryBackupChainLogPath), dest,
            newBackupDirForWrite);

    assertTrue(sConfig.contains(newBackupDir));

    // check
    newRepositoryBackupChainLog = new RepositoryBackupChainLog(dest);

    assertEquals(bch.getBackupConfig().getBackupDir().getCanonicalPath(),
            newRepositoryBackupChainLog.getBackupConfig().getBackupDir().getCanonicalPath());
}

From source file:com.example.util.FileUtils.java

/**
 * Compares the contents of two files to determine if they are equal or not.
 * <p>//from  w  w w  .j a  v  a  2  s  .  c  o  m
 * This method checks to see if the two files point to the same file, 
 * before resorting to line-by-line comparison of the contents.
 * <p>
 *
 * @param file1  the first file
 * @param file2  the second file
 * @param charsetName the character encoding to be used. 
 *        May be null, in which case the platform default is used
 * @return true if the content of the files are equal or neither exists,
 *         false otherwise
 * @throws IOException in case of an I/O error
 * @since 2.2
 * @see IOUtils#contentEqualsIgnoreEOL(Reader, Reader)
 */
public static boolean contentEqualsIgnoreEOL(File file1, File file2, String charsetName) 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.getCanonicalFile().equals(file2.getCanonicalFile())) {
        // same file
        return true;
    }

    Reader input1 = null;
    Reader input2 = null;
    try {
        if (charsetName == null) {
            input1 = new InputStreamReader(new FileInputStream(file1));
            input2 = new InputStreamReader(new FileInputStream(file2));
        } else {
            input1 = new InputStreamReader(new FileInputStream(file1), charsetName);
            input2 = new InputStreamReader(new FileInputStream(file2), charsetName);
        }
        return IOUtils.contentEqualsIgnoreEOL(input1, input2);

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