微软更有效率,更专注。
public static class TestorsBuilder
{
private static readonly string config = "config.test.xml";
public static IEnumerable Testors
{
get
{
XElement root = XElement.Load(config.GetAbsrotePath());
var testorList = root.Element("analyzer.test").Elements("testor")
.Where(testor=> testor.Attribute("enable").Value=="true");
foreach (var testor in testorList)
{
var siteElement = testor.Element("site");
var pagesElement = testor.Element("pages");
var site = new Site(
siteElement.Element("name").Value,
siteElement.Element("uri").Value,
siteElement.Element("analyzer").Value.GetAnalyzer(),
siteElement.Element("encoding").Value.GetEncoding());
var analyzerTestor = new AnalyzerTestor
{
Site = site,
AlphaBetPageUri = pagesElement.Element("alphabetpage").Value,
ComicPageUri = pagesElement.Element("comicpage").Value,
ImagePageUri = pagesElement.Element("imagepage").Value
};
yield return analyzerTestor;
};
}
}
}
public static class Extension
{
public static void ForEach(this IEnumerable source, Action func)
{
foreach (var item in source) func(item);
}
public static IAnalyzer GetAnalyzer(this string name)
{
switch (name)
{
//敏感词替换
case "XXXAnalyzer": return new XXXAnalyzer();
case "YYYAnalyzer": return new YYYAnalyzer();
default: throw new Exception("无效的分析器名称");
}
}
public static Encoding GetEncoding(this string name)
{
switch (name)
{
case "default": return Encoding.Default;
case "utf-8": return Encoding.UTF8;
case "utf-32": return Encoding.UTF32;
case "utf-7": return Encoding.UTF7;
case "ascii": return Encoding.ASCII;
case "big-endian-unicode": return Encoding.BigEndianUnicode;
case "unicode": return Encoding.Unicode;
default: throw new Exception("无效的编码名称");
}
}
public static string GetAbsrotePath(this string path)
{
StringBuilder builder = new StringBuilder();
builder.Append(System.Environment.CurrentDirectory)
.Append(@"\")
.Append(path);
return builder.ToString();
}
}
阅读(3954) | 评论(0) | 转发(0) |