diff --git a/HashingCmd/HashingCmd.csproj b/HashingCmd/HashingCmd.csproj index ec77475b5e1d27daccf5e9a1ded6325072c8c14b..7ee07480a160366124db4c0ab30057e323729952 100644 --- a/HashingCmd/HashingCmd.csproj +++ b/HashingCmd/HashingCmd.csproj @@ -60,10 +60,18 @@ <Service Include="{508349B6-6B84-4DF5-91F0-309BEEBAD82D}" /> </ItemGroup> <ItemGroup> + <ProjectReference Include="..\VectoCommon\VectoCommon\VectoCommon.csproj"> + <Project>{79A066AD-69A9-4223-90F6-6ED5D2D084F4}</Project> + <Name>VectoCommon</Name> + </ProjectReference> <ProjectReference Include="..\VectoCommon\VectoHashing\VectoHashing.csproj"> <Project>{B673E12F-D323-4C4C-8805-9915B2C72D3D}</Project> <Name>VectoHashing</Name> </ProjectReference> + <ProjectReference Include="..\VectoCore\VectoCore\VectoCore.csproj"> + <Project>{CD36938A-ADD9-4C65-96DA-B397CDEEA90A}</Project> + <Name>VectoCore</Name> + </ProjectReference> </ItemGroup> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <!-- To modify your build process, add your task inside one of the targets below and uncomment it. diff --git a/HashingCmd/Program.cs b/HashingCmd/Program.cs index ceb3df8f15576a0a6350f7cbade12f8b53197025..d76254fff24bcdaf45b6b388eb9fce14bfcb6c2c 100644 --- a/HashingCmd/Program.cs +++ b/HashingCmd/Program.cs @@ -7,6 +7,12 @@ using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; using System.Xml; +using System.Xml.Linq; +using System.Xml.Schema; +using System.Xml.XPath; +using TUGraz.VectoCommon.Exceptions; +using TUGraz.VectoCore.InputData.FileIO.XML.Declaration; +using TUGraz.VectoCore.Utils; using TUGraz.VectoHashing; namespace HashingCmd @@ -34,6 +40,7 @@ hashingcmd.exe static Dictionary<string, HashingAction> actions = new Dictionary<string, HashingAction>(); static bool _validateXML = false; + private static bool xmlValid = true; static int Main(string[] args) { @@ -116,6 +123,65 @@ hashingcmd.exe result.WriteTo(writer); writer.Flush(); writer.Close(); + + if (_validateXML) { + ValidateXML(destination); + } + } + + private static void ValidateXML(string filename) + { + try { + var settings = new XmlReaderSettings { + ValidationType = ValidationType.Schema, + ValidationFlags = //XmlSchemaValidationFlags.ProcessInlineSchema | + //XmlSchemaValidationFlags.ProcessSchemaLocation | + XmlSchemaValidationFlags.ReportValidationWarnings + }; + settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack); + settings.Schemas.Add(GetXMLSchema("")); + + var vreader = XmlReader.Create(filename, settings); + var doc = new XmlDocument(); + doc.Load(vreader); + doc.Validate(ValidationCallBack); + //while (vreader.Read()) { + // Console.WriteLine(vreader.Value); + //} + if (xmlValid) { + WriteLine("Valid", ConsoleColor.Green); + } + } catch (Exception e) { + Console.ForegroundColor = ConsoleColor.Red; + Console.Error.WriteLine("Failed to validate hashed XML file!"); + Console.Error.WriteLine(e.Message); + if (e.InnerException != null) { + Console.Error.WriteLine(e.InnerException.Message); + } + Console.ResetColor(); + } + } + + private static void ValidationCallBack(object sender, ValidationEventArgs args) + { + xmlValid = false; + if (args.Severity == XmlSeverityType.Error) { + throw new Exception(string.Format("Validation error: {0}" + Environment.NewLine + + "Line: {1}", args.Message, args.Exception.LineNumber)); + } else { + Console.Error.WriteLine(string.Format("Validation warning: {0}" + Environment.NewLine + + "Line: {1}", args.Message, args.Exception.LineNumber)); + } + } + + private static XmlSchemaSet GetXMLSchema(string version) + { + var resource = RessourceHelper.LoadResourceAsStream(RessourceHelper.ResourceType.XMLSchema, "VectoComponent.xsd"); + var xset = new XmlSchemaSet() { XmlResolver = new XmlResourceResolver() }; + var reader = XmlReader.Create(resource, new XmlReaderSettings(), "schema://"); + xset.Add(XmlSchema.Read(reader, null)); + xset.Compile(); + return xset; } private static void ReadHashAction(string filename, VectoHash h)