From 3b03400ed32d814a155734b7c9f3fadc571827a1 Mon Sep 17 00:00:00 2001 From: Jonathan Preddy Date: Wed, 23 Jul 2014 17:05:15 -0400 Subject: [PATCH] Added Parsers as abstract parent class for InputBase and FilterBase --- TimberWinR.UnitTests/Configuration.cs | 3 - TimberWinR/Filters/FilterBase.cs | 34 +------ TimberWinR/Inputs/IISLog.cs | 6 +- TimberWinR/Inputs/IISW3CLog.cs | 6 +- TimberWinR/Inputs/InputBase.cs | 118 +----------------------- TimberWinR/Inputs/TailFileInput.cs | 6 +- TimberWinR/Inputs/WindowsEvent.cs | 6 +- TimberWinR/Parsers.cs | 124 ++++++++++++++++++++++++++ TimberWinR/TimberWinR.csproj | 1 + 9 files changed, 140 insertions(+), 164 deletions(-) create mode 100644 TimberWinR/Parsers.cs diff --git a/TimberWinR.UnitTests/Configuration.cs b/TimberWinR.UnitTests/Configuration.cs index 40af279..5030d90 100644 --- a/TimberWinR.UnitTests/Configuration.cs +++ b/TimberWinR.UnitTests/Configuration.cs @@ -311,9 +311,6 @@ namespace TimberWinR.UnitTests { if (filter.GetType() == typeof(GrokFilter)) { - Console.WriteLine(((GrokFilter)filter).AddFields[0].Field); - Console.WriteLine(((GrokFilter)filter).AddFields[0].Value); - Assert.AreEqual(field, ((GrokFilter)filter).Field); Assert.AreEqual(match, ((GrokFilter)filter).Match); CollectionAssert.AreEqual(addFields, ((GrokFilter)filter).AddFields); diff --git a/TimberWinR/Filters/FilterBase.cs b/TimberWinR/Filters/FilterBase.cs index 53dd882..d3b1ac9 100644 --- a/TimberWinR/Filters/FilterBase.cs +++ b/TimberWinR/Filters/FilterBase.cs @@ -7,44 +7,12 @@ using Newtonsoft.Json.Linq; namespace TimberWinR.Filters { - public abstract class FilterBase + public abstract class FilterBase : Parsers { public const string TagName = "Filters"; public abstract void Apply(JObject json); - protected static string ParseStringAttribute(XElement e, string attributeName, string defaultValue="") - { - string retValue = defaultValue; - XAttribute a = e.Attribute(attributeName); - if (a != null) - retValue = a.Value; - return retValue; - } - - protected static bool ParseBoolAttribute(XElement e, string attributeName, bool defaultValue) - { - bool retValue = defaultValue; - XAttribute a = e.Attribute(attributeName); - - if (a != null) - { - switch (a.Value) - { - case "ON": - case "true": - retValue = true; - break; - - case "OFF": - case "false": - retValue = false; - break; - } - } - return retValue; - } - public override string ToString() { StringBuilder sb = new StringBuilder(); diff --git a/TimberWinR/Inputs/IISLog.cs b/TimberWinR/Inputs/IISLog.cs index 93f67c0..c5e92e7 100644 --- a/TimberWinR/Inputs/IISLog.cs +++ b/TimberWinR/Inputs/IISLog.cs @@ -39,10 +39,10 @@ namespace TimberWinR.Inputs MinDateMod = ParseDateAttribute(parent, "minDateMod"); Locale = ParseStringAttribute(parent, "locale", "DEF"); ICheckpoint = ParseStringAttribute(parent, "iCheckpoint"); - ParseFields(parent); + parseFields(parent); } - private void ParseFields(XElement parent) + private void parseFields(XElement parent) { Dictionary allPossibleFields = new Dictionary() { @@ -65,7 +65,7 @@ namespace TimberWinR.Inputs { "Parameters", typeof(string) } }; - Fields = base.parseFields(parent, allPossibleFields); + Fields = ParseFields(parent, allPossibleFields); } } diff --git a/TimberWinR/Inputs/IISW3CLog.cs b/TimberWinR/Inputs/IISW3CLog.cs index 06cd239..e443bda 100644 --- a/TimberWinR/Inputs/IISW3CLog.cs +++ b/TimberWinR/Inputs/IISW3CLog.cs @@ -41,10 +41,10 @@ namespace TimberWinR.Inputs DirTime = ParseBoolAttribute(parent, "dirTime", false); ConsolidateLogs = ParseBoolAttribute(parent, "consolidateLogs", false); ICheckpoint = ParseStringAttribute(parent, "iCheckpoint"); - ParseFields(parent); + parseFields(parent); } - private void ParseFields(XElement parent) + private void parseFields(XElement parent) { Dictionary allPossibleFields = new Dictionary() { @@ -82,7 +82,7 @@ namespace TimberWinR.Inputs { "s-stopped-procs", typeof(int) } }; - Fields = base.parseFields(parent, allPossibleFields); + Fields = ParseFields(parent, allPossibleFields); } } diff --git a/TimberWinR/Inputs/InputBase.cs b/TimberWinR/Inputs/InputBase.cs index a5dfea6..9b4e116 100644 --- a/TimberWinR/Inputs/InputBase.cs +++ b/TimberWinR/Inputs/InputBase.cs @@ -8,11 +8,11 @@ using System.Xml.Linq; namespace TimberWinR.Inputs { - public abstract class InputBase + public abstract class InputBase : Parsers { public const string TagName = "Inputs"; - internal List parseFields(XElement parent, Dictionary allPossibleFields) + protected static List ParseFields(XElement parent, Dictionary allPossibleFields) { IEnumerable xml_fields = from el in parent.Elements("Fields").Elements("Field") @@ -57,120 +57,6 @@ namespace TimberWinR.Inputs return fields; } - protected static string ParseRequiredStringAttribute(XElement e, string attributeName) - { - XAttribute a = e.Attribute(attributeName); - if (a != null) - return a.Value; - else - throw new TimberWinR.ConfigurationErrors.MissingRequiredAttributeException(e, attributeName); - } - - protected static string ParseStringAttribute(XElement e, string attributeName, string defaultValue = "") - { - string retValue = defaultValue; - XAttribute a = e.Attribute(attributeName); - if (a != null) - retValue = a.Value; - return retValue; - } - - protected static string ParseDateAttribute(XElement e, string attributeName, string defaultValue = "") - { - string retValue = defaultValue; - XAttribute a = e.Attribute(attributeName); - if (a != null) - { - DateTime dt; - if (DateTime.TryParseExact(a.Value, - "yyyy-MM-dd hh:mm:ss", - CultureInfo.InvariantCulture, - DateTimeStyles.None, - out dt)) - { - retValue = a.Value; - } - else - { - throw new TimberWinR.ConfigurationErrors.InvalidAttributeDateValueException(a); - } - } - - return retValue; - } - - protected static bool ParseRequiredBoolAttribute(XElement e, string attributeName) - { - XAttribute a = e.Attribute(attributeName); - if (a == null) - throw new TimberWinR.ConfigurationErrors.InvalidAttributeValueException(e.Attribute(attributeName)); - - switch (a.Value) - { - case "ON": - case "true": - return true; - - case "OFF": - case "false": - return false; - - default: - throw new TimberWinR.ConfigurationErrors.InvalidAttributeValueException(e.Attribute(attributeName)); - } - } - - protected static string ParseEnumAttribute(XElement e, string attributeName, IEnumerable values, string defaultValue) - { - XAttribute a = e.Attribute(attributeName); - - if (a != null) - { - string v = a.Value; - if (values.Contains(v)) - return v; - else - throw new TimberWinR.ConfigurationErrors.InvalidAttributeValueException(e.Attribute(attributeName)); - } - return defaultValue; - } - - protected static int ParseIntAttribute(XElement e, string attributeName, int defaultValue) - { - XAttribute a = e.Attribute(attributeName); - if (a != null) - { - int valInt; - if (int.TryParse(a.Value, out valInt)) - return valInt; - else - throw new TimberWinR.ConfigurationErrors.InvalidAttributeIntegerValueException(a); - } - return defaultValue; - } - protected static bool ParseBoolAttribute(XElement e, string attributeName, bool defaultValue) - { - bool retValue = defaultValue; - XAttribute a = e.Attribute(attributeName); - - if (a != null) - { - switch (a.Value) - { - case "ON": - case "true": - retValue = true; - break; - - case "OFF": - case "false": - retValue = false; - break; - } - } - return retValue; - } - public override string ToString() { StringBuilder sb = new StringBuilder(); diff --git a/TimberWinR/Inputs/TailFileInput.cs b/TimberWinR/Inputs/TailFileInput.cs index 0d2a569..ebcdd62 100644 --- a/TimberWinR/Inputs/TailFileInput.cs +++ b/TimberWinR/Inputs/TailFileInput.cs @@ -34,10 +34,10 @@ namespace TimberWinR.Inputs ICodepage = ParseIntAttribute(parent, "iCodepage", 0); Recurse = ParseIntAttribute(parent, "recurse", 0); SplitLongLines = ParseBoolAttribute(parent, "splitLongLines", false); - ParseFields(parent); + parseFields(parent); } - private void ParseFields(XElement parent) + private void parseFields(XElement parent) { Dictionary allPossibleFields = new Dictionary() { @@ -46,7 +46,7 @@ namespace TimberWinR.Inputs { "Text", typeof(string) } }; - Fields = base.parseFields(parent, allPossibleFields); + Fields = ParseFields(parent, allPossibleFields); } } diff --git a/TimberWinR/Inputs/WindowsEvent.cs b/TimberWinR/Inputs/WindowsEvent.cs index 623fbc7..91e0683 100644 --- a/TimberWinR/Inputs/WindowsEvent.cs +++ b/TimberWinR/Inputs/WindowsEvent.cs @@ -44,10 +44,10 @@ namespace TimberWinR.Inputs Direction = ParseEnumAttribute(parent, "direction", new string[] { "FW", "BW" }, "FW"); StringsSep = ParseStringAttribute(parent, "stringsSep", "|"); BinaryFormat = ParseEnumAttribute(parent, "binaryFormat", new string[] { "ASC", "PRINT", "HEX" }, "PRINT"); - ParseFields(parent); + parseFields(parent); } - private void ParseFields(XElement parent) + private void parseFields(XElement parent) { Dictionary allPossibleFields = new Dictionary() { @@ -68,7 +68,7 @@ namespace TimberWinR.Inputs { "Data", typeof(string) } }; - Fields = base.parseFields(parent, allPossibleFields); + Fields = ParseFields(parent, allPossibleFields); } } diff --git a/TimberWinR/Parsers.cs b/TimberWinR/Parsers.cs new file mode 100644 index 0000000..bfae4de --- /dev/null +++ b/TimberWinR/Parsers.cs @@ -0,0 +1,124 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Xml; +using System.Xml.Linq; +using TimberWinR.Inputs; + +public abstract class Parsers +{ + protected static string ParseRequiredStringAttribute(XElement e, string attributeName) + { + XAttribute a = e.Attribute(attributeName); + if (a != null) + return a.Value; + else + throw new TimberWinR.ConfigurationErrors.MissingRequiredAttributeException(e, attributeName); + } + + protected static string ParseStringAttribute(XElement e, string attributeName, string defaultValue = "") + { + string retValue = defaultValue; + XAttribute a = e.Attribute(attributeName); + if (a != null) + retValue = a.Value; + return retValue; + } + + protected static string ParseDateAttribute(XElement e, string attributeName, string defaultValue = "") + { + string retValue = defaultValue; + XAttribute a = e.Attribute(attributeName); + if (a != null) + { + DateTime dt; + if (DateTime.TryParseExact(a.Value, + "yyyy-MM-dd hh:mm:ss", + CultureInfo.InvariantCulture, + DateTimeStyles.None, + out dt)) + { + retValue = a.Value; + } + else + { + throw new TimberWinR.ConfigurationErrors.InvalidAttributeDateValueException(a); + } + } + + return retValue; + } + + protected static bool ParseRequiredBoolAttribute(XElement e, string attributeName) + { + XAttribute a = e.Attribute(attributeName); + if (a == null) + throw new TimberWinR.ConfigurationErrors.InvalidAttributeValueException(e.Attribute(attributeName)); + + switch (a.Value) + { + case "ON": + case "true": + return true; + + case "OFF": + case "false": + return false; + + default: + throw new TimberWinR.ConfigurationErrors.InvalidAttributeValueException(e.Attribute(attributeName)); + } + } + + protected static string ParseEnumAttribute(XElement e, string attributeName, IEnumerable values, string defaultValue) + { + XAttribute a = e.Attribute(attributeName); + + if (a != null) + { + string v = a.Value; + if (values.Contains(v)) + return v; + else + throw new TimberWinR.ConfigurationErrors.InvalidAttributeValueException(e.Attribute(attributeName)); + } + return defaultValue; + } + + protected static int ParseIntAttribute(XElement e, string attributeName, int defaultValue) + { + XAttribute a = e.Attribute(attributeName); + if (a != null) + { + int valInt; + if (int.TryParse(a.Value, out valInt)) + return valInt; + else + throw new TimberWinR.ConfigurationErrors.InvalidAttributeIntegerValueException(a); + } + return defaultValue; + } + protected static bool ParseBoolAttribute(XElement e, string attributeName, bool defaultValue) + { + bool retValue = defaultValue; + XAttribute a = e.Attribute(attributeName); + + if (a != null) + { + switch (a.Value) + { + case "ON": + case "true": + retValue = true; + break; + + case "OFF": + case "false": + retValue = false; + break; + } + } + return retValue; + } +} diff --git a/TimberWinR/TimberWinR.csproj b/TimberWinR/TimberWinR.csproj index f7bb167..8acfe06 100644 --- a/TimberWinR/TimberWinR.csproj +++ b/TimberWinR/TimberWinR.csproj @@ -84,6 +84,7 @@ + True