Subversion Repositories XServices

Rev

Rev 147 | Blame | 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 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;


import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import net.brutex.xservices.types.FileInfoType;

// TODO: Auto-generated Javadoc
/**
 * The Class FileWalker.
 *
 * @author Brian Rosenberger, bru(at)brutex.de
 */
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;
        
        /** The logger. */
        private static final Logger logger = LogManager.getLogger();

        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();
        logger.trace("Compare file " + file.toString() + " against pattern '"+pattern+"'.");
        total++;
        if (name != null && matcher.matches(name)) {
            list.add(file);
                        logger.debug("Added file " + file.toString() + " to the result set.");
                        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) {
        logger.warn(String.format("Failed to include file '%s'.", file.toString()));
        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.