public void WhenTheValidatorIsCalledWithGleifValidXmlTheErrorCountIsZero() { var mifidFile = xmlFilePaths.FirstOrDefault(f => f.Contains("20190402-0800-gleif-goldencopy-lei2-intra-day.xml")); var schemaFiles = new string[] { //xsdFilePaths.FirstOrDefault(f => f.Contains("xml.xsd")), xsdFilePaths.FirstOrDefault(f => f.Contains("2017-03-21_lei-cdf-v2-1.xsd")), }; if (schemaFiles.Any(a => string.IsNullOrEmpty(a))) { Assert.Fail("There's a XSD file missing."); } xmlSchemaReader = new XmlSchemaReader(fileSystem, memoryStreamFactory); var schemaSet = new XmlSchemaSet { XmlResolver = new XmlUrlResolver() }; foreach (var item in schemaFiles) { schemaSet.Add(xmlSchemaReader.ReadFromPath(item)); } var validator = new XmlValidator(memoryStreamFactory, fileSystem); ValidationFinishedEventArgs validationFinishedEventArgs = null; validator.ValidationFinished += async(s, e, c) => { await Task.Run(() => { validationFinishedEventArgs = e; }); }; var res = validator.ValidateXmlFileAgainstSchemaAsync(mifidFile, schemaSet, true).Result; var count = 0; while (validationFinishedEventArgs == null && count <= 10) { count++; Thread.Sleep(1000); } Assert.IsNotNull(validationFinishedEventArgs); Assert.AreEqual(6, validationFinishedEventArgs.ErrorCount); }
public async Task <ValidationFinishedEventArgs> ValidateXmlStreamAgainstSchemaAsync(Stream stream, XmlSchemaSet schemaSet, bool reportErrorListAtTheEndOfValidation, CancellationToken cancellationToken) { var stopWatch = new Stopwatch(); stopWatch.Start(); try { xmlValidationUtils.ValidateXmlSchemaSet(schemaSet); } catch (Exception e) { stopWatch.Stop(); return(new ValidationFinishedEventArgs { ElapsedTime = stopWatch.Elapsed, ErrorCount = 1, Errors = new List <ValidationErrorEventArgs> { new ValidationErrorEventArgs(new XmlSchemaException("The XML Schema Set is not valid. See inner exception for more details.", e)) } }); } if (!streamValidator.StreamIsValid(stream)) { stopWatch.Stop(); return(new ValidationFinishedEventArgs { ElapsedTime = stopWatch.Elapsed, ErrorCount = 1, Errors = new List <ValidationErrorEventArgs> { new ValidationErrorEventArgs(new Exception("The XML Stream is not valid. It is either closed or could not be seeked.")) } }); } stream.Seek(0, SeekOrigin.Begin); var errorList = new List <ValidationErrorEventArgs>(); var errorCount = 0; var readerSettings = xmlReaderSettingsFactory.CreateXmlSettings(); readerSettings.Async = true; readerSettings.ValidationEventHandler += (s, e) => { errorCount++; OnErrorOccurred(s, new ValidationErrorEventArgs(e), cancellationToken); }; if (reportErrorListAtTheEndOfValidation) { readerSettings.ValidationEventHandler += (s, e) => { errorList.Add(new ValidationErrorEventArgs(e)); }; } if (!schemaSet.IsCompiled) { try { schemaSet.Compile(); } catch (Exception e) { errorCount++; errorList.Add(new ValidationErrorEventArgs(e)); return(new ValidationFinishedEventArgs { ElapsedTime = stopWatch.Elapsed, ErrorCount = errorCount, Errors = errorList }); } } readerSettings.Schemas = schemaSet; try { using (var reader = XmlReader.Create(stream, readerSettings)) { while (await reader.ReadAsync()) { // Do nothing. Just read and let the reader validate things. } } } catch (Exception e) { errorCount++; var exc = new XmlSchemaException(e.Message, e); if (reportErrorListAtTheEndOfValidation) { errorList.Add(new ValidationErrorEventArgs(exc)); } OnErrorOccurred(this, new ValidationErrorEventArgs(exc, XmlSeverityType.Error), cancellationToken); } finally { stopWatch.Stop(); } var result = new ValidationFinishedEventArgs { ElapsedTime = stopWatch.Elapsed, ErrorCount = errorCount, Errors = errorList }; OnFinishedValidating(this, result, cancellationToken); return(result); }
public void OnFinishedValidating(object sender, ValidationFinishedEventArgs e, CancellationToken cancellationToken) { validationFinishedEventRiser.RaiseEventsOnThreadPool(ValidationFinished, e, cancellationToken); }
/// <inheritdoc/> public async Task <ValidationFinishedEventArgs> ParseXmlFileFromStreamAsync(Stream stream, XmlSchemaSet schemaSet, Type headerType, bool returnErrorListAtTheEndOfTheProcess, CancellationToken cancellationToken, params Type[] types) { var stopWatch = new Stopwatch(); stopWatch.Start(); try { xmlValidationUtils.ValidateXmlSchemaSet(schemaSet); } catch (Exception e) { stopWatch.Stop(); return(new ValidationFinishedEventArgs { ElapsedTime = stopWatch.Elapsed, ErrorCount = 1, Errors = new List <ValidationErrorEventArgs> { new ValidationErrorEventArgs(new XmlSchemaException("The XML Schema Set is not valid. See inner exception for more details.", e)) } }); } if (!streamValidator.StreamIsValid(stream)) { stopWatch.Stop(); return(new ValidationFinishedEventArgs { ElapsedTime = stopWatch.Elapsed, ErrorCount = 1, Errors = new List <ValidationErrorEventArgs> { new ValidationErrorEventArgs(new Exception("The XML Stream is not valid. It is either closed or could not be seeked.")) } }); } stream.Seek(0, SeekOrigin.Begin); var tagsFromSchemaSet = GetTagsFromSchemaSet(schemaSet); var headerTypeName = new KeyValuePair <string, HashSet <string> >(); object header = null; XmlSerializer headerSerializer = null; XmlRootAttribute attrHeader = null; if (headerType != null) { header = Activator.CreateInstance(headerType); headerTypeName = tagsFromSchemaSet.FirstOrDefault(w => w.Key == headerType.Name); attrHeader = ReturnXmlRootAttributeForType(headerTypeName.Value.FirstOrDefault(), headerType); headerSerializer = new XmlSerializer(headerType, attrHeader); } var otherTypeNames = types.Select(s => s.Name).ToArray(); var otherTypeNamesFromDictionary = tagsFromSchemaSet.Where(w => otherTypeNames.Contains(w.Key)).ToArray(); try { CheckTypesForAmbiguity(tagsFromSchemaSet, otherTypeNames, otherTypeNamesFromDictionary); } catch (Exception e) { stopWatch.Stop(); return(new ValidationFinishedEventArgs { ElapsedTime = stopWatch.Elapsed, ErrorCount = 1, Errors = new List <ValidationErrorEventArgs> { new ValidationErrorEventArgs(new Exception("There are ambiguities for the given types. Check inner exception for more details.", e)) } }); } if (cancellationToken.IsCancellationRequested) { stopWatch.Stop(); return(new ValidationFinishedEventArgs { ElapsedTime = stopWatch.Elapsed, ErrorCount = 1, Errors = new List <ValidationErrorEventArgs> { new ValidationErrorEventArgs(new OperationCanceledException("Operation has been canceled.")) } }); } var serializerDictionary = CreateSerializerDictionary(types, otherTypeNamesFromDictionary); otherTypeNames = null; otherTypeNamesFromDictionary = null; var errorList = new List <ValidationErrorEventArgs>(); long errorCount = 0; long nodeCount = 0; var readerSettings = xmlReaderSettingsFactory.CreateXmlSettings(); readerSettings.Async = true; readerSettings.ValidationEventHandler += (s, e) => { errorCount++; OnErrorOccurred(s, new ValidationErrorEventArgs(e), cancellationToken); }; if (returnErrorListAtTheEndOfTheProcess) { readerSettings.ValidationEventHandler += (s, e) => { errorList.Add(new ValidationErrorEventArgs(e)); }; } if (!schemaSet.IsCompiled) { try { schemaSet.Compile(); } catch (Exception e) { errorCount++; errorList.Add(new ValidationErrorEventArgs(e)); return(new ValidationFinishedEventArgs { ElapsedTime = stopWatch.Elapsed, ErrorCount = errorCount, Errors = errorList, ParsedNodeCount = nodeCount }); } } readerSettings.Schemas = schemaSet; try { using (var reader = XmlReader.Create(stream, readerSettings)) { while (await reader.ReadAsync()) { reader.MoveToElement(); if (reader.NodeType != XmlNodeType.Element) { continue; } var elementName = reader.SchemaInfo?.SchemaElement?.Name; if (string.IsNullOrEmpty(elementName)) { continue; } if (cancellationToken.IsCancellationRequested) { #if DEBUG Debug.WriteLine("Operation Cancelled - Cancel Parsing"); #endif reader.Close(); reader.Dispose(); serializerDictionary.Clear(); headerSerializer = null; stopWatch.Stop(); errorCount++; errorList.Add(new ValidationErrorEventArgs(new OperationCanceledException("Operation has been canceled."))); return(new ValidationFinishedEventArgs { ElapsedTime = stopWatch.Elapsed, ErrorCount = errorCount, Errors = errorList, ParsedNodeCount = nodeCount }); } if (serializerDictionary.ContainsKey(elementName)) { try { var serializer = serializerDictionary[elementName]; var obj = serializer.Deserialize(reader.ReadSubtree()); nodeCount++; OnNodeRead(this, new NodeReadEventArgs(header, obj), cancellationToken); } catch (Exception e) { errorCount++; var exc = new XmlSchemaException(e.Message, e); if (returnErrorListAtTheEndOfTheProcess) { errorList.Add(new ValidationErrorEventArgs(exc, XmlSeverityType.Error)); } OnErrorOccurred(this, new ValidationErrorEventArgs(exc, XmlSeverityType.Error), cancellationToken); } } else if (headerSerializer != null && (headerTypeName.Value?.Contains(elementName)).GetValueOrDefault(false)) { try { header = headerSerializer.Deserialize(reader.ReadSubtree()); OnHeaderRead(this, new HeaderReadEventArgs(header), cancellationToken); } catch (Exception e) { errorCount++; var exc = new XmlSchemaException(e.Message, e); if (returnErrorListAtTheEndOfTheProcess) { errorList.Add(new ValidationErrorEventArgs(exc, XmlSeverityType.Error)); } OnErrorOccurred(this, new ValidationErrorEventArgs(exc, XmlSeverityType.Error), cancellationToken); } } } } } catch (Exception e) { errorCount++; var exc = new XmlSchemaException(e.Message, e); if (returnErrorListAtTheEndOfTheProcess) { errorList.Add(new ValidationErrorEventArgs(exc)); } OnErrorOccurred(this, new ValidationErrorEventArgs(exc, XmlSeverityType.Error), cancellationToken); } finally { stopWatch.Stop(); } await Task.WhenAll( nodeReadEventRiser.WaitForTasksToFinish(cancellationToken), headerReadEventRiser.WaitForTasksToFinish(cancellationToken), validationErrorEventRiser.WaitForTasksToFinish(cancellationToken), validationFinishedEventRiser.WaitForTasksToFinish(cancellationToken)); var result = new ValidationFinishedEventArgs { ElapsedTime = stopWatch.Elapsed, ErrorCount = errorCount, ParsedNodeCount = nodeCount, Errors = errorList }; OnFinishedValidating(this, result, cancellationToken); return(result); }