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.apache.synapse.config.xml.MultiXMLConfigurationBuilder.java

private static void createInboundEndpoint(SynapseConfiguration synapseConfig, String rootDirPath,
        Properties properties) {/*from   w w  w .  j ava2s  .  c o  m*/
    File inboundEndpointDir = new File(rootDirPath, INBOUND_ENDPOINT_DIR);
    if (inboundEndpointDir.exists()) {
        if (log.isDebugEnabled()) {
            log.debug("Loading APIs from :" + inboundEndpointDir.getPath());
        }

        Iterator inboundEndpointIterator = FileUtils.iterateFiles(inboundEndpointDir, extensions, false);
        while (inboundEndpointIterator.hasNext()) {
            File file = (File) inboundEndpointIterator.next();
            try {
                OMElement document = getOMElement(file);
                InboundEndpoint inboundEndpoint = SynapseXMLConfigurationFactory
                        .defineInboundEndpoint(synapseConfig, document, properties);
                if (inboundEndpoint != null) {
                    inboundEndpoint.setFileName(file.getName());
                    synapseConfig.getArtifactDeploymentStore().addArtifact(file.getAbsolutePath(),
                            inboundEndpoint.getName());
                }
            } catch (Exception e) {
                String msg = "Inbound Endpoint configuration cannot be built from : " + file.getName();
                handleConfigurationError(SynapseConstants.FAIL_SAFE_MODE_INBOUND_ENDPOINT, msg, e);
            }
        }
    }
}

From source file:org.apache.syncope.core.persistence.SQLSchemaGenerator.java

/**
 * Locates and returns a list of class files found under specified class directory.
 *
 * @param base base class directory/*from   w w  w .  ja  v a  2  s .  co m*/
 * @return list of class files
 */
private static List<File> findEntityClassFiles(final String base) {
    File baseDir = new File(base);
    if (!baseDir.exists() || !baseDir.isDirectory()) {
        throw new IllegalArgumentException(baseDir + " not found or not a directory");
    }

    @SuppressWarnings("unchecked")
    Iterator<File> itor = FileUtils.iterateFiles(baseDir, new String[] { "class" }, true);
    List<File> entityClasses = new ArrayList<File>();
    while (itor.hasNext()) {
        entityClasses.add(itor.next());
    }

    return entityClasses;
}

From source file:org.apache.vxquery.metadata.VXQueryCollectionOperatorDescriptor.java

@Override
public IOperatorNodePushable createPushRuntime(IHyracksTaskContext ctx,
        IRecordDescriptorProvider recordDescProvider, int partition, int nPartitions)
        throws HyracksDataException {
    final FrameTupleAccessor fta = new FrameTupleAccessor(ctx.getFrameSize(),
            recordDescProvider.getInputRecordDescriptor(getActivityId(), 0));
    final int fieldOutputCount = recordDescProvider.getOutputRecordDescriptor(getActivityId(), 0)
            .getFieldCount();//from  ww  w  .  ja v a2s.  c om
    final ByteBuffer frame = ctx.allocateFrame();
    final FrameTupleAppender appender = new FrameTupleAppender(ctx.getFrameSize(), fieldOutputCount);
    final short partitionId = (short) ctx.getTaskAttemptId().getTaskId().getPartition();
    final ITreeNodeIdProvider nodeIdProvider = new TreeNodeIdProvider(partitionId, dataSourceId,
            totalDataSources);
    final String nodeId = ctx.getJobletContext().getApplicationContext().getNodeId();
    final DynamicContext dCtx = (DynamicContext) ctx.getJobletContext().getGlobalJobData();

    final String collectionName = collectionPartitions[partition % collectionPartitions.length];
    final XMLParser parser = new XMLParser(false, nodeIdProvider, nodeId, frame, appender, childSeq,
            dCtx.getStaticContext());

    return new AbstractUnaryInputUnaryOutputOperatorNodePushable() {
        @Override
        public void open() throws HyracksDataException {
            appender.reset(frame, true);
            writer.open();
        }

        @Override
        public void nextFrame(ByteBuffer buffer) throws HyracksDataException {
            fta.reset(buffer);
            String collectionModifiedName = collectionName.replace("${nodeId}", nodeId);
            File collectionDirectory = new File(collectionModifiedName);

            // Go through each tuple.
            if (collectionDirectory.isDirectory()) {
                for (int tupleIndex = 0; tupleIndex < fta.getTupleCount(); ++tupleIndex) {
                    @SuppressWarnings("unchecked")
                    Iterator<File> it = FileUtils.iterateFiles(collectionDirectory, new VXQueryIOFileFilter(),
                            TrueFileFilter.INSTANCE);
                    while (it.hasNext()) {
                        File xmlDocument = it.next();
                        if (LOGGER.isLoggable(Level.FINE)) {
                            LOGGER.fine("Starting to read XML document: " + xmlDocument.getAbsolutePath());
                        }
                        parser.parseElements(xmlDocument, writer, fta, tupleIndex);
                    }
                }
            } else {
                throw new HyracksDataException("Invalid directory parameter (" + nodeId + ":"
                        + collectionDirectory.getAbsolutePath() + ") passed to collection.");
            }
        }

        @Override
        public void fail() throws HyracksDataException {
            writer.fail();
        }

        @Override
        public void close() throws HyracksDataException {
            // Check if needed?
            fta.reset(frame);
            if (fta.getTupleCount() > 0) {
                FrameUtils.flushFrame(frame, writer);
            }
            writer.close();
        }
    };
}

From source file:org.apache.wiki.util.ClassUtil.java

/**
 * Searchs for all the files in classpath under a given package, for a given {@link File}. If the 
 * {@link File} is a directory all files inside it are stored, otherwise the {@link File} itself is
 * stored//from   www .j a va 2  s  . c  o  m
 * 
 * @param results collection in which the found entries are stored
 * @param file given {@link File} to search in.
 * @param rootPackage base package.
 */
static void fileEntriesUnder(List<String> results, File file, String rootPackage) {
    log.debug("scanning [" + file.getName() + "]");
    if (file.isDirectory()) {
        Iterator<File> files = FileUtils.iterateFiles(file, null, true);
        while (files.hasNext()) {
            File subfile = files.next();
            // store an entry similar to the jarSearch(..) below ones
            String entry = StringUtils.replace(subfile.getAbsolutePath(),
                    file.getAbsolutePath() + File.separatorChar, StringUtils.EMPTY);
            results.add(rootPackage + "/" + entry);
        }
    } else {
        results.add(file.getName());
    }
}

From source file:org.apache.wookie.util.WidgetJavascriptSyntaxAnalyzer.java

/**
 * Find occurrences of incompatible setter syntax for Internet explorer
 * i.e. Widget.preferences.foo=bar;/*from   w  ww  .ja va 2  s  .com*/
 * 
 * @throws IOException
 */
private void parseIEIncompatibilities() throws IOException {
    // Pattern match on the syntax 'widget.preferemces.name=value' - including optional quotes & spaces around the value
    Pattern pattern = Pattern.compile("widget.preferences.\\w+\\s*=\\s*\\\"??\\'??.+\\\"??\\'??",
            Pattern.CASE_INSENSITIVE);
    Matcher matcher = pattern.matcher("");
    // Search .js files, but also any html files
    Iterator<?> iter = FileUtils.iterateFiles(_searchFolder, new String[] { "js", "htm", "html" }, true);
    while (iter.hasNext()) {
        File file = (File) iter.next();
        LineNumberReader lineReader = new LineNumberReader(new FileReader(file));
        String line = null;
        while ((line = lineReader.readLine()) != null) {
            matcher.reset(line);
            if (matcher.find()) {
                String message = "\n(Line " + lineReader.getLineNumber() + ") in file " + file;
                message += "\n\t " + line + "\n";
                message += "This file contains preference setter syntax which may not behave correctly in Internet Explorer version 8 and below.\n";
                message += "See https://cwiki.apache.org/confluence/display/WOOKIE/FAQ#FAQ-ie8prefs for more information.\n";
                FlashMessage.getInstance().message(formatWebMessage(message));
                _logger.warn(message);
            }
        }
    }
}

From source file:org.appfuse.mojo.exporter.ModelGeneratorMojo.java

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    getComponentProperties().put("implementation", "jdbcconfiguration");
    getComponentProperties().put("outputDirectory",
            (sourceDirectory != null) ? sourceDirectory : "${basedir}/target/appfuse/generated-sources");

    // default location for reveng file is src/test/resources
    File revengFile = new File("src/test/resources/hibernate.reveng.xml");
    if (revengFile.exists() && getComponentProperty("revengfile") == null) {
        getComponentProperties().put("revengfile", "src/test/resources/hibernate.reveng.xml");
    }//from  w w  w  .  j av  a2 s  .c om

    // Check for existence of hibernate.reveng.xml and if there isn't one, create it
    // Specifying the file explicitly in pom.xml overrides default location
    if (getComponentProperty("revengfile") == null) {
        getComponentProperties().put("revengfile", "target/test-classes/hibernate.reveng.xml");
    }

    File existingConfig = new File(getComponentProperty("revengfile"));
    if (!existingConfig.exists()) {
        InputStream in = this.getClass().getResourceAsStream("/appfuse/model/hibernate.reveng.ftl");
        StringBuffer configFile = new StringBuffer();
        try {
            InputStreamReader isr = new InputStreamReader(in);
            BufferedReader reader = new BufferedReader(isr);
            String line;
            while ((line = reader.readLine()) != null) {
                configFile.append(line).append("\n");
            }
            reader.close();

            getLog().info("Writing 'hibernate.reveng.xml' to " + existingConfig.getPath());
            FileUtils.writeStringToFile(existingConfig, configFile.toString());
        } catch (IOException io) {
            throw new MojoFailureException(io.getMessage());
        }
    }

    // if package name is not configured, default to project's groupId
    if (getComponentProperty("packagename") == null) {
        getComponentProperties().put("packagename", getProject().getGroupId() + ".model");
    }

    if (getComponentProperty("configurationfile") == null) {
        // look for jdbc.properties and set "propertyfile" to its path
        File jdbcProperties = new File("target/classes/jdbc.properties");
        if (!jdbcProperties.exists()) {
            jdbcProperties = new File("target/test-classes/jdbc.properties");
        }
        if (jdbcProperties.exists()) {
            if (getComponentProperty("propertyfile") == null) {
                getComponentProperties().put("propertyfile", jdbcProperties.getPath());
                getLog().debug("Set propertyfile to '" + jdbcProperties.getPath() + "'");
            }
        } else {
            throw new MojoFailureException("Failed to find jdbc.properties in classpath.");
        }
    }

    // For some reason, the classloader created in HibernateExporterMojo does not work
    // when using jdbcconfiguration - it can't find the JDBC Driver (no suitable driver).
    // Skipping the resetting of the classloader and manually adding the dependency (with XML) works.
    // It's ugly, but it works. I wish there was a way to get get this plugin to recognize the jdbc driver
    // from the project.

    super.doExecute();

    if (System.getProperty("disableInstallation") != null) {
        disableInstallation = Boolean.valueOf(System.getProperty("disableInstallation"));
    }

    // allow installation to be supressed when testing
    if (!disableInstallation) {
        // copy the generated file to the model directory of the project
        try {
            String packageName = getComponentProperties().get("packagename");
            String packageAsDir = packageName.replaceAll("\\.", "/");
            File dir = new File(sourceDirectory + "/" + packageAsDir);
            if (dir.exists()) {
                Iterator filesIterator = FileUtils.iterateFiles(dir, new String[] { "java" }, false);
                while (filesIterator.hasNext()) {
                    File f = (File) filesIterator.next();
                    getLog().info("Copying generated '" + f.getName() + "' to project...");
                    FileUtils.copyFileToDirectory(f,
                            new File(destinationDirectory + "/src/main/java/" + packageAsDir));
                }
            } else {
                throw new MojoFailureException("No tables found in database to generate code from.");
            }
            FileUtils.forceDelete(dir);
        } catch (IOException io) {
            throw new MojoFailureException(io.getMessage());
        }
    }
}

From source file:org.bungeni.editor.system.ValidateConfiguration.java

public void validateAll() {
    // start with the doc type 
    String docTypesFile = DocTypesReader.RELATIVE_PATH_TO_SYSTEM_PARAMETERS_FILE;
    List<SAXParseException> dtypeExceptions = validate(new File(docTypesFile),
            this.xsdConfigInfo.get("docTypes"));
    this.xsdConfigInfo.get("docTypes").setExceptions(DocTypesReader.DOCTYPES_FILE, dtypeExceptions);
    String[] extensions = { "xml" };
    // sectionTypes 
    String sectionTypesFolder = SectionTypesReader.getSettingsFolder();
    Iterator<File> fileSectionTypes = FileUtils.iterateFiles(new File(sectionTypesFolder), extensions, false);
    while (fileSectionTypes.hasNext()) {
        //ignore common.xml 
        File f = fileSectionTypes.next();
        if (!f.getName().equals("common.xml")) {
            List<SAXParseException> stypeExceptions = validate(f, this.xsdConfigInfo.get("sectionTypes"));
            this.xsdConfigInfo.get("sectionTypes").setExceptions(f.getName(), stypeExceptions);
        }/*from w w w  .  j  a va 2 s  .  c om*/
    }

    String inlineTypesFolder = InlineTypesReader.getSettingsFolder();

    Iterator<File> fileInlineTypes = FileUtils.iterateFiles(new File(inlineTypesFolder), extensions, false);
    while (fileInlineTypes.hasNext()) {
        //ignore common.xml 
        File f = fileInlineTypes.next();
        if (!f.getName().equals("common.xml")) {
            List<SAXParseException> stypeExceptions = validate(f, this.xsdConfigInfo.get("inlineTypes"));
            this.xsdConfigInfo.get("inlineTypes").setExceptions(f.getName(), stypeExceptions);
        }
    }

    if (areThereExceptions()) {
        showExceptions();
    }
}

From source file:org.canova.api.records.reader.impl.FileRecordReader.java

protected void doInitialize(InputSplit split) {
    URI[] locations = split.locations();

    if (locations != null && locations.length >= 1) {
        if (locations.length > 1) {
            List<File> allFiles = new ArrayList<>();
            for (URI location : locations) {
                File iter = new File(location);
                if (labels == null && appendLabel) {
                    //root dir relative to example where the label is the parent directory and the root directory is
                    //recursively the parent of that
                    File parent = iter.getParentFile().getParentFile();
                    //calculate the labels relative to the parent file
                    labels = new ArrayList<>();

                    for (File labelDir : parent.listFiles())
                        labels.add(labelDir.getName());
                }/*from w  w w  .j  av a 2s .c  o  m*/

                if (iter.isDirectory()) {
                    Iterator<File> allFiles2 = FileUtils.iterateFiles(iter, null, true);
                    while (allFiles2.hasNext())
                        allFiles.add(allFiles2.next());
                }

                else
                    allFiles.add(iter);
            }

            iter = allFiles.listIterator();
        } else {
            File curr = new File(locations[0]);
            if (curr.isDirectory())
                iter = FileUtils.iterateFiles(curr, null, true);
            else
                iter = Collections.singletonList(curr).iterator();
        }
    }

}

From source file:org.canova.image.recordreader.BaseImageRecordReader.java

@Override
public void initialize(InputSplit split) throws IOException {
    inputSplit = split;/* ww w .  j  a  v  a 2  s  . c o  m*/
    if (split instanceof FileSplit) {
        URI[] locations = split.locations();
        if (locations != null && locations.length >= 1) {
            if (locations.length > 1) {
                List<File> allFiles = new ArrayList<>();
                for (URI location : locations) {
                    File imgFile = new File(location);
                    if (!imgFile.isDirectory() && containsFormat(imgFile.getAbsolutePath()))
                        allFiles.add(imgFile);
                    if (appendLabel) {
                        File parentDir = imgFile.getParentFile();
                        String name = parentDir.getName();
                        if (!labels.contains(name))
                            labels.add(name);
                        if (pattern != null) {
                            String label = name.split(pattern)[patternPosition];
                            fileNameMap.put(imgFile.toString(), label);
                        }
                    }
                }
                iter = allFiles.listIterator();
            } else {
                File curr = new File(locations[0]);
                if (!curr.exists())
                    throw new IllegalArgumentException("Path " + curr.getAbsolutePath() + " does not exist!");
                if (curr.isDirectory())
                    iter = FileUtils.iterateFiles(curr, null, true);
                else
                    iter = Collections.singletonList(curr).listIterator();

            }
        }
        //remove the root directory
        FileSplit split1 = (FileSplit) split;
        labels.remove(split1.getRootDir());
    }

    else if (split instanceof InputStreamInputSplit) {
        InputStreamInputSplit split2 = (InputStreamInputSplit) split;
        InputStream is = split2.getIs();
        URI[] locations = split2.locations();
        INDArray load = imageLoader.asRowVector(is);
        record = RecordConverter.toRecord(load);
        for (int i = 0; i < load.length(); i++) {
            if (appendLabel) {
                Path path = Paths.get(locations[0]);
                String parent = path.getParent().toString();
                //could have been a uri
                if (parent.contains("/")) {
                    parent = parent.substring(parent.lastIndexOf('/') + 1);
                }
                int label = labels.indexOf(parent);
                if (label >= 0)
                    record.add(new DoubleWritable(labels.indexOf(parent)));
                else
                    throw new IllegalStateException("Illegal label " + parent);
            }
        }
        is.close();
    }
}

From source file:org.canova.image.recordreader.ImageRecordReader.java

@Override
public void initialize(InputSplit split) throws IOException, InterruptedException {
    if (split instanceof FileSplit) {
        URI[] locations = split.locations();
        if (locations != null && locations.length >= 1) {
            if (locations.length > 1) {
                List<File> allFiles = new ArrayList<>();
                for (URI location : locations) {
                    File iter = new File(location);
                    if (!iter.isDirectory() && containsFormat(iter.getAbsolutePath()))
                        allFiles.add(iter);
                    if (appendLabel) {
                        File parentDir = iter.getParentFile();
                        String name = parentDir.getName();
                        if (!labels.contains(name))
                            labels.add(name);

                    }//from   ww w .  ja  v a  2  s .c om

                }

                iter = allFiles.iterator();
            } else {
                File curr = new File(locations[0]);
                if (!curr.exists())
                    throw new IllegalArgumentException("Path " + curr.getAbsolutePath() + " does not exist!");
                if (curr.isDirectory())
                    iter = FileUtils.iterateFiles(curr, null, true);
                else
                    iter = Collections.singletonList(curr).iterator();
            }
        }
    }

    else if (split instanceof InputStreamInputSplit) {
        InputStreamInputSplit split2 = (InputStreamInputSplit) split;
        InputStream is = split2.getIs();
        URI[] locations = split2.locations();
        INDArray load = imageLoader.asMatrix(is);
        record = RecordConverter.toRecord(load);
        if (appendLabel) {
            Path path = Paths.get(locations[0]);
            String parent = path.getParent().toString();
            record.add(new DoubleWritable(labels.indexOf(parent)));
        }

        is.close();
    }

}