View Javadoc
1   package org.codehaus.mojo.appassembler.util;
2   
3   /*
4    * The MIT License
5    *
6    * Copyright (c) 2006-2012, The Codehaus
7    *
8    * Permission is hereby granted, free of charge, to any person obtaining a copy of
9    * this software and associated documentation files (the "Software"), to deal in
10   * the Software without restriction, including without limitation the rights to
11   * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12   * of the Software, and to permit persons to whom the Software is furnished to do
13   * so, subject to the following conditions:
14   *
15   * The above copyright notice and this permission notice shall be included in all
16   * copies or substantial portions of the Software.
17   *
18   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24   * SOFTWARE.
25   */
26  
27  import org.apache.commons.io.filefilter.FileFilterUtils;
28  import org.apache.commons.io.filefilter.IOFileFilter;
29  import org.apache.commons.io.filefilter.RegexFileFilter;
30  
31  /**
32   * This is helper class to summarize all filters.
33   *
34   * @author <a href="mailto:codehaus@soebes.de">Karl Heinz Marbaise</a>
35   */
36  public class FileFilterHelper
37  {
38      /**
39       * Make the given IOFileFilter aware of directories.
40       *
41       * @param filter The filter to make aware of directories.
42       * @param directoryName The directory name which should be payed attention to.
43       * @return The new generated filter.
44       */
45      public static IOFileFilter makeDirectoryAware( IOFileFilter filter, String directoryName )
46      {
47  
48          IOFileFilter directoryAwareFilter =
49              FileFilterUtils.notFileFilter( FileFilterUtils.andFileFilter( FileFilterUtils.directoryFileFilter(),
50                                                                            FileFilterUtils.nameFileFilter( directoryName ) ) );
51  
52          return FileFilterUtils.andFileFilter( filter, directoryAwareFilter );
53      }
54  
55      /**
56       * Make the given IOFileFilter aware of files.
57       *
58       * @param filter The filter to make aware of files.
59       * @param fileName The file name which should be payed attention to.
60       * @return The new generated filter.
61       */
62      public static IOFileFilter makeFileNameAware( IOFileFilter filter, String fileName )
63      {
64          IOFileFilter directoryAwareFilter =
65              FileFilterUtils.notFileFilter( FileFilterUtils.andFileFilter( FileFilterUtils.fileFileFilter(),
66                                                                            FileFilterUtils.nameFileFilter( fileName ) ) );
67  
68          return FileFilterUtils.andFileFilter( filter, directoryAwareFilter );
69      }
70  
71      /**
72       * Make the given IOFileFilter aware of an suffix.
73       *
74       * @param filter The filter to make aware of an suffix.
75       * @param suffixFileName The suffix name which should be payed attention to.
76       * @return The new generated filter.
77       */
78      public static IOFileFilter makeSuffixAware( IOFileFilter filter, String suffixFileName )
79      {
80          IOFileFilter directoryAwareFilter =
81              FileFilterUtils.notFileFilter( FileFilterUtils.andFileFilter( FileFilterUtils.fileFileFilter(),
82                                                                            FileFilterUtils.suffixFileFilter( suffixFileName ) ) );
83  
84          return FileFilterUtils.andFileFilter( filter, directoryAwareFilter );
85      }
86  
87      /**
88       * Make the given IOFileFilter aware of the given pattern.
89       *
90       * @param filter The filter to make aware of the pattern.
91       * @param pattern The pattern which should be payed attention to.
92       * @return The new generated filter.
93       */
94      public static IOFileFilter makePatternFileNameAware( IOFileFilter filter, String pattern )
95      {
96          IOFileFilter directoryAwareFilter =
97              FileFilterUtils.notFileFilter( FileFilterUtils.andFileFilter( FileFilterUtils.fileFileFilter(),
98                                                                            new RegexFileFilter( pattern ) ) );
99  
100         return FileFilterUtils.andFileFilter( filter, directoryAwareFilter );
101     }
102 
103     /**
104      * This will create a FileFilter which is the same as in plexus-utils (DirectoryScanner.DEFAULTEXCLUDES).
105      *
106      * @return The initialized filter.
107      */
108     public static IOFileFilter createDefaultFilter()
109     {
110 
111         IOFileFilter filter = null;
112 
113         // CVS
114         // "**/CVS", "**/CVS/**",
115         filter = FileFilterUtils.makeCVSAware( filter );
116 
117         // "**/.cvsignore",
118         filter = makeFileNameAware( filter, ".cvsignore" );
119 
120         // Subversion
121         // "**/.svn", "**/.svn/**",
122         filter = FileFilterUtils.makeSVNAware( filter );
123 
124         // RCS
125         // "**/RCS", "**/RCS/**",
126         filter = makeDirectoryAware( filter, "RCS" );
127 
128         // SCCS
129         // "**/SCCS", "**/SCCS/**",
130         filter = makeDirectoryAware( filter, "SCCS" );
131 
132         // "**/*~", "**/#*#", "**/.#*", "**/%*%", "**/._*",
133         filter = makeSuffixAware( filter, "~" );
134         filter = makePatternFileNameAware( filter, "#.*#" );
135         filter = makePatternFileNameAware( filter, "%.*%" );
136         filter = makeSuffixAware( filter, ".#" );
137         filter = makeSuffixAware( filter, "._" );
138 
139         // Visual SourceSafe
140         // "**/vssver.scc",
141         filter = makeFileNameAware( filter, "vssver.scc" );
142 
143         // MKS
144         // "**/project.pj",
145         filter = makeFileNameAware( filter, "project.pj" );
146 
147         // Arch
148         // "**/.arch-ids", "**/.arch-ids/**",
149         filter = makeDirectoryAware( filter, ".arch-ids" );
150 
151         // Bazaar
152         // "**/.bzr", "**/.bzr/**",
153         filter = makeDirectoryAware( filter, ".bzr" );
154 
155         // SurroundSCM
156         // "**/.MySCMServerInfo",
157         filter = makeFileNameAware( filter, ".MySCMServerInfo" );
158 
159         // Mac
160         // "**/.DS_Store",
161         filter = makeDirectoryAware( filter, ".DS_Store" );
162 
163         // Serena Dimensions Version 10
164         // "**/.metadata", "**/.metadata/**",
165         filter = makeDirectoryAware( filter, ".metadata" );
166 
167         // Mercurial
168         // "**/.hg", "**/.hg/**",
169         filter = makeDirectoryAware( filter, ".hg" );
170 
171         // git
172         // "**/.git", "**/.gitignore", "**/.gitattributes", "**/.git/**",
173         filter = makeDirectoryAware( filter, ".git" );
174         filter = makeFileNameAware( filter, ".gitignore" );
175 
176         // BitKeeper
177         // "**/BitKeeper", "**/BitKeeper/**", "**/ChangeSet", "**/ChangeSet/**",
178         filter = makeDirectoryAware( filter, "BitKeeper" );
179         filter = makeDirectoryAware( filter, "ChangeSet" );
180 
181         // darcs
182         // "**/_darcs", "**/_darcs/**", "**/.darcsrepo", "**/.darcsrepo/**", "**/-darcs-backup*", "**/.darcs-temp-mail"
183         // };
184         filter = makeDirectoryAware( filter, "_darcs" );
185         filter = makeDirectoryAware( filter, ".darcsrepo" );
186 
187         return filter;
188     }
189 
190 }