2 using System.Collections;
3 using System.Collections.Specialized;
7 namespace Commons.Xml.Nvdl
9 public class NvdlValidationProvider
11 NvdlValidate validate;
15 public virtual NvdlValidatorGenerator CreateGenerator (NvdlValidate validate, string schemaType, NvdlConfig config)
17 this.validate = validate;
18 this.schema_type = schemaType;
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))
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);
39 r.Read (); // Skip "schema" element
41 if (r.NodeType == XmlNodeType.Element)
44 schema = GetSchemaXmlStream (r.ReadString (), config, validate);
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)
70 public string GetSchemaUri (NvdlValidate validate)
72 if (validate.SchemaUri != null)
73 return validate.SchemaUri;
74 if (validate.SchemaBody == 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;
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));
93 XmlTextReader xtr = new XmlTextReader (uri != null ? uri.ToString () : String.Empty, stream);
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 (
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)