Subversion Repositories XServices

Rev

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

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