-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathXmlSchemaHandler.cs
96 lines (87 loc) · 4.09 KB
/
XmlSchemaHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright © 2024-Present The Synapse Authors
//
// Licensed under the Apache License, Version 2.0 (the "License"),
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using Neuroglia;
using Neuroglia.Serialization;
using ServerlessWorkflow.Sdk;
using System.Net;
using System.Xml;
using System.Xml.Schema;
namespace Synapse.Core.Infrastructure.Services;
/// <summary>
/// Represents the <see cref="ISchemaHandler"/> implementation used to handle XML schemas
/// </summary>
/// <param name="externalResourceProvider">The service used to provide external resources</param>
/// <param name="serializer">The service used to serialize/deserialize data to/from XML</param>
public class XmlSchemaHandler(IExternalResourceProvider externalResourceProvider , IXmlSerializer serializer)
: ISchemaHandler
{
/// <summary>
/// Gets the service used to provide external resources
/// </summary>
protected IExternalResourceProvider ExternalResourceProvider { get; } = externalResourceProvider;
/// <summary>
/// Gets the service used to serialize/deserialize data to/from XML
/// </summary>
protected IXmlSerializer Serializer { get; } = serializer;
/// <inheritdoc/>
public virtual bool Supports(string format) => format.Equals(SchemaFormat.Xml, StringComparison.OrdinalIgnoreCase);
/// <inheritdoc/>
public virtual async Task<IOperationResult> ValidateAsync(object graph, SchemaDefinition schema, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(graph);
ArgumentNullException.ThrowIfNull(schema);
if (!this.Supports(schema.Format)) throw new NotSupportedException($"The specified schema format '{schema.Format}' is not supported in this context");
XmlSchema xmlSchema;
if (schema.Resource == null)
{
using var reader = new StringReader(this.Serializer.SerializeToText(schema.Document));
xmlSchema = XmlSchema.Read(reader, OnValidationError)!;
}
else
{
using var stream = await this.ExternalResourceProvider.ReadAsync(schema.Resource, cancellationToken: cancellationToken).ConfigureAwait(false);
xmlSchema = XmlSchema.Read(stream, OnValidationError)!;
}
var settings = new XmlReaderSettings();
settings.Schemas.Add(xmlSchema);
settings.ValidationType = ValidationType.Schema;
var xml = this.Serializer.SerializeToText(graph);
using var stringReader = new StringReader(xml);
using var xmlReader = XmlReader.Create(stringReader, settings);
var validationErrors = new List<string>();
settings.ValidationEventHandler += (sender, args) => validationErrors.Add(args.Message);
try
{
while (xmlReader.Read()) { }
}
catch (XmlException ex)
{
validationErrors.Add(ex.Message);
}
if (validationErrors.Count != 0) return new OperationResult((int)HttpStatusCode.BadRequest, null, new Neuroglia.Error()
{
Type = ErrorType.Validation,
Title = ErrorTitle.Validation,
Status = ErrorStatus.Validation,
Errors = new(validationErrors.GroupBy(e => e).Select(e => new KeyValuePair<string, string[]>(e.Key, [e.Key])))
});
else return await Task.FromResult(new OperationResult((int)HttpStatusCode.OK));
}
/// <summary>
/// Handles schema validation errors
/// </summary>
protected virtual void OnValidationError(object? sender, ValidationEventArgs e)
{
if (e.Severity == XmlSeverityType.Error) throw new XmlSchemaValidationException(e.Message, e.Exception, e.Exception.LineNumber, e.Exception.LinePosition);
}
}