package hu.qgears.review.tool;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Simplified file set definition class, that enumerates a white list of
 * required components. The definition file must contain valid java regular
 * expressions, that will be evaluated on the source file URLs. Matching sources
 * are part of fileset.
 * 
 * @author agostoni
 * 
 */
public class WhiteListFileSet {

	public static final Object ID = "whitelistfileset";
	private static final String COMMENT_MARK = "#";
	private List<Matcher> validPatterns = new ArrayList<Matcher>();
	
	/**
	 * Creates a new instance. See {@link WhiteListFileSet head comment}
	 * 
	 * @param fileSetDefLines
	 *            The content of definition file read into memory.
	 */
	public WhiteListFileSet(final List<String> fileSetDefLines) {
		parseValidPatterns(fileSetDefLines);
	}


	private void parseValidPatterns(List<String> lines) {
		for (String l : lines){
			l = l.trim();
			if (!ID.equals(l) && !l.startsWith(COMMENT_MARK) && !l.isEmpty()){
				validPatterns.add(Pattern.compile(l).matcher(""));
			}
		}
	}

	/**
	 * Filters non matching sources from specified list, and returns a list
	 * holding only the matching ones.
	 * 
	 * @param stringList
	 * @param problems 
	 * @return
	 */
	public List<String> reduce(final List<String> stringList) {
		List<String> mList = new ArrayList<String>();
		for (String s : stringList){
			for (Matcher m : validPatterns){
				m.reset(s);
				if (m.matches()){
					mList.add(s);
					break;
				}
			}
		}
		return mList;
	}
	
	
	
}

@@ -579,27 +579,152 @@
 ate 
-List%3CMatcher%3E valid
+static final String EXCLUSION_MARK = %22! %22;%0A%09private List%3CMatcher%3E inclusionPatterns = new ArrayList%3CMatcher%3E();%0A%09private List%3CMatcher%3E exclusion
 Patt
@@ -1151,79 +1151,404 @@
 f (!
-ID.equals(l) && !l.startsWith(COMMENT_MARK) && !l.isEmpty())%7B%0A%09%09%09%09valid
+l.isEmpty() && !ID.equals(l)) %7B%0A%09%09%09%09if (l.startsWith(COMMENT_MARK)) %7B%0A%09%09%09%09%09//nothing to do, this line is commented by the used%0A%09%09%09%09%7D else if (l.startsWith(EXCLUSION_MARK)) %7B%0A%09%09%09%09%09//exclusion pattern found%0A%09%09%09%09%09String pattern = l.substring(EXCLUSION_MARK.length());%0A%09%09%09%09%09exclusionPatterns.add(Pattern.compile(pattern).matcher(%22%22));%0A%09%09%09%09%7D else %7B%0A%09%09%09%09%09//normal inclusion pattern found%0A%09%09%09%09%09inclusion
 Patt
@@ -1589,16 +1589,22 @@
 r(%22%22));%0A
+%09%09%09%09%7D%0A
 %09%09%09%7D%0A%09%09%7D
@@ -1925,18 +1925,111 @@
 ingList)
+ 
 %7B%0A
+%09%09%09boolean matches = false;%0A%09%09%09//first find out whether this line matches at least one line%0A
 %09%09%09for (
@@ -2040,21 +2040,25 @@
 her m : 
-valid
+inclusion
 Patterns
@@ -2108,21 +2108,244 @@
 %09%09%09m
-List.add(s)
+atches = true;%0A%09%09%09%09%09break;%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%09//if matches, than check that any exclusion pattern disables the source file %0A%09%09%09if (matches) %7B%0A%09%09%09%09for (Matcher ex : exclusionPatterns)%7B%0A%09%09%09%09%09if (ex.reset(s).matches())%7B%0A%09%09%09%09%09%09matches = false
 ;%0A
+%09
 %09%09%09%09
@@ -2348,33 +2348,80 @@
 %09%09%09%09%09break;%0A%09%09%09%09
-%7D
+%09%7D%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%09if (matches)%7B%0A%09%09%09%09mList.add(s);
 %0A%09%09%09%7D%0A%09%09%7D%0A%09%09retu

Support exlude patterns in whitelist file set (lines starting with '! ' are considered as exlusion patterns)