View Javadoc

1   /**
2    * Copyright 2011-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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 com.dhptech.maven.stripbom;
17  
18  import static java.util.Arrays.asList;
19  
20  import java.io.File;
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import org.codehaus.plexus.util.DirectoryScanner;
25  
26  /**
27   * Logic for recursively scanning a base directory for files that match include/exclude patterns
28   */
29  public final class SimpleScanner {
30  
31      private final File basedir;
32      private final String[] included;
33      private final String[] excluded;
34  
35      private DirectoryScanner scanner;
36  
37      public SimpleScanner(File basedir, String[] included, String[] excluded, boolean useDefaultExcludes) {
38          this.basedir = basedir;
39          this.included = buildInclusions(included);
40          this.excluded = buildExclusions(useDefaultExcludes, excluded);
41      }
42  
43      public String[] getSelectedFiles() {
44          scanIfNeeded();
45          return scanner.getIncludedFiles();
46      }
47  
48      public File getBasedir() {
49          return basedir;
50      }
51  
52      public String[] getIncluded() {
53          return included;
54      }
55  
56      public String[] getExcluded() {
57          return excluded;
58      }
59  
60      protected synchronized void scanIfNeeded() {
61          if (scanner == null) {
62              scanner = new DirectoryScanner();
63              scanner.setBasedir(basedir);
64              scanner.setIncludes(included);
65              scanner.setExcludes(excluded);
66              scanner.scan();
67          }
68      }
69  
70      protected static String[] buildExclusions(boolean useDefaultExcludes, String... excludes) {
71          List<String> exclusions = new ArrayList<String>();
72          if (useDefaultExcludes) {
73              exclusions.addAll(asList(DEFAULT_EXCLUDES));
74          }
75          if (excludes != null && excludes.length > 0) {
76              exclusions.addAll(asList(excludes));
77          }
78          return exclusions.toArray(new String[exclusions.size()]);
79      }
80  
81      protected static String[] buildInclusions(String... includes) {
82          return includes != null && includes.length > 0 ? includes : DEFAULT_INCLUDE;
83      }
84  
85      static final String[] DEFAULT_INCLUDE = new String[] { "**" };
86  
87      static final String[] DEFAULT_EXCLUDES = {
88              // Miscellaneous typical temporary files
89              "**/*~", "**/#*#", "**/.#*",
90              "**/%*%",
91              "**/._*",
92              "**/.repository/**",
93  
94              // CVS
95              "**/CVS",
96              "**/CVS/**",
97              "**/.cvsignore",
98  
99              // RCS
100             "**/RCS",
101             "**/RCS/**",
102 
103             // SCCS
104             "**/SCCS",
105             "**/SCCS/**",
106 
107             // Visual SourceSafe
108             "**/vssver.scc",
109 
110             // Subversion
111             "**/.svn",
112             "**/.svn/**",
113 
114             // Arch
115             "**/.arch-ids",
116             "**/.arch-ids/**",
117 
118             // Bazaar
119             "**/.bzr",
120             "**/.bzr/**",
121 
122             // SurroundSCM
123             "**/.MySCMServerInfo",
124 
125             // Mac
126             "**/.DS_Store",
127 
128             // Serena Dimensions Version 10
129             "**/.metadata",
130             "**/.metadata/**",
131 
132             // Mercurial
133             "**/.hg",
134             "**/.hg/**",
135 
136             // git
137             "**/.git",
138             "**/.git/**",
139 
140             // BitKeeper
141             "**/BitKeeper", "**/BitKeeper/**", "**/ChangeSet",
142             "**/ChangeSet/**",
143 
144             // darcs
145             "**/_darcs", "**/_darcs/**", "**/.darcsrepo", "**/.darcsrepo/**", "**/-darcs-backup*",
146             "**/.darcs-temp-mail",
147 
148             // maven project's temporary files
149             "**/target/**", "**/test-output/**", "**/release.properties", "**/dependency-reduced-pom.xml",
150 
151             // code coverage tools
152             "**/cobertura.ser", "**/.clover/**",
153 
154             // eclipse project files
155             "**/.classpath", "**/.project", "**/.settings/**",
156 
157             // IDEA project files
158             "**/*.iml", "**/*.ipr", "**/*.iws",
159 
160             // descriptors
161             "**/MANIFEST.MF",
162 
163             // binary files - images
164             "**/*.jpg", "**/*.png", "**/*.gif", "**/*.ico", "**/*.bmp", "**/*.tiff", "**/*.tif", "**/*.cr2",
165             "**/*.xcf",
166 
167             // binary files - programs
168             "**/*.class", "**/*.exe",
169 
170             // checksum files
171             "**/*.md5", "**/*.sha1",
172 
173             // binary files - archives
174             "**/*.jar", "**/*.war", "**/*.zip", "**/*.rar", "**/*.tar", "**/*.tar.gz", "**/*.tar.bz2", "**/*.gz",
175 
176             // binary files - documents
177             "**/*.xls",
178 
179             // ServiceLoader files
180             "**/META-INF/services/**" };
181 
182 }