01ff911aeb51bf4d52bb0e67fbee95345104e786
[mono.git] / mcs / class / Commons.Xml.Relaxng / Commons.Xml.Nvdl / NvdlValidationProvider.cs
1 using System;
2 using System.Collections;
3 using System.Collections.Specialized;
4 using System.IO;
5 using System.Xml;
6
7 namespace Commons.Xml.Nvdl
8 {
9         public class NvdlValidationProvider
10         {
11                 public virtual NvdlValidatorGenerator CreateGenerator (NvdlValidate validate, string schemaType, NvdlConfig config)
12                 {
13                         XmlReader schema = null;
14                         // FIXME: we need a bit more strict check.
15                         if (schemaType.Length < 5 ||
16                                 !schemaType.EndsWith ("xml") ||
17                                 Char.IsLetter (schemaType, schemaType.Length - 4))
18                                 return null;
19
20                         string schemaUri = validate.SchemaUri;
21                         XmlElement schemaBody = validate.SchemaBody;
22
23                         if (schemaUri != null) {
24                                 if (schemaBody != null)
25                                         throw new NvdlCompileException ("Both 'schema' attribute and 'schema' element are specified in a 'validate' element.", validate);
26                                 schema = GetSchemaXmlStream (schemaUri, config, validate);
27                         }
28                         else if (validate.SchemaBody != null) {
29                                 XmlReader r = new XmlNodeReader (schemaBody);
30                                 r.MoveToContent ();
31                                 r.Read (); // Skip "schema" element
32                                 r.MoveToContent ();
33                                 if (r.NodeType == XmlNodeType.Element)
34                                         schema = r;
35                                 else
36                                         schema = GetSchemaXmlStream (r.ReadString (), config, validate);
37                         }
38
39                         if (schema == null)
40                                 return null;
41
42                         return CreateGenerator (schema, config);
43                 }
44
45                 public virtual NvdlValidatorGenerator CreateGenerator (XmlReader schema, NvdlConfig config)
46                 {
47                         return null;
48                 }
49
50                 public string GetSchemaUri (NvdlValidate validate)
51                 {
52                         if (validate.SchemaUri != null)
53                                 return validate.SchemaUri;
54                         if (validate.SchemaBody == null)
55                                 return null;
56                         for (XmlNode n = validate.SchemaBody.FirstChild; n != null; n = n.NextSibling)
57                                 if (n.NodeType == XmlNodeType.Element)
58                                         return null; // not a URI
59                         return validate.SchemaBody.InnerText;
60                 }
61
62                 private static XmlReader GetSchemaXmlStream (string schemaUri, NvdlConfig config, NvdlValidate validate)
63                 {
64                         XmlResolver r = config.XmlResolverInternal;
65                         if (r == null)
66                                 return null;
67                         Uri uri = r.ResolveUri (null, validate.SchemaUri);
68                         Stream stream = (Stream) r.GetEntity (
69                                 uri, null, typeof (Stream));
70                         if (stream == null)
71                                 return null;
72                         XmlTextReader xtr = new XmlTextReader (uri != null ? uri.ToString () : String.Empty, stream);
73                         xtr.XmlResolver = r;
74                         xtr.MoveToContent ();
75                         return xtr;
76                 }
77         }
78
79         public abstract class NvdlValidatorGenerator
80         {
81                 // creates individual validator with schema
82                 // (which should be provided in derived constructor).
83                 public abstract XmlReader CreateValidator (XmlReader reader, 
84                         XmlResolver resolver);
85
86                 public virtual XmlReader CreateAttributeValidator (
87                         XmlReader reader,
88                         XmlResolver resolver)
89                 {
90                         throw new NotSupportedException ();
91                 }
92
93                 public abstract bool AddOption (string name, string arg);
94         }
95 }