Added Parsers as abstract parent class for InputBase and FilterBase

This commit is contained in:
Jonathan Preddy
2014-07-23 17:05:15 -04:00
parent 4fce441a04
commit 3b03400ed3
9 changed files with 140 additions and 164 deletions

View File

@@ -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);

View File

@@ -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();

View File

@@ -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<string, Type> allPossibleFields = new Dictionary<string, Type>()
{
@@ -65,7 +65,7 @@ namespace TimberWinR.Inputs
{ "Parameters", typeof(string) }
};
Fields = base.parseFields(parent, allPossibleFields);
Fields = ParseFields(parent, allPossibleFields);
}
}

View File

@@ -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<string, Type> allPossibleFields = new Dictionary<string, Type>()
{
@@ -82,7 +82,7 @@ namespace TimberWinR.Inputs
{ "s-stopped-procs", typeof(int) }
};
Fields = base.parseFields(parent, allPossibleFields);
Fields = ParseFields(parent, allPossibleFields);
}
}

View File

@@ -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<FieldDefinition> parseFields(XElement parent, Dictionary<string, Type> allPossibleFields)
protected static List<FieldDefinition> ParseFields(XElement parent, Dictionary<string, Type> allPossibleFields)
{
IEnumerable<XElement> 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<string> 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();

View File

@@ -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<string, Type> allPossibleFields = new Dictionary<string, Type>()
{
@@ -46,7 +46,7 @@ namespace TimberWinR.Inputs
{ "Text", typeof(string) }
};
Fields = base.parseFields(parent, allPossibleFields);
Fields = ParseFields(parent, allPossibleFields);
}
}

View File

@@ -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<string, Type> allPossibleFields = new Dictionary<string, Type>()
{
@@ -68,7 +68,7 @@ namespace TimberWinR.Inputs
{ "Data", typeof(string) }
};
Fields = base.parseFields(parent, allPossibleFields);
Fields = ParseFields(parent, allPossibleFields);
}
}

124
TimberWinR/Parsers.cs Normal file
View File

@@ -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<string> 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;
}
}

View File

@@ -84,6 +84,7 @@
<Compile Include="Manager.cs" />
<Compile Include="Outputs\OutputSender.cs" />
<Compile Include="Outputs\Redis.cs" />
<Compile Include="Parsers.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>