From c86ef86d63c761138c1e25a30cc0d519b93cb6ab Mon Sep 17 00:00:00 2001 From: Jonathan Preddy Date: Wed, 23 Jul 2014 15:14:31 -0400 Subject: [PATCH 1/5] More tests for filters --- TimberWinR.UnitTests/Configuration.cs | 234 +++++++++++++++----------- TimberWinR.UnitTests/testconf.xml | 10 +- TimberWinR/Filters/FilterBase.cs | 9 + TimberWinR/Filters/GrokFilter.cs | 33 +++- TimberWinR/Inputs/FieldDefinitions.cs | 35 ++++ 5 files changed, 222 insertions(+), 99 deletions(-) diff --git a/TimberWinR.UnitTests/Configuration.cs b/TimberWinR.UnitTests/Configuration.cs index 9b18ca7..40af279 100644 --- a/TimberWinR.UnitTests/Configuration.cs +++ b/TimberWinR.UnitTests/Configuration.cs @@ -77,135 +77,124 @@ namespace TimberWinR.UnitTests public void NumOfIISW3C() { Assert.AreEqual(1, c.IISW3C.ToArray().Length); + } + + [Test] + public void NumOfFilters() + { + Assert.AreEqual(3, c.Filters.ToArray().Length); } [Test] public void FieldsOfEvents() { - Dictionary fields = new Dictionary() + List fields = new List() { - { "EventLog", typeof(string) }, - { "RecordNumber", typeof(int) }, - { "TimeGenerated", typeof(DateTime) }, - { "TimeWritten", typeof(DateTime) }, - { "EventID", typeof(int) }, - { "EventType", typeof(int) }, - { "EventTypeName", typeof(string) }, - { "EventCategory", typeof(int) }, - { "EventCategoryName", typeof(string) }, - { "SourceName", typeof(string) }, - { "Strings", typeof(string) }, - { "ComputerName", typeof(string) }, - { "SID", typeof(string) }, - { "Message", typeof(string) }, - { "Data", typeof(string) } + new FieldDefinition("EventLog", typeof(string)), + new FieldDefinition("RecordNumber", typeof(int)), + new FieldDefinition("TimeGenerated", typeof(DateTime)), + new FieldDefinition("TimeWritten", typeof(DateTime)), + new FieldDefinition("EventID", typeof(int)), + new FieldDefinition("EventType", typeof(int)), + new FieldDefinition("EventTypeName", typeof(string)), + new FieldDefinition("EventCategory", typeof(int)), + new FieldDefinition("EventCategoryName", typeof(string)), + new FieldDefinition("SourceName", typeof(string)), + new FieldDefinition("Strings", typeof(string)), + new FieldDefinition("ComputerName", typeof(string)), + new FieldDefinition("SID", typeof(string)), + new FieldDefinition("Message", typeof(string)), + new FieldDefinition("Data", typeof(string)) }; - foreach (FieldDefinition field in c.Events.ToArray()[0].Fields) - { - Assert.Contains(field.Name, fields.Keys); - } + + CollectionAssert.AreEqual(fields, c.Events.ToArray()[0].Fields); } [Test] public void FieldsOfLogs() { - Dictionary fields = new Dictionary() + List fields = new List() { - { "LogFilename", typeof(string) }, - { "Index", typeof(int) }, - { "Text", typeof(string) } + new FieldDefinition("LogFilename", typeof(string)), + new FieldDefinition("Index", typeof(int)), + new FieldDefinition("Text", typeof(string)) }; - foreach (FieldDefinition field in c.Logs.ToArray()[0].Fields) - { - Assert.Contains(field.Name, fields.Keys); - } - foreach (FieldDefinition field in c.Logs.ToArray()[1].Fields) - { - Assert.Contains(field.Name, fields.Keys); - } - foreach (FieldDefinition field in c.Logs.ToArray()[2].Fields) - { - Assert.Contains(field.Name, fields.Keys); - } + CollectionAssert.AreEqual(fields, c.Logs.ToArray()[0].Fields); + CollectionAssert.AreEqual(fields, c.Logs.ToArray()[1].Fields); + CollectionAssert.AreEqual(fields, c.Logs.ToArray()[2].Fields); } [Test] public void FieldsOfIIS() { - Dictionary fields = new Dictionary() + List fields = new List() { - { "LogFilename", typeof(string) }, - { "LogRow", typeof(int) }, - { "UserIP", typeof(string) }, - { "UserName", typeof(string) }, - { "Date", typeof(DateTime) }, - { "Time", typeof(DateTime) }, - { "ServiceInstance", typeof(string) }, - { "HostName", typeof(string) }, - { "ServerIP", typeof(string) }, - { "TimeTaken", typeof(int) }, - { "BytesSent", typeof(int) }, - { "BytesReceived", typeof(int) }, - { "StatusCode", typeof(int) }, - { "Win32StatusCode", typeof(int) }, - { "RequestType", typeof(string) }, - { "Target", typeof(string) }, - { "Parameters", typeof(string) } + new FieldDefinition("LogFilename", typeof(string)), + new FieldDefinition("LogRow", typeof(int)), + new FieldDefinition("UserIP", typeof(string)), + new FieldDefinition("UserName", typeof(string)), + new FieldDefinition("Date", typeof(DateTime)), + new FieldDefinition("Time", typeof(DateTime)), + new FieldDefinition("ServiceInstance", typeof(string)), + new FieldDefinition("HostName", typeof(string)), + new FieldDefinition("ServerIP", typeof(string)), + new FieldDefinition("TimeTaken", typeof(int)), + new FieldDefinition("BytesSent", typeof(int)), + new FieldDefinition("BytesReceived", typeof(int)), + new FieldDefinition("StatusCode", typeof(int)), + new FieldDefinition("Win32StatusCode", typeof(int)), + new FieldDefinition("RequestType", typeof(string)), + new FieldDefinition("Target", typeof(string)), + new FieldDefinition("Parameters", typeof(string)) }; foreach (var iis in c.IIS.ToArray()) { - foreach (FieldDefinition field in iis.Fields) - { - Assert.Contains(field.Name, fields.Keys); - } + CollectionAssert.AreEquivalent(fields, c.IIS.ToArray()[0].Fields); } - } [Test] public void FieldsOfIISW3C() { - Dictionary fields = new Dictionary() + List fields = new List() { - { "LogFilename", typeof(string) }, - { "LogRow", typeof(int) }, - { "date", typeof(DateTime) }, - { "time", typeof(DateTime) }, - { "c-ip", typeof(string) }, - { "cs-username", typeof(string) }, - { "s-sitename", typeof(string) }, - { "s-computername", typeof(int) }, - { "s-ip", typeof(string) }, - { "s-port", typeof(int) }, - { "cs-method", typeof(string) }, - { "cs-uri-stem", typeof(string) }, - { "cs-uri-query", typeof(string) }, - { "sc-status", typeof(int) }, - { "sc-substatus", typeof(int) }, - { "sc-win32-status", typeof(int) }, - { "sc-bytes", typeof(int) }, - { "cs-bytes", typeof(int) }, - { "time-taken", typeof(int) }, - { "cs-version", typeof(string) }, - { "cs-host", typeof(string) }, - { "cs(User-Agent)", typeof(string) }, - { "cs(Cookie)", typeof(string) }, - { "cs(Referer)", typeof(string) }, - { "s-event", typeof(string) }, - { "s-process-type", typeof(string) }, - { "s-user-time", typeof(double) }, - { "s-kernel-time", typeof(double) }, - { "s-page-faults", typeof(int) }, - { "s-total-procs", typeof(int) }, - { "s-active-procs", typeof(int) }, - { "s-stopped-procs", typeof(int) } + new FieldDefinition("LogFilename", typeof(string)), + new FieldDefinition("LogRow", typeof(int)), + new FieldDefinition("date", typeof(DateTime)), + new FieldDefinition("time", typeof(DateTime)), + new FieldDefinition("c-ip", typeof(string)), + new FieldDefinition("cs-username", typeof(string)), + new FieldDefinition("s-sitename", typeof(string)), + new FieldDefinition("s-computername", typeof(int)), + new FieldDefinition("s-ip", typeof(string)), + new FieldDefinition("s-port", typeof(int)), + new FieldDefinition("cs-method", typeof(string)), + new FieldDefinition("cs-uri-stem", typeof(string)), + new FieldDefinition("cs-uri-query", typeof(string)), + new FieldDefinition("sc-status", typeof(int)), + new FieldDefinition("sc-substatus", typeof(int)), + new FieldDefinition("sc-win32-status", typeof(int)), + new FieldDefinition("sc-bytes", typeof(int)), + new FieldDefinition("cs-bytes", typeof(int)), + new FieldDefinition("time-taken", typeof(int)), + new FieldDefinition("cs-version", typeof(string)), + new FieldDefinition("cs-host", typeof(string)), + new FieldDefinition("cs(User-Agent)", typeof(string)), + new FieldDefinition("cs(Cookie)", typeof(string)), + new FieldDefinition("cs(Referer)", typeof(string)), + new FieldDefinition("s-event", typeof(string)), + new FieldDefinition("s-process-type", typeof(string)), + new FieldDefinition("s-user-time", typeof(double)), + new FieldDefinition("s-kernel-time", typeof(double)), + new FieldDefinition("s-page-faults", typeof(int)), + new FieldDefinition("s-total-procs", typeof(int)), + new FieldDefinition("s-active-procs", typeof(int)), + new FieldDefinition("s-stopped-procs", typeof(int)) }; - foreach (FieldDefinition field in c.IISW3C.ToArray()[0].Fields) - { - Assert.Contains(field.Name, fields.Keys); - } + + CollectionAssert.AreEquivalent(fields, c.IISW3C.ToArray()[0].Fields); } [Test] @@ -305,6 +294,57 @@ namespace TimberWinR.UnitTests Assert.AreEqual(dirTime, iisw3c.DirTime); Assert.AreEqual(consolidateLogs, iisw3c.ConsolidateLogs); Assert.IsEmpty(iisw3c.ICheckpoint); - } + } + + [Test] + public void ParametersOfGrokFilters() + { + List addFields = new List(); + List removeFields = new List(); + + string field = "Text"; + string match = "%{IPAddress:ip1} %{IPAddress:ip2}"; + addFields.Add(new GrokFilter.FieldValuePair("field1", "%{foo}")); + bool dropIfMatch = true; + removeFields.Add("ip1"); + foreach (var filter in c.Filters) + { + 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); + Assert.AreEqual(dropIfMatch, ((GrokFilter)filter).DropIfMatch); + Assert.AreEqual(removeFields, ((GrokFilter)filter).RemoveFields); + } + } + } + + [Test] + public void ParametersOfDateFilters() + { + List patterns = new List(); + + string field = "timestamp"; + string target = "@timestamp"; + bool convertToUTC = true; + patterns.Add("MMM d HH:mm:ss"); + patterns.Add("MMM dd HH:mm:ss"); + patterns.Add("ISO8601"); + + foreach (var filter in c.Filters) + { + if (filter.GetType() == typeof(DateFilter)) + { + Assert.AreEqual(field, ((DateFilter)filter).Field); + Assert.AreEqual(target, ((DateFilter)filter).Target); + Assert.AreEqual(convertToUTC, ((DateFilter)filter).ConvertToUTC); + CollectionAssert.AreEquivalent(patterns, ((DateFilter)filter).Patterns); + } + } + } } } diff --git a/TimberWinR.UnitTests/testconf.xml b/TimberWinR.UnitTests/testconf.xml index 7de93cc..edc6552 100644 --- a/TimberWinR.UnitTests/testconf.xml +++ b/TimberWinR.UnitTests/testconf.xml @@ -18,10 +18,18 @@ - + + + + + + MMM d HH:mm:ss + MMM dd HH:mm:ss + ISO8601 + diff --git a/TimberWinR/Filters/FilterBase.cs b/TimberWinR/Filters/FilterBase.cs index 5d53ac6..53dd882 100644 --- a/TimberWinR/Filters/FilterBase.cs +++ b/TimberWinR/Filters/FilterBase.cs @@ -54,6 +54,15 @@ namespace TimberWinR.Filters { if (prop != null) { + if (prop.PropertyType == typeof(List<>)) + { + sb.Append(String.Format("\t{0}: ", prop.Name)); + foreach (var element in prop.GetValue(this, null) as List) + { + sb.Append(String.Format("{0},", element)); + } + sb.Append("\n"); + } sb.Append(String.Format("\t{0}: {1}\n", prop.Name, prop.GetValue(this, null))); } diff --git a/TimberWinR/Filters/GrokFilter.cs b/TimberWinR/Filters/GrokFilter.cs index 9a439b2..1477742 100644 --- a/TimberWinR/Filters/GrokFilter.cs +++ b/TimberWinR/Filters/GrokFilter.cs @@ -14,8 +14,8 @@ namespace TimberWinR.Filters { public new const string TagName = "Grok"; - public string Match { get; private set; } public string Field { get; private set; } + public string Match { get; private set; } public List AddFields { get; private set; } public bool DropIfMatch { get; private set; } public List RemoveFields { get; private set; } @@ -144,6 +144,37 @@ namespace TimberWinR.Filters Field = field; Value = value; } + + public override bool Equals(System.Object obj) + { + // If parameter is null return false. + if (obj == null) + { + return false; + } + + // If parameter cannot be cast to Point return false. + FieldValuePair p = obj as FieldValuePair; + if ((System.Object)p == null) + { + return false; + } + + // Return true if the fields match: + return (Field == p.Field) && (Value == p.Value); + } + + public bool Equals(FieldValuePair p) + { + // If parameter is null return false: + if ((object)p == null) + { + return false; + } + + // Return true if the fields match: + return (Field == p.Field) && (Value == p.Value); + } } private void ParseAddTags(XElement parent) diff --git a/TimberWinR/Inputs/FieldDefinitions.cs b/TimberWinR/Inputs/FieldDefinitions.cs index f687cb1..0d61655 100644 --- a/TimberWinR/Inputs/FieldDefinitions.cs +++ b/TimberWinR/Inputs/FieldDefinitions.cs @@ -37,6 +37,41 @@ namespace TimberWinR.Inputs Name = fieldName; FieldType = fieldType; } + public override string ToString() + { + return String.Format("{0}", Name); + } + + public override bool Equals(System.Object obj) + { + // If parameter is null return false. + if (obj == null) + { + return false; + } + + // If parameter cannot be cast to Point return false. + FieldDefinition p = obj as FieldDefinition; + if ((System.Object)p == null) + { + return false; + } + + // Return true if the fields match: + return (Name == p.Name) && (FieldType == p.FieldType); + } + + public bool Equals(FieldDefinition p) + { + // If parameter is null return false: + if ((object)p == null) + { + return false; + } + + // Return true if the fields match: + return (Name == p.Name) && (FieldType == p.FieldType); + } } public class FieldDefinitions : IEnumerable From 5bbd0300a520f251a8a7613d132f3a7ec64f3a5c Mon Sep 17 00:00:00 2001 From: Jonathan Preddy Date: Wed, 23 Jul 2014 15:22:53 -0400 Subject: [PATCH 2/5] Updating xsd schema for GrokFilter.AddTag --- TimberWinR/configSchema.xsd | 1 + 1 file changed, 1 insertion(+) diff --git a/TimberWinR/configSchema.xsd b/TimberWinR/configSchema.xsd index 9059af2..2eb8b71 100644 --- a/TimberWinR/configSchema.xsd +++ b/TimberWinR/configSchema.xsd @@ -297,6 +297,7 @@ + From 4fce441a04dfa1a741c12d503e8c1060b6c1b89c Mon Sep 17 00:00:00 2001 From: Jonathan Preddy Date: Wed, 23 Jul 2014 16:27:24 -0400 Subject: [PATCH 3/5] Hashcodes added for classs with Equals overrides for unit tests --- TimberWinR/Filters/GrokFilter.cs | 5 +++++ TimberWinR/Inputs/FieldDefinitions.cs | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/TimberWinR/Filters/GrokFilter.cs b/TimberWinR/Filters/GrokFilter.cs index 1477742..9572633 100644 --- a/TimberWinR/Filters/GrokFilter.cs +++ b/TimberWinR/Filters/GrokFilter.cs @@ -175,6 +175,11 @@ namespace TimberWinR.Filters // Return true if the fields match: return (Field == p.Field) && (Value == p.Value); } + + public override int GetHashCode() + { + return Field.GetHashCode() + Value.GetHashCode(); + } } private void ParseAddTags(XElement parent) diff --git a/TimberWinR/Inputs/FieldDefinitions.cs b/TimberWinR/Inputs/FieldDefinitions.cs index 0d61655..bee3934 100644 --- a/TimberWinR/Inputs/FieldDefinitions.cs +++ b/TimberWinR/Inputs/FieldDefinitions.cs @@ -72,6 +72,11 @@ namespace TimberWinR.Inputs // Return true if the fields match: return (Name == p.Name) && (FieldType == p.FieldType); } + + public override int GetHashCode() + { + return Name.GetHashCode() + FieldType.GetHashCode(); + } } public class FieldDefinitions : IEnumerable From 3b03400ed32d814a155734b7c9f3fadc571827a1 Mon Sep 17 00:00:00 2001 From: Jonathan Preddy Date: Wed, 23 Jul 2014 17:05:15 -0400 Subject: [PATCH 4/5] 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 From 506a24a8a29cae31022f0c80acc89b3d05128469 Mon Sep 17 00:00:00 2001 From: Jonathan Preddy Date: Thu, 24 Jul 2014 14:57:16 -0400 Subject: [PATCH 5/5] - --- TimberWinR/Configuration.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/TimberWinR/Configuration.cs b/TimberWinR/Configuration.cs index 42037f5..54ff61f 100644 --- a/TimberWinR/Configuration.cs +++ b/TimberWinR/Configuration.cs @@ -159,5 +159,6 @@ namespace TimberWinR } } } + } } \ No newline at end of file