View Javadoc
1   package org.codehaus.mojo.jaxb2.shared.filters.pattern;
2   
3   import org.apache.maven.plugin.logging.Log;
4   import org.codehaus.mojo.jaxb2.shared.FileSystemUtilities;
5   import org.codehaus.mojo.jaxb2.shared.Validate;
6   import org.codehaus.mojo.jaxb2.shared.filters.Filter;
7   import org.codehaus.mojo.jaxb2.shared.filters.Filters;
8   
9   import java.io.File;
10  import java.io.FileFilter;
11  import java.util.ArrayList;
12  import java.util.Arrays;
13  import java.util.List;
14  
15  /**
16   * <p>AbstractPatternFilter and FileFilter combination, using a set of Regular expressions
17   * to accept the canonical absolute paths to Files.</p>
18   *
19   * @author <a href="mailto:lj@jguru.se">Lennart J&ouml;relid</a>, jGuru Europe AB
20   * @since 2.0
21   */
22  public class PatternFileFilter extends AbstractPatternFilter<File> implements FileFilter {
23  
24      /**
25       * Java RegExp pattern which should be prepended to any file suffix pattern.
26       * For example, a pattern identifying files ending in "txt", the pattern is
27       * <code>FILE_SUFFIX_PATTERN_PREFIX + "txt"</code>.
28       */
29      public static final String FILE_SUFFIX_PATTERN_PREFIX = "(\\p{javaLetterOrDigit}|\\p{Punct})+";
30  
31      /**
32       * Converter returning the canonical and absolute path for a File.
33       */
34      public static final StringConverter<File> FILE_PATH_CONVERTER = new StringConverter<File>() {
35          @Override
36          public String convert(final File toConvert) {
37              return FileSystemUtilities.getCanonicalPath(toConvert.getAbsoluteFile());
38          }
39      };
40  
41      /**
42       * Compound constructor creating an PatternFileFilter from the supplied parameters.
43       *
44       * @param processNullValues             if {@code true}, this PatternFileFilter process null candidate values.
45       * @param patternPrefix                 a prefix to be prepended to any patterns submitted to
46       *                                      this PatternFileFilter
47       * @param patterns                      The non-null list of Patters which should be applied within this
48       *                                      PatternFileFilter.
49       * @param converter                     The StringConverter which converts Files to Strings for Pattern matching.
50       * @param acceptCandidateOnPatternMatch if {@code true}, this PatternFileFilter will matchAtLeastOnce
51       *                                      candidate objects that match at least one of the supplied patterns.
52       *                                      if {@code false}, this PatternFileFilter will noFilterMatches
53       *                                      candidates that match at least one of the supplied patterns.
54       */
55      public PatternFileFilter(final boolean processNullValues,
56                               final String patternPrefix,
57                               final List<String> patterns,
58                               final StringConverter<File> converter,
59                               final boolean acceptCandidateOnPatternMatch) {
60          super();
61  
62          // Assign internal state
63          setProcessNullValues(processNullValues);
64          setAcceptCandidateOnPatternMatch(acceptCandidateOnPatternMatch);
65          setPatternPrefix(patternPrefix);
66          setPatterns(patterns);
67          setConverter(converter);
68      }
69  
70      /**
71       * Creates a new PatternFileFilter using the supplied patternStrings which are interpreted as file suffixes.
72       * (I.e. prepended with {@code FILE_SUFFIX_PATTERN_PREFIX} and compiled to Patterns).
73       * The {@code FILE_PATH_CONVERTER} is used to convert Files to strings.
74       * The supplied {@code acceptCandidateOnPatternMatch} parameter indicates if this
75       * PatternFileFilter accepts or rejects candidates that match any of the supplied patternStrings.
76       *
77       * @param patternStrings                The list of patternStrings to be used as file path suffixes.
78       * @param acceptCandidateOnPatternMatch if {@code true}, this PatternFileFilter will matchAtLeastOnce
79       *                                      candidate objects that match at least one of the supplied patterns.
80       *                                      if {@code false}, this PatternFileFilter will noFilterMatches
81       *                                      candidates that match at least one of the supplied patterns.
82       * @see #FILE_PATH_CONVERTER
83       * @see #FILE_SUFFIX_PATTERN_PREFIX
84       * @see #convert(java.util.List, String)
85       */
86      public PatternFileFilter(final List<String> patternStrings, final boolean acceptCandidateOnPatternMatch) {
87          this(false, FILE_SUFFIX_PATTERN_PREFIX, patternStrings, FILE_PATH_CONVERTER, acceptCandidateOnPatternMatch);
88      }
89  
90      /**
91       * Creates a new PatternFileFilter using the supplied patternStrings which are interpreted as file suffixes.
92       * (I.e. prepended with {@code FILE_SUFFIX_PATTERN_PREFIX} and compiled to Patterns).
93       * The {@code FILE_PATH_CONVERTER} is used to convert Files to strings.
94       * The retrieved PatternFileFilter accepts candidates that match any of the supplied patternStrings.
95       *
96       * @param patterns The list of patternStrings to be used as file path suffixes.
97       */
98      public PatternFileFilter(final List<String> patterns) {
99          this(false, FILE_SUFFIX_PATTERN_PREFIX, patterns, FILE_PATH_CONVERTER, true);
100     }
101 
102     /**
103      * <p>Creates a new PatternFileFilter with no patternStrings List, implying that calling this constructor must be
104      * followed by a call to the {@code #setPatterns} method.</p>
105      * <p>The default prefix is {@code FILE_SUFFIX_PATTERN_PREFIX}, the default StringConverter is
106      * {@code FILE_PATH_CONVERTER} and this PatternFileFilter does by default accept candidates that match any of
107      * the supplied PatternStrings (i.e. an include-mode filter)</p>
108      */
109     public PatternFileFilter() {
110         this(false, FILE_SUFFIX_PATTERN_PREFIX, new ArrayList<String>(), FILE_PATH_CONVERTER, true);
111     }
112 
113     /**
114      * Creates a new List containing an exclude-mode PatternFileFilter using the supplied patternStrings which
115      * are interpreted as file suffixes. (I.e. prepended with {@code FILE_SUFFIX_PATTERN_PREFIX} and compiled to
116      * Patterns). The {@code FILE_PATH_CONVERTER} is used to convert Files to strings.
117      *
118      * @param patterns A List of suffix patterns to be used in creating a new ExclusionRegularExpressionFileFilter.
119      * @param log      The active Maven Log.
120      * @return A List containing a PatternFileFilter using the supplied suffix patterns to match Files.
121      * @see PatternFileFilter
122      */
123     public static List<Filter<File>> createExcludeFilterList(final Log log,
124                                                              final String... patterns) {
125         return createFilterList(log, false, patterns);
126     }
127 
128     /**
129      * Creates a new List containing an include-mode PatternFileFilter using the supplied patternStrings which
130      * are interpreted as file suffixes. (I.e. prepended with {@code FILE_SUFFIX_PATTERN_PREFIX} and compiled to
131      * Patterns). The {@code FILE_PATH_CONVERTER} is used to convert Files to strings.
132      *
133      * @param patterns A List of suffix patterns to be used in creating a new ExclusionRegularExpressionFileFilter.
134      * @param log      The active Maven Log.
135      * @return A List containing a PatternFileFilter using the supplied suffix patterns to match Files.
136      * @see PatternFileFilter
137      */
138     public static List<Filter<File>> createIncludeFilterList(final Log log,
139                                                              final String... patterns) {
140         return createFilterList(log, true, patterns);
141     }
142 
143     //
144     // Private helpers
145     //
146 
147     private static List<Filter<File>> createFilterList(final Log log,
148                                                        final boolean includeOperation,
149                                                        final String... patterns) {
150 
151         // Check sanity
152         Validate.notNull(patterns, "patterns");
153         Validate.notNull(log, "log");
154 
155         // Convert and return.
156         final List<Filter<File>> toReturn = new ArrayList<Filter<File>>();
157         final List<String> patternStrings = Arrays.asList(patterns);
158         toReturn.add(new PatternFileFilter(patternStrings, includeOperation));
159 
160         // Initialize the filters.
161         Filters.initialize(log, toReturn);
162         return toReturn;
163     }
164 }