File: RelaxngDatatypeProviderTests.cs

package info (click to toggle)
mono 4.6.2.7+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (98 lines) | stat: -rw-r--r-- 2,544 bytes parent folder | download | duplicates (8)
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
97
98
//
// RelaxngDatatypeProviderTest.cs
//
// Authors:
//	Atsushi Enomoto <atsushi@ximian.com>
//

using System;
using System.IO;
using System.Xml;
using Commons.Xml.Relaxng;
using Commons.Xml.Relaxng.XmlSchema;
using NUnit.Framework;

namespace MonoTests.Commons.Xml.Relaxng
{
	[TestFixture]
	public class RelaxngDatatypeProviderTests
	{
		[Test]
		public void CustomTypeProvider ()
		{
			var datatypeLibrary = SetupMyDataProvider ();
			XmlDocument xml = new XmlDocument ();
			xml.LoadXml ("<root> <v1>mytype</v1> <v2>1</v2> </root>");
			XmlDocument schemaXml = ReadDoc ("Test/XmlFiles/463264.rng");
			XmlReader reader = new RelaxngValidatingReader (new XmlNodeReader (xml), new XmlNodeReader (schemaXml), datatypeLibrary);
			while (reader.Read ())
				;
		}

		[Test]
		public void Bug463267 ()
		{
			var datatypeLibrary = SetupMyDataProvider ();
			XmlDocument xml = new XmlDocument ();
			xml.LoadXml ("<root> <v2>1</v2> <v1>mytype</v1> </root>");
			XmlDocument schemaXml = ReadDoc ("Test/XmlFiles/463267.rng");
			XmlReader reader = new RelaxngValidatingReader (new XmlNodeReader (xml), new XmlNodeReader (schemaXml), datatypeLibrary);
			while (reader.Read ())
				;
		}

		RelaxngDatatypeProvider SetupMyDataProvider ()
		{
			var datatypeLibrary = new RelaxngMergedProvider ();

			datatypeLibrary [MyRngTypeProvider.Namespace] = new MyRngTypeProvider();
			datatypeLibrary ["http://www.w3.org/2001/XMLSchema-datatypes"] = XsdDatatypeProvider.Instance;
			datatypeLibrary [System.Xml.Schema.XmlSchema.Namespace] = XsdDatatypeProvider.Instance;
			datatypeLibrary [String.Empty] = RelaxngMergedProvider.DefaultProvider [string.Empty];
			return datatypeLibrary;
		}

		XmlDocument ReadDoc (string s)
		{
			var d = new XmlDocument ();
			d.Load (s);
			return d;
		}
	}

	class MyRngTypeProvider : RelaxngDatatypeProvider
	{
		public static readonly string Namespace = "http://tempuri.org/mytypes";

		public override RelaxngDatatype GetDatatype(string name, string ns, RelaxngParamList parameters)
		{
			switch (name)
			{
			case "mytype":
			    return new MyType();
			}
			return null;
		}
	}

	class MyType : RelaxngDatatype
	{
		public override string Name {
			get { return "mytype"; }
		}

		public override string NamespaceURI {
			get { return MyRngTypeProvider.Namespace; }
		}

		public override object Parse (string text, System.Xml.XmlReader reader)
		{
			return text;
		}

		public override bool IsValid (string text, System.Xml.XmlReader reader)
		{
			return ((string) Parse (text, reader)) == "mytype";
		}
	}
}