New test.
[mono.git] / mcs / class / Commons.Xml.Relaxng / Commons.Xml.Nvdl / NvdlBuiltInValidationProvider.cs
1 using System;
2 using System.Collections;
3 using System.Collections.Specialized;
4 using System.Xml;
5 using Commons.Xml;
6
7 namespace Commons.Xml.Nvdl
8 {
9         public class NvdlBuiltInValidationProvider : NvdlValidationProvider
10         {
11                 public NvdlBuiltInValidationProvider ()
12                 {
13                 }
14
15                 public override NvdlValidatorGenerator CreateGenerator (
16                         XmlReader reader, NvdlConfig config)
17                 {
18                         reader.MoveToContent ();
19                         if (reader.NodeType != XmlNodeType.Element ||
20                                 reader.NamespaceURI != Nvdl.BuiltInValidationNamespace)
21                                 return null;
22                         return new NvdlBuiltInValidatorGenerator (reader.LocalName == "allow");
23                 }
24         }
25
26         internal class NvdlBuiltInValidatorGenerator : NvdlValidatorGenerator
27         {
28                 bool allow;
29
30                 public NvdlBuiltInValidatorGenerator (bool allow)
31                 {
32                         this.allow = allow;
33                 }
34
35                 public override XmlReader CreateValidator (XmlReader reader, XmlResolver resolver)
36                 {
37                         return new NvdlBuiltInValidationReader (reader, allow);
38                 }
39
40                 public override bool AddOption (string name, string arg)
41                 {
42                         return false;
43                 }
44         }
45
46         internal class NvdlBuiltInValidationReader : XmlDefaultReader
47         {
48                 bool allow;
49
50                 public NvdlBuiltInValidationReader (XmlReader reader, bool allow)
51                         : base (reader)
52                 {
53                         this.allow = allow;
54                 }
55
56                 public override bool Read ()
57                 {
58                         if (!Reader.Read ())
59                                 return false;
60                         if (!allow)
61                                 throw new NvdlValidationException (String.Format ("The NVDL script does not allow an element whose namespace is '{0}'", Reader.NamespaceURI), Reader as IXmlLineInfo);
62                         return true;
63                 }
64         }
65 }