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.wsdl.codegen.CodeGenerationEngine.java

/**
 * calculates the base URI Needs improvement but works fine for now ;)
 *
 * @param currentURI//  w  w w . j  ava2  s  . c  om
 */
private String getBaseURI(String currentURI) throws URISyntaxException, IOException {
    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("/") ? "" : "/");
}

From source file:org.ow2.mind.doc.DocumentationIndexGenerator.java

protected void generateIndexLists() throws IOException {
    for (final File directory : sourceDirectories) {
        exploreDirectory(directory.getCanonicalFile(), directory.getCanonicalFile());
    }/*www .j av a  2s .co m*/
    Collections.sort(adlDefinitionEntries, new IndexEntryComparatorNoPackage());
    Collections.sort(itfDefinitionEntries, new IndexEntryComparatorNoPackage());
    Collections.sort(packages, new IndexEntryComparator());
}

From source file:org.apache.axis2.wsdl.codegen.CodeGenerationEngine.java

/**
 * calculates the URI/*w  ww.ja v a  2  s .  com*/
 * needs improvement
 *
 * @param currentURI
 */
private String getURI(String currentURI) throws URISyntaxException, IOException {

    File file = new File(currentURI);
    if (file.exists()) {
        return file.getCanonicalFile().toURI().toString();
    } else {
        return currentURI;
    }

}

From source file:com.blazemeter.bamboo.plugin.ServiceManager.java

public static File resolvePath(TaskContext context, String path, BuildLogger logger) throws Exception {
    File f = null;
    File root = new File("/");
    if (path.startsWith("/")) {
        f = new File(root, path);
    } else {//from w ww. j a  v  a 2 s. c om
        f = new File(context.getWorkingDirectory().getAbsolutePath() + "/build # "
                + context.getBuildContext().getBuildNumber(), path);
    }
    if (!f.exists()) {
        boolean mkDir = false;
        try {
            mkDir = f.mkdirs();
        } catch (Exception e) {
            throw new Exception("Failed to find filepath = " + f.getName());
        } finally {
            if (!mkDir) {
                logger.addBuildLogEntry(
                        "Failed to create " + f.getCanonicalPath() + " , workspace will be used.");
                f = new File(context.getWorkingDirectory(), path);
                f.mkdirs();
                logger.addBuildLogEntry("Resolving path into " + f.getCanonicalPath());
            }
        }
    }
    return f.getCanonicalFile();
}

From source file:CASUAL.communicationstools.heimdall.odin.OdinFile.java

/**
 * Extracts Odin contents to outputDir//  www. ja  v  a 2s .  c  o m
 *
 * @param outputDir temp folder
 * @return an array of files extracted from Odin Package
 * @throws IOException {@inheritDoc}
 * @throws ArchiveException {@inheritDoc}
 * @throws NoSuchAlgorithmException {@inheritDoc}
 */
public File[] extractOdinContents(String outputDir)
        throws IOException, ArchiveException, NoSuchAlgorithmException {
    if (files != null) {
        //for sucessive calls
        return files.toArray(new File[files.size()]);
    }
    files = new ArrayList<File>();
    TarArchiveEntry entry;
    //parse the entries
    while ((entry = (TarArchiveEntry) tarStream.getNextEntry()) != null) {
        final File outputFile = new File(outputDir, entry.getName());
        //make folders
        if (entry.isDirectory()) {
            if (!outputFile.exists()) {
                System.out.println("creating dir:" + outputFile.getCanonicalFile());
                if (!outputFile.mkdirs()) {
                    throw new IllegalStateException();
                }
            }
            //create files
        } else {
            final OutputStream outputFileStream = new FileOutputStream(outputFile);
            System.out.println("decompressing file:" + outputFile.getCanonicalFile());
            byte[] buffer = new byte[1024 * 1024];
            int len;
            while ((len = tarStream.read(buffer)) >= 0) {
                outputFileStream.write(buffer, 0, len);
            }
            outputFileStream.close();
        }
        //add files to output array
        files.add(outputFile);
    }
    return files.toArray(new File[files.size()]);
}

From source file:org.dbgl.util.FileUtils.java

public static void extractEntry(final ZipFile zf, final ZipEntry srcEntry, final File dstFile,
        final ProgressNotifyable prog) throws IOException {
    File foundDstFile = null, temporarilyRenamedFile = null;
    if (PlatformUtils.IS_WINDOWS && dstFile.getName().contains("~")) {
        foundDstFile = dstFile.getCanonicalFile();
        if (!foundDstFile.getName().equals(dstFile.getName()) && foundDstFile.exists()) {
            temporarilyRenamedFile = getUniqueFileName(foundDstFile);
            foundDstFile.renameTo(temporarilyRenamedFile);
        }//from w  ww. j  a v a2 s.c om
    }

    if (dstFile.exists())
        throw new IOException(
                Settings.getInstance().msg("general.error.filetobeextractedexists", new Object[] { dstFile }));
    if (srcEntry.isDirectory()) {
        if (!dstFile.exists())
            createDir(dstFile);
    } else {
        if (dstFile.getParentFile() != null)
            createDir(dstFile.getParentFile());
        FileOutputStream fos = new FileOutputStream(dstFile);
        InputStream is = zf.getInputStream(srcEntry);
        byte[] readBuffer = new byte[ZIP_BUFFER];
        int bytesIn;
        while ((bytesIn = is.read(readBuffer)) != -1)
            fos.write(readBuffer, 0, bytesIn);
        fos.flush();
        fos.close();
        is.close();
        byte[] extra = srcEntry.getExtra();
        if ((extra != null) && (extra.length == 1) && (extra[0] == 1))
            fileSetReadOnly(dstFile);
    }
    fileSetLastModified(dstFile, srcEntry.getTime());

    if (foundDstFile != null && temporarilyRenamedFile != null)
        temporarilyRenamedFile.renameTo(foundDstFile);

    prog.incrProgress((int) (srcEntry.getSize() / 1024));
}

From source file:de.unibremen.informatik.tdki.combo.data.DBLayout.java

public boolean exportProject(String project, File bulkFile) {
    if (!projectExists(project)) {
        return false;
    }/*ww  w  .ja  va2  s  .  c  o m*/
    try {
        BulkFile f = new BulkFile(bulkFile.getCanonicalFile());
        f.open(BulkFile.Open.COMPRESS);
        bulkExportToFile(f.getConceptAssertions().getCanonicalPath(), getTableConceptAssertions(project));
        bulkExportToFile(f.getRoleAssertions().getCanonicalPath(), getTableRoleAssertions(project));
        bulkExportToFile(f.getSymbols().getCanonicalPath(), getTableSymbols(project));
        bulkExportToFile(f.getGenRoles().getCanonicalPath(), getTableGenRoles(project));
        bulkExportToFile(f.getGenConcepts().getCanonicalPath(), getTableGenConcepts(project));
        bulkExportToFile(f.getQualifiedExistentials().getCanonicalPath(),
                getTableQualifiedExistentials(project));
        bulkExportToFile(f.getInclusionAxioms().getCanonicalPath(), getTableTBox(project));
        bulkExportToFile(f.getRoleInclusions().getCanonicalPath(), getTableRBox(project));
        f.close();
    } catch (InterruptedException ex) {
        throw new RuntimeException(ex);
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
    return true;
}

From source file:com.isencia.passerelle.hmi.HMIBase.java

/**
 * Given the name of a file or a URL, convert it to a URL. This first
 * attempts to do that directly by invoking a URL constructor. If that
 * fails, then it tries to interpret the spec as a file name on the local
 * file system. If that fails, then it tries to interpret the spec as a
 * resource accessible to the classloader, which uses the classpath to find
 * the resource. If that fails, then it throws an exception. The
 * specification can give a file name relative to current working directory,
 * or the directory in which this application is started up.
 * /*from  ww w.  j av a 2s.com*/
 * @param spec
 *            The specification.
 * @exception IOException
 *                If it cannot convert the specification to a URL.
 */
public static URL specToURL(final String spec) throws IOException {
    try {
        // First argument is null because we are only
        // processing absolute URLs this way. Relative
        // URLs are opened as ordinary files.
        return new URL(null, spec);
    } catch (final MalformedURLException ex) {
        try {
            final File file = new File(spec);
            if (!file.exists()) {
                throw new MalformedURLException();
            }
            return file.getCanonicalFile().toURL();
        } catch (final MalformedURLException ex2) {
            try {
                // Try one last thing, using the classpath.
                // Need a class context, and this is a static method, so...
                // NOTE: There doesn't seem to be any way to convert
                // this a canonical name, so if a model is opened this
                // way, and then later opened as a file, the model
                // directory will think it has two different files.
                final Class refClass = Class.forName("ptolemy.kernel.util.NamedObj");
                final URL inurl = refClass.getClassLoader().getResource(spec);
                if (inurl == null) {
                    throw new Exception();
                } else {
                    return inurl;
                }
            } catch (final Exception exception) {
                throw new IOException("File not found: " + spec);
            }
        }
    }
}

From source file:org.mrgeo.utils.DependencyLoader.java

private static void moveFileToClasspath(Configuration conf, Set<String> existing, FileSystem fs, Path hdfsBase,
        File file) throws IOException {
    Path hdfsPath = new Path(hdfsBase, file.getName());
    if (!existing.contains(hdfsPath.toString())) {
        if (fs.exists(hdfsPath)) {
            // check the timestamp and exit if the one in hdfs is "newer"
            FileStatus status = fs.getFileStatus(hdfsPath);

            if (file.lastModified() <= status.getModificationTime()) {
                log.debug(file.getPath() + " up to date");

                existing.add(hdfsPath.toString());
                return;
            }//  ww w .  j av  a 2s.  co  m
        }

        // copy the file...
        log.debug("Copying " + file.getPath() + " to HDFS for distribution");

        fs.copyFromLocalFile(new Path(file.getCanonicalFile().toURI()), hdfsPath);
        existing.add(hdfsPath.toString());
    }
}

From source file:hudson.Util.java

/**
 * Checks if the given file represents a symlink.
 *//*  w  ww . ja  va2 s .  c o  m*/
//Taken from http://svn.apache.org/viewvc/maven/shared/trunk/file-management/src/main/java/org/apache/maven/shared/model/fileset/util/FileSetManager.java?view=markup
public static boolean isSymlink(File file) throws IOException {
    String name = file.getName();
    if (name.equals(".") || name.equals(".."))
        return false;

    File fileInCanonicalParent;
    File parentDir = file.getParentFile();
    if (parentDir == null) {
        fileInCanonicalParent = file;
    } else {
        fileInCanonicalParent = new File(parentDir.getCanonicalPath(), name);
    }
    return !fileInCanonicalParent.getCanonicalFile().equals(fileInCanonicalParent.getAbsoluteFile());
}