Improved exception handling for missing required tags.

This commit is contained in:
Jonathan Preddy
2014-07-17 15:44:31 -04:00
parent 9f7576af45
commit 02253de03a

View File

@@ -8,11 +8,21 @@ using System.Xml.Linq;
using System.IO;
using System.Globalization;
using TimberWinR.Inputs;
namespace TimberWinR
{
public class Configuration
{
private class MissingRequiredTagException : Exception
{
public MissingRequiredTagException(string tagName)
: base(
string.Format("Missing required tag \"{0}\"", tagName))
{
}
}
private class MissingRequiredAttributeException : Exception
{
public MissingRequiredAttributeException(XElement e, string attributeName)
@@ -80,197 +90,6 @@ namespace TimberWinR
parseXMLConf(xmlConfFile);
}
static void parseXMLConf(string xmlConfFile)
{
XDocument config = XDocument.Load(xmlConfFile, LoadOptions.SetLineInfo | LoadOptions.SetBaseUri);
IEnumerable<XElement> inputs =
from el in config.Root.Descendants("Inputs")
select el;
// WINDOWS EVENTS
IEnumerable<XElement> xml_events =
from el in inputs.Descendants("WindowsEvents").Descendants("Events")
select el;
foreach (XElement e in xml_events)
{
// Required attributes.
string source;
// Parse required attributes.
string attributeName;
attributeName = "source";
try
{
source = e.Attribute("source").Value;
}
catch (NullReferenceException)
{
throw new MissingRequiredAttributeException(e, attributeName);
}
// Parse fields.
IEnumerable<XElement> xml_fields =
from el in e.Descendants("Fields").Descendants("Field")
select el;
List<FieldDefinition> fields = parseFields_Event(xml_fields);
// Parse parameters.
Params_WindowsEvent args = parseParams_Event(e.Attributes());
WindowsEvent evt = new WindowsEvent(source, fields, args);
_events.Add(evt);
}
// TEXT LOGS
IEnumerable<XElement> xml_logs =
from el in inputs.Descendants("Logs").Descendants("Log")
select el;
foreach (XElement e in xml_logs)
{
// Required attributes.
string name;
string location;
// Parse required attributes.
string attributeName;
attributeName = "name";
try
{
name = e.Attribute(attributeName).Value;
}
catch (NullReferenceException)
{
throw new MissingRequiredAttributeException(e, attributeName);
}
attributeName = "location";
try
{
location = e.Attribute("location").Value;
}
catch (NullReferenceException)
{
throw new MissingRequiredAttributeException(e, attributeName);
}
// Parse fields.
IEnumerable<XElement> xml_fields =
from el in e.Descendants("Fields").Descendants("Field")
select el;
List<FieldDefinition> fields = parseFields_Log(xml_fields);
// Parse parameters.
Params_TextLog args = parseParams_Log(e.Attributes());
TextLog log = new TextLog(name, location, fields, args);
_logs.Add(log);
}
// IIS LOGS
IEnumerable<XElement> xml_iis =
from el in inputs.Descendants("IISLogs").Descendants("IISLog")
select el;
foreach (XElement e in xml_iis)
{
// Required attributes.
string name;
string location;
// Parse required attributes.
string attributeName;
attributeName = "name";
try
{
name = e.Attribute(attributeName).Value;
}
catch (NullReferenceException)
{
throw new MissingRequiredAttributeException(e, attributeName);
}
attributeName = "location";
try
{
location = e.Attribute("location").Value;
}
catch (NullReferenceException)
{
throw new MissingRequiredAttributeException(e, attributeName);
}
// Parse fields.
IEnumerable<XElement> xml_fields =
from el in e.Descendants("Fields").Descendants("Field")
select el;
List<FieldDefinition> fields = parseFields_IIS(xml_fields);
// Parse parameters.
Params_IISLog args = parseParams_IIS(e.Attributes());
IISLog iis = new IISLog(name, location, fields, args);
_iislogs.Add(iis);
}
// IISW3C LOGS
IEnumerable<XElement> xml_iisw3c =
from el in inputs.Descendants("IISW3CLogs").Descendants("IISW3CLog")
select el;
foreach (XElement e in xml_iis)
{
// Required attributes.
string name;
string location;
// Parse required attributes.
string attributeName;
attributeName = "name";
try
{
name = e.Attribute(attributeName).Value;
}
catch (NullReferenceException)
{
throw new MissingRequiredAttributeException(e, attributeName);
}
attributeName = "location";
try
{
location = e.Attribute("location").Value;
}
catch (NullReferenceException)
{
throw new MissingRequiredAttributeException(e, attributeName);
}
// Parse fields.
IEnumerable<XElement> xml_fields =
from el in e.Descendants("Fields").Descendants("Field")
select el;
List<FieldDefinition> fields = parseFields_IISW3C(xml_fields);
// Parse parameters.
Params_IISW3CLog args = parseParams_IISW3C(e.Attributes());
IISW3CLog iisw3c = new IISW3CLog(name, location, fields, args);
_iisw3clogs.Add(iisw3c);
}
Console.WriteLine("end");
}
static List<FieldDefinition> parseFields_Event(IEnumerable<XElement> xml_fields)
{
List<FieldDefinition> fields = new List<FieldDefinition>();
@@ -858,6 +677,207 @@ namespace TimberWinR
return p.Build();
}
static void parseXMLConf(string xmlConfFile)
{
XDocument config = XDocument.Load(xmlConfFile, LoadOptions.SetLineInfo | LoadOptions.SetBaseUri);
IEnumerable<XElement> inputs =
from el in config.Root.Descendants("Inputs")
select el;
string tagName = "Inputs";
if (inputs.Count() == 0)
{
throw new MissingRequiredTagException(tagName);
}
// WINDOWS EVENTS
IEnumerable<XElement> xml_events =
from el in inputs.Descendants("WindowsEvents").Descendants("Events")
select el;
foreach (XElement e in xml_events)
{
// Required attributes.
string source;
string attributeName;
// Parse required attributes.
attributeName = "source";
try
{
source = e.Attribute("source").Value;
}
catch (NullReferenceException)
{
throw new MissingRequiredAttributeException(e, attributeName);
}
// Parse fields.
IEnumerable<XElement> xml_fields =
from el in e.Descendants("Fields").Descendants("Field")
select el;
List<FieldDefinition> fields = parseFields_Event(xml_fields);
// Parse parameters.
Params_WindowsEvent args = parseParams_Event(e.Attributes());
WindowsEvent evt = new WindowsEvent(source, fields, args);
_events.Add(evt);
}
// TEXT LOGS
IEnumerable<XElement> xml_logs =
from el in inputs.Descendants("Logs").Descendants("Log")
select el;
foreach (XElement e in xml_logs)
{
// Required attributes.
string name;
string location;
string attributeName;
// Parse required attributes.
attributeName = "name";
try
{
name = e.Attribute(attributeName).Value;
}
catch (NullReferenceException)
{
throw new MissingRequiredAttributeException(e, attributeName);
}
attributeName = "location";
try
{
location = e.Attribute("location").Value;
}
catch (NullReferenceException)
{
throw new MissingRequiredAttributeException(e, attributeName);
}
// Parse fields.
IEnumerable<XElement> xml_fields =
from el in e.Descendants("Fields").Descendants("Field")
select el;
List<FieldDefinition> fields = parseFields_Log(xml_fields);
// Parse parameters.
Params_TextLog args = parseParams_Log(e.Attributes());
TextLog log = new TextLog(name, location, fields, args);
_logs.Add(log);
}
// IIS LOGS
IEnumerable<XElement> xml_iis =
from el in inputs.Descendants("IISLogs").Descendants("IISLog")
select el;
foreach (XElement e in xml_iis)
{
// Required attributes.
string name;
string location;
string attributeName;
// Parse required attributes.
attributeName = "name";
try
{
name = e.Attribute(attributeName).Value;
}
catch (NullReferenceException)
{
throw new MissingRequiredAttributeException(e, attributeName);
}
attributeName = "location";
try
{
location = e.Attribute("location").Value;
}
catch (NullReferenceException)
{
throw new MissingRequiredAttributeException(e, attributeName);
}
// Parse fields.
IEnumerable<XElement> xml_fields =
from el in e.Descendants("Fields").Descendants("Field")
select el;
List<FieldDefinition> fields = parseFields_IIS(xml_fields);
// Parse parameters.
Params_IISLog args = parseParams_IIS(e.Attributes());
IISLog iis = new IISLog(name, location, fields, args);
_iislogs.Add(iis);
}
// IISW3C LOGS
IEnumerable<XElement> xml_iisw3c =
from el in inputs.Descendants("IISW3CLogs").Descendants("IISW3CLog")
select el;
foreach (XElement e in xml_iis)
{
// Required attributes.
string name;
string location;
string attributeName;
// Parse required attributes.
attributeName = "name";
try
{
name = e.Attribute(attributeName).Value;
}
catch (NullReferenceException)
{
throw new MissingRequiredAttributeException(e, attributeName);
}
attributeName = "location";
try
{
location = e.Attribute("location").Value;
}
catch (NullReferenceException)
{
throw new MissingRequiredAttributeException(e, attributeName);
}
// Parse fields.
IEnumerable<XElement> xml_fields =
from el in e.Descendants("Fields").Descendants("Field")
select el;
List<FieldDefinition> fields = parseFields_IISW3C(xml_fields);
// Parse parameters.
Params_IISW3CLog args = parseParams_IISW3C(e.Attributes());
IISW3CLog iisw3c = new IISW3CLog(name, location, fields, args);
_iisw3clogs.Add(iisw3c);
}
Console.WriteLine("end");
}
public class WindowsEvent
{
public string Source { get; private set; }