File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\util\DirScanner.java.html

1 package hudson.util;
2
3 import hudson.Util;
4 import org.apache.tools.ant.DirectoryScanner;
5 import org.apache.tools.ant.types.FileSet;
6
7 import java.io.File;
8 import java.io.FileFilter;
9 import java.io.IOException;
10 import java.io.InterruptedIOException;
11 import java.io.Serializable;
12
13 import static hudson.Util.fixEmpty;
14
15 /**
16 * Visits a directory and its contents and pass them to the {@link FileVisitor}.
17 *
18 * A {@link DirScanner} encapsulates the logic of how it filters files in the directory. It is also remotable.
19 *
20 * @since 1.343
21 * @see FileVisitor
22 */
23 public abstract class DirScanner implements Serializable {
24 /**
25 * Scans the given directory and pass files onto the given visitor.
26 */
27 public abstract void scan(File dir, FileVisitor visitor) throws IOException;
28
29 /**
30 * Scans everything recursively.
31 */
32 public static class Full extends DirScanner {
33 private void scan(File f, String path, FileVisitor visitor) throws IOException {
34 if (f.canRead()) {
35 if (visitor.understandsSymlink()) {
36 try {
37 String target;
38 try {
39 target = Util.resolveSymlink(f);
40 } catch (IOException x) { // JENKINS-13202
41 target = null;
42 }
43 if (target!=null) {
44 visitor.visitSymlink(f,target,path+f.getName());
45 return;
46 }
47 } catch (InterruptedException e) {
48 throw (IOException)new InterruptedIOException().initCause(e);
49 }
50 }
51 visitor.visit(f,path+f.getName());
52 if(f.isDirectory()) {
53 for( File child : f.listFiles() )
54 scan(child,path+f.getName()+'/',visitor);
55 }
56 }
57 }
58
59 public void scan(File dir, FileVisitor visitor) throws IOException {
60 scan(dir,"",visitor);
61 }
62
63 private static final long serialVersionUID = 1L;
64 }
65
66 /**
67 * Scans by filtering things out from {@link FileFilter}
68 */
69 public static class Filter extends Full {
70 private final FileFilter filter;
71
72 public Filter(FileFilter filter) {
73 this.filter = filter;
74 }
75
76 @Override
77 public void scan(File dir, FileVisitor visitor) throws IOException {
78 super.scan(dir,visitor.with(filter));
79 }
80
81 private static final long serialVersionUID = 1L;
82 }
83
84 /**
85 * Scans by using Ant GLOB syntax.
86 */
87 public static class Glob extends DirScanner {
88 private final String includes, excludes;
89
90 private boolean useDefaultExcludes = true;
91
92 public Glob(String includes, String excludes) {
93 this.includes = includes;
94 this.excludes = excludes;
95 }
96
97 public Glob(String includes, String excludes, boolean useDefaultExcludes) {
98 this(includes, excludes);
99 this.useDefaultExcludes = useDefaultExcludes;
100 }
101
102 public void scan(File dir, FileVisitor visitor) throws IOException {
103 if(fixEmpty(includes)==null && excludes==null) {
104 // optimization
105 new Full().scan(dir,visitor);
106 return;
107 }
108
109 FileSet fs = Util.createFileSet(dir,includes,excludes);
110 fs.setDefaultexcludes(useDefaultExcludes);
111
112 if(dir.exists()) {
113 DirectoryScanner ds = fs.getDirectoryScanner(new org.apache.tools.ant.Project());
114 for( String f : ds.getIncludedFiles()) {
115 File file = new File(dir, f);
116
117 if (visitor.understandsSymlink()) {
118 try {
119 String target;
120 try {
121 target = Util.resolveSymlink(file);
122 } catch (IOException x) { // JENKINS-13202
123 target = null;
124 }
125 if (target!=null) {
126 visitor.visitSymlink(file,target,f);
127 continue;
128 }
129 } catch (InterruptedException e) {
130 throw (IOException)new InterruptedIOException().initCause(e);
131 }
132 }
133 visitor.visit(file,f);
134 }
135 }
136 }
137
138 private static final long serialVersionUID = 1L;
139 }
140
141 private static final long serialVersionUID = 1L;
142 }
143