Code development platform for open source projects from the European Union institutions

Skip to content
Snippets Groups Projects
Commit 952d0752 authored by Markus QUARITSCH's avatar Markus QUARITSCH
Browse files

adding code for validating generated XML

parent af8499a7
No related branches found
No related tags found
No related merge requests found
......@@ -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.
......
......@@ -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)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment