Exemplo n.º 1
0
        /// <summary>
        /// Validates the content of the resource XML.
        /// </summary>
        /// <param name="xmlContent">Content of the XML.</param>
        /// <param name="xsdPath">The XSD path.</param>
        /// <param name="errors">The errors.</param>
        /// <param name="warnings">The warnings.</param>
        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)
            {
                if (!(ex is SerializationException))
                    err.Add(ex.Message);
                else
                    res = null;
            }

            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);
                else
                    return; //One or more schemas is not found. Cannot proceed. Let MG figure it out

                //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"); //NOXLATE
                if (pc != null)
                    xsds.Add("PlatformCommon-1.0.0.xsd", pc); //NOXLATE

                if (res.ResourceType == ResourceTypes.LayerDefinition)
                {
                    string version = res.ResourceVersion.ToString();
                    if (version.StartsWith("1.1.0")) //NOXLATE
                    {
                        var sym = GetXsd(xsdPath, "SymbolDefinition-1.0.0.xsd"); //NOXLATE
                        if (sym != null)
                            xsds.Add("SymbolDefinition-1.0.0.xsd", sym); //NOXLATE
                    }
                    else if (version.StartsWith("1.2.0") || version.StartsWith("1.3.0")) //NOXLATE
                    {
                        var sym = GetXsd(xsdPath, "SymbolDefinition-1.1.0.xsd"); //NOXLATE
                        if (sym != null)
                            xsds.Add("SymbolDefinition-1.1.0.xsd", sym); //NOXLATE
                    }
                    else if (version.StartsWith("2.3.0")) //NOXLATE
                    {
                        var wmd = GetXsd(xsdPath, "WatermarkDefinition-2.3.0.xsd"); //NOXLATE
                        if (wmd != null)
                            xsds.Add("WatermarkDefinition-2.3.0.xsd", wmd); //NOXLATE
                    }
                    else if (version.StartsWith("2.4.0")) //NOXLATE
                    {
                        var sym = GetXsd(xsdPath, "SymbolDefinition-2.4.0.xsd"); //NOXLATE
                        if (sym != null)
                            xsds.Add("SymbolDefinition-2.4.0.xsd", sym); //NOXLATE

                        var wmd = GetXsd(xsdPath, "WatermarkDefinition-2.4.0.xsd"); //NOXLATE
                        if (wmd != null)
                            xsds.Add("WatermarkDefinition-2.4.0.xsd", wmd); //NOXLATE
                    }
                }

                if (res.ResourceType == ResourceTypes.WatermarkDefinition)
                {
                    string version = res.ResourceVersion.ToString();
                    if (version.StartsWith("2.3.0")) //NOXLATE
                    {
                        var sym = GetXsd(xsdPath, "SymbolDefinition-1.1.0.xsd"); //NOXLATE
                        if (sym != null)
                            xsds.Add("SymbolDefinition-1.1.0.xsd", sym); //NOXLATE
                    }
                    else if (version.StartsWith("2.4.0"))
                    {
                        var sym = GetXsd(xsdPath, "SymbolDefinition-2.4.0.xsd"); //NOXLATE
                        if (sym != null)
                            xsds.Add("SymbolDefinition-2.4.0.xsd", sym); //NOXLATE
                    }
                }

                if (res.ResourceType == ResourceTypes.MapDefinition)
                {
                    string version = res.ResourceVersion.ToString();
                    if (version.StartsWith("2.3.0"))
                    {
                        var sym = GetXsd(xsdPath, "SymbolDefinition-1.1.0.xsd"); //NOXLATE
                        if (sym != null)
                            xsds.Add("SymbolDefinition-1.1.0.xsd", sym); //NOXLATE

                        var wmd = GetXsd(xsdPath, "WatermarkDefinition-2.3.0.xsd"); //NOXLATE
                        if (wmd != null)
                            xsds.Add("WatermarkDefinition-2.3.0.xsd", wmd); //NOXLATE
                    }
                    else if (version.StartsWith("2.4.0"))
                    {
                        var sym = GetXsd(xsdPath, "SymbolDefinition-2.4.0.xsd"); //NOXLATE
                        if (sym != null)
                            xsds.Add("SymbolDefinition-2.4.0.xsd", sym); //NOXLATE

                        var wmd = GetXsd(xsdPath, "WatermarkDefinition-2.4.0.xsd"); //NOXLATE
                        if (wmd != null)
                            xsds.Add("WatermarkDefinition-2.4.0.xsd", wmd); //NOXLATE
                    }
                }

                string xsdName = res.ResourceType.ToString() + "-" + res.ResourceVersion.ToString() + ".xsd"; //NOXLATE
                if (!xsds.ContainsKey(xsdName))
                {
                    var schemaObj = GetXsd(xsdPath, xsdName);
                    if (schemaObj != null)
                        xsds.Add(xsdName, schemaObj);
                }

                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();
        }
        /// <summary>
        /// Validates the content of the resource XML.
        /// </summary>
        /// <param name="xmlContent">Content of the XML.</param>
        /// <param name="xsdPath">The XSD path.</param>
        /// <param name="errors">The errors.</param>
        /// <param name="warnings">The warnings.</param>
        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 = ObjectFactory.DeserializeXml(xmlContent);
            }
            catch (Exception ex)
            {
                if (!(ex is SerializationException))
                {
                    err.Add(ex.Message);
                }
                else
                {
                    res = null;
                }
            }

            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);
                }
                else
                {
                    return; //One or more schemas is not found. Cannot proceed. Let MG figure it out
                }
                //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"); //NOXLATE
                if (pc != null)
                {
                    xsds.Add("PlatformCommon-1.0.0.xsd", pc); //NOXLATE
                }
                if (res.ResourceType == ResourceTypes.LayerDefinition.ToString())
                {
                    string version = res.ResourceVersion.ToString();
                    if (version.StartsWith("1.1.0"))                             //NOXLATE
                    {
                        var sym = GetXsd(xsdPath, "SymbolDefinition-1.0.0.xsd"); //NOXLATE
                        if (sym != null)
                        {
                            xsds.Add("SymbolDefinition-1.0.0.xsd", sym); //NOXLATE
                        }
                    }
                    else if (version.StartsWith("1.2.0") || version.StartsWith("1.3.0")) //NOXLATE
                    {
                        var sym = GetXsd(xsdPath, "SymbolDefinition-1.1.0.xsd");         //NOXLATE
                        if (sym != null)
                        {
                            xsds.Add("SymbolDefinition-1.1.0.xsd", sym); //NOXLATE
                        }
                    }
                    else if (version.StartsWith("2.3.0"))                           //NOXLATE
                    {
                        var wmd = GetXsd(xsdPath, "WatermarkDefinition-2.3.0.xsd"); //NOXLATE
                        if (wmd != null)
                        {
                            xsds.Add("WatermarkDefinition-2.3.0.xsd", wmd); //NOXLATE
                        }
                    }
                    else if (version.StartsWith("2.4.0"))                        //NOXLATE
                    {
                        var sym = GetXsd(xsdPath, "SymbolDefinition-2.4.0.xsd"); //NOXLATE
                        if (sym != null)
                        {
                            xsds.Add("SymbolDefinition-2.4.0.xsd", sym);            //NOXLATE
                        }
                        var wmd = GetXsd(xsdPath, "WatermarkDefinition-2.4.0.xsd"); //NOXLATE
                        if (wmd != null)
                        {
                            xsds.Add("WatermarkDefinition-2.4.0.xsd", wmd); //NOXLATE
                        }
                    }
                }

                if (res.ResourceType == ResourceTypes.WatermarkDefinition.ToString())
                {
                    string version = res.ResourceVersion.ToString();
                    if (version.StartsWith("2.3.0"))                             //NOXLATE
                    {
                        var sym = GetXsd(xsdPath, "SymbolDefinition-1.1.0.xsd"); //NOXLATE
                        if (sym != null)
                        {
                            xsds.Add("SymbolDefinition-1.1.0.xsd", sym); //NOXLATE
                        }
                    }
                    else if (version.StartsWith("2.4.0"))
                    {
                        var sym = GetXsd(xsdPath, "SymbolDefinition-2.4.0.xsd"); //NOXLATE
                        if (sym != null)
                        {
                            xsds.Add("SymbolDefinition-2.4.0.xsd", sym); //NOXLATE
                        }
                    }
                }

                if (res.ResourceType == ResourceTypes.MapDefinition.ToString())
                {
                    string version = res.ResourceVersion.ToString();
                    if (version.StartsWith("2.3.0"))
                    {
                        var sym = GetXsd(xsdPath, "SymbolDefinition-1.1.0.xsd"); //NOXLATE
                        if (sym != null)
                        {
                            xsds.Add("SymbolDefinition-1.1.0.xsd", sym);            //NOXLATE
                        }
                        var wmd = GetXsd(xsdPath, "WatermarkDefinition-2.3.0.xsd"); //NOXLATE
                        if (wmd != null)
                        {
                            xsds.Add("WatermarkDefinition-2.3.0.xsd", wmd); //NOXLATE
                        }
                    }
                    else if (version.StartsWith("2.4.0") || version.StartsWith("3.0.0"))
                    {
                        var sym = GetXsd(xsdPath, "SymbolDefinition-2.4.0.xsd"); //NOXLATE
                        if (sym != null)
                        {
                            xsds.Add("SymbolDefinition-2.4.0.xsd", sym);            //NOXLATE
                        }
                        var wmd = GetXsd(xsdPath, "WatermarkDefinition-2.4.0.xsd"); //NOXLATE
                        if (wmd != null)
                        {
                            xsds.Add("WatermarkDefinition-2.4.0.xsd", wmd); //NOXLATE
                        }
                    }
                }

                string xsdName = res.ResourceType.ToString() + "-" + res.ResourceVersion.ToString() + ".xsd"; //NOXLATE
                if (!xsds.ContainsKey(xsdName))
                {
                    var schemaObj = GetXsd(xsdPath, xsdName);
                    if (schemaObj != null)
                    {
                        xsds.Add(xsdName, schemaObj);
                    }
                }

                var validator = new XmlValidator();
                using (var ms = MemoryStreamPool.GetStream("ValidateResourceXmlContent", Encoding.UTF8.GetBytes(xml))) //NOXLATE
                {
                    validator.Validate(ms, new List <XmlSchema>(xsds.Values).ToArray());
                }

                err.AddRange(validator.ValidationErrors);
                warn.AddRange(validator.ValidationWarnings);
            }

            errors   = err.ToArray();
            warnings = warn.ToArray();
        }