public override object GetEntity(Uri absoluteUri, string role, Type ofObjectToReturn)
        {
            CacheEntry ce = cache.FindSchemaByUri(absoluteUri);

            if (ce != null && ce.HasUpToDateSchema)
            {
                return(ce.Schema);
            }
            Stream stm = base.GetEntity(absoluteUri, role, typeof(Stream)) as Stream;

            using (stm) {
                if (stm != null)
                {
                    XmlSchema s = XmlSchema.Read(stm, handler);
                    s.SourceUri = absoluteUri.AbsoluteUri;
                    if (ce != null)
                    {
                        ce.Schema = s;
                    }
                    else
                    {
                        cache.Add(s);
                    }
                    return(s);
                }
            }
            return(null);
        }
Beispiel #2
0
        public void ValidateContext(XmlCache xcache)
        {
            this._cache = xcache;
            if (string.IsNullOrEmpty(_cache.FileName))
            {
                _baseUri = null;
            }
            else
            {
                _baseUri = new Uri(new Uri(xcache.FileName), new Uri(".", UriKind.Relative));
            }

            SchemaResolver resolver = xcache.SchemaResolver as SchemaResolver;

            resolver.Handler = OnValidationEvent;
            XmlDocument doc = xcache.Document;

            this._info       = new XmlSchemaInfo();
            this._nsResolver = new MyXmlNamespaceResolver(doc.NameTable);
            XmlSchemaSet set = new XmlSchemaSet();
            // Make sure the SchemaCache is up to date with document.
            SchemaCache sc = xcache.SchemaCache;

            foreach (XmlSchema s in doc.Schemas.Schemas())
            {
                sc.Add(s);
            }

            if (LoadSchemas(doc, set, resolver))
            {
                set.ValidationEventHandler += OnValidationEvent;
                set.Compile();
                set.ValidationEventHandler -= OnValidationEvent;
            }

            this._validator = new XmlSchemaValidator(doc.NameTable, set, _nsResolver,
                                                     XmlSchemaValidationFlags.AllowXmlAttributes |
                                                     XmlSchemaValidationFlags.ProcessIdentityConstraints |
                                                     XmlSchemaValidationFlags.ProcessInlineSchema);

            this._validator.ValidationEventHandler += OnValidationEvent;
            this._validator.XmlResolver             = resolver;
            this._validator.Initialize();

            this._nsResolver.Context = doc;
            ValidateContent(doc);
            this._nsResolver.Context = doc;

            this._validator.EndValidation();
        }
        public override object GetEntity(Uri absoluteUri, string role, Type ofObjectToReturn)
        {
            CacheEntry ce = cache.FindSchemaByUri(absoluteUri);

            if (ce != null && ce.HasUpToDateSchema)
            {
                return(ce.Schema);
            }

            XmlSchema s = null;

            if (ofObjectToReturn == typeof(XmlSchema))
            {
                XmlReaderSettings settings = new XmlReaderSettings();
                settings.ValidationEventHandler += handler;
                settings.XmlResolver             = this;
                XmlReader r = XmlReader.Create(absoluteUri.AbsoluteUri, settings);
                if (r != null)
                {
                    s = XmlSchema.Read(r, handler);
                    if (s != null)
                    {
                        s.SourceUri = absoluteUri.AbsoluteUri;
                        if (ce != null)
                        {
                            ce.Schema = s;
                        }
                        else
                        {
                            cache.Add(s);
                        }
                        return(s);
                    }
                }
            }
            else
            {
                return(base.GetEntity(absoluteUri, role, typeof(Stream)) as Stream);
            }

            return(null);
        }