net.sf.javagimmicks.io.folderdiff.FileScanner Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gimmicks-io Show documentation
Show all versions of gimmicks-io Show documentation
Basic I/O utilities and a diff engine for file system folders
The newest version!
package net.sf.javagimmicks.io.folderdiff;
import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.List;
import net.sf.javagimmicks.io.FileTraverser;
import net.sf.javagimmicks.io.FileTraverser.FileVisitor;
import net.sf.javagimmicks.io.FileTraverser.TypeFilter;
import net.sf.javagimmicks.io.FileUtils;
import net.sf.javagimmicks.io.FilenameFilterFileFilterAdapter;
import net.sf.javagimmicks.io.FilenameFilterFileFilterAdapter.Mode;
import net.sf.javagimmicks.io.folderdiff.FileInfo.Origin;
class FileScanner
{
private final Origin _origin;
private final FolderDiffBuilder _builder;
private final FilenameFilter _filter;
private final File _rootFile;
private final boolean _recursive;
public FileScanner(final Origin origin, final FolderDiffBuilder builder, final File rootFile,
final FilenameFilter filter,
final boolean recursive)
{
if (rootFile == null)
{
throw new IllegalArgumentException("Root file may not be null!");
}
_origin = origin;
_builder = builder;
_rootFile = rootFile;
_filter = filter;
_recursive = recursive;
}
public List scan()
{
final ArrayList result = new ArrayList();
if (_rootFile.isDirectory())
{
final int skipSegments = FileUtils.getPathSegments(_rootFile).size();
final FileVisitor scanVisitor = new FileVisitor()
{
@Override
public void visit(final File file)
{
if (file.isDirectory())
{
_builder.fireEvent(new FolderDiffEvent(_builder, file));
}
result.add(new FileInfo(file, skipSegments, _origin));
}
};
final FileTraverser traverser = new FileTraverser(_rootFile);
traverser.setFileFilter(new FilenameFilterFileFilterAdapter(_filter, Mode.BOTH));
traverser.setTypeFilter(TypeFilter.BOTH);
traverser.setRecursive(_recursive);
traverser.run(scanVisitor);
}
return result;
}
}