Webiyo

This page was generated using Webiyo. See its source code and unit tests.

org/webiyo/examples/viewjava/SourceDir.java

package org.webiyo.examples.viewjava;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.List;

public class SourceDir {

    private static final FileFilter JAVA_FILTER = new FileFilter() {

        public boolean accept(File file) {
            return file.getName().endsWith(".java");
        }

    };

    private static final FileFilter DIRECTORY_FILTER = new FileFilter() {

        public boolean accept(File file) {
            return file.isDirectory() && !file.getName().equals("CVS");
        }

    };

    private final File dir;

    public SourceDir(File dir) {
        this.dir = dir;
    }

    public SourceDir(String path) {
        this(new File(path));
    }

    public SourceDir(File parent, String path) {
        this(new File(parent, path));
    }

    public File getDir() {
        return dir;
    }

    public List<SourceFile> findSourceFiles() {
        return findSourceFiles(dir);
    }
    
    // end of public methods

    private List<SourceFile> findSourceFiles(File dir) {
        List<SourceFile> result = new ArrayList<SourceFile>();

        File[] javaFiles = dir.listFiles(JAVA_FILTER);
        for (File file : javaFiles) {
            result.add(new SourceFile(getDir(), file));
        }

        File[] subDirs = dir.listFiles(DIRECTORY_FILTER);
        for (File subDir : subDirs) {
            result.addAll(findSourceFiles(subDir));
        }

        return result;
    }

}

SourceForge