[mapguide-commits] r5699 - in trunk/Tools/Maestro: Maestro.Base/Editor OSGeo.MapGuide.MaestroAPI

svn_mapguide at osgeo.org svn_mapguide at osgeo.org
Wed Apr 13 07:38:06 EDT 2011


Author: jng
Date: 2011-04-13 04:38:06 -0700 (Wed, 13 Apr 2011)
New Revision: 5699

Modified:
   trunk/Tools/Maestro/Maestro.Base/Editor/XmlEditor.cs
   trunk/Tools/Maestro/Maestro.Base/Editor/XmlEditorDialog.cs
   trunk/Tools/Maestro/OSGeo.MapGuide.MaestroAPI/XmlValidator.cs
Log:
#1662: The validation against local xsds feature (#1658) breaks down when validating xml content against a schema that *includes* other xml schemas. This submission hard-codes the list of known xsd dependencies and will load them into the XML validator if the content being validated matches (eg. LayerDefinition, which includes PlatformCommon and SymbolDefinition)

Also refactor the redundant XML validation logic into a reusable XmlValidator.ValidateResourceXmlContent() utility method


Modified: trunk/Tools/Maestro/Maestro.Base/Editor/XmlEditor.cs
===================================================================
--- trunk/Tools/Maestro/Maestro.Base/Editor/XmlEditor.cs	2011-04-12 12:22:01 UTC (rev 5698)
+++ trunk/Tools/Maestro/Maestro.Base/Editor/XmlEditor.cs	2011-04-13 11:38:06 UTC (rev 5699)
@@ -55,105 +55,9 @@
 
         private void ValidateXml(out string[] errors, out string[] warnings)
         {
-            errors = new string[0];
-            warnings = new string[0];
-
-            List<string> err = new List<string>();
-            List<string> warn = new List<string>();
-
-            var res = this.Resource;
-
-            //Test for well-formedness
-            try
-            {
-                XmlDocument doc = new XmlDocument();
-                doc.LoadXml(editor.XmlContent);
-            }
-            catch (XmlException ex)
-            {
-                err.Add(ex.Message);
-            }
-
-            //If strongly-typed, test that this is serializable
-            if (res.IsStronglyTyped)
-            {
-                try
-                {
-                    //Test by simply attempting to deserialize the current xml content
-                    using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(editor.XmlContent)))
-                    {
-                        //Use original resource type to determine how to deserialize
-                        var obj = ResourceTypeRegistry.Deserialize(res.ResourceType, ms);
-                    }
-                }
-                catch (Exception ex)
-                {
-                    err.Add(ex.Message);
-                }
-            }
-
-            //Finally verify the content itself
-            var xml = this.XmlContent;
-            var xsd = GetXsd(res.ValidatingSchema);
-            var validator = new XmlValidator();
-            using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(xml)))
-            {
-                validator.Validate(ms, xsd);
-            }
-
-            err.AddRange(validator.ValidationErrors);
-            warn.AddRange(validator.ValidationWarnings);
-
-            /*
-            var xml = this.XmlContent;
-            var config = new XmlReaderSettings();
-            
-            config.ValidationType = ValidationType.Schema;
-            config.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
-            config.ValidationFlags |= XmlSchemaValidationFlags.ProcessInlineSchema;
-            config.ValidationFlags |= XmlSchemaValidationFlags.ProcessSchemaLocation;
-            //This will trap all the errors and warnings that are raised
-            config.ValidationEventHandler += (s, e) =>
-            {
-                if (e.Severity == XmlSeverityType.Warning)
-                {
-                    warn.Add(e.Message);
-                }
-                else
-                {
-                    err.Add(e.Message);
-                }
-            };
-
-            using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(xml)))
-            {
-                using (var reader = XmlReader.Create(ms, config))
-                {
-                    while (reader.Read()) { } //Trigger the validation
-                }
-            }*/
-
-            errors = err.ToArray();
-            warnings = warn.ToArray();
+            XmlValidator.ValidateResourceXmlContent(editor.XmlContent, this.XsdPath, out errors, out warnings);
         }
 
-        private XmlSchema GetXsd(string xsdFile)
-        {
-            string path = xsdFile;
-
-            if (!string.IsNullOrEmpty(this.XsdPath))
-                path = Path.Combine(this.XsdPath, xsdFile);
-
-            if (File.Exists(path))
-            {
-                ValidationEventHandler handler = (s, e) =>
-                {
-                };
-                return XmlSchema.Read(File.OpenRead(path), handler);
-            }
-            return null;
-        }
-
         private IEditorService _edSvc;
 
         protected override void Bind(IEditorService service)
@@ -187,6 +91,20 @@
             //Put through ValidationResultSet to weed out redundant messages
             var set = new ValidationResultSet(issues);
 
+            try
+            {
+                var res = ResourceTypeRegistry.Deserialize(editor.XmlContent);
+                var context = new ResourceValidationContext(_edSvc.ResourceService, _edSvc.FeatureService);
+                //We don't care about dependents, we just want to validate *this* resource
+                var resIssues = ResourceValidatorSet.Validate(context, res, false);
+                set.AddIssues(resIssues);
+            }
+            catch 
+            { 
+                //This can fail because the XML may be for something that Maestro does not offer a strongly-typed class for yet.
+                //So the XML may be legit, just not for this version of Maestro that is doing the validating
+            }
+
             //Only care about errors. Warnings and other types should not derail us from saving
             return set.GetAllIssues(ValidationStatus.Error);
         }

Modified: trunk/Tools/Maestro/Maestro.Base/Editor/XmlEditorDialog.cs
===================================================================
--- trunk/Tools/Maestro/Maestro.Base/Editor/XmlEditorDialog.cs	2011-04-12 12:22:01 UTC (rev 5698)
+++ trunk/Tools/Maestro/Maestro.Base/Editor/XmlEditorDialog.cs	2011-04-13 11:38:06 UTC (rev 5699)
@@ -130,86 +130,7 @@
 
         private void ValidateXml(out string[] errors, out string[] warnings)
         {
-            errors = new string[0];
-            warnings = new string[0];
-
-            List<string> err = new List<string>();
-            List<string> warn = new List<string>();
-
-            var res = _edSvc.GetEditedResource();
-
-            //Test for well-formedness
-            try
-            {
-                XmlDocument doc = new XmlDocument();
-                doc.LoadXml(_ed.XmlContent);
-            }
-            catch (XmlException ex)
-            {
-                err.Add(ex.Message);
-            }
-
-            //If strongly-typed, test that this is serializable
-            if (res.IsStronglyTyped)
-            {
-                try
-                {
-                    //Test by simply attempting to deserialize the current xml content
-                    using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(_ed.XmlContent)))
-                    {
-                        //Use original resource type to determine how to deserialize
-                        var obj = ResourceTypeRegistry.Deserialize(res.ResourceType, ms);
-                    }
-                }
-                catch (Exception ex)
-                {
-                    err.Add(ex.Message);
-                }
-            }
-
-            //Finally verify the content itself
-            var xml = this.XmlContent;
-            var xsd = GetXsd(res.ValidatingSchema);
-            var validator = new XmlValidator();
-            using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(xml)))
-            {
-                validator.Validate(ms, xsd);
-            }
-
-            err.AddRange(validator.ValidationErrors);
-            warn.AddRange(validator.ValidationWarnings);
-
-            /*
-            var xml = this.XmlContent;
-            var config = new XmlReaderSettings();
-            
-            config.ValidationType = ValidationType.Schema;
-            config.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
-            config.ValidationFlags |= XmlSchemaValidationFlags.ProcessInlineSchema;
-            config.ValidationFlags |= XmlSchemaValidationFlags.ProcessSchemaLocation;
-            //This will trap all the errors and warnings that are raised
-            config.ValidationEventHandler += (s, e) =>
-            {
-                if (e.Severity == XmlSeverityType.Warning)
-                {
-                    warn.Add(e.Message);
-                }
-                else
-                {
-                    err.Add(e.Message);
-                }
-            };
-
-            using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(xml)))
-            {
-                using (var reader = XmlReader.Create(ms, config))
-                {
-                    while (reader.Read()) { } //Trigger the validation
-                }
-            }*/
-
-            errors = err.ToArray();
-            warnings = warn.ToArray();
+            XmlValidator.ValidateResourceXmlContent(this.XmlContent, this.XsdPath, out errors, out warnings);
         }
 
         private void btnCancel_Click(object sender, EventArgs e)

Modified: trunk/Tools/Maestro/OSGeo.MapGuide.MaestroAPI/XmlValidator.cs
===================================================================
--- trunk/Tools/Maestro/OSGeo.MapGuide.MaestroAPI/XmlValidator.cs	2011-04-12 12:22:01 UTC (rev 5698)
+++ trunk/Tools/Maestro/OSGeo.MapGuide.MaestroAPI/XmlValidator.cs	2011-04-13 11:38:06 UTC (rev 5699)
@@ -30,6 +30,7 @@
 	using System.Text;
     using System.Collections.Generic;
     using System.Collections.ObjectModel;
+    using OSGeo.MapGuide.MaestroAPI.Resource;
 
 	///<summary>
 	/// Class that makes XSD validation
@@ -53,15 +54,18 @@
         /// Validates the specified XML.
         /// </summary>
         /// <param name="xml">The XML.</param>
-        /// <param name="xsd">The XSD.</param>
-		public void Validate(System.IO.Stream xml, XmlSchema xsd)
+        /// <param name="xsds">The array of <see cref="T:System.Xml.Schema.XmlSchema"/> objects to validate against.</param>
+		public void Validate(System.IO.Stream xml, XmlSchema[] xsds)
 		{
             this.warnings.Clear();
             this.errors.Clear();
 
             var config = new XmlReaderSettings();
-            if (xsd != null)
-                config.Schemas.Add(xsd);
+            if (xsds != null && xsds.Length > 0)
+            {
+                foreach(var xsd in xsds)
+                    config.Schemas.Add(xsd);
+            }
             config.ValidationType = ValidationType.Schema;
             config.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
             config.ValidationFlags |= XmlSchemaValidationFlags.ProcessInlineSchema;
@@ -85,5 +89,100 @@
                 while (reader.Read()) { } //Trigger the validation
             }
 		}
+
+        private static XmlSchema GetXsd(string xsdPath, string xsdFile)
+        {
+            string path = xsdFile;
+
+            if (!string.IsNullOrEmpty(xsdPath))
+                path = Path.Combine(xsdPath, xsdFile);
+
+            if (File.Exists(path))
+            {
+                ValidationEventHandler handler = (s, e) =>
+                {
+                };
+                return XmlSchema.Read(File.OpenRead(path), handler);
+            }
+            return null;
+        }
+
+        public static void ValidateResourceXmlContent(string xmlContent, string xsdPath, out string[] errors, out string[] warnings)
+        {
+            errors = new string[0];
+            warnings = new string[0];
+
+            List<string> err = new List<string>();
+            List<string> warn = new List<string>();
+
+            //Test for well-formedness
+            try
+            {
+                XmlDocument doc = new XmlDocument();
+                doc.LoadXml(xmlContent);
+            }
+            catch (XmlException ex)
+            {
+                err.Add(ex.Message);
+            }
+
+            IResource res = null;
+            //Test for serializablility
+            try
+            {
+                //Use original resource type to determine how to deserialize
+                res = ResourceTypeRegistry.Deserialize(xmlContent);
+            }
+            catch (Exception ex)
+            {
+                err.Add(ex.Message);
+            }
+
+            if (res != null)
+            {
+                //Finally verify the content itself
+                var xml = xmlContent;
+                var xsds = new Dictionary<string, XmlSchema>();
+                var xsd = GetXsd(xsdPath, res.ValidatingSchema);
+                if (xsd != null)
+                    xsds.Add(res.ValidatingSchema, xsd);
+
+                //HACK: Yes this is hard-coded because XmlSchema's dependency resolution sucks!
+
+                //Nearly all relevant xsds include this anyway so add it to the set
+                var pc = GetXsd(xsdPath, "PlatformCommon-1.0.0.xsd");
+                if (pc != null)
+                    xsds.Add("PlatformCommon-1.0.0.xsd", pc);
+
+                if (res.ResourceType == ResourceTypes.LayerDefinition)
+                {
+                    string version = res.ResourceVersion.ToString();
+                    if (version.StartsWith("1.1.0"))
+                    {
+                        var sym = GetXsd(xsdPath, "SymbolDefinition-1.0.0.xsd");
+                        if (sym != null)
+                            xsds.Add("SymbolDefinition-1.0.0.xsd", sym);
+                    }
+                    else if (version.StartsWith("1.2.0") || version.StartsWith("1.3.0"))
+                    {
+                        var sym = GetXsd(xsdPath, "SymbolDefinition-1.1.0.xsd");
+                        if (sym != null)
+                            xsds.Add("SymbolDefinition-1.1.0.xsd", sym);
+                    }
+                }
+
+                var validator = new XmlValidator();
+                using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(xml)))
+                {
+                    validator.Validate(ms, new List<XmlSchema>(xsds.Values).ToArray());
+                }
+
+                err.AddRange(validator.ValidationErrors);
+                warn.AddRange(validator.ValidationWarnings);
+            }
+
+            errors = err.ToArray();
+            warnings = warn.ToArray();
+        }
 	}
 }



More information about the mapguide-commits mailing list