2 using System.Collections;
3 using System.Collections.Specialized;
7 namespace Commons.Xml.Nvdl
9 public class NvdlValidationProvider
11 public virtual NvdlValidatorGenerator CreateGenerator (NvdlValidate validate, string schemaType, NvdlConfig config)
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))
20 string schemaUri = validate.SchemaUri;
21 XmlElement schemaBody = validate.SchemaBody;
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);
28 else if (validate.SchemaBody != null) {
29 XmlReader r = new XmlNodeReader (schemaBody);
31 r.Read (); // Skip "schema" element
33 if (r.NodeType == XmlNodeType.Element)
36 schema = GetSchemaXmlStream (r.ReadString (), config, validate);
42 return CreateGenerator (schema, config);
45 public virtual NvdlValidatorGenerator CreateGenerator (XmlReader schema, NvdlConfig config)
50 public string GetSchemaUri (NvdlValidate validate)
52 if (validate.SchemaUri != null)
53 return validate.SchemaUri;
54 if (validate.SchemaBody == 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;
62 private static XmlReader GetSchemaXmlStream (string schemaUri, NvdlConfig config, NvdlValidate validate)
64 XmlResolver r = config.XmlResolverInternal;
67 Uri uri = r.ResolveUri (null, validate.SchemaUri);
68 Stream stream = (Stream) r.GetEntity (
69 uri, null, typeof (Stream));
72 XmlTextReader xtr = new XmlTextReader (uri != null ? uri.ToString () : String.Empty, stream);
79 public abstract class NvdlValidatorGenerator
81 // creates individual validator with schema
82 // (which should be provided in derived constructor).
83 public abstract XmlReader CreateValidator (XmlReader reader,
84 XmlResolver resolver);
86 public virtual XmlReader CreateAttributeValidator (
90 throw new NotSupportedException ();
93 public abstract bool AddOption (string name, string arg);