1 using System.Collections.Generic;
2 using System.ComponentModel;
5 using System.Xml.Serialization;
7 namespace DirectOutput.GlobalConfiguration
12 public class FilePattern : INotifyPropertyChanged, IXmlSerializable
16 #region IXmlSerializable Member
21 public System.Xml.Schema.XmlSchema GetSchema()
30 public void ReadXml(System.Xml.XmlReader reader)
32 Pattern = reader.ReadString();
33 if (!reader.IsEmptyElement)
35 reader.ReadEndElement();
39 reader.ReadStartElement();
47 public void WriteXml(System.Xml.XmlWriter writer)
49 writer.WriteString(Pattern);
54 #region INotifyPropertyChanged Member
61 private void NotifyPropertyChanged(
string PropertyName)
63 if (PropertyChanged != null)
65 PropertyChanged(
this,
new PropertyChangedEventArgs(PropertyName));
71 private string _Pattern;
81 get {
return _Pattern; }
84 if (_Pattern != value)
87 NotifyPropertyChanged(
"Pattern");
97 public string ReplacePlaceholders(Dictionary<string, string> ReplaceValues = null)
100 if (ReplaceValues != null)
102 foreach (KeyValuePair<string, string> KV
in ReplaceValues)
104 P = P.Replace(
"{" + (KV.Key) +
"}", KV.Value);
116 public List<FileInfo> GetMatchingFiles(Dictionary<string, string> ReplaceValues = null)
118 if (Pattern.IsNullOrWhiteSpace())
return new List<FileInfo>();
120 string P = ReplacePlaceholders(ReplaceValues);
125 DirectoryInfo FilesDirectory =
new DirectoryInfo((Path.GetDirectoryName(P).IsNullOrWhiteSpace() ?
"." : Path.GetDirectoryName(P)));
126 if (FilesDirectory.Exists)
128 return FilesDirectory.GetFiles(Path.GetFileName(P)).ToList<FileInfo>();
132 return new List<FileInfo>();
137 return new List<FileInfo>();
146 public FileInfo GetFirstMatchingFile(Dictionary<string, string> ReplaceValues = null)
148 List<FileInfo> L = GetMatchingFiles(ReplaceValues);
170 if (Pattern.IsNullOrWhiteSpace())
return true;
174 FileInfo DummyFileInfo =
new FileInfo(Pattern.Replace(
"*",
"test").Replace(
"?",
"X"));
181 bool BracketOpen =
false;
182 bool JustOpend =
false;
183 foreach (
char C
in Pattern)
188 if (BracketOpen)
return false;
193 if (!BracketOpen)
return false;
194 if (JustOpend)
return false;
201 if (BracketOpen)
return false;
209 if (BracketOpen)
return false;
223 this.Pattern = Pattern;
234 public override string ToString()