-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBlogMLValidator.cs
More file actions
64 lines (55 loc) · 2.17 KB
/
BlogMLValidator.cs
File metadata and controls
64 lines (55 loc) · 2.17 KB
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
using System;
using System.IO;
using System.Reflection;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Schema;
namespace BlogML.Core
{
/// BlogMLValidator is a new version of BlogMlResource updated to use the Linq to xml classes
public class BlogMLValidator
{
public void Validate(XDocument document) => Validate(document, null);
public void Validate(XDocument document, ValidationEventHandler validationEventHandler)
{
document.Validate(
GetBlogMLSchemaSet(),
validationEventHandler ?? new ValidationEventHandler(ValidationEvent)
);
}
public void Validate(XmlTextReader textReader) => Validate(textReader, null);
public void Validate(XmlTextReader textReader, ValidationEventHandler validationEventHandler)
{
XDocument blog = XDocument.Load(textReader);
Validate(blog, validationEventHandler);
}
public void Validate(string inputUri) => Validate(inputUri, null);
public void Validate(string inputUri, ValidationEventHandler validationEventHandler)
{
XDocument blog = XDocument.Load(inputUri);
Validate(blog, validationEventHandler);
}
private void ValidationEvent(object sender, ValidationEventArgs e)
{
throw new InvalidOperationException(
string.Format("Validation {0} : {1}", e.Severity, e.Message)
);
}
public XmlSchemaSet GetBlogMLSchemaSet()
{
using (Stream resourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("BlogML.Core.lib.blogml.xsd"))
{
if (resourceStream == null)
{
throw new InvalidOperationException("Schema resource not found");
}
using(XmlReader xmlResource = XmlReader.Create(resourceStream))
{
XmlSchemaSet schemaSet = new XmlSchemaSet();
schemaSet.Add("http://www.blogml.com/2006/09/BlogML", xmlResource);
return schemaSet;
}
}
}
}
}