* roottypes.cs: Rename from tree.cs.
[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                 NvdlValidate validate;
12                 string schema_type;
13                 NvdlConfig config;
14
15                 public virtual NvdlValidatorGenerator CreateGenerator (NvdlValidate validate, string schemaType, NvdlConfig config)
16                 {
17                         this.validate = validate;
18                         this.schema_type = schemaType;
19                         this.config = config;
20
21                         XmlReader schema = null;
22                         // FIXME: we need a bit more strict check.
23                         if (schemaType.Length < 5 ||
24                                 !schemaType.EndsWith ("xml") ||
25                                 Char.IsLetter (schemaType, schemaType.Length - 4))
26                                 return null;
27
28                         string schemaUri = validate.SchemaUri;
29                         XmlElement schemaBody = validate.SchemaBody;
30
31                         if (schemaUri != null) {
32                                 if (schemaBody != null)
33                                         throw new NvdlCompileException ("Both 'schema' attribute and 'schema' element are specified in a 'validate' element.", validate);
34                                 schema = GetSchemaXmlStream (schemaUri, config, validate);
35                         }
36                         else if (validate.SchemaBody != null) {
37                                 XmlReader r = new XmlNodeReader (schemaBody);
38                                 r.MoveToContent ();
39                                 r.Read (); // Skip "schema" element
40                                 r.MoveToContent ();
41                                 if (r.NodeType == XmlNodeType.Element)
42                                         schema = r;
43                                 else
44                                         schema = GetSchemaXmlStream (r.ReadString (), config, validate);
45                         }
46
47                         if (schema == null)
48                                 return null;
49
50                         return CreateGenerator (schema, config);
51                 }
52
53                 public NvdlValidate ValidateAction {
54                         get { return validate; }
55                 }
56
57                 public NvdlConfig Config {
58                         get { return config; }
59                 }
60
61                 public string SchemaType {
62                         get { return schema_type; }
63                 }
64
65                 public virtual NvdlValidatorGenerator CreateGenerator (XmlReader schema, NvdlConfig config)
66                 {
67                         return null;
68                 }
69
70                 public string GetSchemaUri (NvdlValidate validate)
71                 {
72                         if (validate.SchemaUri != null)
73                                 return validate.SchemaUri;
74                         if (validate.SchemaBody == null)
75                                 return null;
76                         for (XmlNode n = validate.SchemaBody.FirstChild; n != null; n = n.NextSibling)
77                                 if (n.NodeType == XmlNodeType.Element)
78                                         return null; // not a URI
79                         return validate.SchemaBody.InnerText;
80                 }
81
82                 private static XmlReader GetSchemaXmlStream (string schemaUri, NvdlConfig config, NvdlValidate validate)
83                 {
84                         XmlResolver r = config.XmlResolverInternal;
85                         if (r == null)
86                                 return null;
87                         Uri baseUri = r.ResolveUri (null, validate.SourceUri);
88                         Uri uri = r.ResolveUri (baseUri, validate.SchemaUri);
89                         Stream stream = (Stream) r.GetEntity (
90                                 uri, null, typeof (Stream));
91                         if (stream == null)
92                                 return null;
93                         XmlTextReader xtr = new XmlTextReader (uri != null ? uri.ToString () : String.Empty, stream);
94                         xtr.XmlResolver = r;
95                         xtr.MoveToContent ();
96                         return xtr;
97                 }
98         }
99
100         public abstract class NvdlValidatorGenerator
101         {
102                 // creates individual validator with schema
103                 // (which should be provided in derived constructor).
104                 public abstract XmlReader CreateValidator (XmlReader reader, 
105                         XmlResolver resolver);
106
107                 public virtual XmlReader CreateAttributeValidator (
108                         XmlReader reader,
109                         XmlResolver resolver)
110                 {
111                         throw new NotSupportedException ();
112                 }
113
114                 public abstract bool AddOption (string name, string arg);
115
116                 public virtual bool HandleError (Exception ex, XmlReader reader, string nvdlLocation)
117                 {
118                         return false;
119                 }
120         }
121 }