Example usage for org.apache.commons.io FilenameUtils isExtension

List of usage examples for org.apache.commons.io FilenameUtils isExtension

Introduction

In this page you can find the example usage for org.apache.commons.io FilenameUtils isExtension.

Prototype

public static boolean isExtension(String filename, Collection<String> extensions) 

Source Link

Document

Checks whether the extension of the filename is one of those specified.

Usage

From source file:org.codice.ddf.spatial.geocoding.extract.GeoNamesFileExtractor.java

/**
 * Determines the appropriate InputStream to get from the given file resource.  If there is no
 * file extension, the resource is downloaded from a url ( default : http://download.geonames.org/export/dump/ )
 * otherwise it is treated as an absolute path for a file.
 *
 * @param resource           - a String representing the resource.  Could be a data set from GeoNames
 *                           (allCities, cities15000, AU, US etc) or an absolute path.
 * @param extractionCallback - the callback to receive updates about the progress, may be
 *                           null if you don't want any updates
 * @return the InputStream for the given resource
 * @throws GeoEntryExtractionException     if an error occurs during extraction, obtaining the InputStream
 *                                         from a local resource, or if the resource is not a .zip or .txt file
 * @throws GeoNamesRemoteDownloadException if an error occurs when attempting to get an InputStream
 *                                         from a URL
 *//*from w  w w  .  ja v  a 2 s  . c  o m*/

private InputStream getInputStreamFromResource(String resource, ProgressCallback extractionCallback)
        throws GeoEntryExtractionException, GeoNamesRemoteDownloadException {

    if (FilenameUtils.isExtension(resource, "zip")) {
        InputStream inputStream = getFileInputStream(resource);
        return unZipInputStream(resource, inputStream);

    } else if (FilenameUtils.isExtension(resource, "txt")) {
        return getFileInputStream(resource);

    } else if (StringUtils.isAlphanumeric(resource)) {

        // Support the "all" keyword
        if (resource.equalsIgnoreCase("all")) {
            resource = "allCountries";
            // Support case insensitive "cities" keyword
        } else if (resource.matches("((?i)cities[0-9]+)")) {
            resource = resource.toLowerCase();
            // Support case insensitive country codes
        } else if (!resource.equalsIgnoreCase("allCountries")) {
            resource = resource.toUpperCase();
        }

        Response response = createConnection(resource);
        InputStream urlInputStream = getUrlInputStreamFromWebClient();
        InputStream inputStream = getInputStreamFromUrl(resource, response, urlInputStream, extractionCallback);

        return unZipInputStream(resource, inputStream);
    }
    throw new GeoEntryExtractionException("Unable to update the index.  " + resource
            + " is not a .zip or .txt, or an invalid country code was entered.");
}

From source file:org.datacleaner.extension.output.CreateExcelSpreadsheetAnalyzer.java

@Validate
public void validate() {
    sheetName = sheetName.trim();/*from w  w w  . jav  a  2 s .  c  o m*/

    if (sheetName.length() > SHEET_NAME_MAX_LENGTH) {
        throw new IllegalStateException(
                "Sheet name must be maximum " + SHEET_NAME_MAX_LENGTH + " characters long");
    }

    for (char c : SHEET_NAME_ILLEGAL_CHARS) {
        if (sheetName.indexOf(c) != -1) {
            throw new IllegalStateException("Sheet name cannot contain '" + c + "'");
        }
    }

    if (file.exists()) {
        Datastore datastore = new ExcelDatastore(file.getName(), new FileResource(file),
                file.getAbsolutePath());
        try (final DatastoreConnection connection = datastore.openConnection()) {
            final String[] tableNames = connection.getDataContext().getDefaultSchema().getTableNames();
            for (int i = 0; i < tableNames.length; i++) {
                if (tableNames[i].equals(sheetName)) {
                    if (!overwriteSheetIfExists) {
                        throw new IllegalStateException("The sheet '" + sheetName
                                + "' already exists. Please select another sheet name.");
                    }
                }
            }
        }
    }

    if (!FilenameUtils.isExtension(file.getName(), excelExtension)) {
        throw new IllegalStateException("Please add the '.xlsx'  or '.xls' extension to the filename");
    }

}

From source file:org.eclipse.thym.ios.core.xcode.IOSPluginInstallationActionsFactory.java

@Override
public IPluginInstallationAction getConfigFileAction(String target, String parent, String value) {
    File[] configFile = FileUtils.resolveFile(getProjectDirectory(), target);
    File file = configFile[0];/*www . j  a v a2s.c o m*/

    if (configFile.length == 1 && FilenameUtils.isExtension(file.toString(), "plist")) {
        return new PlistConfigFileAction(file, parent, value);
    }
    if (configFile.length > 1) {
        HybridProject hybridProject = HybridProject.getHybridProject(getProject());
        String projectPfile = hybridProject.getBuildArtifactAppName() + "-Info.plist";
        for (File cfile : configFile) {
            if (FilenameUtils.getBaseName(cfile.toString()).equals(projectPfile)) {
                return new PlistConfigFileAction(cfile, parent, value);
            }
        }
    }
    return new XMLConfigFileAction(file, parent, value);
}

From source file:org.freeplane.plugin.script.GenericScript.java

private boolean disableScriptCompilation(File scriptFile) {
    return FilenameUtils.isExtension(scriptFile.getName(),
            ScriptResources.SCRIPT_COMPILATION_DISABLED_EXTENSIONS);
}

From source file:org.gdms.TestBase.java

public File getTempCopyOf(File f) throws IOException {
    File p = dsf.getResultDir().getParentFile();
    File dest = File.createTempFile("temp", f.getName(), p);
    dest.delete();// ww w .j a  v a2  s .c o m
    if (FilenameUtils.isExtension(f.getName(), "shp")) {
        String destbase = FilenameUtils.removeExtension(dest.getName());
        FileUtils.copyFile(org.orbisgis.utils.FileUtils.getFileWithExtension(f, "dbf"),
                new File(dest.getParent(), destbase + ".dbf"));
        FileUtils.copyFile(org.orbisgis.utils.FileUtils.getFileWithExtension(f, "shx"),
                new File(dest.getParent(), destbase + ".shx"));
    }

    FileUtils.copyFile(f, dest);
    dest.deleteOnExit();
    return dest;
}

From source file:org.geoserver.security.iride.util.io.Filename.java

/**
 * Checks whether the extension of the filename is that specified, appending it if not.
 * Returns the filename, with the specified extension if it was not found.
 *
 * @param filename the filename to check
 * @param extension the extension to check for, {@code null} or empty checks for no extension
 * @return the filename, with the specified extension if it was not found
 *//*from   ww  w .ja  va 2 s .  co  m*/
public static String ensureFileWithExtension(String filename, String extension) {
    if (!FilenameUtils.isExtension(filename, extension)) {
        return filename + "." + extension;
    }

    return filename;
}

From source file:org.getspout.spout.io.FileUtil.java

public static boolean canCache(File file) {
    String filename = FileUtil.getFileName(file.getPath());
    return FilenameUtils.isExtension(filename, validExtensions);
}

From source file:org.getspout.spout.io.FileUtil.java

public static boolean canCache(String fileUrl) {
    String filename = FileUtil.getFileName(fileUrl);
    return FilenameUtils.isExtension(filename, validExtensions);
}

From source file:org.getspout.spout.player.SimpleFileManager.java

@Override
public boolean canCache(File file) {
    String filename = FileUtil.getFileName(file.getPath());
    return FilenameUtils.isExtension(filename, validExtensions);
}

From source file:org.getspout.spout.player.SimpleFileManager.java

@Override
public boolean canCache(String fileUrl) {
    String filename = FileUtil.getFileName(fileUrl);
    return FilenameUtils.isExtension(filename, validExtensions);
}