Example usage for org.apache.commons.io FileUtils iterateFiles

List of usage examples for org.apache.commons.io FileUtils iterateFiles

Introduction

In this page you can find the example usage for org.apache.commons.io FileUtils iterateFiles.

Prototype

public static Iterator iterateFiles(File directory, String[] extensions, boolean recursive) 

Source Link

Document

Allows iteration over the files in a given directory (and optionally its subdirectories) which match an array of extensions.

Usage

From source file:org.oscarehr.admin.traceability.GenerateTraceabilityUtil.java

public static Map<String, String> buildTraceMap(HttpServletRequest request) throws Exception {
    Map<String, String> traceMap = new HashMap<String, String>();
    HttpSession session = request.getSession();
    ServletContext servletContext = session.getServletContext();
    String realPath = servletContext.getRealPath("/");
    Iterator<File> iterator = FileUtils.iterateFiles(new File(realPath), null, true);
    while (iterator.hasNext()) {
        File f_ = iterator.next();
        FileInputStream fi_ = new FileInputStream(f_);
        String path = f_.getAbsolutePath();
        path = path.replace(realPath, "");
        traceMap.put(path, DigestUtils.sha256Hex(fi_));
        fi_.close();//from   w  ww. j av  a  2 s. c  o m
    }

    return traceMap;
}

From source file:org.patterson.WebsiteGenerator.java

/**
 * @throws IOException/*  w w w .  jav a  2  s.c om*/
 * @throws WebsiteGeneratorException
 * 
 */
@SuppressWarnings("unchecked")
private void processSourceDir() throws IOException, WebsiteGeneratorException {
    File tempSrcDir = new File(settings.getSrcDir());
    logger.info("Processing source dir: " + tempSrcDir.getAbsolutePath());
    processor = getTemplateProcessor();

    for (Iterator<File> tempFileIter = FileUtils.iterateFiles(tempSrcDir, null, true); tempFileIter
            .hasNext();) {
        File tempFile = tempFileIter.next();
        currentResource = createResource();
        initResourceFor(currentResource, tempFile);

        if (tempFile.isDirectory()) {
            // no op
        } else if (tempFile.getParent() != null && tempFile.getParentFile().getName().equals("CVS")) {
            logger.fine("Skipping CVS resource " + tempFile.getAbsolutePath());
        } else if (processor.isCopyFile(tempFile)) {
            copyFileToTargetDir(tempFile);
        } else if (processor.isProcessingRequired(tempFile)) {
            processor.process(this, tempFile, null);
        } else {
            // Maybe a resource referenced by a velocity template
            logger.fine("Skipping velocity resource " + tempFile.getAbsolutePath());
        }
        menuManager = null;// reset, so every file gets the original menu
    }
}

From source file:org.paxle.filter.blacklist.impl.BlacklistFileStore.java

/**
 * Reads all blacklists from the blacklist directory and instantiates the 
 * blacklist objects.//from   www .  ja v  a 2s .  co  m
 */
@SuppressWarnings("unchecked")
private void readBlacklists() {
    // init the blacklist objects
    Iterator<File> eter = FileUtils.iterateFiles(this.blacklistDir, null, false);
    this.logger.info(String.format("Reading blacklists from directory: %s", blacklistDir.toString()));

    final StringBuilder blacklistNames = new StringBuilder();
    while (eter.hasNext()) {
        final File blacklistFile = eter.next();
        final String blacklistName = blacklistFile.getName();
        try {
            final IBlacklist blacklist = new Blacklist(blacklistName, FileUtils.readLines(blacklistFile), this);
            this.blacklists.put(blacklistName, blacklist);
            blacklistNames.append(String.format("\n- '%s' : %d pattern(s)", blacklistName, blacklist.size()));
        } catch (InvalidBlacklistnameException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    this.logger.info(String.format("%d blacklist(s) found: %s", this.blacklists.size(), blacklistNames));
}

From source file:org.retroduction.carma.reportgenerator.beanbuilder.ProjectSourceCodeFileListBeanBuilder.java

@SuppressWarnings("unchecked")
public ProjectSourceCodeFileListBean getSourceCodeFileList(MutationRun report, List<File> sourceFolders) {

    ProjectSourceCodeFileListBean project = new ProjectSourceCodeFileListBean();

    for (File folderName : sourceFolders) {

        try {/*from  w  w  w  .  j  av  a  2 s  .  com*/
            URL sourceFolderUrl = folderName.toURL();

            Iterator fileIterator = FileUtils.iterateFiles(folderName, new String[] { "java" }, true);

            while (fileIterator.hasNext()) {
                try {

                    File sourceFileName = (File) fileIterator.next();

                    URL sourceFileUrl = sourceFileName.toURL();

                    SourceFile sourceFile = new SourceFile();

                    sourceFile.setFileName(sourceFileName.getPath());
                    sourceFile.setPackageName(this.extractPackageName(sourceFolderUrl, sourceFileUrl));
                    sourceFile.setClassName(FilenameUtils.getBaseName(sourceFileUrl.toString()));

                    sourceFile.setSourceText(FileUtils.readLines(new File(sourceFileUrl.getPath())));
                    project.addSourceFile(sourceFile);
                } catch (IOException e) {
                    logger.warn("Source folder " + folderName + " is invalid. Skipping ...");
                    continue;
                }

            }
        } catch (IOException e) {
            logger.warn("Source folder " + folderName + " is invalid. Skipping ...");
            continue;
        }

    }

    return project;

}

From source file:org.semanticscience.narf.structures.factories.tertiary.X3DnaDssr.java

/**
 * Get all nucleic acid extracted structures found in the input directory.
 * Store the annotator's output files in the output directory
 * /*from ww w. j  a  v  a 2  s  .  c  o m*/
 * @param anInputDir
 *            the input directory containing pdb files
 * @param anOutputDir
 *            the directory where the output of the annotator will be stored
 * @return a map of a set of extracted tertiary structures, where the key is
 *         a PDBId and the value is the set of extracted tertiary structures
 * @throws IOException
 *             if either the input or output directory are not valid
 */
public Map<String, Set<ExtractedTertiaryStructure>> getStructures(File anInputDir, File anOutputDir)
        throws IOException {
    Map<String, Set<ExtractedTertiaryStructure>> rm = new HashMap<String, Set<ExtractedTertiaryStructure>>();
    // this method makes use of getStructures(File, String[]).
    // iterate over the input directory and call the sibling method
    Iterator<File> itr = FileUtils.iterateFiles(anInputDir, new String[] { "pdb", "PDB" }, false);
    while (itr.hasNext()) {
        File aPdbFile = itr.next();
        // construct the output file
        File outputFile = new File(anOutputDir.getAbsolutePath() + "/" + aPdbFile.getName() + ".out");
        // construct the command array
        String[] cmdArr = new String[] { dssrPath + "/x3dna-dssr", "-i=" + aPdbFile.getAbsolutePath(),
                "-o=" + outputFile.getAbsolutePath() };
        try {
            Set<ExtractedTertiaryStructure> ets = this.getStructures(aPdbFile, cmdArr);
            rm.put(aPdbFile.getName(), ets);
        } catch (InvalidResidueException e) {
            e.printStackTrace();
        }
    }
    return rm;
}

From source file:org.sherlok.FileBased.java

/** @return a list of the paths of all resources */
public static Collection<String> allResources() throws SherlokException {
    try {/*from   w ww. j  a  va2s  .c o  m*/
        List<String> resources = list();
        File dir = new File(RUTA_RESOURCES_PATH);
        validateArgument(dir.exists(), "resources directory '" + RUTA_RESOURCES_PATH
                + "' does not exist (resolves to '" + dir.getAbsolutePath() + "')");
        Iterator<File> fit = FileUtils.iterateFiles(dir, null, true);
        while (fit.hasNext()) {
            File f = fit.next();
            if (!f.getName().startsWith(".")) { // filtering hidden files
                String relativePath = Paths.get(dir.getAbsolutePath())
                        .relativize(Paths.get(f.getAbsolutePath())).toString();
                if (!relativePath.startsWith(".")) {
                    resources.add(relativePath);
                }
            }
        }
        return resources;
    } catch (Throwable e) { // you never know...
        throw new SherlokException("could not list resources").setDetails(e.getStackTrace());
    }
}

From source file:org.sipfoundry.voicemail.Messages.java

/**
 * Load the map with the files from the directory
 * @param directory//from  w  w w  .j av  a 2 s. co m
 * @param map
 * @param countUnheard count number of unheard messages while doing this
 */

@SuppressWarnings("unchecked") // FileUtls.itereateFiles isn't type safe
synchronized void loadFolder(File directory, HashMap<String, VmMessage> map, boolean countUnheard,
        List<String> msgIds) {
    Pattern p = Pattern.compile("^(\\d+)-00\\.xml$");
    // Load the directory and count unheard
    Iterator<File> fileIterator = FileUtils.iterateFiles(directory, null, false);
    while (fileIterator.hasNext()) {
        File file = fileIterator.next();
        String name = file.getName();
        Matcher m = p.matcher(name);

        if (m.matches()) {
            String id = m.group(1); // The ID

            if (msgIds != null) {
                msgIds.add(id);
            }

            // If this message is already in the map, skip it
            VmMessage vmMessage = map.get(id);
            if (vmMessage != null) {
                continue;
            }

            // Otherwise make a new one.
            vmMessage = VmMessage.loadMessage(directory, id);
            if (vmMessage == null) {
                continue;
            }

            if (countUnheard && vmMessage.isUnHeard()) {
                m_numUnheard++;
                if (vmMessage.isUrgent()) {
                    m_numUnheadUrgent++;
                }
            }

            map.put(id, vmMessage);
        }
    }
}

From source file:org.sonar.plugins.kt.advance.batch.DirScanTest.java

@BeforeClass
public static void setup() throws JAXBException {
    final URL url = DirScanTest.class.getResource("/test_project");
    BASEDIR = new File(url.getFile());
    MODULE_BASEDIR = new File(BASEDIR, "redis/");

    fileSystem = new DefaultFileSystem(MODULE_BASEDIR.toPath());

    fileSystem.add(Factory.makeDefaultInputFile(MODULE_BASEDIR, SRC_MEMTEST_C, 282));
    {/*from   w ww.  j  av a2  s  .  c  o m*/
        final Iterator<File> iter = FileUtils.iterateFiles(MODULE_BASEDIR,
                new String[] { FsAbstraction.XML_EXT }, true);

        while (iter.hasNext()) {
            final File file = iter.next();

            if (file.isFile() && FsAbstraction.ppoFileFilter.accept(file)) {
                fileSystem.add(Factory.makeDefaultInputFile(MODULE_BASEDIR, file.getAbsolutePath(), 282));
            }
        }
    }
    resourcePerspectives = mock(ResourcePerspectives.class);
    final Issuable issuable = mock(Issuable.class);
    final IssueBuilder issueBuilderMock = mock(IssueBuilder.class);
    when(issueBuilderMock.ruleKey(any())).thenReturn(issueBuilderMock);
    when(issueBuilderMock.effortToFix(any())).thenReturn(issueBuilderMock);
    when(issueBuilderMock.severity(any())).thenReturn(issueBuilderMock);
    when(issueBuilderMock.at(any())).thenReturn(issueBuilderMock);
    when(issueBuilderMock.build()).thenReturn(mock(Issue.class));
    when(issueBuilderMock.newLocation()).thenAnswer(invocation -> new DefaultIssueLocation());

    when(issuable.newIssueBuilder()).thenReturn(issueBuilderMock);
    when(resourcePerspectives.as(any(), any(InputPath.class))).thenReturn(issuable);

    activeRules = mock(ActiveRules.class);
    final ActiveRule ruleMock = mock(ActiveRule.class);
    when(ruleMock.param(any())).thenReturn("1.0");
    when(activeRules.find(any(RuleKey.class))).thenReturn(ruleMock);

    settings = mock(Settings.class);
    when(settings.getFloat(any())).thenReturn(1f);
    session = new KtAdvanceSensor(settings, fileSystem, activeRules, resourcePerspectives);
    ppoFile = new File(MODULE_BASEDIR, TEST_PPO_FILE);
}

From source file:org.tellervo.desktop.bulkdataentry.command.PopulateFromODKCommand.java

public void execute(MVCEvent argEvent) {

    try {/*from  ww w.  j  av  a2  s.  co  m*/
        MVC.splitOff(); // so other mvc events can execute
    } catch (IllegalThreadException e) {
        // this means that the thread that called splitOff() was not an MVC thread, and the next event's won't be blocked anyways.
        e.printStackTrace();
    } catch (IncorrectThreadException e) {
        // this means that this MVC thread is not the main thread, it was already splitOff() previously
        e.printStackTrace();
    }

    PopulateFromODKFileEvent event = (PopulateFromODKFileEvent) argEvent;
    ArrayList<ODKParser> filesProcessed = new ArrayList<ODKParser>();
    ArrayList<ODKParser> filesFailed = new ArrayList<ODKParser>();
    Path instanceFolder = null;

    // Launch the ODK wizard to collect parameters from user
    ODKImportWizard wizard = new ODKImportWizard(BulkImportModel.getInstance().getMainView());

    if (wizard.wasCancelled())
        return;

    if (wizard.isRemoteAccessSelected()) {
        // Doing remote server download of ODK files
        try {

            // Request a zip file of ODK files from the server ensuring the temp file is deleted on exit
            URI uri;
            uri = new URI(
                    App.prefs.getPref(PrefKey.WEBSERVICE_URL, "invalid url!") + "/" + "odk/fetchInstances.php");
            String file = getRemoteODKFiles(uri);

            if (file == null) {
                // Download was cancelled
                return;
            }

            new File(file).deleteOnExit();

            // Unzip to a temporary folder, again ensuring it is deleted on exit 
            instanceFolder = Files.createTempDirectory("odk-unzip");
            instanceFolder.toFile().deleteOnExit();

            log.debug("Attempting to open zip file: '" + file + "'");

            ZipFile zipFile = new ZipFile(file);
            try {
                Enumeration<? extends ZipEntry> entries = zipFile.entries();
                while (entries.hasMoreElements()) {
                    ZipEntry entry = entries.nextElement();
                    File entryDestination = new File(instanceFolder.toFile(), entry.getName());
                    entryDestination.deleteOnExit();
                    if (entry.isDirectory()) {
                        entryDestination.mkdirs();
                    } else {
                        entryDestination.getParentFile().mkdirs();
                        InputStream in = zipFile.getInputStream(entry);
                        OutputStream out = new FileOutputStream(entryDestination);
                        IOUtils.copy(in, out);
                        IOUtils.closeQuietly(in);
                        out.close();
                    }
                }
            } finally {
                zipFile.close();
            }
        } catch (URISyntaxException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } else {
        // Accessing ODK files from local folder
        instanceFolder = new File(wizard.getODKInstancesFolder()).toPath();
    }

    // Check the instance folder specified exists
    File folder = instanceFolder.toFile();
    if (!folder.exists()) {

        log.error("Instances folder does not exist");
        return;
    }

    // Compile a hash set of all media files in the instance folder and subfolders
    File file = null;
    File[] mediaFileArr = null;
    if (wizard.isIncludeMediaFilesSelected()) {
        HashSet<File> mediaFiles = new HashSet<File>();

        // Array of file extensions to consider as media files
        String[] mediaExtensions = { "jpg", "mpg", "snd", "mp4", "m4a" };
        for (String ext : mediaExtensions) {
            SuffixFileFilter filter = new SuffixFileFilter("." + ext);
            Iterator<File> it = FileUtils.iterateFiles(folder, filter, TrueFileFilter.INSTANCE);
            while (it.hasNext()) {
                file = it.next();
                mediaFiles.add(file);
            }
        }

        // Copy files to consolidate to a new folder 
        mediaFileArr = mediaFiles.toArray(new File[mediaFiles.size()]);
        String copyToFolder = wizard.getCopyToLocation();
        for (int i = 0; i < mediaFileArr.length; i++) {
            file = mediaFileArr[i];

            File target = new File(copyToFolder + file.getName());
            try {
                FileUtils.copyFile(file, target, true);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            mediaFileArr[i] = target;
        }
    }

    SampleModel smodel = event.sampleModel;
    ElementModel emodel = event.elementModel;
    ObjectModel model = event.objectModel;

    try {
        if (smodel.getTableModel().getRowCount() == 1 && smodel.getTableModel().getAllSingleRowModels().get(0)
                .getProperty(SingleSampleModel.TITLE) == null) {
            // Empty table first
            smodel.getTableModel().getAllSingleRowModels().clear();
        }
    } catch (Exception ex) {
        log.debug("Error deleting empty rows");
    }

    try {
        if (emodel.getTableModel().getRowCount() == 1 && emodel.getTableModel().getAllSingleRowModels().get(0)
                .getProperty(SingleElementModel.TITLE) == null) {
            // Empty table first
            emodel.getTableModel().getAllSingleRowModels().clear();
        }
    } catch (Exception ex) {
        log.debug("Error deleting empty rows");
    }

    try {
        if (model.getTableModel().getRowCount() == 1 && model.getTableModel().getAllSingleRowModels().get(0)
                .getProperty(SingleObjectModel.OBJECT_CODE) == null) {
            // Empty table first
            model.getTableModel().getAllSingleRowModels().clear();
        }
    } catch (Exception ex) {
        log.debug("Error deleting empty rows");
    }

    SuffixFileFilter fileFilter = new SuffixFileFilter(".xml");
    Iterator<File> iterator = FileUtils.iterateFiles(folder, fileFilter, TrueFileFilter.INSTANCE);
    while (iterator.hasNext()) {
        file = iterator.next();
        filesFound++;

        try {

            ODKParser parser = new ODKParser(file);
            filesProcessed.add(parser);

            if (!parser.isValidODKFile()) {
                filesFailed.add(parser);
                continue;
            } else if (parser.getFileType() == null) {
                filesFailed.add(parser);
                continue;
            } else if (parser.getFileType() == ODKFileType.OBJECTS) {
                addObjectToTableFromParser(parser, model, wizard, mediaFileArr);
            } else if (parser.getFileType() == ODKFileType.ELEMENTS_AND_SAMPLES) {
                addElementFromParser(parser, emodel, wizard, mediaFileArr);
                addSampleFromParser(parser, smodel, wizard, mediaFileArr);
            } else {
                filesFailed.add(parser);
                continue;
            }

        } catch (FileNotFoundException e) {
            otherErrors += "<p color=\"red\">Error loading file:</p>\n"
                    + ODKParser.formatFileNameForReport(file);
            otherErrors += "<br/>  - File not found<br/><br/>";
        } catch (IOException e) {
            otherErrors += "<p color=\"red\">Error loading file:</p>\n"
                    + ODKParser.formatFileNameForReport(file);
            otherErrors += "<br/>  - IOException - " + e.getLocalizedMessage() + "<br/><br/>";
        } catch (Exception e) {
            otherErrors += "<p color=\"red\">Error parsing file:</p>\n"
                    + ODKParser.formatFileNameForReport(file);
            otherErrors += "<br/>  - Exception - " + e.getLocalizedMessage() + "<br/><br/>";
        }

    }

    // Create a CSV file of metadata if the user requested it
    if (wizard.isCreateCSVFileSelected()) {
        try {
            createCSVFile(filesProcessed, wizard.getCSVFilename());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    // Compile logs to display to user
    StringBuilder log = new StringBuilder();
    log.append("<html>\n");
    for (ODKParser parser : filesFailed) {
        log.append("<p color=\"red\">Error loading file:</p>\n"
                + ODKParser.formatFileNameForReport(parser.getFile()));
        log.append("<br/>  - " + parser.getParseErrorMessage() + "<br/><br/>");
    }
    for (ODKParser parser : filesProcessed) {
        if (filesFailed.contains(parser))
            continue;
        if (parser.getParseErrorMessage() == "")
            continue;

        log.append("<p color=\"orange\">Warning loading file:</p>\n"
                + ODKParser.formatFileNameForReport(parser.getFile()));
        log.append("<br/>  - " + parser.getParseErrorMessage() + "<br/><br/>");
    }
    log.append(otherErrors);
    log.append("</html>");
    ODKParserLogViewer logDialog = new ODKParserLogViewer(BulkImportModel.getInstance().getMainView());
    logDialog.setLog(log.toString());
    logDialog.setFileCount(filesFound, filesLoadedSuccessfully);

    // Display log if there were any errors or if no files were found
    if (filesFound > filesLoadedSuccessfully) {
        logDialog.setVisible(true);
    } else if (filesFound == 0 && wizard.isRemoteAccessSelected()) {
        Alert.error(BulkImportModel.getInstance().getMainView(), "Not found",
                "No ODK data files were found on the server.  Please ensure you've used the 'send finalized form' option in ODK Collect and try again");
        return;
    } else if (filesFound == 0) {
        Alert.error(BulkImportModel.getInstance().getMainView(), "Not found",
                "No ODK data files were found in the specified folder.  Please check and try again.");
        return;
    } else if (wizard.isIncludeMediaFilesSelected()) {
        Alert.message(BulkImportModel.getInstance().getMainView(), "Download Complete",
                "The ODK download is complete.  As requested, your media files have been temporarily copied to the local folder '"
                        + wizard.getCopyToLocation()
                        + "'.  Please remember to move them to their final location");
    }

}

From source file:org.vafer.jdependency.Clazzpath.java

public ClazzpathUnit addClazzpathUnit(final File pFile, final String pId) throws IOException {
    if (pFile.isFile()) {
        return addClazzpathUnit(new FileInputStream(pFile), pId);
    }/*  w w w.j  av a2  s  .  com*/
    if (pFile.isDirectory()) {
        final String prefix = FilenameUtils.separatorsToUnix(FilenameUtils
                .normalize(new StringBuilder(pFile.getAbsolutePath()).append(File.separatorChar).toString()));
        final boolean recursive = true;
        @SuppressWarnings("unchecked")
        final Iterator<File> files = FileUtils.iterateFiles(pFile, new String[] { "class" }, recursive);
        return addClazzpathUnit(new Iterable<Resource>() {

            public Iterator<Resource> iterator() {
                return new Iterator<Clazzpath.Resource>() {

                    public boolean hasNext() {
                        return files.hasNext();
                    }

                    public Resource next() {
                        final File file = files.next();
                        return new Resource(file.getAbsolutePath().substring(prefix.length())) {

                            @Override
                            InputStream getInputStream() throws IOException {
                                return new FileInputStream(file);
                            }
                        };
                    }

                    public void remove() {
                        throw new UnsupportedOperationException();
                    }
                };
            }

        }, pId, true);
    }
    throw new IllegalArgumentException();
}