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

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

surround loading files with try/catch, handle null validation argument

parent 61816ccd
No related branches found
No related tags found
No related merge requests found
...@@ -72,13 +72,15 @@ namespace HashingTool.ViewModel.UserControl ...@@ -72,13 +72,15 @@ namespace HashingTool.ViewModel.UserControl
private bool? _contentValid; private bool? _contentValid;
private RelayCommand _browseFileCommand; private RelayCommand _browseFileCommand;
private string _prefix; 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) Func<XmlDocument, IErrorLogger, bool?> contentCheck = null)
{ {
IoService = ioservice; IoService = ioservice;
_validate = validate; _validate = validate;
_prefix = prefix; _prefix = prefix;
_expectedDocumentType = expectedDocumentType;
_browseFileCommand = new RelayCommand(BrowseXMLFile, () => !_busy); _browseFileCommand = new RelayCommand(BrowseXMLFile, () => !_busy);
XMLValidationErrors = new ObservableCollection<string>(); XMLValidationErrors = new ObservableCollection<string>();
HasContentValidation = contentCheck != null; HasContentValidation = contentCheck != null;
...@@ -181,8 +183,13 @@ namespace HashingTool.ViewModel.UserControl ...@@ -181,8 +183,13 @@ namespace HashingTool.ViewModel.UserControl
IsValid = XmlFileStatus.Unknown; IsValid = XmlFileStatus.Unknown;
return; 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 ...@@ -190,14 +197,17 @@ namespace HashingTool.ViewModel.UserControl
private async void BrowseXMLFile() private async void BrowseXMLFile()
{ {
string filename; 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)) { Source = filename;
if (stream == null) { //await LoadXMLFile(stream);
return;
} }
} catch (Exception e) {
await LoadXMLFile(stream); LogError(e.Message);
Source = filename;
} }
} }
...@@ -272,18 +282,32 @@ namespace HashingTool.ViewModel.UserControl ...@@ -272,18 +282,32 @@ namespace HashingTool.ViewModel.UserControl
var valid = true; var valid = true;
try { try {
var validator = new AsyncXMLValidator(xml, r => { valid = r; }, var validator = new AsyncXMLValidator(xml, r => { valid = r; },
(s, e) => { (s, e) => {
Application.Current.Dispatcher.Invoke( Application.Current.Dispatcher.Invoke(
() => () => {
LogError(string.Format("Validation {0} Line {2}: {1}", if (e.ValidationEventArgs == null) {
s == XmlSeverityType.Warning ? "WARNING" : "ERROR", LogError(
e.ValidationEventArgs == null string.Format(
? e.Exception.Message + "XML file does not validate against a supported version of {0}",
(e.Exception.InnerException != null ? Environment.NewLine + e.Exception.InnerException.Message : "") _expectedDocumentType.ToString()));
: e.ValidationEventArgs.Message, } else {
e.ValidationEventArgs == null ? 0 : e.ValidationEventArgs.Exception.LineNumber))); LogError(
}); string.Format(
await validator.ValidateXML(XmlDocumentType.DeclarationComponentData | XmlDocumentType.DeclarationJobData | XmlDocumentType.CustomerReport | XmlDocumentType.ManufacturerReport); "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) { } catch (Exception e) {
LogError(e.Message); LogError(e.Message);
} }
......
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