Example usage for org.apache.commons.io.filefilter IOFileFilter IOFileFilter

List of usage examples for org.apache.commons.io.filefilter IOFileFilter IOFileFilter

Introduction

In this page you can find the example usage for org.apache.commons.io.filefilter IOFileFilter IOFileFilter.

Prototype

IOFileFilter

Source Link

Usage

From source file:org.opendatakit.services.database.utlities.ODKDatabaseImplUtils.java

/**
 * Drop the given tableId and remove all the files (both configuration and
 * data attachments) associated with that table.
 *
 * @param db/* w  w w  .  j a va  2 s . c o  m*/
 * @param tableId
 */
public void deleteTableAndAllData(OdkConnectionInterface db, final String tableId) {

    SyncETagsUtils seu = new SyncETagsUtils();
    boolean dbWithinTransaction = db.inTransaction();

    Object[] whereArgs = { tableId };

    try {
        if (!dbWithinTransaction) {
            db.beginTransactionNonExclusive();
        }

        // Drop the table used for the formId
        StringBuilder b = new StringBuilder();
        b.append("DROP TABLE IF EXISTS ").append(tableId).append(";");
        db.execSQL(b.toString(), null);

        // Delete the server sync ETags associated with this table
        seu.deleteAllSyncETagsForTableId(db, tableId);

        // Delete the table definition for the tableId
        int count;
        {
            String whereClause = K_TABLE_DEFS_TABLE_ID_EQUALS_PARAM;

            count = db.delete(DatabaseConstants.TABLE_DEFS_TABLE_NAME, whereClause, whereArgs);
        }

        // Delete the column definitions for this tableId
        {
            String whereClause = K_COLUMN_DEFS_TABLE_ID_EQUALS_PARAM;

            db.delete(DatabaseConstants.COLUMN_DEFINITIONS_TABLE_NAME, whereClause, whereArgs);
        }

        // Delete the uploads for the tableId
        {
            String uploadWhereClause = InstanceColumns.DATA_TABLE_TABLE_ID + " = ?";
            db.delete(DatabaseConstants.UPLOADS_TABLE_NAME, uploadWhereClause, whereArgs);
        }

        // Delete the values from the 4 key value stores
        {
            String whereClause = K_KVS_TABLE_ID_EQUALS_PARAM;

            db.delete(DatabaseConstants.KEY_VALUE_STORE_ACTIVE_TABLE_NAME, whereClause, whereArgs);
        }

        if (!dbWithinTransaction) {
            db.setTransactionSuccessful();
        }

    } finally {
        if (!dbWithinTransaction) {
            db.endTransaction();
        }
    }

    // And delete the files from the SDCard...
    String tableDir = ODKFileUtils.getTablesFolder(db.getAppName(), tableId);
    try {
        FileUtils.deleteDirectory(new File(tableDir));
    } catch (IOException e1) {
        e1.printStackTrace();
        throw new IllegalStateException("Unable to delete the " + tableDir + " directory", e1);
    }

    String assetsCsvDir = ODKFileUtils.getAssetsCsvFolder(db.getAppName());
    try {
        File file = new File(assetsCsvDir);
        if (file.exists()) {
            Collection<File> files = FileUtils.listFiles(file, new IOFileFilter() {

                @Override
                public boolean accept(File file) {
                    String[] parts = file.getName().split("\\.");
                    return (parts[0].equals(tableId) && parts[parts.length - 1].equals("csv")
                            && (parts.length == 2 || parts.length == 3
                                    || (parts.length == 4 && parts[parts.length - 2].equals("properties"))));
                }

                @Override
                public boolean accept(File dir, String name) {
                    String[] parts = name.split("\\.");
                    return (parts[0].equals(tableId) && parts[parts.length - 1].equals("csv")
                            && (parts.length == 2 || parts.length == 3
                                    || (parts.length == 4 && parts[parts.length - 2].equals("properties"))));
                }
            }, new IOFileFilter() {

                // don't traverse into directories
                @Override
                public boolean accept(File arg0) {
                    return false;
                }

                // don't traverse into directories
                @Override
                public boolean accept(File arg0, String arg1) {
                    return false;
                }
            });

            FileUtils.deleteDirectory(new File(tableDir));
            for (File f : files) {
                FileUtils.deleteQuietly(f);
            }
        }
    } catch (IOException e1) {
        e1.printStackTrace();
        throw new IllegalStateException("Unable to delete the " + tableDir + " directory", e1);
    }
}

From source file:org.sonar.plugins.python.DirectoryScanner.java

public List<File> getIncludedFiles() {
    final String baseDirAbsolutePath = baseDir.getAbsolutePath();
    IOFileFilter fileFilter = new IOFileFilter() {

        @Override/*from  w  w  w.  j  av a2 s .c  om*/
        public boolean accept(File dir, String name) {
            return accept(new File(dir, name));
        }

        @Override
        public boolean accept(File file) {
            String path = file.getAbsolutePath();
            path = path.substring(Math.min(baseDirAbsolutePath.length(), path.length()));
            return pattern.match(FilenameUtils.separatorsToUnix(path));
        }
    };
    return Lists.newArrayList(FileUtils.listFiles(baseDir, fileFilter, TrueFileFilter.INSTANCE));
}

From source file:org.walkmod.util.FileResource.java

@Override
public Iterator<File> iterator() {
    String fileNormalized = FilenameUtils.normalize(file.getAbsolutePath(), true);
    if (includes != null) {
        for (int i = 0; i < includes.length; i++) {

            if (!includes[i].startsWith(fileNormalized)) {

                includes[i] = fileNormalized + "/" + includes[i];

            }/*from  w  w  w. j  av a2 s  . c  o m*/
            if (includes[i].endsWith("**")) {
                includes[i] = includes[i].substring(0, includes[i].length() - 3);
            }
        }
    }
    if (excludes != null) {
        for (int i = 0; i < excludes.length; i++) {

            if (!excludes[i].startsWith(fileNormalized)) {
                excludes[i] = fileNormalized + "/" + excludes[i];

            }
            if (excludes[i].endsWith("**")) {
                excludes[i] = excludes[i].substring(0, excludes[i].length() - 3);
            }
        }
    }

    if (file.isDirectory()) {

        IOFileFilter filter = null;

        IOFileFilter directoryFilter = TrueFileFilter.INSTANCE;
        if (excludes != null || includes != null) {

            directoryFilter = new IOFileFilter() {

                @Override
                public boolean accept(File dir, String name) {

                    boolean excludesEval = false;
                    boolean includesEval = false;
                    String aux = FilenameUtils.normalize(name, true);
                    if (excludes != null) {
                        for (int i = 0; i < excludes.length && !excludesEval; i++) {
                            excludesEval = (FilenameUtils.wildcardMatch(aux, excludes[i])
                                    || dir.getAbsolutePath().startsWith(excludes[i]));
                        }
                    }
                    if (includes != null) {
                        for (int i = 0; i < includes.length && !includesEval; i++) {
                            includesEval = matches(aux, includes[i]);
                        }
                    } else {
                        includesEval = true;
                    }
                    return (includesEval && !excludesEval) || (includes == null && excludes == null);

                }

                @Override
                public boolean accept(File file) {
                    boolean excludesEval = false;
                    boolean includesEval = false;

                    String aux = FilenameUtils.normalize(file.getAbsolutePath(), true);
                    if (excludes != null) {

                        for (int i = 0; i < excludes.length && !excludesEval; i++) {
                            excludesEval = (FilenameUtils.wildcardMatch(aux, excludes[i])
                                    || file.getParentFile().getAbsolutePath().startsWith(excludes[i]));
                        }
                    }
                    if (includes != null) {
                        for (int i = 0; i < includes.length && !includesEval; i++) {
                            includesEval = matches(aux, includes[i]);
                        }
                    } else {
                        includesEval = true;
                    }
                    boolean result = (includesEval && !excludesEval) || (includes == null && excludes == null);

                    return result;

                }
            };
            if (extensions == null) {
                filter = directoryFilter;

            } else {
                String[] suffixes = toSuffixes(extensions);
                filter = new SuffixFileFilter(suffixes);
            }

        } else {
            if (extensions == null) {
                filter = TrueFileFilter.INSTANCE;
            } else {
                String[] suffixes = toSuffixes(extensions);
                filter = new SuffixFileFilter(suffixes);
            }
        }

        return FileUtils.listFiles(file, filter, directoryFilter).iterator();
    }
    Collection<File> aux = new LinkedList<File>();
    if (extensions == null) {
        aux.add(file);
    }
    return aux.iterator();
}

From source file:org.wisdom.maven.mojos.JavaCompilerMojo.java

/**
 * A new (accepted) file was deleted. This methods triggers the Java compilation.
 *
 * @param file the file/*from  ww  w .  ja v a2  s.  co  m*/
 * @return {@literal true}
 * @throws WatchingException thrown on compilation error. The thrown exception contains the file, line,
 *                           character and reason of the compilation error.
 */
@Override
public boolean fileDeleted(final File file) throws WatchingException {
    // Delete the associated class file.
    // We delete more than required... but the inner class case is very tricky.
    Collection<File> files = FileUtils.listFiles(classes, new IOFileFilter() {
        @Override
        public boolean accept(File test) {
            String classname = FilenameUtils.getBaseName(test.getName());
            String filename = FilenameUtils.getBaseName(file.getName());
            return classname.equals(filename) || classname.startsWith(filename + "$");
        }

        @Override
        public boolean accept(File dir, String name) {
            return accept(new File(dir, name));
        }
    }, TrueFileFilter.INSTANCE);

    for (File clazz : files) {
        getLog().debug("Deleting " + clazz.getAbsolutePath() + " : " + clazz.delete());
    }

    compile();
    return true;
}

From source file:waazdoh.client.storage.local.FileBeanStorage.java

private Iterator<File> getFileItarator(final String search) {
    return FileUtils.iterateFiles(new File(path), new IOFileFilter() {

        @Override//  w w  w.  ja v a 2s. c om
        public boolean accept(File f) {
            String path = f.getAbsolutePath().replace(File.separator, "");
            log.info("fileiterator accept " + f);
            return path.indexOf(search) >= 0;
        }

        @Override
        public boolean accept(File arg0, String arg1) {
            log.info("not accepting " + arg0);

            return false;
        }

    }, new IOFileFilter() {

        @Override
        public boolean accept(File arg0, String arg1) {
            return true;
        }

        @Override
        public boolean accept(File arg0) {
            return true;
        }
    });
}

From source file:zz.filecollector.ExtensionRegister.java

public IOFileFilter getFileFilter() {
    return new IOFileFilter() {
        @Override//  w  ww.  j a v  a  2s . c o  m
        public boolean accept(File file) {
            if (file != null) {
                return this.accept(file.getParentFile(), file.getName());
            }
            return false;
        }

        @Override
        public boolean accept(File dir, String name) {
            if (StringUtils.isNoneEmpty(name)) {
                String ext = FilenameUtils.getExtension(name);
                if (StringUtils.isNoneEmpty(ext)) {
                    return isPhoto(ext) || isVideo(ext);
                }
            }
            return false;
        }
    };
}