Example #1
0
#pragma warning disable 618
        internal bool CompileSchema(XmlSchemaCollection?xsc, XmlResolver?resolver, SchemaInfo schemaInfo, string?ns, ValidationEventHandler?validationEventHandler, XmlNameTable nameTable, bool CompileContentModel)
        {
            //Need to lock here to prevent multi-threading problems when same schema is added to set and compiled
            lock (this)
            {
                //Preprocessing
                SchemaCollectionPreprocessor prep = new SchemaCollectionPreprocessor(nameTable, null, validationEventHandler);
                prep.XmlResolver = resolver;
                if (!prep.Execute(this, ns, true, xsc))
                {
                    return(false);
                }

                //Compilation
                SchemaCollectionCompiler compiler = new SchemaCollectionCompiler(nameTable, validationEventHandler);
                _isCompiled = compiler.Execute(this, schemaInfo, CompileContentModel);
                this.SetIsCompiled(_isCompiled);
                return(_isCompiled);
            }
        }
Example #2
0
 public static XmlSchema?Read(TextReader reader, ValidationEventHandler?validationEventHandler)
 {
     return(Read(new XmlTextReader(reader), validationEventHandler));
 }
Example #3
0
        public DocumentSchemaValidator(XmlDocument ownerDocument, XmlSchemaSet schemas, ValidationEventHandler?eventHandler)
        {
            _schemas              = schemas;
            _eventHandler         = eventHandler;
            _document             = ownerDocument;
            _internalEventHandler = new ValidationEventHandler(InternalValidationCallBack);

            _nameTable = _document.NameTable;
            _nsManager = new XmlNamespaceManager(_nameTable);

            Debug.Assert(schemas != null && schemas.Count > 0);

            _nodeValueGetter  = new XmlValueGetter(GetNodeValue);
            _psviAugmentation = true;

            //Add common strings to be compared to NameTable
            _nsXmlNs = _nameTable.Add(XmlReservedNs.NsXmlNs);
            _nsXsi   = _nameTable.Add(XmlReservedNs.NsXsi);
            _xsiType = _nameTable.Add("type");
            _xsiNil  = _nameTable.Add("nil");
        }
Example #4
0
 public static XmlSchema?Read(Stream stream, ValidationEventHandler?validationEventHandler)
 {
     return(Read(new XmlTextReader(stream), validationEventHandler));
 }
Example #5
0
 public static XmlSchema?Read(Stream stream, ValidationEventHandler?validationEventHandler)
 {
     ArgumentNullException.ThrowIfNull(stream);
     return(Read(new XmlTextReader(stream), validationEventHandler));
 }
Example #6
0
        public void Compile(ValidationEventHandler?handler, bool fullCompile)
        {
            if (_isCompiled)
            {
                return;
            }

            foreach (XmlSchema s in delayedSchemas.Values)
            {
                Merge(s);
            }
            delayedSchemas.Clear();

            if (fullCompile)
            {
                _schemaSet                         = new XmlSchemaSet();
                _schemaSet.XmlResolver             = null;
                _schemaSet.ValidationEventHandler += handler;

                foreach (XmlSchema s in References.Values)
                {
                    _schemaSet.Add(s);
                }
                int schemaCount = _schemaSet.Count;

                foreach (XmlSchema s in List)
                {
                    if (!SchemaSet.Contains(s))
                    {
                        _schemaSet.Add(s);
                        schemaCount++;
                    }
                }

                if (!SchemaSet.Contains(XmlSchema.Namespace))
                {
                    AddReference(XsdSchema);
                    _schemaSet.Add(XsdSchema);
                    schemaCount++;
                }

                if (!SchemaSet.Contains(XmlReservedNs.NsXml))
                {
                    AddReference(XmlSchema);
                    _schemaSet.Add(XmlSchema);
                    schemaCount++;
                }
                _schemaSet.Compile();
                _schemaSet.ValidationEventHandler -= handler;
                _isCompiled = _schemaSet.IsCompiled && schemaCount == _schemaSet.Count;
            }
            else
            {
                try
                {
                    XmlNameTable nameTable = new System.Xml.NameTable();
                    Preprocessor prep      = new Preprocessor(nameTable, new SchemaNames(nameTable), null);
                    prep.XmlResolver      = null;
                    prep.SchemaLocations  = new Hashtable();
                    prep.ChameleonSchemas = new Hashtable();
                    foreach (XmlSchema schema in SchemaSet.Schemas())
                    {
                        prep.Execute(schema, schema.TargetNamespace, true);
                    }
                }
                catch (XmlSchemaException e)
                {
                    throw CreateValidationException(e, e.Message);
                }
            }
        }
Example #7
0
        public static void CheckDefaultValue(
            string value,
            SchemaAttDef attdef,
            SchemaInfo sinfo,
            XmlNamespaceManager nsManager,
            XmlNameTable NameTable,
            object?sender,
            ValidationEventHandler?eventhandler,
            string?baseUri,
            int lineNo,
            int linePos
            )
        {
            try
            {
                XmlSchemaDatatype dtype = attdef.Datatype;
                if (dtype == null)
                {
                    return; // no reason to check
                }

                if (dtype.TokenizedType != XmlTokenizedType.CDATA)
                {
                    value = value.Trim();
                }
                if (value.Length == 0)
                {
                    return; // don't need to check
                }
                object typedValue = dtype.ParseValue(value, NameTable, nsManager);

                // Check special types
                XmlTokenizedType ttype = dtype.TokenizedType;
                if (ttype == XmlTokenizedType.ENTITY)
                {
                    if (dtype.Variety == XmlSchemaDatatypeVariety.List)
                    {
                        string[] ss = (string[])typedValue;
                        for (int i = 0; i < ss.Length; ++i)
                        {
                            ProcessEntity(sinfo, ss[i], sender, eventhandler, baseUri, lineNo, linePos);
                        }
                    }
                    else
                    {
                        ProcessEntity(sinfo, (string)typedValue, sender, eventhandler, baseUri, lineNo, linePos);
                    }
                }
                else if (ttype == XmlTokenizedType.ENUMERATION)
                {
                    if (!attdef.CheckEnumeration(typedValue))
                    {
                        XmlSchemaException e = new XmlSchemaException(SR.Sch_EnumerationValue, typedValue.ToString(), baseUri, lineNo, linePos);
                        if (eventhandler != null)
                        {
                            eventhandler(sender, new ValidationEventArgs(e));
                        }
                        else
                        {
                            throw e;
                        }
                    }
                }
                attdef.DefaultValueTyped = typedValue;
            }
#if DEBUG
            catch (XmlSchemaException)
#else
            catch
#endif
            {
                XmlSchemaException e = new XmlSchemaException(SR.Sch_AttributeDefaultDataType, attdef.Name.ToString(), baseUri, lineNo, linePos);
                if (eventhandler != null)
                {
                    eventhandler(sender, new ValidationEventArgs(e));
                }
                else
                {
                    throw e;
                }
            }
        }
 public BaseProcessor(XmlNameTable nameTable, SchemaNames?schemaNames, ValidationEventHandler?eventHandler)
     : this(nameTable, schemaNames, eventHandler, new XmlSchemaCompilationSettings())
 {
 }   //Use the default for XmlSchemaCollection
Example #9
0
 // XmlValidatingReaderImpl helper methods
 internal void AddHandler(ValidationEventHandler handler)
 {
     _eventHandler += handler;
 }
Example #10
0
 internal void RemoveHandler(ValidationEventHandler handler)
 {
     _eventHandler -= handler;
 }
Example #11
0
        /// <summary>
        /// Validate a <see cref="XAttribute"/>
        /// </summary>
        /// <param name="source">Extension point</param>
        /// <param name="partialValidationType">An <see cref="XmlSchemaAttribute"/> or
        /// <see cref="XmlSchemaType"/> object used to initialize the partial validation
        /// context</param>
        /// <param name="schemas">The <see cref="XmlSchemaSet"/> used for validation</param>
        /// <param name="validationEventHandler">The <see cref="ValidationEventHandler"/> that
        /// receives schema validation warnings and errors encountered during schema
        /// validation</param>
        /// <param name="addSchemaInfo">If enabled the <see cref="XAttribute"/> is augmented with PSVI
        /// in the form of <see cref="IXmlSchemaInfo"/> annotations, default attributes and
        /// default element values</param>
        public static void Validate(this XAttribute source, XmlSchemaObject partialValidationType, XmlSchemaSet schemas, ValidationEventHandler?validationEventHandler, bool addSchemaInfo)
        {
            ArgumentNullException.ThrowIfNull(source);
            ArgumentNullException.ThrowIfNull(partialValidationType);
            ArgumentNullException.ThrowIfNull(schemas);

            new XNodeValidator(schemas, validationEventHandler).Validate(source, partialValidationType, addSchemaInfo);
        }
Example #12
0
 /// <summary>
 /// Validate a <see cref="XAttribute"/>
 /// </summary>
 /// <param name="source">Extension point</param>
 /// <param name="partialValidationType">An <see cref="XmlSchemaAttribute"/> or
 /// <see cref="XmlSchemaType"/> object used to initialize the partial validation
 /// context</param>
 /// <param name="schemas">The <see cref="XmlSchemaSet"/> used for validation</param>
 /// <param name="validationEventHandler">The <see cref="ValidationEventHandler"/> that
 /// receives schema validation warnings and errors encountered during schema
 /// validation</param>
 public static void Validate(this XAttribute source, XmlSchemaObject partialValidationType, XmlSchemaSet schemas, ValidationEventHandler?validationEventHandler)
 {
     source.Validate(partialValidationType, schemas, validationEventHandler, false);
 }
Example #13
0
 /// <summary>
 /// Validate a <see cref="XDocument"/>
 /// </summary>
 /// <param name="source">Extension point</param>
 /// <param name="schemas">The <see cref="XmlSchemaSet"/> used for validation</param>
 /// <param name="validationEventHandler">The <see cref="ValidationEventHandler"/>
 /// that receives schema validation warnings and errors encountered during schema
 /// validation</param>
 public static void Validate(this XDocument source, XmlSchemaSet schemas, ValidationEventHandler?validationEventHandler)
 {
     source.Validate(schemas, validationEventHandler, false);
 }
Example #14
0
 public Parser(SchemaType schemaType, XmlNameTable nameTable, SchemaNames schemaNames, ValidationEventHandler?eventHandler)
 {
     _schemaType    = schemaType;
     _nameTable     = nameTable;
     _schemaNames   = schemaNames;
     _eventHandler  = eventHandler;
     _xmlResolver   = null;
     _processMarkup = true;
     _dummyDocument = new XmlDocument();
 }
Example #15
0
 /// <summary>
 /// Validate a <see cref="XAttribute"/>
 /// </summary>
 /// <param name="source">Extension point</param>
 /// <param name="partialValidationType">An <see cref="XmlSchemaAttribute"/> or
 /// <see cref="XmlSchemaType"/> object used to initialize the partial validation
 /// context</param>
 /// <param name="schemas">The <see cref="XmlSchemaSet"/> used for validation</param>
 /// <param name="validationEventHandler">The <see cref="ValidationEventHandler"/> that
 /// receives schema validation warnings and errors encountered during schema
 /// validation</param>
 /// <param name="addSchemaInfo">If enabled the <see cref="XAttribute"/> is augmented with PSVI
 /// in the form of <see cref="IXmlSchemaInfo"/> annotations, default attributes and
 /// default element values</param>
 public static void Validate(this XAttribute source, XmlSchemaObject partialValidationType, XmlSchemaSet schemas, ValidationEventHandler?validationEventHandler, bool addSchemaInfo)
 {
     if (source == null)
     {
         throw new ArgumentNullException(nameof(source));
     }
     if (partialValidationType == null)
     {
         throw new ArgumentNullException(nameof(partialValidationType));
     }
     if (schemas == null)
     {
         throw new ArgumentNullException(nameof(schemas));
     }
     new XNodeValidator(schemas, validationEventHandler).Validate(source, partialValidationType, addSchemaInfo);
 }