Subversion Repositories XServices

Rev

Rev 185 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
147 brianR 1
/*
2
 *   Copyright 2013 Brian Rosenberger (Brutex Network)
3
 *
4
 *   Licensed under the Apache License, Version 2.0 (the "License");
5
 *   you may not use this file except in compliance with the License.
6
 *   You may obtain a copy of the License at
7
 *
8
 *       http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 *   Unless required by applicable law or agreed to in writing, software
11
 *   distributed under the License is distributed on an "AS IS" BASIS,
12
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 *   See the License for the specific language governing permissions and
14
 *   limitations under the License.
15
 */
16
package net.brutex.xservices.util;
17
 
18
import java.io.IOException;
19
import java.nio.file.FileSystems;
20
import java.nio.file.FileVisitResult;
21
import java.nio.file.Path;
22
import java.nio.file.PathMatcher;
23
import java.nio.file.SimpleFileVisitor;
24
import java.nio.file.attribute.BasicFileAttributes;
25
import java.util.ArrayList;
26
import java.util.List;
27
 
28
 
185 brianR 29
import org.apache.logging.log4j.LogManager;
30
import org.apache.logging.log4j.Logger;
31
 
147 brianR 32
import net.brutex.xservices.types.FileInfoType;
33
 
34
// TODO: Auto-generated Javadoc
35
/**
36
 * The Class FileWalker.
37
 *
38
 * @author Brian Rosenberger, bru(at)brutex.de
39
 */
40
public class FileWalker extends SimpleFileVisitor<Path> {
41
 
42
	/** The matcher. */
43
	private final PathMatcher matcher;
44
 
45
	/** The num. */
46
	private long num=0;
47
 
48
	/** The total. */
49
	private long total=0;
50
 
51
	/** The pattern. */
52
	private final String pattern;
53
 
54
	/** The logger. */
185 brianR 55
	private static final Logger logger = LogManager.getLogger();
147 brianR 56
 
57
	List<Path> list;
58
 
59
	/**
60
	 * Instantiates a new file walker.
61
	 *
62
	 * @param pattern the pattern
63
	 */
64
	public FileWalker(String pattern) {
65
        matcher = FileSystems.getDefault()
66
                .getPathMatcher("glob:" + pattern);
67
        this.pattern = "glob:"+pattern;
68
        this.list = new ArrayList<Path>();
69
    }
70
 
71
 
72
	 // Compares the glob pattern against
73
    // the file or directory name.
74
    /**
75
 	 * Find.
76
 	 *
77
 	 * @param file the file
78
 	 */
79
 	void find(Path file) {
80
        Path name = file.getFileName();
81
        logger.trace("Compare file " + file.toString() + " against pattern '"+pattern+"'.");
82
        total++;
83
        if (name != null && matcher.matches(name)) {
84
            list.add(file);
85
			logger.debug("Added file " + file.toString() + " to the result set.");
86
			num++;
87
        }
88
    }
89
 
90
    // Invoke the pattern matching
91
    // method on each file.
92
    /* (non-Javadoc)
93
     * @see java.nio.file.SimpleFileVisitor#visitFile(java.lang.Object, java.nio.file.attribute.BasicFileAttributes)
94
     */
95
    @Override
96
    public FileVisitResult visitFile(Path file,
97
            BasicFileAttributes attrs) {
98
 
99
        find(file);
100
        return FileVisitResult.CONTINUE;
101
    }
102
 
103
    // Invoke the pattern matching
104
    // method on each directory.
105
    /* (non-Javadoc)
106
     * @see java.nio.file.SimpleFileVisitor#preVisitDirectory(java.lang.Object, java.nio.file.attribute.BasicFileAttributes)
107
     */
108
    @Override
109
    public FileVisitResult preVisitDirectory(Path dir,
110
            BasicFileAttributes attrs) {
111
        find(dir);
112
        return FileVisitResult.CONTINUE;
113
    }
114
 
115
    /* (non-Javadoc)
116
     * @see java.nio.file.SimpleFileVisitor#visitFileFailed(java.lang.Object, java.io.IOException)
117
     */
118
    @Override
119
    public FileVisitResult visitFileFailed(Path file,
120
            IOException exc) {
121
        logger.warn(String.format("Failed to include file '%s'.", file.toString()));
122
        return FileVisitResult.CONTINUE;
123
    }
124
 
125
    /**
126
     * Gets the count.
127
     *
128
     * @return the count
129
     */
130
    public long getCount() {
131
    	return num;
132
    }
133
 
134
    /**
135
     * Gets the total.
136
     *
137
     * @return the total
138
     */
139
    public long getTotal() {
140
    	return total;
141
    }
142
 
143
    /**
144
     * Get result list
145
     */
146
    public List<Path> getResult() {
147
    	return list;
148
    }
149
 
150
}