Subversion Repositories XServices

Rev

Rev 198 | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

/*
 *   Copyright 2013 Brian Rosenberger (Brutex Network)
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */
package net.brutex.xservices.util;

import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;



// TODO: Auto-generated Javadoc
/**
 * The Class FileWalker.
 *
 * @author Brian Rosenberger, bru(at)brutex.de
 */
@Slf4j
public class FileWalker extends SimpleFileVisitor<Path> {
        
        /** The matcher. */
        private final PathMatcher matcher;
        
        /** The num. */
        private long num=0;
        
        /** The total. */
        private long total=0;
        
        /** The pattern. */
        private final String pattern;
        
        List<Path> list;
        
        /**
         * Instantiates a new file walker.
         *
         * @param pattern the pattern
         */
        public FileWalker(String pattern) {
        matcher = FileSystems.getDefault()
                .getPathMatcher("glob:" + pattern);
        this.pattern = "glob:"+pattern;
        this.list = new ArrayList<Path>();
    }
        
        
         // Compares the glob pattern against
    // the file or directory name.
    /**
         * Find.
         *
         * @param file the file
         */
        void find(Path file) {
        Path name = file.getFileName();
        log.debug("Compare file '{}' against pattern '{}'.", file, pattern);
        total++;
        if (name != null && matcher.matches(name)) {
            list.add(file);
                        log.debug("Added file '{}' to the result set.", file);
                        num++;
        }
    }

    // Invoke the pattern matching
    // method on each file.
    /* (non-Javadoc)
     * @see java.nio.file.SimpleFileVisitor#visitFile(java.lang.Object, java.nio.file.attribute.BasicFileAttributes)
     */
    @Override
    public FileVisitResult visitFile(Path file,
            BasicFileAttributes attrs) {
        
        find(file);
        return FileVisitResult.CONTINUE;
    }

    // Invoke the pattern matching
    // method on each directory.
    /* (non-Javadoc)
     * @see java.nio.file.SimpleFileVisitor#preVisitDirectory(java.lang.Object, java.nio.file.attribute.BasicFileAttributes)
     */
    @Override
    public FileVisitResult preVisitDirectory(Path dir,
            BasicFileAttributes attrs) {
        find(dir);
        return FileVisitResult.CONTINUE;
    }

    /* (non-Javadoc)
     * @see java.nio.file.SimpleFileVisitor#visitFileFailed(java.lang.Object, java.io.IOException)
     */
    @Override
    public FileVisitResult visitFileFailed(Path file,
            IOException exc) {
        log.warn("Failed to include file '{}'.", file);
        return FileVisitResult.CONTINUE;
    }
    
    /**
     * Gets the count.
     *
     * @return the count
     */
    public long getCount() {
        return num;
    }
    
    /**
     * Gets the total.
     *
     * @return the total
     */
    public long getTotal() {
        return total;
    }
    
    /**
     * Get result list
     */
    public List<Path> getResult() {
        return list;
    }
        
}

Generated by GNU Enscript 1.6.5.90.