00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 using System;
00038 using System.Collections;
00039 using System.Text.RegularExpressions;
00040
00041 namespace ICSharpCode.SharpZipLib.Core
00042 {
00053 public class NameFilter
00054 {
00059 public NameFilter(string filter)
00060 {
00061 this.filter = filter;
00062 inclusions = new ArrayList();
00063 exclusions = new ArrayList();
00064 Compile();
00065 }
00066
00072 public static bool IsValidExpression(string e)
00073 {
00074 bool result = true;
00075 try {
00076 Regex exp = new Regex(e, RegexOptions.IgnoreCase | RegexOptions.Singleline);
00077 }
00078 catch {
00079 result = false;
00080 }
00081 return result;
00082 }
00083
00089 public static bool IsValidFilterExpression(string toTest)
00090 {
00091 bool result = true;
00092
00093 try
00094 {
00095 string[] items = toTest.Split(';');
00096 for (int i = 0; i < items.Length; ++i) {
00097 if (items[i] != null && items[i].Length > 0) {
00098 string toCompile;
00099
00100 if (items[i][0] == '+')
00101 toCompile = items[i].Substring(1, items[i].Length - 1);
00102 else if (items[i][0] == '-')
00103 toCompile = items[i].Substring(1, items[i].Length - 1);
00104 else
00105 toCompile = items[i];
00106
00107 Regex testRE = new Regex(toCompile, RegexOptions.IgnoreCase | RegexOptions.Singleline);
00108 }
00109 }
00110 }
00111 catch (Exception) {
00112 result = false;
00113 }
00114
00115 return result;
00116 }
00117
00122 public override string ToString()
00123 {
00124 return filter;
00125 }
00126
00132 public bool IsIncluded(string testValue)
00133 {
00134 bool result = false;
00135 if (inclusions.Count == 0)
00136 result = true;
00137 else {
00138 foreach (Regex r in inclusions) {
00139 if (r.IsMatch(testValue)) {
00140 result = true;
00141 break;
00142 }
00143 }
00144 }
00145 return result;
00146 }
00147
00153 public bool IsExcluded(string testValue)
00154 {
00155 bool result = false;
00156 foreach (Regex r in exclusions) {
00157 if (r.IsMatch(testValue)) {
00158 result = true;
00159 break;
00160 }
00161 }
00162 return result;
00163 }
00164
00170 public bool IsMatch(string testValue)
00171 {
00172 return IsIncluded(testValue) == true && IsExcluded(testValue) == false;
00173 }
00174
00178 void Compile()
00179 {
00180 if (filter == null)
00181 return;
00182
00183 string[] items = filter.Split(';');
00184 for (int i = 0; i < items.Length; ++i) {
00185 if (items[i] != null && items[i].Length > 0) {
00186 bool include = items[i][0] != '-';
00187 string toCompile;
00188
00189 if (items[i][0] == '+')
00190 toCompile = items[i].Substring(1, items[i].Length - 1);
00191 else if (items[i][0] == '-')
00192 toCompile = items[i].Substring(1, items[i].Length - 1);
00193 else
00194 toCompile = items[i];
00195
00196
00197
00198
00199 if (include)
00200 inclusions.Add(new Regex(toCompile, RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Singleline));
00201 else
00202 exclusions.Add(new Regex(toCompile, RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Singleline));
00203 }
00204 }
00205 }
00206
00207 #region Instance Fields
00208 string filter;
00209 ArrayList inclusions;
00210 ArrayList exclusions;
00211 #endregion
00212 }
00213 }