2006-04-03 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / Commons.Xml.Relaxng / Commons.Xml.Nvdl / NvdlXsdSupport.cs
1 using System;
2 using System.Collections;
3 using System.Collections.Specialized;
4 using System.Xml;
5 using System.Xml.Schema;
6
7 namespace Commons.Xml.Nvdl
8 {
9         public class NvdlXsdValidatorProvider : NvdlValidationProvider
10         {
11                 public override NvdlValidatorGenerator CreateGenerator (
12                         XmlReader reader, NvdlConfig config)
13                 {
14                         if (reader.NamespaceURI != XmlSchema.Namespace)
15                                 return null;
16                         ArrayList al = new ArrayList ();
17                         while (!reader.EOF) {
18                                 if (reader.NodeType != XmlNodeType.Element) {
19                                         reader.Read ();
20                                         continue;
21                                 }
22                                 reader.MoveToContent ();
23                                 XmlSchema xs = XmlSchema.Read (reader, null);
24                                 xs.Compile (null, config.XmlResolverInternal);
25                                 al.Add (xs);
26                                 reader.Read ();
27                         }
28                         return new NvdlXsdValidatorGenerator (al.ToArray (typeof (XmlSchema)) as XmlSchema []);
29                 }
30         }
31
32         internal class NvdlXsdValidatorGenerator : NvdlValidatorGenerator
33         {
34                 XmlSchema [] schemas;
35
36                 public NvdlXsdValidatorGenerator (XmlSchema [] schemas)
37                 {
38                         this.schemas = schemas;
39                 }
40
41                 public override XmlReader CreateValidator (XmlReader reader,
42                         XmlResolver resolver)
43                 {
44 #if NET_2_0
45                         XmlReaderSettings s = new XmlReaderSettings ();
46                         s.ValidationType = ValidationType.Schema;
47                         // do not allow inline schema and schemaLocation.
48                         s.ValidationFlags = XmlSchemaValidationFlags.ProcessIdentityConstraints;
49                         s.XmlResolver = resolver;
50                         foreach (XmlSchema schema in schemas)
51                                 s.Schemas.Add (schema);
52                         return XmlReader.Create (reader, s);
53 #else
54                         XmlValidatingReader xvr =
55                                 new XmlValidatingReader (reader);
56                         xvr.XmlResolver = resolver;
57                         foreach (XmlSchema schema in schemas)
58                                 xvr.Schemas.Add (schema);
59                         return xvr;
60 #endif
61                 }
62
63                 public override bool AddOption (string name, string arg)
64                 {
65                         return false;
66                 }
67
68                 public override bool HandleError (Exception ex, XmlReader reader, string nvdlLocation)
69                 {
70                         if (ex is XmlSchemaException)
71                                 throw new NvdlInstanceValidationException (String.Format ("XML schema validation error occured as a part of NVDL validation."), ex, this, nvdlLocation);
72                         return false;
73                 }
74         }
75 }