forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXmlSchemas.cs
57 lines (48 loc) · 1.82 KB
/
XmlSchemas.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
using System.IO;
using System.Reflection;
using System.Xml;
using System.Xml.Schema;
namespace NHibernate.Cfg
{
internal class XmlSchemas
{
private const string CfgSchemaResource = "NHibernate.nhibernate-configuration.xsd";
private const string MappingSchemaResource = "NHibernate.nhibernate-mapping.xsd";
private static readonly XmlSchemaSet ConfigSchemaSet = ReadXmlSchemaFromEmbeddedResource(CfgSchemaResource);
private static class MappingSchemaHolder
{
public static readonly XmlSchemaSet MappingSchemaSet = ReadXmlSchemaFromEmbeddedResource(MappingSchemaResource);
}
public XmlReaderSettings CreateConfigReaderSettings()
{
XmlReaderSettings result = CreateXmlReaderSettings(ConfigSchemaSet);
result.ValidationEventHandler += ConfigSettingsValidationEventHandler;
result.IgnoreComments = true;
return result;
}
public XmlReaderSettings CreateMappingReaderSettings()
{
return CreateXmlReaderSettings(MappingSchemaHolder.MappingSchemaSet);
}
private static XmlSchemaSet ReadXmlSchemaFromEmbeddedResource(string resourceName)
{
Assembly executingAssembly = Assembly.GetExecutingAssembly();
using (Stream resourceStream = executingAssembly.GetManifestResourceStream(resourceName))
{
var xmlSchema = XmlSchema.Read(resourceStream, null);
var xmlSchemaSet = new XmlSchemaSet();
xmlSchemaSet.Add(xmlSchema);
xmlSchemaSet.Compile();
return xmlSchemaSet;
}
}
private static XmlReaderSettings CreateXmlReaderSettings(XmlSchemaSet xmlSchemaSet)
{
return new XmlReaderSettings {ValidationType = ValidationType.Schema, Schemas = xmlSchemaSet};
}
private static void ConfigSettingsValidationEventHandler(object sender, ValidationEventArgs e)
{
throw new HibernateConfigException("An exception occurred parsing configuration :" + e.Message, e.Exception);
}
}
}