Exemplo n.º 1
0
        public ValidationResultList Validate(T input)
        {
            if (null == this.Source)
            {
                throw new InvalidOperationException("The Source property must be set before calling Validate");
            }
            ValidationResultCollection results = new ValidationResultCollection();

            this.Source.ValidationEventHandler = (e) =>
            {
                if (null == e)
                {
                    throw new ArgumentNullException("e");
                }
                if (null == e.Exception)
                {
                    throw new ArgumentException("The validation event args must contain an exception");
                }
                ValidationResult r = this.XmlExceptionInterpreters.Interpret("Structure", e.Exception);
                if (null != r)
                {
                    ValidationInstance vi = null;
                    if (e.Exception is System.Xml.Schema.XmlSchemaException)
                    {
                        vi = new ValidationInstance()
                        {
                            LinePosition = (e.Exception as System.Xml.Schema.XmlSchemaException).LinePosition,
                            LineNumber   = (e.Exception as System.Xml.Schema.XmlSchemaException).LineNumber,
                            Status       = ValidationStatus.Exception
                        };
                    }
                    if (null != vi)
                    {
                        r.Instances.Add(vi);
                    }
                    results.Add(r.Message, r);
                }
                else
                {
                    results.Add(e.Message, new ValidationResult()
                    {
                        Exception = e.Exception,
                        Message   = e.Message
                    });
                }
            };
            System.Xml.Linq.XDocument doc = null;
            //doc.Schemas = xmlReader.Settings.Schemas;
            using (this.TimedLogs.Step("Loading and parsing XML file"))
            {
                DateTime start = DateTime.Now;
                try
                {
                    using (XmlReader xmlReader = this.Source.GetXmlReader(input))
                    {
                        doc = System.Xml.Linq.XDocument.Load(xmlReader, System.Xml.Linq.LoadOptions.SetLineInfo);
                    }
                }
                catch (System.Xml.XmlException e)
                {
                    ValidationResult r = this.XmlExceptionInterpreters.Interpret("Structure", e);
                    r.Instances.Add(new ValidationInstance()
                    {
                        LineNumber   = e.LineNumber,
                        LinePosition = e.LinePosition,
                        Status       = ValidationStatus.Exception
                    });
                    if (null != r)
                    {
                        results.Add(r.Message, r);
                    }
                }
                // Check to see whether there was an xsi:schemaLocation
                if (
                    (null != doc)
                    &&
                    (results.Count != 0)
                    &&
                    this.AttemptSchemaLocationInjection
                    )
                {
                    XmlNamespaceManager xmlnsmgr = new XmlNamespaceManager(new NameTable());
                    xmlnsmgr.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
                    if (((double)doc.XPathEvaluate("count(//@xsi:schemaLocation)", xmlnsmgr)) == 0)
                    {
                        results.Clear();
                        // Add in to say we're missing an xsi:schemaLocation attribute
                        ValidationResult r = this.XmlExceptionInterpreters.Interpret("Structure", new ValidationException("Missing xsi:schemaLocation attribute"));
                        r.Instances.Add(new ValidationInstance()
                        {
                            LineNumber   = (doc.Root as IXmlLineInfo).LineNumber,
                            LinePosition = (doc.Root as IXmlLineInfo).LinePosition,
                            Status       = ValidationStatus.Warning
                        });
                        if (null != r)
                        {
                            results.Add(r.Message, r);
                        }
                        // If we're missing an xsi:schemaLocation then it'll all go to pot.
                        // Be nice and try and add the data.
                        XmlSchemaSet schemaSet = new XmlSchemaSet(new NameTable());
                        schemaSet.XmlResolver = this.XmlCachingResolver as XmlResolver;
                        schemaSet.Add
                        (
                            "http://xcri.org/profiles/1.2/catalog",
                            "http://www.xcri.co.uk/bindings/xcri_cap_1_2.xsd"
                        );
                        schemaSet.Add
                        (
                            "http://xcri.org/profiles/1.2/catalog/terms",
                            "http://www.xcri.co.uk/bindings/xcri_cap_terms_1_2.xsd"
                        );
                        schemaSet.Add
                        (
                            "http://xcri.co.uk",
                            "http://www.xcri.co.uk/bindings/coursedataprogramme.xsd"
                        );
                        schemaSet.Compile();
                        doc.Validate(schemaSet, new ValidationEventHandler((o, e) =>
                        {
                            this.Source.ValidationEventHandler(e);
                        }));
                    }
                }
                if (null != doc)
                {
                    using (this.TimedLogs.Step("Executing content validators"))
                    {
                        if (null != this.XmlContentValidators)
                        {
                            foreach (var cv in this.XmlContentValidators)
                            {
                                var vrc = cv.Validate(doc.Root);
                                if (null != vrc)
                                {
                                    foreach (var vr in vrc)
                                    {
                                        if (null != vr)
                                        {
                                            results.Add(vr.Message, vr);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return(new ValidationResultList(results.Values)
            {
                Document = doc
            });
        }