2005-03-07 Atsushi Enomoto <atsushi@ximian.com>
authorAtsushi Eno <atsushieno@gmail.com>
Mon, 7 Mar 2005 05:31:56 +0000 (05:31 -0000)
committerAtsushi Eno <atsushieno@gmail.com>
Mon, 7 Mar 2005 05:31:56 +0000 (05:31 -0000)
* README :
  more description.
* NvdlBuiltInValidationProvider.cs :
  When it is "allow", it should not always return false.
* NvdlFilteredXmlReader.cs :
  Added support for PlanAtt validation.
* NvdlConfig.cs :
  Removed unused code.
* NvdlSimplified.cs :
  Attribute default rule is "attach", not "allow". Removed unused
  code. Added attribute validation support.
* NvdlValidationProvider.cs :
  Extended MIME type check to "*/*-xml" (not only text/xml).
  Added CreateAttributeValidator() method for "schema rewriting for
  attribute sections" support (FCD spec 8.7.3).

svn path=/trunk/mcs/; revision=41499

mcs/class/Commons.Xml.Relaxng/Commons.Xml.Nvdl/ChangeLog
mcs/class/Commons.Xml.Relaxng/Commons.Xml.Nvdl/NvdlBuiltInValidationProvider.cs
mcs/class/Commons.Xml.Relaxng/Commons.Xml.Nvdl/NvdlConfig.cs
mcs/class/Commons.Xml.Relaxng/Commons.Xml.Nvdl/NvdlFilteredXmlReader.cs
mcs/class/Commons.Xml.Relaxng/Commons.Xml.Nvdl/NvdlSimplified.cs
mcs/class/Commons.Xml.Relaxng/Commons.Xml.Nvdl/NvdlValidationProvider.cs

index 707746ff9cdf98dd932d58866ffd7c69901de442..8a0f8c6b360269fbc840d8ec1fbfbdaaf99323b0 100644 (file)
@@ -1,3 +1,21 @@
+2005-03-07  Atsushi Enomoto <atsushi@ximian.com>
+
+       * README :
+         more description.
+       * NvdlBuiltInValidationProvider.cs :
+         When it is "allow", it should not always return false.
+       * NvdlFilteredXmlReader.cs :
+         Added support for PlanAtt validation.
+       * NvdlConfig.cs :
+         Removed unused code.
+       * NvdlSimplified.cs :
+         Attribute default rule is "attach", not "allow". Removed unused
+         code. Added attribute validation support.
+       * NvdlValidationProvider.cs :
+         Extended MIME type check to "*/*-xml" (not only text/xml).
+         Added CreateAttributeValidator() method for "schema rewriting for
+         attribute sections" support (FCD spec 8.7.3).
+
 2005-03-06  Atsushi Enomoto <atsushi@ximian.com>
 
        * NvdlFilteredXmlReader.cs : code refactory.
index ec1245640216750123fe313ce1edb042459e3f46..7ff4973881c667a6ce8b24abab27a7bd6ae4a404 100755 (executable)
@@ -55,9 +55,11 @@ namespace Commons.Xml.Nvdl
 
                public override bool Read ()
                {
-                       if (Reader.Read () && !allow)
+                       if (!Reader.Read ())
+                               return false;
+                       if (!allow)
                                throw new NvdlValidationException (String.Format ("The NVDL script does not allow an element whose namespace is '{0}'", Reader.NamespaceURI), Reader as IXmlLineInfo);
-                       return false;
+                       return true;
                }
        }
 }
index d6bbe2af53ab75388ad61feaa6649a055601a05e..13aa8230ed9159252153d29521ae7e5579de3460 100755 (executable)
@@ -48,72 +48,5 @@ namespace Commons.Xml.Nvdl
 
                        throw new NvdlCompileException (String.Format ("Either schema type '{0}' or the target schema document is not supported in this configuration. Add custom provider that supports this schema type.", schemaType), validate);
                }
-
-/*
-               public virtual NvdlValidatorGenerator GetGenerator (string mimeType, string schemaAttribute, XmlElement schemaElement, NvdlValidate validate)
-               {
-                       XmlReader schemaReader = null;
-                       if (schemaType == "text/xml") {
-                               if (schemaAttribute != null) {
-                                       if (schemaElement != null)
-                                               throw new NvdlCompileException ("Both 'schema' attribute and 'schema' element are specified in a 'validate' element.", validate);
-                               return GetSchemaXmlStream (schemaUri, config, validate);
-                               else if (validate.SchemaBody != null) {
-                                       XmlReader r = new XmlNodeReader (validate.SchemaBody);
-                                       r.MoveToContent ();
-                                       r.Read (); // Skip "schema" element
-                                       r.MoveToContent ();
-                                       if (r.NodeType == XmlNodeType.Element)
-                                               return r;
-                                       else
-                                               return GetSchemaXmlStream (r.ReadString (), config, validate);
-                               }
-                               else
-                                       return null;
-                       }
-                       else
-                               throw new NvdlCompileException (String.Format ("Either MIME type '{0}' or the target schema document is not supported.", schemaType), validate);
-
-                       schemaReader.MoveToContent ();
-
-                       NvdlValidationProvider provider =
-                               ctx.Config.GetProvider (schemaReader.NamespaceURI);
-
-                       generator = provider.CreateGenerator (schemaReader, ctx.Config.XmlResolverInternal);
-
-                       foreach (NvdlOption option in validate.Options) {
-                               bool mustSupport = option.MustSupport != null ?
-                                       XmlConvert.ToBoolean (option.MustSupport) : false;
-                               if (!generator.AddOption (option.Name,
-                                       option.Arg) && mustSupport)
-                                       throw new NvdlCompileException (String.Format ("Option '{0}' with argument '{1}' is not supported for schema type '{2}'.",
-                                               option.Name, option.Arg,
-                                               schemaType), validate);
-                       }
-               }
-
-               public static XmlReader GetSchemaXmlStream (string schemaUri, NvdlConfig config, NvdlValidate validate)
-               {
-                       XmlResolver r = config.Resolver;
-                       if (r == null)
-                               return null;
-                       Uri uri = r.ResolveUri (null, validate.schemaUri);
-                       Stream stream = r.GetEntity (uri, null, typeof (Stream));
-                       if (stream == null)
-                               return null;
-                       XmlTextReader xtr = new XmlTextReader (uri != null ? uri.ToString () : String.Empty, stream);
-                       xtr.XmlResolver = r;
-                       xtr.MoveToContent ();
-                       return xtr;
-               }
-
-               private bool ElementHasElementChild (XmlElement el)
-               {
-                       foreach (XmlNode n in el.ChildNodes)
-                               if (n.NodeType == XmlNodeType.Element)
-                                       return true;
-                       return false;
-               }
-*/
        }
 }
index 4775f08d6c513d72b67aeca4ffc19e5a01338e59..29f8dc2148d1bc1096cd285d67edd92be3368745 100644 (file)
@@ -5,6 +5,29 @@ using System.Xml;
 
 namespace Commons.Xml.Nvdl
 {
+#if false
+       internal class NvdlFilteredXmlReader : XmlDefaultReader
+       {
+               public NvdlFilteredXmlReader (XmlReader reader,
+                       NvdlValidateInterp validate)
+                       : base (reader)
+               {
+               }
+
+               public bool Read ()
+               {
+                       return !Reader.EOF;
+               }
+
+               public void AttachPlaceHolder ()
+               {
+               }
+
+               public void DetachPlaceHolder ()
+               {
+               }
+       }
+#else
        internal class NvdlFilteredXmlReader : XmlReader
        {
                int placeHolderDepth = -1;
@@ -18,6 +41,9 @@ namespace Commons.Xml.Nvdl
                AttributeInfo [] attributes = new AttributeInfo [10];
                int attributeCount;
 
+               // PlanAtt validation cache.
+               Hashtable attributeValidators = new Hashtable ();
+
                class AttributeInfo
                {
                        public string LocalName;
@@ -91,22 +117,22 @@ namespace Commons.Xml.Nvdl
                        if (reader.EOF)
                                return false;
 
+                       MoveToElement ();
+                       attributeCount = 0;
+
                        if (nextPlaceHolder != XmlNodeType.None) {
                                placeHolder = nextPlaceHolder;
                                nextPlaceHolder = XmlNodeType.None;
                                return true;
                        }
 
-                       MoveToElement ();
-                       attributeCount = 0;
-
                        if (!reader.MoveToFirstAttribute ())
                                return true;
+
+                       // Attribute rule application
+                       attributeValidators.Clear ();
                        do {
-                               if (reader.NamespaceURI == String.Empty) {
-                                       AddAttribute ();
-                                       continue;
-                               }
+                               // FIXME: could be more efficient
                                SimpleRule rule = FindAttributeRule (
                                        reader.NamespaceURI,
                                        validate.CreatedMode);
@@ -118,11 +144,18 @@ namespace Commons.Xml.Nvdl
                                                AddAttribute ();
                                        if (ra != null)
                                                continue;
-                                       // FIXME: validate global attribute (PlanAtt).
-                                       SimpleValidate v = a as SimpleValidate;
+                                       attributeValidators [reader.NamespaceURI] = a;
                                }
                        } while (reader.MoveToNextAttribute ());
                        reader.MoveToElement ();
+
+                       if (attributeValidators.Count > 0) {
+                               foreach (string ns in attributeValidators.Keys) {
+                                       ((SimpleValidate) attributeValidators [
+                                               ns]).ValidateAttributes (reader, ns);
+                               }
+                       }
+
                        return true;
                }
 
@@ -523,5 +556,6 @@ namespace Commons.Xml.Nvdl
                        }
                }
        }
+#endif
 }
 
index 1f70fa95a615272d29a5b0ae5ea1913f5b4f9fa4..861d6662b5e80f8502cdce2c5b55b8b076834129 100755 (executable)
@@ -256,12 +256,12 @@ namespace Commons.Xml.Nvdl
                                ann.LineNumber = this.LineNumber;
                                ann.LinePosition = this.LinePosition;
 
-                               NvdlAllow allow = new NvdlAllow ();
-                               allow.SourceUri = this.SourceUri;
-                               allow.LineNumber = this.LineNumber;
-                               allow.LinePosition = this.LinePosition;
+                               NvdlAttach attach = new NvdlAttach ();
+                               attach.SourceUri = this.SourceUri;
+                               attach.LineNumber = this.LineNumber;
+                               attach.LinePosition = this.LinePosition;
                                ann.Match = NvdlRuleTarget.Attributes;
-                               ann.Actions.Add (allow);
+                               ann.Actions.Add (attach);
 
                                al.Add (new SimpleRule (ann, true, ctx));
                        }
@@ -542,59 +542,6 @@ namespace Commons.Xml.Nvdl
                        // 6.4.7
                        generator = ctx.Config.GetGenerator (validate,
                                ctx.Rules.SchemaType);
-/*
-                       this.resolver = ctx.Config.XmlResolverInternal;
-
-                       // 6.4.7
-                       string schemaType = validate.SchemaType;
-                       if (schemaType == null)
-                               schemaType = ctx.Rules.SchemaType;
-                       if (schemaType == null && validate.SchemaBody != null && !ElementHasElementChild (validate.SchemaBody))
-                               schemaType = validate.SchemaBody.InnerText;
-                       if (schemaType == null)
-                               schemaType = "text/xml";
-
-                       // FIXME: this part must be totally rewritten.
-
-                       XmlReader schemaReader = null;
-                       if (schemaType == "text/xml") {
-                               if (validate.SchemaUri != null) {
-                                       if (validate.SchemaBody != null)
-                                               throw new NvdlCompileException ("Both 'schema' attribute and 'schema' element are specified in a 'validate' element.", validate);
-                                       // FIXME: use NvdlConfig
-                                       schemaReader = new XmlTextReader (validate.SchemaUri);
-                               }
-                               else if (validate.SchemaBody != null) {
-                                       schemaReader = new XmlNodeReader (validate.SchemaBody);
-                                       schemaReader.MoveToContent ();
-                                       schemaReader.Read (); // Skip "schema" element
-                               }
-                               else
-                                       throw new NvdlCompileException ("Neither 'schema' attribute nor 'schema' element is specified in a 'validate' element.", validate);
-                       }
-                       else
-                               throw new NvdlCompileException (String.Format ("MIME type '{0}' is not supported at this moment.", schemaType), validate);
-
-                       schemaReader.MoveToContent ();
-
-                       NvdlValidationProvider provider =
-                               ctx.Config.GetProvider (schemaReader.NamespaceURI);
-
-                       if (provider == null)
-                               throw new NvdlCompileException (String.Format ("Schema type '{0}' is not supported in this configuration. Use custom provider that supports this schema type.", schemaType), validate);
-
-                       generator = provider.CreateGenerator (schemaReader, ctx.Config.XmlResolverInternal);
-
-                       foreach (NvdlOption option in validate.Options) {
-                               bool mustSupport = option.MustSupport != null ?
-                                       XmlConvert.ToBoolean (option.MustSupport) : false;
-                               if (!generator.AddOption (option.Name,
-                                       option.Arg) && mustSupport)
-                                       throw new NvdlCompileException (String.Format ("Option '{0}' with argument '{1}' is not supported for schema type '{2}'.",
-                                               option.Name, option.Arg,
-                                               schemaType), validate);
-                       }
-*/
                }
 
                internal NvdlValidatorGenerator Generator {
@@ -609,6 +556,25 @@ namespace Commons.Xml.Nvdl
                {
                        return generator.CreateValidator (reader, resolver);
                }
+
+               public void ValidateAttributes (XmlReader reader, string ns)
+               {
+                       XmlDocument doc = new XmlDocument ();
+                       XmlElement el = doc.CreateElement ("virtualElement",
+                               Nvdl.PlaceHolderNamespace);
+                       for (int i = 0; i < reader.AttributeCount; i++) {
+                               reader.MoveToAttribute (i);
+                               if (reader.NamespaceURI != ns)
+                                       continue;
+                               el.SetAttribute (reader.LocalName,
+                                       reader.NamespaceURI, reader.Value);
+                       }
+                       reader.MoveToElement ();
+                       XmlReader r = generator.CreateAttributeValidator (
+                               new XmlNodeReader (el), resolver);
+                       while (!r.EOF)
+                               r.Read ();
+               }
        }
 
        internal class SimpleResultAction : SimpleAction
index 3f46033b3a28f336816c751da133da043f23c3b5..01ff911aeb51bf4d52bb0e67fbee95345104e786 100755 (executable)
@@ -11,7 +11,10 @@ namespace Commons.Xml.Nvdl
                public virtual NvdlValidatorGenerator CreateGenerator (NvdlValidate validate, string schemaType, NvdlConfig config)
                {
                        XmlReader schema = null;
-                       if (schemaType != "text/xml")
+                       // FIXME: we need a bit more strict check.
+                       if (schemaType.Length < 5 ||
+                               !schemaType.EndsWith ("xml") ||
+                               Char.IsLetter (schemaType, schemaType.Length - 4))
                                return null;
 
                        string schemaUri = validate.SchemaUri;
@@ -77,7 +80,15 @@ namespace Commons.Xml.Nvdl
        {
                // creates individual validator with schema
                // (which should be provided in derived constructor).
-               public abstract XmlReader CreateValidator (XmlReader reader, XmlResolver resolver);
+               public abstract XmlReader CreateValidator (XmlReader reader, 
+                       XmlResolver resolver);
+
+               public virtual XmlReader CreateAttributeValidator (
+                       XmlReader reader,
+                       XmlResolver resolver)
+               {
+                       throw new NotSupportedException ();
+               }
 
                public abstract bool AddOption (string name, string arg);
        }