Subversion Repositories XServices

Rev

Rev 198 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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