Example usage for com.google.common.io PatternFilenameFilter PatternFilenameFilter

List of usage examples for com.google.common.io PatternFilenameFilter PatternFilenameFilter

Introduction

In this page you can find the example usage for com.google.common.io PatternFilenameFilter PatternFilenameFilter.

Prototype

public PatternFilenameFilter(Pattern pattern) 

Source Link

Document

Constructs a pattern file name filter object.

Usage

From source file:fr.loria.parole.artimate.model.PosConcatenator.java

/**
 * Concatenate all <tt>*.pos</tt> files in directory <code>arg[0]</code> to a single target <tt>.pos</tt> file
 * <code>args[1]</code>./* ww w .j a  va  2s.  c  om*/
 * 
 * @param args
 *            source directory, target file
 * @throws IOException
 */
public static void main(String[] args) throws IOException {
    String inputDirectoryName = args[0];
    String outputFileName = args[1];

    // configure logging
    String logFormat = System.getProperty("log.format");
    String logLevel = System.getProperty("log.level");
    if (logFormat == null || logFormat.isEmpty()) {
        logFormat = "[%c{1}] [%p] %m%n";
    }
    ConsoleAppender consoleAppender = new ConsoleAppender(new PatternLayout(logFormat));
    BasicConfigurator.configure(consoleAppender);
    Level level = Level.toLevel(logLevel);
    Logger.getRootLogger().setLevel(level);

    File inputDirectory = new File(inputDirectoryName);
    PatternFilenameFilter posFileFilter = new PatternFilenameFilter(".*\\.pos");
    File[] inputFiles = inputDirectory.listFiles(posFileFilter);
    if (inputFiles == null) {
        logger.error("Could not find input *.pos files in " + inputDirectoryName);
        throw new IOException();
    }
    logger.debug("Found " + inputFiles.length + " input files");

    File outputFile = new File(outputFileName);
    Files.createParentDirs(outputFile);
    FileOutputStream output = Files.newOutputStreamSupplier(outputFile).getOutput();
    logger.info("Appending to " + outputFileName);

    for (File input : inputFiles) {
        Files.copy(input, output);
        logger.debug("Appended " + input.getName());
    }
}

From source file:fr.loria.parole.artimate.model.TextGridConcatenator.java

/**
 * Concatenate all <tt>*.TextGrid</tt> files in directory <code>arg[0]</code> to a single target <tt>.lab</tt> file
 * <code>args[1]</code>./* www  .jav  a2 s .  co m*/
 * 
 * @param args
 *            source directory, target file
 * @throws Exception
 */
public static void main(String[] args) throws Exception {
    String inputDirectoryName = args[0];
    String outputFileName = args[1];

    // configure logging
    String logFormat = System.getProperty("log.format");
    String logLevel = System.getProperty("log.level");
    if (logFormat == null || logFormat.isEmpty()) {
        logFormat = "[%c{1}] [%p] %m%n";
    }
    ConsoleAppender consoleAppender = new ConsoleAppender(new PatternLayout(logFormat));
    BasicConfigurator.configure(consoleAppender);
    Level level = Level.toLevel(logLevel);
    Logger.getRootLogger().setLevel(level);

    File inputDirectory = new File(inputDirectoryName);
    PatternFilenameFilter tgFileFilter = new PatternFilenameFilter(".*\\.TextGrid");
    File[] inputFiles = inputDirectory.listFiles(tgFileFilter);
    if (inputFiles == null) {
        logger.error("Could not find input *.TextGrid files in " + inputDirectoryName);
        throw new IOException();
    }
    logger.debug("Found " + inputFiles.length + " input files");

    File outputFile = new File(outputFileName);
    Files.createParentDirs(outputFile);
    logger.info("Appending to " + outputFileName);

    Annotation annotation = null;
    for (File input : inputFiles) {
        TextGrid inputTextGrid = (TextGrid) new PraatTextFile().read(input);
        if (annotation == null) {
            annotation = new Annotation(inputTextGrid);
        } else {
            annotation.append(new Annotation(inputTextGrid));
        }

        logger.debug("Appended " + input.getName());
    }
    TextGrid outputTextGrid = annotation.toTextGrid();
    PraatFile.writeText(outputTextGrid, outputFile, Charsets.UTF_8);
}

From source file:com.facebook.util.FileUtils.java

public static String backtrackToFile(String startDir, String targetFile) {
    FilenameFilter filenameFilter = new PatternFilenameFilter(targetFile);
    File currentDir = new File(startDir).getAbsoluteFile();

    while (currentDir.getParentFile() != null) {
        String[] files = currentDir.list(filenameFilter);
        if (files.length == 1) {
            return currentDir.getAbsolutePath();
        } else {//from  w w  w  .  j a va 2s .  c o  m
            currentDir = currentDir.getParentFile();
        }
    }

    return null;
}

From source file:org.jberet.testapps.loopback.ScanDirectoryBatchlet.java

@Override
public String process() throws Exception {
    final File[] files = directory.listFiles(new PatternFilenameFilter(pattern));
    if (files == null || files.length == 0) {
        jobContext.setTransientUserData(null);
        return END;
    }/* w  ww . j  a v a  2  s.c o  m*/
    jobContext.setTransientUserData(files[0]);
    return CONTINUE;
}

From source file:net.myrrix.online.generation.InputFilesReader.java

static void readInputFiles(FastByIDMap<FastIDSet> knownItemIDs, FastByIDMap<FastByIDFloatMap> rbyRow,
        FastByIDMap<FastByIDFloatMap> rbyColumn, FastIDSet itemTagIDs, FastIDSet userTagIDs, File inputDir)
        throws IOException {

    FilenameFilter csvFilter = new PatternFilenameFilter(".+\\.csv(\\.(zip|gz))?");

    File[] otherFiles = inputDir.listFiles(new InvertedFilenameFilter(csvFilter));
    if (otherFiles != null) {
        for (File otherFile : otherFiles) {
            log.info("Skipping file {}", otherFile.getName());
        }//from   w  w  w. j  a v  a 2  s  .  c om
    }

    File[] inputFiles = inputDir.listFiles(csvFilter);
    if (inputFiles == null) {
        log.info("No input files in {}", inputDir);
        return;
    }
    Arrays.sort(inputFiles, ByLastModifiedComparator.INSTANCE);

    IDMigrator hash = new OneWayMigrator();

    int lines = 0;
    int badLines = 0;
    for (File inputFile : inputFiles) {
        log.info("Reading {}", inputFile);
        for (String line : new FileLineIterable(inputFile)) {

            if (badLines > 100) { // Crude check
                throw new IOException("Too many bad lines; aborting");
            }

            lines++;

            if (line.isEmpty() || line.charAt(0) == '#') {
                continue;
            }

            Iterator<String> it = COMMA.split(line).iterator();

            long userID;
            boolean userIsTag;
            long itemID;
            boolean itemIsTag;
            float value;
            try {

                String userIDString = it.next();
                userIsTag = userIDString.startsWith("\"");
                if (userIsTag) {
                    userID = hash.toLongID(userIDString.substring(1, userIDString.length() - 1));
                } else {
                    userID = Long.parseLong(userIDString);
                }

                String itemIDString = it.next();
                itemIsTag = itemIDString.startsWith("\"");
                if (itemIsTag) {
                    itemID = hash.toLongID(itemIDString.substring(1, itemIDString.length() - 1));
                } else {
                    itemID = Long.parseLong(itemIDString);
                }

                if (it.hasNext()) {
                    String valueToken = it.next();
                    value = valueToken.isEmpty() ? Float.NaN : LangUtils.parseFloat(valueToken);
                } else {
                    value = 1.0f;
                }

            } catch (NoSuchElementException ignored) {
                log.warn("Ignoring line with too few columns: '{}'", line);
                badLines++;
                continue;
            } catch (IllegalArgumentException iae) { // includes NumberFormatException
                if (lines == 1) {
                    log.info("Ignoring header line: '{}'", line);
                } else {
                    log.warn("Ignoring unparseable line: '{}'", line);
                    badLines++;
                }
                continue;
            }

            if (userIsTag && itemIsTag) {
                log.warn("Two tags not allowed: '{}'", line);
                badLines++;
                continue;
            }

            if (userIsTag) {
                itemTagIDs.add(userID);
            }

            if (itemIsTag) {
                userTagIDs.add(itemID);
            }

            if (Float.isNaN(value)) {
                // Remove, not set
                MatrixUtils.remove(userID, itemID, rbyRow, rbyColumn);
            } else {
                MatrixUtils.addTo(userID, itemID, value, rbyRow, rbyColumn);
            }

            if (knownItemIDs != null) {
                FastIDSet itemIDs = knownItemIDs.get(userID);
                if (Float.isNaN(value)) {
                    // Remove, not set
                    if (itemIDs != null) {
                        itemIDs.remove(itemID);
                        if (itemIDs.isEmpty()) {
                            knownItemIDs.remove(userID);
                        }
                    }
                } else {
                    if (itemIDs == null) {
                        itemIDs = new FastIDSet();
                        knownItemIDs.put(userID, itemIDs);
                    }
                    itemIDs.add(itemID);
                }
            }

            if (lines % 1000000 == 0) {
                log.info("Finished {} lines", lines);
            }
        }
    }

    log.info("Pruning near-zero entries");
    removeSmall(rbyRow);
    removeSmall(rbyColumn);
}

From source file:cherry.common.testtool.StubConfigLoader.java

@Override
public void afterPropertiesSet() throws IOException {
    File[] jsonfiles = defDir.listFiles(new PatternFilenameFilter(".*\\.json$"));
    configure(jsonfiles, jsonObjectMapper);
    File[] yamlfiles = defDir.listFiles(new PatternFilenameFilter(".*\\.yaml$"));
    configure(yamlfiles, yamlObjectMapper);
}

From source file:org.apache.people.mreutegg.mhmp.RenderCommandExecutor.java

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    if (!(sender instanceof Player)) {
        return false;
    }/*from   w w  w . j ava2  s.c  o  m*/
    Player player = (Player) sender;
    if (command.getName().equalsIgnoreCase("render")) {
        if (args.length < 1) {
            sender.sendMessage("You need to specify a .xyz file.");
            return false;
        }
        Iterable<Coordinate> coordinates;
        try {
            List<File> files = Lists.newArrayList(new File(".").listFiles(new PatternFilenameFilter(args[0])));
            Iterable<Iterable<Coordinate>> allCoords = transform(files, file -> {
                try {
                    if (file.getName().endsWith(".asc")) {
                        return ASCReader.fromFile(file);
                    } else {
                        return CoordinateReader.fromFile(file);
                    }
                } catch (IOException e) {
                    throw new UncheckedIOException(e);
                }
            });
            coordinates = Iterables.concat(allCoords);
        } catch (UncheckedIOException e) {
            plugin.getLogger().warning("File not found " + args[0]);
            return false;
        }
        Vector scale = scaleFromArgs(args);
        new RenderTask(plugin, player, coordinates, scale).runTaskAsynchronously(plugin);
        return true;
    }
    return false;
}

From source file:com.nesscomputing.migratory.loader.FileLoader.java

@Override
public Collection<URI> loadFolder(final URI folderUri, final String searchPattern) {
    final Pattern pattern = (searchPattern == null) ? null : Pattern.compile(searchPattern);

    final File folderLocation = new File(folderUri);
    if (folderLocation.exists() && folderLocation.canRead()) {
        if (folderLocation.isDirectory()) {
            final File[] files = (pattern == null) ? folderLocation.listFiles()
                    : folderLocation.listFiles(new PatternFilenameFilter(pattern));
            return Collections2.transform(Arrays.asList(files), FILE_TO_URL);
        } else {//from   ww  w  .j  a  v  a  2  s .  co  m
            throw new MigratoryException(Reason.INTERNAL, "%s is not a directory!", folderUri);
        }
    } else {
        throw new MigratoryException(Reason.INTERNAL, "Can not access %s!", folderUri);
    }
}

From source file:eu.crisis_economics.abm.dashboard.cluster.ExperimentEndHandler.java

@Override
@Async/*from  w ww .ja  v  a2 s  . com*/
public void signalEnd(final List<RecorderInfo> recorders, final File workingDir) {
    // let's merge the results
    String fileName = recorders.get(0).getOutputFile().getName();

    int i = 1;
    File[] genDirs = workingDir.listFiles(new PatternFilenameFilter(".*gen-\\d+"));
    for (File genDir : genDirs) {
        File file = new File(genDir, fileName);
        try {
            Files.copy(file, new File(fileName + ".part" + i++));
        } catch (IOException e) {
            log.error("Could not copy '" + file.getName() + "' to '" + fileName + ".part" + i, e);
        }

    }

    ResultFileMerger merger = new ResultFileMerger();
    merger.merge(recorders, workingDir);

    context.publishEvent(new ClusterMain.ExperimentEndedEvent(this));

}

From source file:com.base2.kagura.core.storage.FileReportsProvider.java

/**
 * Custom separate dload report component, so it can be called elsewhere, or overwritten by child Providers. Checks
 * the "report" to ensure it is a directory then looks for reportconf.yaml or reportconf.json inside the file. If it
 * exists loads it./* www  .  j ava2 s  .  c  o  m*/
 * @param result The collection of reports to load the contents into.
 * @param report The directory that contains the report files.
 * @param reportId The report id
 * @throws IOException
 */
protected void loadReport(ReportsConfig result, File report, String reportId) throws IOException {
    if (report.isDirectory()) {
        FilenameFilter configYamlFilter = new PatternFilenameFilter("^reportconf.(yaml|json)$");
        File[] selectYaml = report.listFiles(configYamlFilter);
        if (selectYaml != null && selectYaml.length == 1) {
            File selectedYaml = selectYaml[0];
            loadReport(result, FileUtils.openInputStream(selectedYaml), reportId);
        }
    }
}