Mes documents/Visual Studio 2005/Projects/TES4ModTranslator/TES4ModTranslator/Core/NameFilter.cs

Go to the documentation of this file.
00001 // NameFilter.cs
00002 //
00003 // Copyright 2005 John Reilly
00004 //
00005 // This program is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU General Public License
00007 // as published by the Free Software Foundation; either version 2
00008 // of the License, or (at your option) any later version.
00009 //
00010 // This program is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with this program; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00018 //
00019 // Linking this library statically or dynamically with other modules is
00020 // making a combined work based on this library.  Thus, the terms and
00021 // conditions of the GNU General Public License cover the whole
00022 // combination.
00023 // 
00024 // As a special exception, the copyright holders of this library give you
00025 // permission to link this library with independent modules to produce an
00026 // executable, regardless of the license terms of these independent
00027 // modules, and to copy and distribute the resulting executable under
00028 // terms of your choice, provided that you also meet, for each linked
00029 // independent module, the terms and conditions of the license of that
00030 // module.  An independent module is a module which is not derived from
00031 // or based on this library.  If you modify this library, you may extend
00032 // this exception to your version of the library, but you are not
00033 // obligated to do so.  If you do not wish to do so, delete this
00034 // exception statement from your version.
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                                         // NOTE: Regular expressions can fail to compile here for a number of reasons that cause an exception
00197                                         // these are left unhandled here as the caller is responsible for ensuring all is valid.
00198                                         // several functions IsValidFilterExpression and IsValidExpression are provided for such checking
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 }

Generated on Fri Jun 23 21:50:04 2006 for OblivionModTranslator by  doxygen 1.4.6-NO