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:org.commonjava.web.config.io.ConfigFileUtils.java

public static File[] findMatching(final File dir, String glob) throws IOException {
    if (!glob.matches(GLOB_IDENTIFYING_PATTERN)) {
        File f = new File(glob);
        if (!f.isAbsolute()) {
            f = new File(dir, glob);
        }//from w  ww .  j  a  v  a 2  s .c  om

        return new File[] { f };
    }

    final Matcher m = Pattern.compile(GLOB_BASE_PATTERN).matcher(glob);
    String base = null;
    String pattern = null;
    if (m.matches()) {
        base = m.group(1);
        pattern = m.group(2);

        if (!new File(base).isAbsolute()) {
            base = new File(dir, base).getAbsolutePath();
        }
    } else {
        base = dir.getAbsolutePath();
        pattern = glob;
    }

    if (pattern.length() < 1) {
        return new File[] { new File(base).getCanonicalFile() };
    }

    final StringBuilder regex = new StringBuilder();
    for (int i = 0; i < pattern.length(); i++) {
        final char c = pattern.charAt(i);
        switch (c) {
        case '*': {
            if (i + 1 < pattern.length() && pattern.charAt(i + 1) == '*') {
                regex.append(".+");
                i++;
            } else {
                regex.append("[^\\\\\\/]*");
            }
            break;
        }
        case '.': {
            regex.append("\\.");
            break;
        }
        case '?': {
            regex.append(".");
            break;
        }
        default: {
            regex.append(c);
        }
        }
    }

    final boolean dirsOnly = pattern.endsWith("/") || pattern.endsWith("\\");
    final String globRegex = regex.toString();
    final File bdir = new File(base).getCanonicalFile();
    final int bdirLen = bdir.getPath().length() + 1;

    final List<File> allFiles = listRecursively(bdir);
    for (final Iterator<File> it = allFiles.iterator(); it.hasNext();) {
        final File f = it.next();
        if (dirsOnly && !f.isDirectory()) {
            it.remove();
            continue;
        }

        final String sub = f.getAbsolutePath().substring(bdirLen);

        if (!sub.matches(globRegex)) {
            it.remove();
        }
    }

    return allFiles.toArray(new File[] {});
}

From source file:org.apache.maven.cli.configuration.SettingsXmlConfigurationProcessor.java

static File resolveFile(File file, String workingDirectory) {
    if (file == null) {
        return null;
    } else if (file.isAbsolute()) {
        return file;
    } else if (file.getPath().startsWith(File.separator)) {
        // drive-relative Windows path
        return file.getAbsoluteFile();
    } else {//from ww w  .  j a  va  2s  .c  om
        return new File(workingDirectory, file.getPath()).getAbsoluteFile();
    }
}

From source file:com.thoughtworks.go.util.FileUtil.java

public static boolean isAbsolutePath(String filename) {
    File file = new File(filename);
    boolean absolute = file.isAbsolute();
    if (absolute && OperatingSystem.isFamily(OperatingSystem.WINDOWS)) {
        if (filename.startsWith("\\\\") && !filename.matches("\\\\\\\\.*\\\\.+")) {
            absolute = false;/*from  w  w w. jav a 2  s  .c  om*/
        }
    }
    return absolute;
}

From source file:net.ymate.platform.plugin.util.PluginUtils.java

/**
 * @param cfgFile/*from w ww .  j ava2  s  .  c  o  m*/
 * @param plugin
 * @return ????
 */
public static File getResourceFile(String cfgFile, IPlugin plugin) {
    if (plugin != null) {
        // ?
        String _path = StringUtils.defaultIfEmpty(plugin.getPluginMeta().getPath(),
                plugin.getPluginFactory().getPluginConfig().getPluginHomePath());
        if (StringUtils.isNotBlank(_path)) {
            File _result = new File(new File(_path, StringUtils
                    .defaultIfEmpty(plugin.getPluginMeta().getAlias(), plugin.getPluginMeta().getId())),
                    cfgFile);
            if (_result.exists() && _result.isAbsolute() && _result.canRead()) {
                return _result;
            }
        }
        // ??
        URL _targetFileURL = ResourceUtils.getResource(cfgFile, plugin.getClass());
        if (_targetFileURL != null) {
            return FileUtils.toFile(_targetFileURL);
        }
    }
    return null;
}

From source file:org.ebayopensource.turmeric.tools.codegen.util.ClassPathUtil.java

private static List<File> getJarClassPathRefs(File file) {
    List<File> refs = new ArrayList<File>();

    JarFile jar = null;//from w ww  .ja  v a 2  s .c o  m
    try {
        jar = new JarFile(file);
        Manifest manifest = jar.getManifest();
        if (manifest == null) {
            // No manifest, no classpath.
            return refs;
        }

        Attributes attrs = manifest.getMainAttributes();
        if (attrs == null) {
            /*
             * No main attributes. (not sure how that's possible, but we can skip this jar)
             */
            return refs;
        }
        String classPath = attrs.getValue(Attributes.Name.CLASS_PATH);
        if (CodeGenUtil.isEmptyString(classPath)) {
            return refs;
        }

        String parentDir = FilenameUtils.getFullPath(file.getAbsolutePath());
        File possible;
        for (String path : StringUtils.splitStr(classPath, ' ')) {
            possible = new File(path);

            if (!possible.isAbsolute()) {
                // relative path?
                possible = new File(FilenameUtils.normalize(parentDir + path));
            }

            if (!refs.contains(possible)) {
                refs.add(possible);
            }
        }
    } catch (IOException e) {
        LOG.log(Level.WARNING, "Unable to load/read/parse Jar File: " + file.getAbsolutePath(), e);
    } finally {
        CodeGenUtil.closeQuietly(jar);
    }

    return refs;
}

From source file:org.artifactory.security.crypto.CryptoHelper.java

/**
 * @return Master encryption file configured location. The file might not exist.
 *//* w w  w . j  a va 2  s .  c  o m*/
public static File getMasterKeyFile() {
    ArtifactoryHome home = ArtifactoryHome.get();
    String keyFileLocation = ConstantValues.securityMasterKeyLocation.getString();
    File keyFile = new File(keyFileLocation);
    if (!keyFile.isAbsolute()) {
        keyFile = new File(home.getHaAwareEtcDir(), keyFileLocation);
    }
    return keyFile;
}

From source file:jp.ikedam.jenkins.plugins.extensible_choice_parameter.FilenameChoiceListProvider.java

/**
 * Returns a directory to scan for files from the passed path.
 * //from   ww  w .  j  a va  2 s . c om
 * Whether the path is relative or absolute, is resolved here.
 * 
 * @param baseDirPath a path string to the directory.
 * @return A file object specifying the proper path of the directory.
 */
protected static File getBaseDir(String baseDirPath) {
    File rawDir = new File(baseDirPath);
    if (rawDir.isAbsolute()) {
        return rawDir;
    }

    return new File(Jenkins.getInstance().getRootDir(), baseDirPath);
}

From source file:org.paxml.util.PaxmlUtils.java

/**
 * Create a file object./*from  ww w . ja  va  2  s  . c o m*/
 * 
 * @param pathWithoutPrefix
 *            the path without spring resource prefix.
 * @return if the given path is relative, try the following locations in
 *         order: - current working dir - user.home dir - dir pointed by
 *         PAXML_HOME system property - if file not found in all above
 *         locations, then return {@code}new File(pathWithoutPrefix);{@code}
 */
public static File getFile(String pathWithoutPrefix) {
    File file = new File(pathWithoutPrefix);
    if (!file.isAbsolute()) {
        file = getFileUnderCurrentDir(pathWithoutPrefix);
        if (!file.exists()) {
            file = getFileUnderUserHome(pathWithoutPrefix);
        }
        if (!file.exists()) {
            File f = getFileUnderPaxmlHome(pathWithoutPrefix, false);
            if (f != null && f.exists()) {
                file = f;
            }
        }
    }

    return file;
}

From source file:org.eclipse.smila.utils.config.ConfigUtils.java

/**
 * Gets the configuration stream for the denoted config file. The stream is to be closed by the caller.
 * <p>/* w  ww .ja  v  a 2s  .co  m*/
 * <strong>Note</strong>: While this methods supports the bundle name to be an arbitrary string that is not the name
 * of the bundle for "normal" configs the defaultConfigPath will only work if the given bundle name is actually the
 * name of the bundle.
 * 
 * @param bundleName
 *          the bundle name
 * @param configPath
 *          the path which may be either relative or absolute. <br/>
 *          <em>While absolute paths are supported they are strongly discurraged.</em>
 * @param defaultConfigPath
 *          the default config path to a resource that is contained in the bundle itself if the configPath doesnt
 *          resolve to an existing file.
 * 
 * @throws ConfigurationLoadException
 *           if the denoted config file doesnt exist or cannot be opened.
 */
public static InputStream getConfigStream(final String bundleName, final String configPath,
        final String defaultConfigPath) {

    String effectivePath = "<ConfigRoot>" + File.pathSeparator
            + new File(bundleName, configPath).getAbsolutePath();
    if (getConfigurationFolder() != null) {
        final File bundleConfigRoot = new File(getConfigurationFolder(), bundleName);
        if (bundleConfigRoot.exists()) {
            final File configFile = new File(configPath);
            final File effectiveConfigFile = configFile.isAbsolute() ? configFile
                    : new File(bundleConfigRoot, configPath);
            effectivePath = effectiveConfigFile.getAbsolutePath();
            if (effectiveConfigFile.exists()) {
                try {
                    return new FileInputStream(effectiveConfigFile);
                } catch (final Exception e) {
                    throw new ConfigurationLoadException("Failed to open config file: " + effectivePath);
                }
            }
        }
    }
    if (defaultConfigPath == null) {
        throw new ConfigurationLoadException(
                String.format("Configuration resource %s for the bundle %s not found", configPath, bundleName));
    }
    final Bundle bundle = Platform.getBundle(bundleName);
    if (bundle == null) {
        throw new ConfigurationLoadException("Unable to find bundle: " + bundleName);
    }
    final URL url = bundle.getEntry(defaultConfigPath);
    if (url == null) {
        throw new ConfigurationLoadException(
                String.format("Unable to find config file '%s' nor fallback config '%s' in bundle '%s'",
                        effectivePath, defaultConfigPath, bundleName));
    }
    try {
        return url.openStream();
    } catch (final IOException e) {
        throw new ConfigurationLoadException(e);
    }
}

From source file:org.apache.accumulo.core.rpc.SslConnectionParams.java

private static String findKeystore(String keystorePath) throws FileNotFoundException {
    try {/*from  ww w.  ja v  a  2  s  .  com*/
        // first just try the file
        File file = new File(keystorePath);
        if (file.exists())
            return file.getAbsolutePath();
        if (!file.isAbsolute()) {
            // try classpath
            URL url = SslConnectionParams.class.getClassLoader().getResource(keystorePath);
            if (url != null) {
                file = new File(url.toURI());
                if (file.exists())
                    return file.getAbsolutePath();
            }
        }
    } catch (Exception e) {
        log.warn("Exception finding keystore", e);
    }
    throw new FileNotFoundException("Failed to load SSL keystore from " + keystorePath);
}