Java Code Examples for java.io.File.getCanonicalFile()

The following are Jave code examples for showing how to use getCanonicalFile() of the java.io.File class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: lazycat   File: FileDirContext.java   View Source Code Vote up 6 votes
/**
 * Set the document root.
 *
 * @param docBase
 *            The new document root
 *
 * @exception IllegalArgumentException
 *                if the specified value is not supported by this
 *                implementation
 * @exception IllegalArgumentException
 *                if this would create a malformed URL
 */
@Override
public void setDocBase(String docBase) {

	// Validate the format of the proposed document root
	if (docBase == null)
		throw new IllegalArgumentException(sm.getString("resources.null"));

	// Calculate a File object referencing this document base directory
	base = new File(docBase);
	try {
		base = base.getCanonicalFile();
	} catch (IOException e) {
		// Ignore
	}

	// Validate that the document base is an existing directory
	if (!base.exists() || !base.isDirectory() || !base.canRead())
		throw new IllegalArgumentException(sm.getString("fileResources.base", docBase));
	this.absoluteBase = base.getAbsolutePath();
	super.setDocBase(docBase);

}
 
Example 2
Project: boohee_v5.6   File: FileProvider.java   View Source Code Vote up 6 votes
public File getFileForUri(Uri uri) {
    String path = uri.getEncodedPath();
    int splitIndex = path.indexOf(47, 1);
    String tag = Uri.decode(path.substring(1, splitIndex));
    path = Uri.decode(path.substring(splitIndex + 1));
    File root = (File) this.mRoots.get(tag);
    if (root == null) {
        throw new IllegalArgumentException("Unable to find configured root for " + uri);
    }
    File file = new File(root, path);
    try {
        file = file.getCanonicalFile();
        if (file.getPath().startsWith(root.getPath())) {
            return file;
        }
        throw new SecurityException("Resolved path jumped beyond configured root");
    } catch (IOException e) {
        throw new IllegalArgumentException("Failed to resolve canonical path for " + file);
    }
}
 
Example 3
Project: dev-courses   File: JDBCClobFile.java   View Source Code Vote up 6 votes
/**
 * Constructs a new JDBCClobFile instance backed by the given File object
 * using the given encoding to read and write file content.<p>
 *
 * @param file that is to back the new CLOB instance.
 * @param encoding the name of the character encoding used to read and write
 *         character data in the underlying file, as well as to determine
 *         the character length of and character offsets into the underlying
 *         file. Specify null to denote the platform encoding.
 *
 * @throws SQLException if the given encoding is unsupported;
 *         an I/O error occurs, which is possible because the
 *         construction of the canonical pathname may require
 *         file-system queries; a required system property value
 *         cannot be accessed; a security manager exists and its
 *         <code>{@link java.lang.SecurityManager#checkRead}</code>
 *         method denies read access to the file
 */
public JDBCClobFile(File file, String encoding) throws SQLException {

    if (file == null) {
        throw JDBCUtil.nullArgument("file");
    }

    try {
        setEncoding(encoding);

        m_file = file.getCanonicalFile();

        checkIsFile( /*checkExists*/false);

        m_deleteOnFree = false;
    } catch (Exception ex) {
        throw JDBCUtil.sqlException(ex);
    }
}
 
Example 4
Project: incubator-netbeans   File: WindowsNativeUtils.java   View Source Code Vote up 6 votes
@Override
public File getRoot(final File file) {
    if(isUNCPath(file.getPath())) {
        // tmp = server\folder;
        File parent = file;
        File previous = null;
        File can;
        try {
            while(parent.getParentFile()!=null) {
                can = parent.getCanonicalFile();
                previous = parent;
                parent = parent.getParentFile();
            }                
        } catch (IOException e) {
            // this occurs when file path is equal the server name : \\server
            // then go to finally and return previous file
        } finally {
            return previous;
        }
    } else {
        return super.getRoot(file);
    }
}
 
Example 5
Project: incubator-netbeans   File: Manager.java   View Source Code Vote up 6 votes
/**
 * Normalize java.io.File, that is make sure that returned File has
 * normalized case on Windows; that old Windows 8.3 filename is normalized;
 * that Unix symlinks are not followed; that relative path is changed to 
 * absolute; etc.
 * @param file file to normalize
 * @return normalized file
 */
public static File normalizeFile(File file) {
    Runnable off = Log.internalLog();
    try {
        // taken from org.openide.util.FileUtil
        if (System.getProperty ("os.name").startsWith("Windows")) { // NOI18N
            // On Windows, best to canonicalize.
            try {
                file = file.getCanonicalFile();
            } catch (IOException e) {
                Logger.getLogger(Manager.class.getName()).warning("getCanonicalFile() on file " + file + " failed: " + e);
                // OK, so at least try to absolutize the path
                file = file.getAbsoluteFile();
            }
        } else {
            // On Unix, do not want to traverse symlinks.
            file = new File(file.toURI().normalize()).getAbsoluteFile();
        }
        return file;
    } finally {
        off.run();
    }
}
 
Example 6
Project: optashift-employee-rostering   File: AbstractClientArquillianTest.java   View Source Code Vote up 6 votes
private static File findPomFile() {
    File file = new File("pom.xml");
    if (!file.exists()) {
        throw new IllegalStateException("The file (" + file + ") does not exist.\n"
                + "This test needs to be run with the working directory " +  POM_DIRECTORY_NAME + ".");
    }
    try {
        file = file.getCanonicalFile();
    } catch (IOException e) {
        throw new IllegalStateException("Could not get cannonical file for file (" + file + ").", e);
    }
    if (!file.getParentFile().getName().equals(POM_DIRECTORY_NAME)) {
        throw new IllegalStateException("The file (" + file + ") is not correct.\n"
                + "This test needs to be run with the working directory " + POM_DIRECTORY_NAME + ".");
    }
    return file;
}
 
Example 7
Project: reflect   File: Scanner.java   View Source Code Vote up 6 votes
private void scan(URL url, ClassLoader loader) throws Exception {
  if (!url.getProtocol().equals("file")) {
    logger.error("Illegal url, currently only file:// url is supported! {}", url);
    return;
  }

  File file = new File(url.toURI());
  file = file.getCanonicalFile();

  if (!scannedFiles.add(file)) {
    return;
  }

  if (!file.exists()) {
    return;
  }

  if (file.isDirectory()) {
    scanDir(file, loader);
  } else {
    scanJar(file, loader);
  }
}
 
Example 8
Project: incubator-netbeans   File: ClassBasedBreakpoint.java   View Source Code Vote up 6 votes
protected final boolean compareSourceRoots(String sr1, String sr2) {
    if (sr1.equals(sr2)) {
        return true;
    }
    File f1 = new File(sr1);
    File f2 = new File(sr2);
    if (f1.isDirectory() && f2.isDirectory() ||
        f1.isFile() && f2.isFile()) { // An archive file
        
        if (f1.equals(f2)) {
            return true;
        }
        try {
            f1 = f1.getCanonicalFile();
            f2 = f2.getCanonicalFile();
            return f1.equals(f2);
        } catch (IOException ioex) {}
    }
    return false;
}
 
Example 9
Project: hadoop   File: DockerLinuxContainerRuntime.java   View Source Code Vote up 6 votes
private boolean isSubDirectory(File parent, File child){
  try {
    parent = parent.getCanonicalFile();
    child = child.getCanonicalFile();
    File parentFile = child;
    while (parentFile != null){
      if (parent.equals(parentFile)){
        return true;
      }
      parentFile = parentFile.getParentFile();
    }
  } catch (IOException e) {
    e.printStackTrace();
  }
  return false;
}
 
Example 10
Project: chipKIT-importer   File: ProjectSetupStep.java   View Source Code Vote up 5 votes
private static File getCanonicalFile(File file) {
    try {
        return file.getCanonicalFile();
    } catch (IOException e) {
        // TODO: What should we do with this exception?
        return null;
    }
}
 
Example 11
Project: dev-courses   File: FileUtil.java   View Source Code Vote up 5 votes
DatabaseFilenameFilter(String dbNamePath, boolean extras) {

            canonicalFile = new File(dbNamePath);

            try {
                canonicalFile = canonicalFile.getCanonicalFile();
            } catch (Exception e) {}

            dbName     = canonicalFile.getName();
            parent     = canonicalFile.getParentFile();
            extraFiles = extras;
        }
 
Example 12
Project: OpenDiabetes   File: FileUtil.java   View Source Code Vote up 5 votes
DatabaseFilenameFilter(String dbNamePath, boolean extras) {

            canonicalFile = new File(dbNamePath);

            try {
                canonicalFile = canonicalFile.getCanonicalFile();
            } catch (Exception e) {}

            dbName     = canonicalFile.getName();
            parent     = canonicalFile.getParentFile();
            extraFiles = extras;
        }
 
Example 13
Project: RetroMusicPlayer   File: FoldersFragment.java   View Source Code Vote up 5 votes
private static File tryGetCanonicalFile(File file) {
    try {
        return file.getCanonicalFile();
    } catch (IOException e) {
        e.printStackTrace();
        return file;
    }
}
 
Example 14
Project: apache-tomcat-7.0.73-with-comment   File: WebappClassLoaderBase.java   View Source Code Vote up 5 votes
/**
 * Get the URI for the given file.
 */
protected URL getURI(File file)
    throws MalformedURLException {


    File realFile = file;
    try {
        realFile = realFile.getCanonicalFile();
    } catch (IOException e) {
        // Ignore
    }
    return realFile.toURI().toURL();

}
 
Example 15
Project: parabuild-ci   File: IoUtils.java   View Source Code Vote up 5 votes
/**
 * Obtains canonical File from given path. If there is an
 * IOException while processing, will throw IllegalStateException.
 *
 * @throws IllegalStateException
 */
public static File getCanonicalFileHard(final File file) {
  try {
    return file.getCanonicalFile();
  } catch (IOException e) {
    log.fatal("Can not get canonical file for " + file.toString(), e);
    throw makeIllegalStateException(e);
  }
}
 
Example 16
Project: OpenDiabetes   File: JDBCBlobFile.java   View Source Code Vote up 5 votes
/**
 *  Constructs a new instance backed by the given File object.
 *
 * @param file used to back this BLOB instance.
 * @param deleteOnFree Assigns whether an attempt to delete the backing file
 *                     is to be made in response to invocation of {@link #free()}.
 * @throws SQLException If an I/O error occurs, which is possible because
 *         the construction of the canonical pathname may require file system
 *         queries; if a required system property value cannot be accessed,
 *         or if a security manager exists and its <code>{@link
 *         java.lang.SecurityManager#checkRead}</code> method denies
 *         read access to the file
 */
public JDBCBlobFile(final File file,
                    boolean deleteOnFree) throws SQLException {

    m_deleteOnFree = deleteOnFree;

    try {
        m_file = file.getCanonicalFile();
    } catch (Exception ex) {
        throw JDBCUtil.sqlException(ex);
    }

    checkIsFile( /*checkExists*/false);
}
 
Example 17
Project: alfresco-repository   File: IndexInfo.java   View Source Code Vote up 5 votes
/**
 * Get the IndexInfo object based in the given directory. There is only one object per directory per JVM.
 * 
 * @param file File
 * @param config LuceneConfig
 * @return IndexInfo
 * @throws IndexerException
 */
public static synchronized IndexInfo getIndexInfo(File file, LuceneConfig config) throws IndexerException
{
    File canonicalFile;
    try
    {
        canonicalFile = file.getCanonicalFile();
        IndexInfo indexInfo = indexInfos.get(canonicalFile);
        if (indexInfo == null)
        {
            indexInfo = new IndexInfo(canonicalFile, config);
            indexInfos.put(canonicalFile, indexInfo);
            if (s_logger.isDebugEnabled())
            {
                s_logger.debug("Made " + indexInfo + " for " + file.getAbsolutePath());
            }
        }

        if (s_logger.isDebugEnabled())
        {
            s_logger.debug("Got " + indexInfo + " for " + file.getAbsolutePath());
        }
        return indexInfo;
    }
    catch (IOException e)
    {
        throw new IndexerException("Failed to transform a file into is canonical form", e);
    }

}
 
Example 18
Project: incubator-netbeans   File: ClassBasedBreakpoint.java   View Source Code Vote up 5 votes
@Override
protected boolean isEnabled() {
    String sourceRoot = getSourceRoot();
    if (sourceRoot == null) {
        return true;
    }
    if (sourceRootsCache.getRootPaths().contains(sourceRoot)) {
        return true;
    }
    File rootFile = new File(sourceRoot);
    try {
        rootFile = rootFile.getCanonicalFile();
    } catch (IOException ioex) {}
    if (sourceRootsCache.getRootCanonicalFiles().contains(rootFile)) {
        return true;
    }
    if (sourceRootsCache.getProjectRootPaths().contains(sourceRoot) ||
        sourceRootsCache.getProjectRootCanonicalFiles().contains(rootFile)) {
            setValidity(VALIDITY.INVALID,
                        NbBundle.getMessage(ClassBasedBreakpoint.class,
                                    "MSG_DisabledSourceRoot",
                                    sourceRoot));
            return false;
    }
    // Breakpoint is not in debugger's source roots,
    // though it still might get hit if the app loads additional classes...
    return true;
    /*if (logger.isLoggable(Level.FINE)) {
        logger.fine("Breakpoint "+getBreakpoint()+
                    " NOT submitted because it's source root "+sourceRoot+
                    " is not contained in debugger's source roots: "+
                    java.util.Arrays.asList(sourceRoots));
    }
    return false;
     */
}
 
Example 19
Project: tomcat7   File: WebappClassLoaderBase.java   View Source Code Vote up 5 votes
/**
 * Get the URI for the given file.
 */
protected URL getURI(File file)
    throws MalformedURLException {


    File realFile = file;
    try {
        realFile = realFile.getCanonicalFile();
    } catch (IOException e) {
        // Ignore
    }
    return realFile.toURI().toURL();

}
 
Example 20
Project: MockableJarGenerator   File: TestUtils.java   View Source Code Vote up 2 votes
/**
 * returns a File for the subfolder of the test resource data.
 *
 * The full path is canonized.
 * This is basically ".../src/test/resources/testData/$name".
 *
 * @param names the names of the subfolders.
 *
 * @return a File
 */
public static File getCanonicalRoot(String... names) throws IOException {
    File root = getRoot(names);
    return root.getCanonicalFile();
}