Code development platform for open source projects from the European Union institutions :large_blue_circle: EU Login authentication by SMS will be completely phased out by mid-2025. To see alternatives please check here

Skip to content
Snippets Groups Projects
Commit 19465658 authored by Markus Quaritsch's avatar Markus Quaritsch
Browse files

refactor xml validation to use xsd type to load according schema

parent a0d6dcf2
No related branches found
No related tags found
No related merge requests found
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
namespace TUGraz.VectoCore.Utils
{
......@@ -81,14 +82,25 @@ namespace TUGraz.VectoCore.Utils
public static string GetSchemaFilename(XmlDocumentType type, string version)
public static string GetSchemaFilename(XmlDocumentType type, string xsdType)
{
if (!schemaFilenames.ContainsKey(type)) {
throw new Exception(string.Format("Invalid argument {0} - only use single flags", type));
}
var entry = schemaFilenames[type];
var version = GetSchemaVersion(xsdType);
return string.Format(entry, string.IsNullOrWhiteSpace(version) ? "" : "." + version);
}
public static string GetSchemaVersion(string nodeType)
{
var parts = nodeType?.Split(':');
if (parts?.Length == 2) {
return XMLHelper.GetVersionFromNamespaceUri(parts[0]);
}
return null;
}
}
}
......@@ -68,18 +68,6 @@ namespace TUGraz.VectoCore.Utils
// return GetVersionFromNamespaceUri(node.NamespaceURI);
//}
public static string GetSchemaVersion(XmlNode node)
{
var nodeType = node.Attributes?.GetNamedItem("type", "http://www.w3.org/2001/XMLSchema-instance");
if (nodeType != null) {
var parts = nodeType.InnerText.Split(':');
if (parts.Length == 2) {
return GetVersionFromNamespaceUri(nodeType.GetNamespaceOfPrefix(parts[0]));
}
}
return GetVersionFromNamespaceUri(node.NamespaceURI);
}
public static string GetVersionFromNamespaceUri(XNamespace namespaceUri)
{
const string versionPrefix = "v";
......
......@@ -78,26 +78,12 @@ namespace TUGraz.VectoCore.Utils
throw new Exception("empty XML document");
}
var version = XMLHelper.GetSchemaVersion(_doc.DocumentElement);
var xsdType = _doc.DocumentElement.Attributes?.GetNamedItem("type", "http://www.w3.org/2001/XMLSchema-instance")
?.InnerText;
_doc.Schemas = GetXMLSchema(docType, version);
_doc.Schemas = GetXMLSchema(docType, xsdType);
_doc.Validate(ValidationCallBack);
//var settings = new XmlReaderSettings();
//settings.Schemas = GetXMLSchema(docType, version);
//settings.ValidationType = ValidationType.Schema;
//settings.ValidationFlags =
// XmlSchemaValidationFlags.ReportValidationWarnings | XmlSchemaValidationFlags.AllowXmlAttributes;
//settings.ValidationEventHandler += ValidationCallBack;
//var m = new MemoryStream();
//var w = new XmlTextWriter(m, Encoding.UTF8);
//_doc.WriteTo(w);
//w.Flush();
//m.Flush();
//m.Seek(0, SeekOrigin.Begin);
//var r = new XmlTextReader(m);
//var reader = XmlReader.Create(r, settings);
//_doc = new XmlDocument();
//_doc.Load(reader);
return _valid;
}
......@@ -115,7 +101,7 @@ namespace TUGraz.VectoCore.Utils
}
}
private static XmlSchemaSet GetXMLSchema(XmlDocumentType docType, string version)
private static XmlSchemaSet GetXMLSchema(XmlDocumentType docType, string xsdType)
{
var xset = new XmlSchemaSet() { XmlResolver = new XmlResourceResolver() };
......@@ -124,7 +110,7 @@ namespace TUGraz.VectoCore.Utils
continue;
}
var schemaFile = XMLDefinitions.GetSchemaFilename(entry, version);
var schemaFile = XMLDefinitions.GetSchemaFilename(entry, xsdType);
if (schemaFile == null) {
continue;
}
......@@ -134,7 +120,7 @@ namespace TUGraz.VectoCore.Utils
resource = RessourceHelper.LoadResourceAsStream(RessourceHelper.ResourceType.XMLSchema, schemaFile);
} catch (Exception e) {
throw new Exception(
string.Format("Unknown XML schema! version: {0}, xml document type: {1} ({2})", entry, version, schemaFile), e);
string.Format("Unknown XML schema! version: {0}, xml document type: {1} ({2})", entry, xsdType, schemaFile), e);
}
var reader = XmlReader.Create(resource, new XmlReaderSettings(), "schema://");
......
......@@ -31,7 +31,6 @@
using System.IO;
using NUnit.Framework;
using NUnit.Framework.Internal;
using TUGraz.VectoCommon.InputData;
using TUGraz.VectoCore.InputData.FileIO.JSON;
using TUGraz.VectoCore.Tests.Utils;
......
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