Subversion Repositories XServices

Rev

Rev 198 | 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
 
199 brianR 18
import lombok.extern.slf4j.Slf4j;
19
 
147 brianR 20
import java.io.IOException;
21
import java.nio.file.FileSystems;
22
import java.nio.file.FileVisitResult;
23
import java.nio.file.Path;
24
import java.nio.file.PathMatcher;
25
import java.nio.file.SimpleFileVisitor;
26
import java.nio.file.attribute.BasicFileAttributes;
27
import java.util.ArrayList;
28
import java.util.List;
29
 
30
 
185 brianR 31
 
147 brianR 32
// TODO: Auto-generated Javadoc
33
/**
34
 * The Class FileWalker.
35
 *
36
 * @author Brian Rosenberger, bru(at)brutex.de
37
 */
199 brianR 38
@Slf4j
147 brianR 39
public class FileWalker extends SimpleFileVisitor<Path> {
40
 
41
	/** The matcher. */
42
	private final PathMatcher matcher;
43
 
44
	/** The num. */
45
	private long num=0;
46
 
47
	/** The total. */
48
	private long total=0;
49
 
50
	/** The pattern. */
51
	private final String pattern;
52
 
53
	List<Path> list;
54
 
55
	/**
56
	 * Instantiates a new file walker.
57
	 *
58
	 * @param pattern the pattern
59
	 */
60
	public FileWalker(String pattern) {
61
        matcher = FileSystems.getDefault()
62
                .getPathMatcher("glob:" + pattern);
63
        this.pattern = "glob:"+pattern;
64
        this.list = new ArrayList<Path>();
65
    }
66
 
67
 
68
	 // Compares the glob pattern against
69
    // the file or directory name.
70
    /**
71
 	 * Find.
72
 	 *
73
 	 * @param file the file
74
 	 */
75
 	void find(Path file) {
76
        Path name = file.getFileName();
199 brianR 77
        log.debug("Compare file '{}' against pattern '{}'.", file, pattern);
147 brianR 78
        total++;
79
        if (name != null && matcher.matches(name)) {
80
            list.add(file);
199 brianR 81
			log.debug("Added file '{}' to the result set.", file);
147 brianR 82
			num++;
83
        }
84
    }
85
 
86
    // Invoke the pattern matching
87
    // method on each file.
88
    /* (non-Javadoc)
89
     * @see java.nio.file.SimpleFileVisitor#visitFile(java.lang.Object, java.nio.file.attribute.BasicFileAttributes)
90
     */
91
    @Override
92
    public FileVisitResult visitFile(Path file,
93
            BasicFileAttributes attrs) {
94
 
95
        find(file);
96
        return FileVisitResult.CONTINUE;
97
    }
98
 
99
    // Invoke the pattern matching
100
    // method on each directory.
101
    /* (non-Javadoc)
102
     * @see java.nio.file.SimpleFileVisitor#preVisitDirectory(java.lang.Object, java.nio.file.attribute.BasicFileAttributes)
103
     */
104
    @Override
105
    public FileVisitResult preVisitDirectory(Path dir,
106
            BasicFileAttributes attrs) {
107
        find(dir);
108
        return FileVisitResult.CONTINUE;
109
    }
110
 
111
    /* (non-Javadoc)
112
     * @see java.nio.file.SimpleFileVisitor#visitFileFailed(java.lang.Object, java.io.IOException)
113
     */
114
    @Override
115
    public FileVisitResult visitFileFailed(Path file,
116
            IOException exc) {
199 brianR 117
        log.warn("Failed to include file '{}'.", file);
147 brianR 118
        return FileVisitResult.CONTINUE;
119
    }
120
 
121
    /**
122
     * Gets the count.
123
     *
124
     * @return the count
125
     */
126
    public long getCount() {
127
    	return num;
128
    }
129
 
130
    /**
131
     * Gets the total.
132
     *
133
     * @return the total
134
     */
135
    public long getTotal() {
136
    	return total;
137
    }
138
 
139
    /**
140
     * Get result list
141
     */
142
    public List<Path> getResult() {
143
    	return list;
144
    }
145
 
146
}