Subversion Repositories XServices

Rev

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