001/*
002 * Stallion Core: A Modern Web Framework
003 *
004 * Copyright (C) 2015 - 2016 Stallion Software LLC.
005 *
006 * This program is free software: you can redistribute it and/or modify it under the terms of the
007 * GNU General Public License as published by the Free Software Foundation, either version 2 of
008 * the License, or (at your option) any later version. This program is distributed in the hope that
009 * it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
011 * License for more details. You should have received a copy of the GNU General Public License
012 * along with this program.  If not, see <http://www.gnu.org/licenses/gpl-2.0.html>.
013 *
014 *
015 *
016 */
017
018package io.stallion.fileSystem;
019
020import java.nio.file.FileVisitor;
021import java.nio.file.Path;
022import java.util.ArrayList;
023import java.nio.file.attribute.BasicFileAttributes;
024import java.nio.file.FileVisitResult;
025import java.io.IOException;
026import java.util.List;
027
028/**
029 * Used for finding a list of all text files in a directory.
030 */
031public class TreeVisitor implements FileVisitor<Path> {
032    private List<Path> paths = new ArrayList<Path>();
033
034    public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)  {
035        return FileVisitResult.CONTINUE;
036    }
037    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
038        if (file != null) {
039            // ignore emacs autosave files
040            if (file.toString().contains(".#")) {
041                return FileVisitResult.CONTINUE;
042            }
043            if (file.toString().toLowerCase().endsWith(".html")
044                    || file.toString().toLowerCase().endsWith(".htm")
045                    || file.toString().toLowerCase().endsWith(".md")
046                    || file.toString().toLowerCase().endsWith(".toml")
047                    || file.toString().toLowerCase().endsWith(".json")
048                    || file.toString().toLowerCase().endsWith(".txt")) {
049                getPaths().add(file);
050            }
051        }
052        return FileVisitResult.CONTINUE;
053    }
054    public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
055        return FileVisitResult.CONTINUE;
056    }
057
058
059    public FileVisitResult visitFileFailed(Path file, IOException exc) {
060        return FileVisitResult.CONTINUE;
061    }
062
063
064    public List<Path> getPaths() {
065        return paths;
066    }
067
068    public void setPaths(List<Path> paths) {
069        this.paths = paths;
070    }
071}