In Test/System.Windows.Forms:
[mono-project.git] / mcs / class / Commons.Xml.Relaxng / Commons.Xml.Nvdl / NvdlValidationProvider.cs
blob0501b0e3b38b76548e498f38ba264b512b46f071
1 using System;
2 using System.Collections;
3 using System.Collections.Specialized;
4 using System.IO;
5 using System.Xml;
7 namespace Commons.Xml.Nvdl
9 public class NvdlValidationProvider
11 NvdlValidate validate;
12 string schema_type;
13 NvdlConfig config;
15 public virtual NvdlValidatorGenerator CreateGenerator (NvdlValidate validate, string schemaType, NvdlConfig config)
17 this.validate = validate;
18 this.schema_type = schemaType;
19 this.config = config;
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;
28 string schemaUri = validate.SchemaUri;
29 XmlElement schemaBody = validate.SchemaBody;
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);
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);
47 if (schema == null)
48 return null;
50 return CreateGenerator (schema, config);
53 public NvdlValidate ValidateAction {
54 get { return validate; }
57 public NvdlConfig Config {
58 get { return config; }
61 public string SchemaType {
62 get { return schema_type; }
65 public virtual NvdlValidatorGenerator CreateGenerator (XmlReader schema, NvdlConfig config)
67 return null;
70 public string GetSchemaUri (NvdlValidate validate)
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;
82 private static XmlReader GetSchemaXmlStream (string schemaUri, NvdlConfig config, NvdlValidate validate)
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;
100 public abstract class NvdlValidatorGenerator
102 // creates individual validator with schema
103 // (which should be provided in derived constructor).
104 public abstract XmlReader CreateValidator (XmlReader reader,
105 XmlResolver resolver);
107 public virtual XmlReader CreateAttributeValidator (
108 XmlReader reader,
109 XmlResolver resolver)
111 throw new NotSupportedException ();
114 public abstract bool AddOption (string name, string arg);
116 public virtual bool HandleError (Exception ex, XmlReader reader, string nvdlLocation)
118 return false;