Add() public method

public Add ( SchemaInfo sinfo, ValidationEventHandler eventhandler ) : void
sinfo SchemaInfo
eventhandler ValidationEventHandler
return void
        private void LoadSchema(string uri, string url)
        {
            if (this.XmlResolver == null)
            {
                return;
            }
            if (SchemaInfo.TargetNamespaces.ContainsKey(uri) && nsManager.LookupPrefix(uri) != null)
            {
                return;
            }

            SchemaInfo schemaInfo = null;

            if (SchemaCollection != null)
            {
                schemaInfo = SchemaCollection.GetSchemaInfo(uri);
            }
            if (schemaInfo != null)
            {
                if (schemaInfo.SchemaType != SchemaType.XSD)
                {
                    throw new XmlException(Res.Xml_MultipleValidaitonTypes, string.Empty, this.PositionInfo.LineNumber, this.PositionInfo.LinePosition);
                }
                SchemaInfo.Add(schemaInfo, EventHandler);
                return;
            }
            if (url != null)
            {
                LoadSchemaFromLocation(uri, url);
            }
        }
Example #2
0
 private void ProcessInlineSchema()
 {
     if (!_inlineSchemaParser !.ParseReaderNode())
     { // Done
         _inlineSchemaParser.FinishParsing();
         XmlSchema?schema = _inlineSchemaParser.XmlSchema;
         string?   inlineNS;
         if (schema != null && schema.ErrorCount == 0)
         {
             try
             {
                 SchemaInfo inlineSchemaInfo = new SchemaInfo();
                 inlineSchemaInfo.SchemaType = SchemaType.XSD;
                 inlineNS = schema.TargetNamespace == null ? string.Empty : schema.TargetNamespace;
                 if (!SchemaInfo !.TargetNamespaces.ContainsKey(inlineNS))
                 {
                     if (SchemaCollection !.Add(inlineNS, inlineSchemaInfo, schema, true) != null)
                     { //If no errors on compile
                         //Add to validator's SchemaInfo
                         SchemaInfo.Add(inlineSchemaInfo, EventHandler);
                     }
                 }
             }
             catch (XmlSchemaException e)
             {
                 SendValidationEvent(SR.Sch_CannotLoadSchema, new string[] { BaseUri !.AbsoluteUri, e.Message }, XmlSeverityType.Error);
Example #3
0
        private void LoadSchemaFromLocation(string uri, string url)
        {
            XmlReader  reader     = null;
            SchemaInfo schemaInfo = null;

            try
            {
                Uri    ruri = this.XmlResolver.ResolveUri(BaseUri, url);
                Stream stm  = (Stream)this.XmlResolver.GetEntity(ruri, null, null);
                reader = new XmlTextReader(ruri.ToString(), stm, NameTable);
                //XmlSchema schema = SchemaCollection.Add(uri, reader, this.XmlResolver);

                Parser parser = new Parser(SchemaType.XSD, NameTable, SchemaNames, EventHandler);
                parser.XmlResolver = this.XmlResolver;
                SchemaType schemaType = parser.Parse(reader, uri);

                schemaInfo            = new SchemaInfo();
                schemaInfo.SchemaType = schemaType;
                if (schemaType == SchemaType.XSD)
                {
                    if (SchemaCollection.EventHandler == null)
                    {
                        SchemaCollection.EventHandler = this.EventHandler;
                    }
                    SchemaCollection.Add(uri, schemaInfo, parser.XmlSchema, true);
                }
                //Add to validator's SchemaInfo
                SchemaInfo.Add(schemaInfo, EventHandler);

                while (reader.Read())
                {
                    ;                  // wellformness check
                }
            }
            catch (XmlSchemaException e)
            {
                schemaInfo = null;
                SendValidationEvent(SR.Sch_CannotLoadSchema, new string[] { uri, e.Message }, XmlSeverityType.Error);
            }
            catch (Exception e)
            {
                schemaInfo = null;
                SendValidationEvent(SR.Sch_CannotLoadSchema, new string[] { uri, e.Message }, XmlSeverityType.Warning);
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }
        }
        internal void CopyFromCompiledSet(XmlSchemaSet otherSet) {
            XmlSchema currentSchema;
            SortedList copyFromList = otherSet.SortedSchemas;
            bool setIsCompiled = schemas.Count == 0 ? true : false; 
            ArrayList existingSchemas = new ArrayList();

            SchemaInfo newCompiledInfo = new SchemaInfo();
            Uri baseUri;
            for(int i=0; i < copyFromList.Count; i++) {
                currentSchema = (XmlSchema)copyFromList.GetByIndex(i);
                baseUri = currentSchema.BaseUri;
                if (schemas.ContainsKey(currentSchema.SchemaId) || (baseUri != null && baseUri.OriginalString.Length != 0 && schemaLocations[baseUri] != null)) {
                    existingSchemas.Add(currentSchema);
                    continue;
                }
                schemas.Add(currentSchema.SchemaId, currentSchema);
                if (baseUri != null && baseUri.OriginalString.Length != 0) {
                    schemaLocations.Add(baseUri, currentSchema);
                }
                string tns = GetTargetNamespace(currentSchema);
                if (targetNamespaces[tns] == null) {
                    targetNamespaces.Add(tns, tns);
                }
            }

            VerifyTables();
            foreach (XmlSchemaElement element in otherSet.GlobalElements.Values) {
                if(!AddToTable(elements, element.QualifiedName, element)) {
                    goto RemoveAll;
                }
            }
            foreach (XmlSchemaAttribute attribute in otherSet.GlobalAttributes.Values) {
                if (!AddToTable(attributes, attribute.QualifiedName, attribute)) {
                    goto RemoveAll;
                }
            }
            foreach (XmlSchemaType schemaType in otherSet.GlobalTypes.Values) {
                if (!AddToTable(schemaTypes, schemaType.QualifiedName, schemaType)) {
                    goto RemoveAll;
                }
            }
            //TODO get otherSet's substitutionGroups
            ProcessNewSubstitutionGroups(otherSet.SubstitutionGroups, false);

            newCompiledInfo.Add(cachedCompiledInfo, eventHandler); //Add all the items from the old to the new compiled object
            newCompiledInfo.Add(otherSet.CompiledInfo,eventHandler); //TODO: Throw error on conflicting types that are not from the same schema / baseUri
            cachedCompiledInfo = newCompiledInfo; //Replace the compiled info in the set after successful compilation
            if (setIsCompiled) {
                isCompiled = true;
                compileAll = false;
            }
            return;
        
            RemoveAll:
                foreach (XmlSchema schemaToRemove in copyFromList.Values) {
                    if (!existingSchemas.Contains(schemaToRemove)) {
                        Remove(schemaToRemove, false);
                    }
                }
                foreach (XmlSchemaElement elementToRemove in otherSet.GlobalElements.Values) {
                    if(!existingSchemas.Contains((XmlSchema)elementToRemove.Parent)) {
                        elements.Remove(elementToRemove.QualifiedName);
                    }
                }
                foreach (XmlSchemaAttribute attributeToRemove in otherSet.GlobalAttributes.Values) {
                    if(!existingSchemas.Contains((XmlSchema)attributeToRemove.Parent)) {
                        attributes.Remove(attributeToRemove.QualifiedName);
                    }
                }
                foreach (XmlSchemaType schemaTypeToRemove in otherSet.GlobalTypes.Values) {
                    if(!existingSchemas.Contains((XmlSchema)schemaTypeToRemove.Parent)) {
                        schemaTypes.Remove(schemaTypeToRemove.QualifiedName);
                    }
                }
        }
        /// <include file='doc\XmlSchemaSet.uex' path='docs/doc[@for="XmlSchemaSet.Compile"]/*' />
        /// <devdoc>
        ///    <para>[To be supplied.]</para>
        /// </devdoc>
        public void Compile() {
            if (schemas.Count == 0) {
                ClearTables(); //Clear any previously present compiled state left by calling just Remove() on the set
                return;
            }
            if (isCompiled) {
                return;
            }
            lock (InternalSyncObject) {
                
                if (!isCompiled) { //Locking before checking isCompiled to avoid problems with double locking
                    Compiler compiler = new Compiler(nameTable, eventHandler, schemaForSchema, compilationSettings);
                    SchemaInfo newCompiledInfo = new SchemaInfo();
                    int schemaIndex = 0;
                    if (!compileAll) { //if we are not compiling everything again, Move the pre-compiled schemas to the compiler's tables
                        compiler.ImportAllCompiledSchemas(this); 
                    }
                    try { //First thing to do in the try block is to acquire locks since finally will try to release them. 
                        //If we dont accuire the locks first, and an exception occurs in the code before the locking code, then Threading.SynchronizationLockException will be thrown
                        //when attempting to release it in the finally block
                        XmlSchema currentSchema;
                        XmlSchema xmlNSSchema = Preprocessor.GetBuildInSchema();
                        for (schemaIndex = 0; schemaIndex < schemas.Count; schemaIndex++) {
                            currentSchema = (XmlSchema)schemas.GetByIndex(schemaIndex);
                            
                            //Lock schema to be compiled
                            Monitor.Enter(currentSchema);
                            if (!currentSchema.IsPreprocessed) {
                                SendValidationEvent(new XmlSchemaException(Res.Sch_SchemaNotPreprocessed, string.Empty), XmlSeverityType.Error);
                                isCompiled = false;
                                return;
                            }
                            if (currentSchema.IsCompiledBySet) {
                                if (!compileAll) {
                                    continue;
                                }
                                else if ((object)currentSchema == (object)xmlNSSchema) { // prepare for xml namespace schema without cleanup
                                    compiler.Prepare(currentSchema, false);
                                    continue;
                                }
                            }
                            compiler.Prepare(currentSchema, true);
                        }

                        isCompiled = compiler.Execute(this, newCompiledInfo);
                        if (isCompiled) { 
                            compileAll = false;
                            newCompiledInfo.Add(cachedCompiledInfo, eventHandler); //Add all the items from the old to the new compiled object
                            cachedCompiledInfo = newCompiledInfo; //Replace the compiled info in the set after successful compilation
                        }
                    }
                    finally {
                        //Release locks on all schemas
                        XmlSchema currentSchema;
                        if (schemaIndex == schemas.Count) {
                            schemaIndex--;
                        }
                        for (int i = schemaIndex; i >= 0; i--) {
                            currentSchema = (XmlSchema)schemas.GetByIndex(i);
                            if (currentSchema == Preprocessor.GetBuildInSchema()) { //dont re-set compiled flags for xml namespace schema
                                Monitor.Exit(currentSchema);
                                continue;
                            }
                            currentSchema.IsCompiledBySet = isCompiled;
                            Monitor.Exit(currentSchema);
                        }
                    }
                }
            }
            return;
        }
 internal void CopyFromCompiledSet(XmlSchemaSet otherSet)
 {
     SortedList sortedSchemas = otherSet.SortedSchemas;
     bool flag = this.schemas.Count == 0;
     ArrayList list2 = new ArrayList();
     SchemaInfo info = new SchemaInfo();
     for (int i = 0; i < sortedSchemas.Count; i++)
     {
         XmlSchema byIndex = (XmlSchema) sortedSchemas.GetByIndex(i);
         Uri baseUri = byIndex.BaseUri;
         if (this.schemas.ContainsKey(byIndex.SchemaId) || (((baseUri != null) && (baseUri.OriginalString.Length != 0)) && (this.schemaLocations[baseUri] != null)))
         {
             list2.Add(byIndex);
         }
         else
         {
             this.schemas.Add(byIndex.SchemaId, byIndex);
             if ((baseUri != null) && (baseUri.OriginalString.Length != 0))
             {
                 this.schemaLocations.Add(baseUri, byIndex);
             }
             string targetNamespace = this.GetTargetNamespace(byIndex);
             if (this.targetNamespaces[targetNamespace] == null)
             {
                 this.targetNamespaces.Add(targetNamespace, targetNamespace);
             }
         }
     }
     this.VerifyTables();
     foreach (XmlSchemaElement element in otherSet.GlobalElements.Values)
     {
         if (!this.AddToTable(this.elements, element.QualifiedName, element))
         {
             goto Label_026E;
         }
     }
     foreach (XmlSchemaAttribute attribute in otherSet.GlobalAttributes.Values)
     {
         if (!this.AddToTable(this.attributes, attribute.QualifiedName, attribute))
         {
             goto Label_026E;
         }
     }
     foreach (XmlSchemaType type in otherSet.GlobalTypes.Values)
     {
         if (!this.AddToTable(this.schemaTypes, type.QualifiedName, type))
         {
             goto Label_026E;
         }
     }
     this.ProcessNewSubstitutionGroups(otherSet.SubstitutionGroups, false);
     info.Add(this.cachedCompiledInfo, this.eventHandler);
     info.Add(otherSet.CompiledInfo, this.eventHandler);
     this.cachedCompiledInfo = info;
     if (flag)
     {
         this.isCompiled = true;
         this.compileAll = false;
     }
     return;
 Label_026E:
     foreach (XmlSchema schema2 in sortedSchemas.Values)
     {
         if (!list2.Contains(schema2))
         {
             this.Remove(schema2, false);
         }
     }
     foreach (XmlSchemaElement element2 in otherSet.GlobalElements.Values)
     {
         if (!list2.Contains((XmlSchema) element2.Parent))
         {
             this.elements.Remove(element2.QualifiedName);
         }
     }
     foreach (XmlSchemaAttribute attribute2 in otherSet.GlobalAttributes.Values)
     {
         if (!list2.Contains((XmlSchema) attribute2.Parent))
         {
             this.attributes.Remove(attribute2.QualifiedName);
         }
     }
     foreach (XmlSchemaType type2 in otherSet.GlobalTypes.Values)
     {
         if (!list2.Contains((XmlSchema) type2.Parent))
         {
             this.schemaTypes.Remove(type2.QualifiedName);
         }
     }
 }
 public void Compile()
 {
     if (!this.isCompiled)
     {
         if (this.schemas.Count == 0)
         {
             this.ClearTables();
             this.cachedCompiledInfo = new SchemaInfo();
             this.isCompiled = true;
             this.compileAll = false;
         }
         else
         {
             lock (this.InternalSyncObject)
             {
                 if (!this.isCompiled)
                 {
                     Compiler compiler = new Compiler(this.nameTable, this.eventHandler, this.schemaForSchema, this.compilationSettings);
                     SchemaInfo schemaCompiledInfo = new SchemaInfo();
                     int index = 0;
                     if (!this.compileAll)
                     {
                         compiler.ImportAllCompiledSchemas(this);
                     }
                     try
                     {
                         XmlSchema buildInSchema = Preprocessor.GetBuildInSchema();
                         index = 0;
                         while (index < this.schemas.Count)
                         {
                             XmlSchema byIndex = (XmlSchema) this.schemas.GetByIndex(index);
                             Monitor.Enter(byIndex);
                             if (!byIndex.IsPreprocessed)
                             {
                                 this.SendValidationEvent(new XmlSchemaException("Sch_SchemaNotPreprocessed", string.Empty), XmlSeverityType.Error);
                                 this.isCompiled = false;
                                 goto Label_01BA;
                             }
                             if (byIndex.IsCompiledBySet)
                             {
                                 if (!this.compileAll)
                                 {
                                     goto Label_00FD;
                                 }
                                 if (byIndex == buildInSchema)
                                 {
                                     compiler.Prepare(byIndex, false);
                                     goto Label_00FD;
                                 }
                             }
                             compiler.Prepare(byIndex, true);
                         Label_00FD:
                             index++;
                         }
                         this.isCompiled = compiler.Execute(this, schemaCompiledInfo);
                         if (this.isCompiled)
                         {
                             if (!this.compileAll)
                             {
                                 schemaCompiledInfo.Add(this.cachedCompiledInfo, this.eventHandler);
                             }
                             this.compileAll = false;
                             this.cachedCompiledInfo = schemaCompiledInfo;
                         }
                     }
                     finally
                     {
                         if (index == this.schemas.Count)
                         {
                             index--;
                         }
                         for (int i = index; i >= 0; i--)
                         {
                             XmlSchema schema3 = (XmlSchema) this.schemas.GetByIndex(i);
                             if (schema3 == Preprocessor.GetBuildInSchema())
                             {
                                 Monitor.Exit(schema3);
                             }
                             else
                             {
                                 schema3.IsCompiledBySet = this.isCompiled;
                                 Monitor.Exit(schema3);
                             }
                         }
                     }
                 }
             Label_01BA:;
             }
         }
     }
 }
Example #8
0
        internal void CopyFromCompiledSet(XmlSchemaSet otherSet)
        {
            SortedList sortedSchemas = otherSet.SortedSchemas;
            bool       flag          = this.schemas.Count == 0;
            ArrayList  list2         = new ArrayList();
            SchemaInfo info          = new SchemaInfo();

            for (int i = 0; i < sortedSchemas.Count; i++)
            {
                XmlSchema byIndex = (XmlSchema)sortedSchemas.GetByIndex(i);
                Uri       baseUri = byIndex.BaseUri;
                if (this.schemas.ContainsKey(byIndex.SchemaId) || (((baseUri != null) && (baseUri.OriginalString.Length != 0)) && (this.schemaLocations[baseUri] != null)))
                {
                    list2.Add(byIndex);
                }
                else
                {
                    this.schemas.Add(byIndex.SchemaId, byIndex);
                    if ((baseUri != null) && (baseUri.OriginalString.Length != 0))
                    {
                        this.schemaLocations.Add(baseUri, byIndex);
                    }
                    string targetNamespace = this.GetTargetNamespace(byIndex);
                    if (this.targetNamespaces[targetNamespace] == null)
                    {
                        this.targetNamespaces.Add(targetNamespace, targetNamespace);
                    }
                }
            }
            this.VerifyTables();
            foreach (XmlSchemaElement element in otherSet.GlobalElements.Values)
            {
                if (!this.AddToTable(this.elements, element.QualifiedName, element))
                {
                    goto Label_026E;
                }
            }
            foreach (XmlSchemaAttribute attribute in otherSet.GlobalAttributes.Values)
            {
                if (!this.AddToTable(this.attributes, attribute.QualifiedName, attribute))
                {
                    goto Label_026E;
                }
            }
            foreach (XmlSchemaType type in otherSet.GlobalTypes.Values)
            {
                if (!this.AddToTable(this.schemaTypes, type.QualifiedName, type))
                {
                    goto Label_026E;
                }
            }
            this.ProcessNewSubstitutionGroups(otherSet.SubstitutionGroups, false);
            info.Add(this.cachedCompiledInfo, this.eventHandler);
            info.Add(otherSet.CompiledInfo, this.eventHandler);
            this.cachedCompiledInfo = info;
            if (flag)
            {
                this.isCompiled = true;
                this.compileAll = false;
            }
            return;

Label_026E:
            foreach (XmlSchema schema2 in sortedSchemas.Values)
            {
                if (!list2.Contains(schema2))
                {
                    this.Remove(schema2, false);
                }
            }
            foreach (XmlSchemaElement element2 in otherSet.GlobalElements.Values)
            {
                if (!list2.Contains((XmlSchema)element2.Parent))
                {
                    this.elements.Remove(element2.QualifiedName);
                }
            }
            foreach (XmlSchemaAttribute attribute2 in otherSet.GlobalAttributes.Values)
            {
                if (!list2.Contains((XmlSchema)attribute2.Parent))
                {
                    this.attributes.Remove(attribute2.QualifiedName);
                }
            }
            foreach (XmlSchemaType type2 in otherSet.GlobalTypes.Values)
            {
                if (!list2.Contains((XmlSchema)type2.Parent))
                {
                    this.schemaTypes.Remove(type2.QualifiedName);
                }
            }
        }
Example #9
0
        public void Compile()
        {
            if (!this.isCompiled)
            {
                if (this.schemas.Count == 0)
                {
                    this.ClearTables();
                    this.cachedCompiledInfo = new SchemaInfo();
                    this.isCompiled         = true;
                    this.compileAll         = false;
                }
                else
                {
                    lock (this.InternalSyncObject)
                    {
                        if (!this.isCompiled)
                        {
                            Compiler   compiler           = new Compiler(this.nameTable, this.eventHandler, this.schemaForSchema, this.compilationSettings);
                            SchemaInfo schemaCompiledInfo = new SchemaInfo();
                            int        index = 0;
                            if (!this.compileAll)
                            {
                                compiler.ImportAllCompiledSchemas(this);
                            }
                            try
                            {
                                XmlSchema buildInSchema = Preprocessor.GetBuildInSchema();
                                index = 0;
                                while (index < this.schemas.Count)
                                {
                                    XmlSchema byIndex = (XmlSchema)this.schemas.GetByIndex(index);
                                    Monitor.Enter(byIndex);
                                    if (!byIndex.IsPreprocessed)
                                    {
                                        this.SendValidationEvent(new XmlSchemaException("Sch_SchemaNotPreprocessed", string.Empty), XmlSeverityType.Error);
                                        this.isCompiled = false;
                                        goto Label_01BA;
                                    }
                                    if (byIndex.IsCompiledBySet)
                                    {
                                        if (!this.compileAll)
                                        {
                                            goto Label_00FD;
                                        }
                                        if (byIndex == buildInSchema)
                                        {
                                            compiler.Prepare(byIndex, false);
                                            goto Label_00FD;
                                        }
                                    }
                                    compiler.Prepare(byIndex, true);
Label_00FD:
                                    index++;
                                }
                                this.isCompiled = compiler.Execute(this, schemaCompiledInfo);
                                if (this.isCompiled)
                                {
                                    if (!this.compileAll)
                                    {
                                        schemaCompiledInfo.Add(this.cachedCompiledInfo, this.eventHandler);
                                    }
                                    this.compileAll         = false;
                                    this.cachedCompiledInfo = schemaCompiledInfo;
                                }
                            }
                            finally
                            {
                                if (index == this.schemas.Count)
                                {
                                    index--;
                                }
                                for (int i = index; i >= 0; i--)
                                {
                                    XmlSchema schema3 = (XmlSchema)this.schemas.GetByIndex(i);
                                    if (schema3 == Preprocessor.GetBuildInSchema())
                                    {
                                        Monitor.Exit(schema3);
                                    }
                                    else
                                    {
                                        schema3.IsCompiledBySet = this.isCompiled;
                                        Monitor.Exit(schema3);
                                    }
                                }
                            }
                        }
                        Label_01BA :;
                    }
                }
            }
        }