1 package org.codehaus.mojo.jaxb2.shared.filters.pattern; 2 3 /* 4 * Licensed to the Apache Software Foundation (ASF) under one 5 * or more contributor license agreements. See the NOTICE file 6 * distributed with this work for additional information 7 * regarding copyright ownership. The ASF licenses this file 8 * to you under the Apache License, Version 2.0 (the 9 * "License"); you may not use this file except in compliance 10 * with the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, 15 * software distributed under the License is distributed on an 16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 * KIND, either express or implied. See the License for the 18 * specific language governing permissions and limitations 19 * under the License. 20 */ 21 22 import org.apache.maven.plugin.logging.Log; 23 import org.codehaus.mojo.jaxb2.shared.FileSystemUtilities; 24 import org.codehaus.mojo.jaxb2.shared.Validate; 25 import org.codehaus.mojo.jaxb2.shared.filters.Filter; 26 import org.codehaus.mojo.jaxb2.shared.filters.Filters; 27 28 import java.io.File; 29 import java.io.FileFilter; 30 import java.util.ArrayList; 31 import java.util.Arrays; 32 import java.util.List; 33 import java.util.regex.Pattern; 34 35 /** 36 * <p>AbstractPatternFilter and FileFilter combination, using a set of Regular expressions 37 * to accept the canonical absolute paths to Files.</p> 38 * 39 * @author <a href="mailto:lj@jguru.se">Lennart Jörelid</a>, jGuru Europe AB 40 * @since 2.0 41 */ 42 public class PatternFileFilter extends AbstractPatternFilter<File> implements FileFilter { 43 44 /** 45 * Java RegExp pattern matching one or more letters/digits/punctuation characters. 46 * It can be flexibly used to separate normative text in a pattern: 47 * <ol> 48 * <li>Pattern matching <strong>ends of strings</strong>. <code>PATTERN_LETTER_DIGIT_PUNCT + "txt"</code> 49 * matches all file paths ending in "txt", such as "some/foobar.txt"</li> 50 * <li>Pattern matching <strong>strings containing patterns</strong>. <code>PATTERN_LETTER_DIGIT_PUNCT + 51 * "foobar" + PATTERN_LETTER_DIGIT_PUNCT</code> matches all file paths containing "foobar" such as 52 * "the/file/in/directory/foobar/blah.java"</li> 53 * <li>Pattern matching <strong>start of strings</strong>. <code>"/some/prefix" 54 * + PATTERN_LETTER_DIGIT_PUNCT</code> matches all file paths starting in "/some/prefix", such as 55 * "some/prefix/another/specification.xsd"</li> 56 * </ol> 57 */ 58 public static final String PATTERN_LETTER_DIGIT_PUNCT = "(\\p{javaLetterOrDigit}|\\p{Punct})+"; 59 60 /** 61 * Converter returning the canonical and absolute path for a File. 62 */ 63 public static final StringConverter<File> FILE_PATH_CONVERTER = new StringConverter<File>() { 64 @Override 65 public String convert(final File toConvert) { 66 return FileSystemUtilities.getCanonicalPath(toConvert.getAbsoluteFile()); 67 } 68 }; 69 70 /** 71 * Compound constructor creating an PatternFileFilter from the supplied parameters. 72 * 73 * @param processNullValues if {@code true}, this PatternFileFilter process null candidate values. 74 * @param patternPrefix a prefix to be prepended to any patterns submitted to 75 * this PatternFileFilter 76 * @param patterns The non-null list of Patters which should be applied within this 77 * PatternFileFilter. 78 * @param converter The StringConverter which converts Files to Strings for Pattern matching. 79 * @param acceptCandidateOnPatternMatch if {@code true}, this PatternFileFilter will matchAtLeastOnce 80 * candidate objects that match at least one of the supplied patterns. 81 * if {@code false}, this PatternFileFilter will noFilterMatches 82 * candidates that match at least one of the supplied patterns. 83 */ 84 public PatternFileFilter(final boolean processNullValues, 85 final String patternPrefix, 86 final List<String> patterns, 87 final StringConverter<File> converter, 88 final boolean acceptCandidateOnPatternMatch) { 89 super(); 90 91 // Assign internal state 92 setProcessNullValues(processNullValues); 93 setAcceptCandidateOnPatternMatch(acceptCandidateOnPatternMatch); 94 setPatternPrefix(patternPrefix); 95 setPatterns(patterns); 96 setConverter(converter); 97 } 98 99 /** 100 * Creates a new PatternFileFilter using the supplied patternStrings which are interpreted as file suffixes. 101 * (I.e. prepended with {@code PATTERN_LETTER_DIGIT_PUNCT} and compiled to Patterns). 102 * The {@code FILE_PATH_CONVERTER} is used to convert Files to strings. 103 * The supplied {@code acceptCandidateOnPatternMatch} parameter indicates if this 104 * PatternFileFilter accepts or rejects candidates that match any of the supplied patternStrings. 105 * 106 * @param patternStrings The list of patternStrings to be used as file path suffixes. 107 * @param acceptCandidateOnPatternMatch if {@code true}, this PatternFileFilter will matchAtLeastOnce 108 * candidate objects that match at least one of the supplied patterns. 109 * if {@code false}, this PatternFileFilter will noFilterMatches 110 * candidates that match at least one of the supplied patterns. 111 * @see #FILE_PATH_CONVERTER 112 * @see #PATTERN_LETTER_DIGIT_PUNCT 113 * @see #convert(java.util.List, String) 114 */ 115 public PatternFileFilter(final List<String> patternStrings, final boolean acceptCandidateOnPatternMatch) { 116 this(false, PATTERN_LETTER_DIGIT_PUNCT, patternStrings, FILE_PATH_CONVERTER, acceptCandidateOnPatternMatch); 117 } 118 119 /** 120 * Creates a new PatternFileFilter using the supplied patternStrings which are interpreted as file suffixes. 121 * (I.e. prepended with {@code PATTERN_LETTER_DIGIT_PUNCT} and compiled to Patterns). 122 * The {@code FILE_PATH_CONVERTER} is used to convert Files to strings. 123 * The retrieved PatternFileFilter accepts candidates that match any of the supplied patternStrings. 124 * 125 * @param patterns The list of patternStrings to be used as file path suffixes. 126 */ 127 public PatternFileFilter(final List<String> patterns) { 128 this(false, PATTERN_LETTER_DIGIT_PUNCT, patterns, FILE_PATH_CONVERTER, true); 129 } 130 131 /** 132 * <p>Creates a new PatternFileFilter with no patternStrings List, implying that calling this constructor must be 133 * followed by a call to the {@code #setPatterns} method.</p> 134 * <p>The default prefix is {@code PATTERN_LETTER_DIGIT_PUNCT}, the default StringConverter is 135 * {@code FILE_PATH_CONVERTER} and this PatternFileFilter does by default accept candidates that match any of 136 * the supplied PatternStrings (i.e. an include-mode filter)</p> 137 */ 138 public PatternFileFilter() { 139 this(false, PATTERN_LETTER_DIGIT_PUNCT, new ArrayList<String>(), FILE_PATH_CONVERTER, true); 140 } 141 142 /** 143 * Creates a new List containing an exclude-mode PatternFileFilter using the supplied patternStrings which 144 * are interpreted as file suffixes. (I.e. prepended with {@code PATTERN_LETTER_DIGIT_PUNCT} and compiled to 145 * Patterns). The {@code FILE_PATH_CONVERTER} is used to convert Files to strings. 146 * 147 * @param patterns A List of suffix patterns to be used in creating a new ExclusionRegularExpressionFileFilter. 148 * @param log The active Maven Log. 149 * @return A List containing a PatternFileFilter using the supplied suffix patterns to match Files. 150 * @see PatternFileFilter 151 */ 152 public static List<Filter<File>> createExcludeFilterList(final Log log, 153 final String... patterns) { 154 return createFilterList(log, false, patterns); 155 } 156 157 /** 158 * Creates a new List containing an include-mode PatternFileFilter using the supplied patternStrings which 159 * are interpreted as file suffixes. (I.e. prepended with {@code PATTERN_LETTER_DIGIT_PUNCT} and compiled to 160 * Patterns). The {@code FILE_PATH_CONVERTER} is used to convert Files to strings. 161 * 162 * @param patterns A List of suffix patterns to be used in creating a new ExclusionRegularExpressionFileFilter. 163 * @param log The active Maven Log. 164 * @return A List containing a PatternFileFilter using the supplied suffix patterns to match Files. 165 * @see PatternFileFilter 166 */ 167 public static List<Filter<File>> createIncludeFilterList(final Log log, 168 final String... patterns) { 169 return createFilterList(log, true, patterns); 170 } 171 172 // 173 // Private helpers 174 // 175 176 private static List<Filter<File>> createFilterList(final Log log, 177 final boolean includeOperation, 178 final String... patterns) { 179 180 // Check sanity 181 Validate.notNull(patterns, "patterns"); 182 Validate.notNull(log, "log"); 183 184 // Convert and return. 185 final List<Filter<File>> toReturn = new ArrayList<Filter<File>>(); 186 final List<String> patternStrings = Arrays.asList(patterns); 187 toReturn.add(new PatternFileFilter(patternStrings, includeOperation)); 188 189 // Initialize the filters. 190 Filters.initialize(log, toReturn); 191 return toReturn; 192 } 193 }