diff --git a/HashingTool/ViewModel/HashComponentDataViewModel.cs b/HashingTool/ViewModel/HashComponentDataViewModel.cs index f8e1aab09a6008cd15f5883a3ad4a8fdd4030f3d..e65a6bb0f254f837f6b38b9d89ed0ba0ae1e1951 100644 --- a/HashingTool/ViewModel/HashComponentDataViewModel.cs +++ b/HashingTool/ViewModel/HashComponentDataViewModel.cs @@ -43,6 +43,7 @@ using HashingTool.Helper; using HashingTool.Util; using HashingTool.ViewModel.UserControl; using TUGraz.VectoHashing; +using XmlDocumentType = TUGraz.VectoCore.Utils.XmlDocumentType; namespace HashingTool.ViewModel { @@ -60,7 +61,7 @@ namespace HashingTool.ViewModel private DateTime? _date; public HashComponentDataViewModel() - : base("Hash Component Data", false, HashingHelper.IsComponentFile) + : base("Hash Component Data", false, HashingHelper.IsComponentFile, XmlDocumentType.DeclarationComponentData) { _xmlFile.PropertyChanged += SourceChanged; _saveCommand = new RelayCommand(SaveDocument, diff --git a/HashingTool/ViewModel/UserControl/CustomerReportXMLFile.cs b/HashingTool/ViewModel/UserControl/CustomerReportXMLFile.cs index 659c73f7db894857b7bccaf37c84faf20de16b07..22fc7ba3282ff7593154660ac6da2d17e513af28 100644 --- a/HashingTool/ViewModel/UserControl/CustomerReportXMLFile.cs +++ b/HashingTool/ViewModel/UserControl/CustomerReportXMLFile.cs @@ -34,6 +34,7 @@ using System.ComponentModel; using System.Linq; using System.Xml; using TUGraz.VectoHashing; +using XmlDocumentType = TUGraz.VectoCore.Utils.XmlDocumentType; namespace HashingTool.ViewModel.UserControl { @@ -48,7 +49,7 @@ namespace HashingTool.ViewModel.UserControl private bool _manufacturerReportDigestValid; public CustomerReportXMLFile(string name, Func<XmlDocument, IErrorLogger, bool?> contentCheck, - Action<XmlDocument, VectoXMLFile> hashValidation = null) : base(name, contentCheck, hashValidation) + Action<XmlDocument, VectoXMLFile> hashValidation = null) : base(name, contentCheck, XmlDocumentType.CustomerReport, hashValidation) { } diff --git a/HashingTool/ViewModel/UserControl/HashedXMLFile.cs b/HashingTool/ViewModel/UserControl/HashedXMLFile.cs index 1072a9caed32c0841318061c4e7b928cf66e5407..fa172c3614b1630cf90651122a72f1e02c81a729 100644 --- a/HashingTool/ViewModel/UserControl/HashedXMLFile.cs +++ b/HashingTool/ViewModel/UserControl/HashedXMLFile.cs @@ -33,6 +33,7 @@ using System; using System.Collections.ObjectModel; using System.ComponentModel; using System.Xml; +using XmlDocumentType = TUGraz.VectoCore.Utils.XmlDocumentType; namespace HashingTool.ViewModel.UserControl { @@ -41,8 +42,10 @@ namespace HashingTool.ViewModel.UserControl protected string _digestValueRead; private DateTime? _date; - public HashedXMLFile(string name, Func<XmlDocument, IErrorLogger, bool?> contentCheck, - Action<XmlDocument, VectoXMLFile> hashValidation = null) : base(name, true, contentCheck, hashValidation) {} + public HashedXMLFile( + string name, Func<XmlDocument, IErrorLogger, bool?> contentCheck, XmlDocumentType xmlDocumentType, + Action<XmlDocument, VectoXMLFile> hashValidation = null) : base( + name, true, contentCheck, xmlDocumentType, hashValidation) { } public string DigestValueRead { diff --git a/HashingTool/ViewModel/UserControl/ManufacturerReportXMLFile.cs b/HashingTool/ViewModel/UserControl/ManufacturerReportXMLFile.cs index a1fbbb883280bf5bbb4ffd2994ce5436b21e4e60..a612e475025e699311da331a2ddbc288ecb61306 100644 --- a/HashingTool/ViewModel/UserControl/ManufacturerReportXMLFile.cs +++ b/HashingTool/ViewModel/UserControl/ManufacturerReportXMLFile.cs @@ -40,6 +40,7 @@ using TUGraz.VectoCommon.Resources; using TUGraz.VectoCommon.Utils; using TUGraz.VectoCore.InputData.FileIO.XML.Declaration; using TUGraz.VectoHashing; +using XmlDocumentType = TUGraz.VectoCore.Utils.XmlDocumentType; namespace HashingTool.ViewModel.UserControl { @@ -48,7 +49,7 @@ namespace HashingTool.ViewModel.UserControl private bool _manufacturerReportValid; public ManufacturerReportXMLFile(string name, Func<XmlDocument, IErrorLogger, bool?> contentCheck, - Action<XmlDocument, VectoXMLFile> hashValidation = null) : base(name, contentCheck, hashValidation) + Action<XmlDocument, VectoXMLFile> hashValidation = null) : base(name, contentCheck, XmlDocumentType.ManufacturerReport, hashValidation) { _xmlFile.PropertyChanged += UpdateComponents; } diff --git a/HashingTool/ViewModel/UserControl/ReportXMLFile.cs b/HashingTool/ViewModel/UserControl/ReportXMLFile.cs index dd4c831c8d1ef5fd0a9ab8b247b3bdecdc1d3beb..6c4c2496c1088e578a6a5e8122ce541574dd939c 100644 --- a/HashingTool/ViewModel/UserControl/ReportXMLFile.cs +++ b/HashingTool/ViewModel/UserControl/ReportXMLFile.cs @@ -37,6 +37,7 @@ using System.Xml; using TUGraz.VectoCommon.InputData; using TUGraz.VectoCore.InputData.FileIO.XML.Declaration; using TUGraz.VectoHashing; +using XmlDocumentType = TUGraz.VectoCore.Utils.XmlDocumentType; namespace HashingTool.ViewModel.UserControl { @@ -52,9 +53,8 @@ namespace HashingTool.ViewModel.UserControl protected VectoJobFile _jobData; private string _reportVin; - public ReportXMLFile(string name, Func<XmlDocument, IErrorLogger, bool?> contentCheck, - Action<XmlDocument, VectoXMLFile> hashValidation = null) - : base(name, contentCheck, hashValidation) + public ReportXMLFile(string name, Func<XmlDocument, IErrorLogger, bool?> contentCheck, XmlDocumentType xmlDocumentType, Action<XmlDocument, VectoXMLFile> hashValidation = null) + : base(name, contentCheck, xmlDocumentType, hashValidation) { _xmlFile.PropertyChanged += ReportChanged; } @@ -123,7 +123,8 @@ namespace HashingTool.ViewModel.UserControl JobDigestMatchesReport = vinMatch && digestMatch; - } catch (Exception) { + } catch (Exception e) { + _xmlFile.LogError(e.Message); JobDigestValueComputed = ""; JobDigestMatchesReport = false; } diff --git a/HashingTool/ViewModel/UserControl/VectoJobFile.cs b/HashingTool/ViewModel/UserControl/VectoJobFile.cs index 814b309bf3ad171a59d50782ff7e59ec7788dcb3..57fb7a4ae60b1418bc9acd0a2e9a9886eb2086df 100644 --- a/HashingTool/ViewModel/UserControl/VectoJobFile.cs +++ b/HashingTool/ViewModel/UserControl/VectoJobFile.cs @@ -38,6 +38,7 @@ using HashingTool.Helper; using TUGraz.VectoCommon.Hashing; using TUGraz.VectoCommon.Resources; using TUGraz.VectoHashing; +using XmlDocumentType = TUGraz.VectoCore.Utils.XmlDocumentType; namespace HashingTool.ViewModel.UserControl { @@ -50,7 +51,7 @@ namespace HashingTool.ViewModel.UserControl public VectoJobFile(string name, Func<XmlDocument, IErrorLogger, bool?> contentCheck, - Action<XmlDocument, VectoXMLFile> hashValidation = null) : base(name, true, contentCheck, hashValidation) + Action<XmlDocument, VectoXMLFile> hashValidation = null) : base(name, true, contentCheck, XmlDocumentType.DeclarationJobData, hashValidation) { _xmlFile.PropertyChanged += JobFilechanged; Components = new ObservableCollection<ComponentEntry>(); diff --git a/HashingTool/ViewModel/UserControl/VectoXMLFile.cs b/HashingTool/ViewModel/UserControl/VectoXMLFile.cs index 9eabe8527b5a1313cb4bba0cfbd66b95616f2b85..564582ca54b838b529c8412d9cf59ef0fd9d656b 100644 --- a/HashingTool/ViewModel/UserControl/VectoXMLFile.cs +++ b/HashingTool/ViewModel/UserControl/VectoXMLFile.cs @@ -35,6 +35,7 @@ using System.Collections.ObjectModel; using System.ComponentModel; using System.Xml; using HashingTool.Helper; +using XmlDocumentType = TUGraz.VectoCore.Utils.XmlDocumentType; namespace HashingTool.ViewModel.UserControl { @@ -51,11 +52,10 @@ namespace HashingTool.ViewModel.UserControl private string _digestMethod; - public VectoXMLFile(string name, bool validate, Func<XmlDocument, IErrorLogger, bool?> contentCheck, - Action<XmlDocument, VectoXMLFile> hashValidation = null) + public VectoXMLFile(string name, bool validate, Func<XmlDocument, IErrorLogger, bool?> contentCheck, XmlDocumentType xmlDocumentType, Action<XmlDocument, VectoXMLFile> hashValidation = null) { _validateHashes = hashValidation; - _xmlFile = new XMLFileSelector(IoService, name, validate, contentCheck); + _xmlFile = new XMLFileSelector(IoService, name, xmlDocumentType, validate, contentCheck); _xmlFile.PropertyChanged += FileChanged; Name = name; CanonicalizationMethods = new ObservableCollection<string>(); diff --git a/HashingTool/ViewModel/UserControl/XMLFileSelector.cs b/HashingTool/ViewModel/UserControl/XMLFileSelector.cs index 831815cdfa4382c7aa43bbe1e468adeda6a21967..4e6053f9df6df57b23ba4bd71f84964baca8528c 100644 --- a/HashingTool/ViewModel/UserControl/XMLFileSelector.cs +++ b/HashingTool/ViewModel/UserControl/XMLFileSelector.cs @@ -72,13 +72,15 @@ namespace HashingTool.ViewModel.UserControl private bool? _contentValid; private RelayCommand _browseFileCommand; private string _prefix; + private XmlDocumentType _expectedDocumentType; - public XMLFileSelector(IOService ioservice, string prefix, bool validate = false, + public XMLFileSelector(IOService ioservice, string prefix, XmlDocumentType expectedDocumentType, bool validate = false, Func<XmlDocument, IErrorLogger, bool?> contentCheck = null) { IoService = ioservice; _validate = validate; _prefix = prefix; + _expectedDocumentType = expectedDocumentType; _browseFileCommand = new RelayCommand(BrowseXMLFile, () => !_busy); XMLValidationErrors = new ObservableCollection<string>(); HasContentValidation = contentCheck != null; @@ -181,8 +183,13 @@ namespace HashingTool.ViewModel.UserControl IsValid = XmlFileStatus.Unknown; return; } - using (var stream = File.OpenRead(fileName)) { - await LoadXMLFile(stream); + + try { + using (var stream = File.OpenRead(fileName)) { + await LoadXMLFile(stream); + } + } catch (Exception e) { + LogError(e.Message); } } @@ -190,14 +197,17 @@ namespace HashingTool.ViewModel.UserControl private async void BrowseXMLFile() { string filename; + try { + using (var stream = IoService.OpenFileDialog(null, ".xml", "VECTO XML file|*.xml", out filename)) { + if (stream == null) { + return; + } - using (var stream = IoService.OpenFileDialog(null, ".xml", "VECTO XML file|*.xml", out filename)) { - if (stream == null) { - return; + Source = filename; + //await LoadXMLFile(stream); } - - await LoadXMLFile(stream); - Source = filename; + } catch (Exception e) { + LogError(e.Message); } } @@ -272,18 +282,32 @@ namespace HashingTool.ViewModel.UserControl var valid = true; try { var validator = new AsyncXMLValidator(xml, r => { valid = r; }, - (s, e) => { - Application.Current.Dispatcher.Invoke( - () => - LogError(string.Format("Validation {0} Line {2}: {1}", - s == XmlSeverityType.Warning ? "WARNING" : "ERROR", - e.ValidationEventArgs == null - ? e.Exception.Message + - (e.Exception.InnerException != null ? Environment.NewLine + e.Exception.InnerException.Message : "") - : e.ValidationEventArgs.Message, - e.ValidationEventArgs == null ? 0 : e.ValidationEventArgs.Exception.LineNumber))); - }); - await validator.ValidateXML(XmlDocumentType.DeclarationComponentData | XmlDocumentType.DeclarationJobData | XmlDocumentType.CustomerReport | XmlDocumentType.ManufacturerReport); + (s, e) => { + Application.Current.Dispatcher.Invoke( + () => { + if (e.ValidationEventArgs == null) { + LogError( + string.Format( + "XML file does not validate against a supported version of {0}", + _expectedDocumentType.ToString())); + } else { + LogError( + string.Format( + "Validation {0} Line {2}: {1}", + s == XmlSeverityType.Warning ? "WARNING" : "ERROR", + e.ValidationEventArgs == null + ? e.Exception.Message + + (e.Exception.InnerException != null + ? Environment.NewLine + e.Exception.InnerException.Message + : "") + : e.ValidationEventArgs.Message, + e.ValidationEventArgs == null ? 0 : e.ValidationEventArgs.Exception.LineNumber)); + } + } + ); + } + ); + valid = await validator.ValidateXML(_expectedDocumentType); } catch (Exception e) { LogError(e.Message); } diff --git a/HashingTool/ViewModel/VerifyComponentInputDataViewModel.cs b/HashingTool/ViewModel/VerifyComponentInputDataViewModel.cs index cd43531d6338b986f7eba58ac422d48b195bc3e2..16c45b4ec86a1564e46f8cd2101c8f0363910d1e 100644 --- a/HashingTool/ViewModel/VerifyComponentInputDataViewModel.cs +++ b/HashingTool/ViewModel/VerifyComponentInputDataViewModel.cs @@ -39,6 +39,7 @@ using System.Windows.Input; using HashingTool.Helper; using HashingTool.ViewModel.UserControl; using TUGraz.VectoCommon.Hashing; +using TUGraz.VectoCore.Utils; using TUGraz.VectoHashing; namespace HashingTool.ViewModel @@ -49,7 +50,7 @@ namespace HashingTool.ViewModel //private bool _componentDataValid; public VerifyComponentInputDataViewModel() - : base("Verify Component Data", HashingHelper.IsComponentFile, HashingHelper.ValidateDocumentHash) + : base("Verify Component Data", HashingHelper.IsComponentFile, XmlDocumentType.DeclarationComponentData, HashingHelper.ValidateDocumentHash) { _xmlFile.PropertyChanged += ComponentFilechanged; } diff --git a/VectoCore/VectoCore/Utils/XMLValidator.cs b/VectoCore/VectoCore/Utils/XMLValidator.cs index eb5d5e61fcd3f68e2c23a96892ae79ad18234421..76dcdfde63168ce6864f98df6e985eaea00e9359 100644 --- a/VectoCore/VectoCore/Utils/XMLValidator.cs +++ b/VectoCore/VectoCore/Utils/XMLValidator.cs @@ -84,6 +84,7 @@ namespace TUGraz.VectoCore.Utils if (_doc.SchemaInfo.Validity != XmlSchemaValidity.Valid || _doc.DocumentElement?.SchemaInfo == null || _doc.DocumentElement.SchemaInfo.SchemaType == null) { + ValidationCallBack(null, null); _valid = false; } @@ -94,7 +95,7 @@ namespace TUGraz.VectoCore.Utils { _resultAction(false); _valid = false; - _validationErrorAction(args.Severity, new ValidationEvent { ValidationEventArgs = args }); + _validationErrorAction(args?.Severity ?? XmlSeverityType.Error, new ValidationEvent { ValidationEventArgs = args }); } public static void CallBackExceptionOnError(XmlSeverityType severity, ValidationEvent evt)