Example usage for java.io File isAbsolute

List of usage examples for java.io File isAbsolute

Introduction

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

Prototype

public boolean isAbsolute() 

Source Link

Document

Tests whether this abstract pathname is absolute.

Usage

From source file:ezbake.deployer.impl.Files.java

public static File resolve(File base, File... child) {
    File resolved = base;//from   www. ja v a 2 s .  c  o m
    for (File c : child) {
        if (c.isAbsolute()) {
            resolved = c;
        } else {
            resolved = new File(resolved, c.getPath());
        }
    }
    return resolved;
}

From source file:it.greenvulcano.util.file.FileManager.java

/**
 * Copy the file/directory to a new one.<br>
 * The filePattern may be a regular expression: in this case, all the filenames
 * matching the pattern will be copied to the target directory.<br>
 * If a file, whose name matches the <code>filePattern</code> pattern, already
 * exists in the same target directory, it will be overwritten.<br>
 *
 * @param sourcePath/*ww  w  .  j  ava  2 s.co  m*/
 *            Absolute pathname of the source file/directory.
 * @param targetPath
 *            Absolute pathname of the target file/directory.
 * @param filePattern
 *            A regular expression defining the name of the files to be copied. Used only if
 *            srcPath identify a directory. Null or "" disable file filtering.
 * @throws Exception
 */
public static void cp(String sourcePath, String targetPath, String filePattern) throws Exception {
    File src = new File(sourcePath);
    if (!src.isAbsolute()) {
        throw new IllegalArgumentException("The pathname of the source file is NOT absolute: " + sourcePath);
    }
    File target = new File(targetPath);
    if (!target.isAbsolute()) {
        throw new IllegalArgumentException("The pathname of the target file is NOT absolute: " + targetPath);
    }

    if (src.isDirectory()) {
        if ((filePattern == null) || filePattern.equals("") || filePattern.equals(".*")) {
            logger.debug(
                    "Copying directory " + src.getAbsolutePath() + " to directory " + target.getAbsolutePath());
            FileUtils.copyDirectory(src, new File(targetPath));
        } else {
            Set<FileProperties> files = ls(sourcePath, filePattern);
            for (FileProperties file : files) {
                logger.debug("Copying file " + file.getName() + " from directory " + src.getAbsolutePath()
                        + " to directory " + target.getAbsolutePath());
                if (file.isDirectory()) {
                    FileUtils.copyDirectoryToDirectory(new File(src, file.getName()), target);
                } else {
                    FileUtils.copyFileToDirectory(new File(src, file.getName()), target);
                }
            }
        }
    } else {
        if (target.isDirectory()) {
            logger.debug("Copying file " + src.getAbsolutePath() + " to directory " + target.getAbsolutePath());
            FileUtils.copyFileToDirectory(src, target);
        } else {
            logger.debug("Copying file " + src.getAbsolutePath() + " to " + target.getAbsolutePath());
            FileUtils.copyFile(src, target);
        }
    }
}

From source file:com.seitenbau.jenkins.plugins.dynamicparameter.config.DynamicParameterManagement.java

private static File getRebasedFile(String path) throws IOException {
    String basePath = getBaseDirectoryPath();
    File file = new File(path);
    if (!file.isAbsolute()) {
        file = new File(basePath, path);
    }//from ww w  .j  av a2 s  .  c  om

    String canonicalFilePath = file.getCanonicalPath();
    if (FileUtils.isDescendant(basePath, canonicalFilePath)) {
        return file;
    } else {
        // don't leave the base directory
        String fileName = new File(canonicalFilePath).getName();
        if (fileName.length() == 0) {
            // don't return the base directory in any case
            fileName = DEFAULT_NAME;
        }
        File rebasedFile = new File(basePath, fileName);
        return rebasedFile;
    }
}

From source file:it.greenvulcano.util.file.FileManager.java

/**
 * Move a file/directory on the local file system.<br>
 *
 * @param oldPath//from w w  w .j  ava 2  s .co m
 *            Absolute pathname of the file/directory to be renamed
 * @param newPath
 *            Absolute pathname of the new file/directory
 * @param filePattern
 *            A regular expression defining the name of the files to be copied. Used only if
 *            srcPath identify a directory. Null or "" disable file filtering.
 * @throws Exception
 */
public static void mv(String oldPath, String newPath, String filePattern) throws Exception {
    File src = new File(oldPath);
    if (!src.isAbsolute()) {
        throw new IllegalArgumentException("The pathname of the source is NOT absolute: " + oldPath);
    }
    File target = new File(newPath);
    if (!target.isAbsolute()) {
        throw new IllegalArgumentException("The pathname of the destination is NOT absolute: " + newPath);
    }
    if (target.isDirectory()) {
        if ((filePattern == null) || filePattern.equals("")) {
            FileUtils.deleteQuietly(target);
        }
    }
    if (src.isDirectory()) {
        if ((filePattern == null) || filePattern.equals("")) {
            logger.debug("Moving directory " + src.getAbsolutePath() + " to " + target.getAbsolutePath());
            FileUtils.moveDirectory(src, target);
        } else {
            Set<FileProperties> files = ls(oldPath, filePattern);
            for (FileProperties file : files) {
                logger.debug("Moving file " + file.getName() + " from directory " + src.getAbsolutePath()
                        + " to directory " + target.getAbsolutePath());
                File finalTarget = new File(target, file.getName());
                FileUtils.deleteQuietly(finalTarget);
                if (file.isDirectory()) {
                    FileUtils.moveDirectoryToDirectory(new File(src, file.getName()), finalTarget, true);
                } else {
                    FileUtils.moveFileToDirectory(new File(src, file.getName()), target, true);
                }
            }
        }
    } else {
        if (target.isDirectory()) {
            File finalTarget = new File(target, src.getName());
            FileUtils.deleteQuietly(finalTarget);
            logger.debug("Moving file " + src.getAbsolutePath() + " to directory " + target.getAbsolutePath());
            FileUtils.moveFileToDirectory(src, target, true);
        } else {
            logger.debug("Moving file " + src.getAbsolutePath() + " to " + target.getAbsolutePath());
            FileUtils.moveFile(src, target);
        }
    }
}

From source file:org.hawkular.apm.api.services.ConfigurationLoader.java

/**
 * This method loads the configuration from the supplied URI.
 *
 * @param uri The URI//from  w  w  w. j a va 2 s.  c  o  m
 * @param type The type, or null if default (jvm)
 * @return The configuration
 */
protected static CollectorConfiguration loadConfig(String uri, String type) {
    final CollectorConfiguration config = new CollectorConfiguration();

    if (type == null) {
        type = DEFAULT_TYPE;
    }

    uri += java.io.File.separator + type;

    File f = new File(uri);

    if (!f.isAbsolute()) {
        if (f.exists()) {
            uri = f.getAbsolutePath();
        } else if (System.getProperties().containsKey("jboss.server.config.dir")) {
            uri = System.getProperty("jboss.server.config.dir") + java.io.File.separatorChar + uri;
        } else {
            try {
                URL url = Thread.currentThread().getContextClassLoader().getResource(uri);
                if (url != null) {
                    uri = url.getPath();
                } else {
                    log.severe("Failed to get absolute path for uri '" + uri + "'");
                }
            } catch (Exception e) {
                log.log(Level.SEVERE, "Failed to get absolute path for uri '" + uri + "'", e);
                uri = null;
            }
        }
    }

    if (uri != null) {
        String[] uriParts = uri.split(Matcher.quoteReplacement(File.separator));
        int startIndex = 0;

        // Remove any file prefix
        if (uriParts[0].equals("file:")) {
            startIndex++;
        }

        try {
            Path path = getPath(startIndex, uriParts);

            Files.walkFileTree(path, new FileVisitor<Path>() {

                @Override
                public FileVisitResult postVisitDirectory(Path path, IOException exc) throws IOException {
                    return FileVisitResult.CONTINUE;
                }

                @Override
                public FileVisitResult preVisitDirectory(Path path, BasicFileAttributes attrs)
                        throws IOException {
                    return FileVisitResult.CONTINUE;
                }

                @Override
                public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException {
                    if (path.toString().endsWith(".json")) {
                        String json = new String(Files.readAllBytes(path));
                        CollectorConfiguration childConfig = mapper.readValue(json,
                                CollectorConfiguration.class);
                        if (childConfig != null) {
                            config.merge(childConfig, false);
                        }
                    }
                    return FileVisitResult.CONTINUE;
                }

                @Override
                public FileVisitResult visitFileFailed(Path path, IOException exc) throws IOException {
                    return FileVisitResult.CONTINUE;
                }

            });
        } catch (Throwable e) {
            log.log(Level.SEVERE, "Failed to load configuration", e);
        }
    }

    return config;
}

From source file:org.apache.hadoop.hdfs.qjournal.server.JournalNode.java

private static void validateAndCreateJournalDir(File dir) throws IOException {
    if (!dir.isAbsolute()) {
        throw new IllegalArgumentException("Journal dir '" + dir + "' should be an absolute path");
    }/*from   w  w  w  . j a v a 2  s  . c om*/

    DiskChecker.checkDir(dir);
}

From source file:ca.phon.media.util.MediaLocator.java

/**
 * Search for a file in the media include path.
 * /*from w w  w .j  a va2s  . c  o  m*/
 * Will look in the project resource directory first if
 * project is not null.
 * 
 * @param filename
 * @param project (may be <code>null</code>)
 * @param corpus (may be <code>null</code>)
 * @return the file object for the file or null if not found
 */
public static File findMediaFile(String filename, Project project, String corpus) {
    File retVal = null;

    if (filename == null)
        return retVal;

    // do we already have an absolute path
    final File mediaFile = new File(filename);
    if (mediaFile.isAbsolute()) {
        retVal = mediaFile;
    } else {
        /* 
         * Look for media in the following location order:
         * 
         * <media folder>/<project>/<corpus>/<file>
         * <media folder>/<project>/<file>
         * <media folder>/<corpus>/<file>
         * <media folder>/<file>
         */
        final List<String> checkList = new ArrayList<String>();
        if (project != null && corpus != null) {
            checkList.add(project.getName() + File.separator + corpus + File.separator + filename);
        }
        if (project != null) {
            checkList.add(project.getName() + File.separator + filename);
        }
        if (corpus != null) {
            checkList.add(corpus + File.separator + filename);
        }
        checkList.add(File.separator + filename);

        final List<String> mediaPaths = new ArrayList<String>();
        mediaPaths.addAll(getMediaIncludePaths());

        if (project != null) {
            // check resources
            File resFile = new File(project.getResourceLocation());
            File resMediaFile = new File(resFile, "media");
            mediaPaths.add(0, resMediaFile.getAbsolutePath());
        }

        // look in rest of search paths
        for (String path : mediaPaths) {
            for (String checkName : checkList) {
                final File checkFile = new File(path, checkName);
                if (checkFile.exists()) {
                    retVal = checkFile;
                    break;
                }
            }
        }

    }

    return retVal;
}

From source file:org.apache.cocoon.components.language.markup.xsp.XSPUtil.java

public static String relativeFilename(String filename, Map objectModel) throws IOException {
    File file = new File(filename);
    if (file.isAbsolute() && file.exists()) {
        return filename;
    }//from   w w  w .  j a v  a2 s . c  om
    Context context = ObjectModelHelper.getContext(objectModel);
    URL resource = context.getResource(filename);
    if (resource == null) {
        throw new FileNotFoundException("The file " + filename + " does not exist!");
    }
    return NetUtils.getPath(resource.toExternalForm());
}

From source file:org.geoserver.security.password.URLMasterPasswordProvider.java

static InputStream input(URL url, File configDir) throws IOException {
    //check for a file url
    if ("file".equalsIgnoreCase(url.getProtocol())) {
        File f = DataUtilities.urlToFile(url);
        //check if the file is relative
        if (!f.isAbsolute()) {
            //make it relative to the config directory for this password provider
            f = new File(configDir, f.getPath());
        }// w w w.j  a  v a 2s .co m
        return new FileInputStream(f);
    } else {
        return url.openStream();
    }
}

From source file:net.sourceforge.pmd.ant.Formatter.java

private static Writer getToFileWriter(String baseDir, File toFile, Charset charset) throws IOException {
    final File file;
    if (toFile.isAbsolute()) {
        file = toFile;/*from  w ww .  ja  v  a2s  . c  o m*/
    } else {
        file = new File(baseDir + System.getProperty("file.separator") + toFile.getPath());
    }

    OutputStream output = null;
    Writer writer = null;
    boolean isOnError = true;
    try {
        output = Files.newOutputStream(file.toPath());
        writer = new OutputStreamWriter(output, charset);
        writer = new BufferedWriter(writer);
        isOnError = false;
    } finally {
        if (isOnError) {
            IOUtils.closeQuietly(output);
            IOUtils.closeQuietly(writer);
        }
    }
    return writer;
}