From 1ceb21411f86c17b5afc15362a871a4c7e7f6d49 Mon Sep 17 00:00:00 2001 From: Jonathan Preddy Date: Fri, 18 Jul 2014 11:07:48 -0400 Subject: [PATCH 1/3] Added xsd schema validation and updated xsd file. --- TimberWinR/Configuration.cs | 35 ++++- TimberWinR/Properties/Resources.Designer.cs | 83 ++++++++++++ TimberWinR/Properties/Resources.resx | 124 ++++++++++++++++++ TimberWinR/TimberWinR.csproj | 15 ++- TimberWinR/{testconf.xsd => configSchema.xsd} | 68 +++++++++- 5 files changed, 315 insertions(+), 10 deletions(-) create mode 100644 TimberWinR/Properties/Resources.Designer.cs create mode 100644 TimberWinR/Properties/Resources.resx rename TimberWinR/{testconf.xsd => configSchema.xsd} (71%) diff --git a/TimberWinR/Configuration.cs b/TimberWinR/Configuration.cs index dcb968c..a4f1845 100644 --- a/TimberWinR/Configuration.cs +++ b/TimberWinR/Configuration.cs @@ -8,6 +8,7 @@ using System.Xml.Linq; using System.IO; using System.Globalization; using TimberWinR.Inputs; +using System.Xml.Schema; namespace TimberWinR { @@ -87,13 +88,29 @@ namespace TimberWinR public Configuration(string xmlConfFile) { - parseXMLConf(xmlConfFile); + parseXMLConf(xmlConfFile, Properties.Resources.configSchema); } - static void parseXMLConf(string xmlConfFile) + static void parseXMLConf(string xmlConfFile, string xsdSchema) { XDocument config = XDocument.Load(xmlConfFile, LoadOptions.SetLineInfo | LoadOptions.SetBaseUri); + + // Ensure that the xml configuration file provided obeys the xsd schema. + XmlSchemaSet schemas = new XmlSchemaSet(); + schemas.Add("", XmlReader.Create(new StringReader(xsdSchema))); + + bool errors = false; + config.Validate(schemas, (o, e) => + { + Console.WriteLine("{0}", e.Message); + errors = true; + }); + Console.WriteLine("The XML configuration file provided {0}", errors ? "did not validate." : "validated."); + + + + // Begin parsing the xml configuration file. IEnumerable inputs = from el in config.Root.Descendants("Inputs") select el; @@ -114,9 +131,10 @@ namespace TimberWinR // Required attributes. string source; - string attributeName; // Parse required attributes. + string attributeName; + attributeName = "source"; try { @@ -153,9 +171,10 @@ namespace TimberWinR string name; string location; - string attributeName; // Parse required attributes. + string attributeName; + attributeName = "name"; try { @@ -201,9 +220,10 @@ namespace TimberWinR string name; string location; - string attributeName; // Parse required attributes. + string attributeName; + attributeName = "name"; try { @@ -250,9 +270,10 @@ namespace TimberWinR string name; string location; - string attributeName; // Parse required attributes. + string attributeName; + attributeName = "name"; try { @@ -285,7 +306,7 @@ namespace TimberWinR IISW3CLog iisw3c = new IISW3CLog(name, location, fields, args); _iisw3clogs.Add(iisw3c); - } + } } static List parseFields_Event(IEnumerable xml_fields) diff --git a/TimberWinR/Properties/Resources.Designer.cs b/TimberWinR/Properties/Resources.Designer.cs new file mode 100644 index 0000000..8b421c8 --- /dev/null +++ b/TimberWinR/Properties/Resources.Designer.cs @@ -0,0 +1,83 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.18444 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace TimberWinR.Properties { + using System; + + + /// + /// A strongly-typed resource class, for looking up localized strings, etc. + /// + // This class was auto-generated by the StronglyTypedResourceBuilder + // class via a tool like ResGen or Visual Studio. + // To add or remove a member, edit your .ResX file then rerun ResGen + // with the /str option, or rebuild your VS project. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Resources { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Resources() { + } + + /// + /// Returns the cached ResourceManager instance used by this class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager { + get { + if (object.ReferenceEquals(resourceMan, null)) { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("TimberWinR.Properties.Resources", typeof(Resources).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + + /// + /// Looks up a localized string similar to <xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"> + /// <xs:element name="TimberWinR"> + /// <xs:complexType> + /// <xs:sequence> + /// <xs:element name="Inputs"> + /// <xs:complexType> + /// <xs:sequence> + /// <xs:element name="WindowsEvents" minOccurs="0"> + /// <xs:complexType> + /// <xs:sequence> + /// <xs:element name="Event" maxOccurs="unbounded" minOccurs="0"> + /// [rest of string was truncated]";. + /// + internal static string configSchema { + get { + return ResourceManager.GetString("configSchema", resourceCulture); + } + } + } +} diff --git a/TimberWinR/Properties/Resources.resx b/TimberWinR/Properties/Resources.resx new file mode 100644 index 0000000..60554cd --- /dev/null +++ b/TimberWinR/Properties/Resources.resx @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + + ..\configSchema.xsd;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8 + + \ No newline at end of file diff --git a/TimberWinR/TimberWinR.csproj b/TimberWinR/TimberWinR.csproj index f05aca8..2b15558 100644 --- a/TimberWinR/TimberWinR.csproj +++ b/TimberWinR/TimberWinR.csproj @@ -71,15 +71,26 @@ + + True + True + Resources.resx + - - + Designer + + + + + ResXFileCodeGenerator + Resources.Designer.cs +