Inheritance: XmlSchemaContentModel
Example #1
0
        private SOMList GetAttributes(ComplexType ct)
        {
            if (ct.ContentModel == null)
            {
                return(ct.Attributes);
            }

            SimpleModel sc = ct.ContentModel as SimpleModel;

            if (sc != null)
            {
                SimpleExt sce = sc.Content as SimpleExt;
                if (sce != null)
                {
                    return(sce.Attributes);
                }
                SimpleRst scr = sc.Content as SimpleRst;
                if (scr != null)
                {
                    return(scr.Attributes);
                }
                else
                {
                    throw Error(sc, "Invalid simple content model.");
                }
            }
            ComplexModel cc = ct.ContentModel as ComplexModel;

            if (cc != null)
            {
                ComplexExt cce = cc.Content as ComplexExt;
                if (cce != null)
                {
                    return(cce.Attributes);
                }
                ComplexRst ccr = cc.Content as ComplexRst;
                if (ccr != null)
                {
                    return(ccr.Attributes);
                }
                else
                {
                    throw Error(cc, "Invalid simple content model.");
                }
            }
            throw Error(cc, "Invalid complexType. Should not happen.");
        }
Example #2
0
        private void InferAsEmptyElement(Element el, string ns,
                                         bool isNew)
        {
            ComplexType ct = el.SchemaType as ComplexType;

            if (ct != null)
            {
                SimpleModel sm =
                    ct.ContentModel as SimpleModel;
                if (sm != null)
                {
                    ToEmptiableSimpleContent(sm, isNew);
                    return;
                }

                ComplexModel cm = ct.ContentModel
                                  as ComplexModel;
                if (cm != null)
                {
                    ToEmptiableComplexContent(cm, isNew);
                    return;
                }

                if (ct.Particle != null)
                {
                    ct.Particle.MinOccurs = 0;
                }
                return;
            }
            SimpleType st = el.SchemaType as SimpleType;

            if (st != null)
            {
                st = MakeBaseTypeAsEmptiable(st);
                switch (st.QualifiedName.Namespace)
                {
                case XmlSchema.Namespace:
                case XdtNamespace:
                    el.SchemaTypeName = st.QualifiedName;
                    break;

                default:
                    el.SchemaType = st;
                    break;
                }
            }
        }
Example #3
0
        // It makes complexType not to have Simple content model.
        private void ToComplexContentType(ComplexType type)
        {
            SimpleModel sm = type.ContentModel as SimpleModel;

            if (sm == null)
            {
                return;
            }

            SOMList atts = GetAttributes(type);

            foreach (SOMObject o in atts)
            {
                type.Attributes.Add(o);
            }
            // FIXME: need to copy AnyAttribute.
            // (though not considered right now)
            type.ContentModel = null;
            type.IsMixed      = true;
        }
Example #4
0
        private void ToEmptiableSimpleContent(
            SimpleModel sm, bool isNew)
        {
            SimpleExt se = sm.Content as SimpleExt;

            if (se != null)
            {
                se.BaseTypeName = QNameString;
            }
            else
            {
                SimpleRst sr = sm.Content
                               as SimpleRst;
                if (sr == null)
                {
                    throw Error(sm, "Invalid simple content model was passed.");
                }
                sr.BaseTypeName = QNameString;
                sr.BaseType     = null;
            }
        }
Example #5
0
            internal void ProcessAttributes(ref XmlSchemaElement xse, XmlSchemaType effectiveSchemaType, bool bCreatingNewType, XmlSchema parentSchema)
            {
                XmlSchemaObjectCollection attributesSeen = new XmlSchemaObjectCollection();
                XmlSchemaComplexType ct = effectiveSchemaType as XmlSchemaComplexType;
                
                Debug.Assert(xtr.NodeType == XmlNodeType.Attribute);
                do
                {
                    if (xtr.NamespaceURI == XmlSchema.Namespace)
                    {
                        throw new XmlSchemaInferenceException(Res.SchInf_schema, 0, 0);
                    }

                    if (xtr.NamespaceURI == XmlReservedNs.NsXmlNs)
                    {
                        if (xtr.Prefix=="xmlns") 
                            NamespaceManager.AddNamespace(xtr.LocalName, xtr.Value);
                    }
                    else if (xtr.NamespaceURI == XmlReservedNs.NsXsi)
                    {
                        string localName = xtr.LocalName;
                        if (localName == "nil") 
                        {
                            xse.IsNillable = true;
                        }
                        else if (localName != "type" && localName != "schemaLocation" && localName != "noNamespaceSchemaLocation")
                        {
                            throw new XmlSchemaInferenceException(Res.Sch_NotXsiAttribute,localName);
                        }
                    }
                    else
                    {
                        if (ct == null || ct == XmlSchemaComplexType.AnyType)
                        {
                            ct = new XmlSchemaComplexType();
                            xse.SchemaType = ct;
                        }

                        XmlSchemaAttribute xsa=null;
                        //The earlier assumption of checking just schemaTypeName !Empty is not correct for schemas that are not generated by us, schemaTypeName can point to any complex type as well
                        //Check that it is a simple type by checking typeCode
                        //Switch to complex type simple content extension
                        if (effectiveSchemaType != null && effectiveSchemaType.Datatype != null && !xse.SchemaTypeName.IsEmpty)  
                        {
                            //type was previously simple type, now it will become complex with simple type extension
                            Debug.Assert(ct != null);
                            XmlSchemaSimpleContent sc = new XmlSchemaSimpleContent();
                            ct.ContentModel = sc;
                            XmlSchemaSimpleContentExtension sce = new XmlSchemaSimpleContentExtension();
                            sc.Content = sce;
                            sce.BaseTypeName = xse.SchemaTypeName;
                            sce.LineNumber = xse.LineNumber;
                            xse.LineNumber = 0;
                            xse.SchemaTypeName = XmlQualifiedName.Empty; //re-set the name
                        }
                        
                        Debug.Assert(ct != null); //either the user-defined type itself is a complex type or we switched from a simple type to a complex type
                        if (ct.ContentModel != null) 
                        {
                            XmlSchemaSimpleContentExtension sce = CheckSimpleContentExtension(ct);
                            Debug.Assert(sce != null);
                            xsa = AddAttribute(xtr.LocalName, xtr.Prefix, xtr.NamespaceURI, xtr.Value, bCreatingNewType, parentSchema, sce.Attributes, ct.AttributeUses);
                        } 
                        else //add atributes directly to complex type
                        {
                            xsa = AddAttribute(xtr.LocalName, xtr.Prefix, xtr.NamespaceURI, xtr.Value, bCreatingNewType, parentSchema, ct.Attributes, ct.AttributeUses);
                        }
                        if (xsa != null) {
                            attributesSeen.Add(xsa);
                        }
                    }
                    
                } while (xtr.MoveToNextAttribute());
                if (!bCreatingNewType)
                {   
                    //make attributes that did not appear this time optional
                    if (ct!=null) {
                        MakeExistingAttributesOptional(ct, attributesSeen);
                    }
                }
            }
 private XmlQualifiedName ExportStructMapping(StructMapping mapping, string ns, XmlSchemaElement element)
 {
     if (mapping.TypeDesc.IsRoot)
     {
         this.needToExportRoot = true;
         return XmlQualifiedName.Empty;
     }
     if (mapping.IsAnonymousType)
     {
         if (this.references[mapping] != null)
         {
             throw new InvalidOperationException(Res.GetString("XmlCircularReference2", new object[] { mapping.TypeDesc.Name, "AnonymousType", "false" }));
         }
         this.references[mapping] = mapping;
     }
     XmlSchemaComplexType item = (XmlSchemaComplexType) this.types[mapping];
     if (item == null)
     {
         if (!mapping.IncludeInSchema)
         {
             throw new InvalidOperationException(Res.GetString("XmlCannotIncludeInSchema", new object[] { mapping.TypeDesc.Name }));
         }
         this.CheckForDuplicateType(mapping, mapping.Namespace);
         item = new XmlSchemaComplexType();
         if (!mapping.IsAnonymousType)
         {
             item.Name = mapping.TypeName;
             this.AddSchemaItem(item, mapping.Namespace, ns);
             this.types.Add(mapping, item);
         }
         item.IsAbstract = mapping.TypeDesc.IsAbstract;
         bool isOpenModel = mapping.IsOpenModel;
         if ((mapping.BaseMapping != null) && mapping.BaseMapping.IncludeInSchema)
         {
             if (mapping.BaseMapping.IsAnonymousType)
             {
                 throw new InvalidOperationException(Res.GetString("XmlAnonymousBaseType", new object[] { mapping.TypeDesc.Name, mapping.BaseMapping.TypeDesc.Name, "AnonymousType", "false" }));
             }
             if (mapping.HasSimpleContent)
             {
                 XmlSchemaSimpleContent content = new XmlSchemaSimpleContent();
                 XmlSchemaSimpleContentExtension extension = new XmlSchemaSimpleContentExtension {
                     BaseTypeName = this.ExportStructMapping(mapping.BaseMapping, mapping.Namespace, null)
                 };
                 content.Content = extension;
                 item.ContentModel = content;
             }
             else
             {
                 XmlSchemaComplexContentExtension extension2 = new XmlSchemaComplexContentExtension {
                     BaseTypeName = this.ExportStructMapping(mapping.BaseMapping, mapping.Namespace, null)
                 };
                 XmlSchemaComplexContent content2 = new XmlSchemaComplexContent {
                     Content = extension2,
                     IsMixed = XmlSchemaImporter.IsMixed((XmlSchemaComplexType) this.types[mapping.BaseMapping])
                 };
                 item.ContentModel = content2;
             }
             isOpenModel = false;
         }
         this.ExportTypeMembers(item, mapping.Members, mapping.TypeName, mapping.Namespace, mapping.HasSimpleContent, isOpenModel);
         this.ExportDerivedMappings(mapping);
         if (mapping.XmlnsMember != null)
         {
             this.AddXmlnsAnnotation(item, mapping.XmlnsMember.Name);
         }
     }
     else
     {
         this.AddSchemaImport(mapping.Namespace, ns);
     }
     if (mapping.IsAnonymousType)
     {
         this.references[mapping] = null;
         if (element != null)
         {
             element.SchemaType = item;
         }
         return XmlQualifiedName.Empty;
     }
     XmlQualifiedName name = new XmlQualifiedName(item.Name, mapping.Namespace);
     if (element != null)
     {
         element.SchemaTypeName = name;
     }
     return name;
 }
Example #7
0
		void ImportSimpleContent (XmlQualifiedName typeQName, XmlTypeMapping map, XmlSchemaSimpleContent content, CodeIdentifiers classIds, bool isMixed)
		{
			XmlSchemaSimpleContentExtension ext = content.Content as XmlSchemaSimpleContentExtension;
			ClassMap cmap = (ClassMap)map.ObjectMap;
			XmlQualifiedName qname = GetContentBaseType (content.Content);
			TypeData simpleType = null;
			
			if (!IsPrimitiveTypeNamespace (qname.Namespace))
			{
				// Add base map members to this map
	
				XmlTypeMapping baseMap = ImportType (qname, null, true);
				BuildPendingMap (baseMap);
				
				if (baseMap.IsSimpleType) {
					simpleType = baseMap.TypeData;
				} else {
					ClassMap baseClassMap = (ClassMap)baseMap.ObjectMap;
		
					foreach (XmlTypeMapMember member in baseClassMap.AllMembers)
						cmap.AddMember (member);
		
					map.BaseMap = baseMap;
					baseMap.DerivedTypes.Add (map);
				}
			}
			else
				simpleType = FindBuiltInType (qname);
				
			if (simpleType != null) {
				XmlTypeMapMemberElement member = new XmlTypeMapMemberElement ();
				member.Name = classIds.AddUnique("Value", member);
				member.TypeData = simpleType;
				member.ElementInfo.Add (CreateTextElementInfo (typeQName.Namespace, member, member.TypeData));
				member.IsXmlTextCollector = true;
				cmap.AddMember (member);
			}
			
			if (ext != null)
				ImportAttributes (typeQName, cmap, ext.Attributes, ext.AnyAttribute, classIds);
		}
		XmlCompletionItemCollection GetAttributeCompletion(XmlSchemaSimpleContent simpleContent, XmlNamespaceCollection namespacesInScope)
		{
			XmlCompletionItemCollection completionItems = new XmlCompletionItemCollection();
			
			XmlSchemaSimpleContentExtension extension = simpleContent.Content as XmlSchemaSimpleContentExtension;
			if (extension != null) {
				completionItems.AddRange(GetAttributeCompletion(extension, namespacesInScope));
			}
			
			return completionItems;
		}
Example #9
0
        internal static XmlSchemaSimpleContent Read(XmlSchemaReader reader, ValidationEventHandler h)
        {
            XmlSchemaSimpleContent xmlSchemaSimpleContent = new XmlSchemaSimpleContent();

            reader.MoveToElement();
            if (reader.NamespaceURI != "http://www.w3.org/2001/XMLSchema" || reader.LocalName != "simpleContent")
            {
                XmlSchemaObject.error(h, "Should not happen :1: XmlSchemaComplexContent.Read, name=" + reader.Name, null);
                reader.SkipToEnd();
                return(null);
            }
            xmlSchemaSimpleContent.LineNumber   = reader.LineNumber;
            xmlSchemaSimpleContent.LinePosition = reader.LinePosition;
            xmlSchemaSimpleContent.SourceUri    = reader.BaseURI;
            while (reader.MoveToNextAttribute())
            {
                if (reader.Name == "id")
                {
                    xmlSchemaSimpleContent.Id = reader.Value;
                }
                else if ((reader.NamespaceURI == string.Empty && reader.Name != "xmlns") || reader.NamespaceURI == "http://www.w3.org/2001/XMLSchema")
                {
                    XmlSchemaObject.error(h, reader.Name + " is not a valid attribute for simpleContent", null);
                }
                else
                {
                    XmlSchemaUtil.ReadUnhandledAttribute(reader, xmlSchemaSimpleContent);
                }
            }
            reader.MoveToElement();
            if (reader.IsEmptyElement)
            {
                return(xmlSchemaSimpleContent);
            }
            int num = 1;

            while (reader.ReadNextElement())
            {
                if (reader.NodeType == XmlNodeType.EndElement)
                {
                    if (reader.LocalName != "simpleContent")
                    {
                        XmlSchemaObject.error(h, "Should not happen :2: XmlSchemaSimpleContent.Read, name=" + reader.Name, null);
                    }
                    break;
                }
                if (num <= 1 && reader.LocalName == "annotation")
                {
                    num = 2;
                    XmlSchemaAnnotation xmlSchemaAnnotation = XmlSchemaAnnotation.Read(reader, h);
                    if (xmlSchemaAnnotation != null)
                    {
                        xmlSchemaSimpleContent.Annotation = xmlSchemaAnnotation;
                    }
                }
                else
                {
                    if (num <= 2)
                    {
                        if (reader.LocalName == "restriction")
                        {
                            num = 3;
                            XmlSchemaSimpleContentRestriction xmlSchemaSimpleContentRestriction = XmlSchemaSimpleContentRestriction.Read(reader, h);
                            if (xmlSchemaSimpleContentRestriction != null)
                            {
                                xmlSchemaSimpleContent.content = xmlSchemaSimpleContentRestriction;
                            }
                            continue;
                        }
                        if (reader.LocalName == "extension")
                        {
                            num = 3;
                            XmlSchemaSimpleContentExtension xmlSchemaSimpleContentExtension = XmlSchemaSimpleContentExtension.Read(reader, h);
                            if (xmlSchemaSimpleContentExtension != null)
                            {
                                xmlSchemaSimpleContent.content = xmlSchemaSimpleContentExtension;
                            }
                            continue;
                        }
                    }
                    reader.RaiseInvalidElementError();
                }
            }
            return(xmlSchemaSimpleContent);
        }
        static void AddScopesType(DiscoveryVersion discoveryVersion, XmlSchema schema)
        {
            // <xs:complexType name="ScopesType">
            XmlSchemaComplexType scopesType = new XmlSchemaComplexType();
            scopesType.Name = ProtocolStrings.SchemaNames.ScopesType;

            //    <xs:simpleContent>
            XmlSchemaSimpleContent scopesTypeContent = new XmlSchemaSimpleContent();

            //       <xs:extension base="tns:UriListType">
            XmlSchemaSimpleContentExtension scopesTypeContentExtension = new XmlSchemaSimpleContentExtension();
            scopesTypeContentExtension.BaseTypeName = discoveryVersion.Implementation.QualifiedNames.UriListType;

            //          <xs:attribute name="MatchBy" type="xs:anyURI" />    
            XmlSchemaAttribute matchBy = new XmlSchemaAttribute();
            matchBy.Name = ProtocolStrings.SchemaNames.MatchByAttribute;
            matchBy.SchemaTypeName = discoveryVersion.Implementation.QualifiedNames.AnyUriType;

            //          <xs:anyAttribute namespace="##other" processContents="lax" />
            XmlSchemaAnyAttribute anyAttribute = new XmlSchemaAnyAttribute();
            anyAttribute.Namespace = "##other";
            anyAttribute.ProcessContents = XmlSchemaContentProcessing.Lax;

            //       </xs:extension>
            scopesTypeContentExtension.Attributes.Add(matchBy);
            scopesTypeContentExtension.AnyAttribute = anyAttribute;

            //    </xs:simpleContent>
            scopesTypeContent.Content = scopesTypeContentExtension;

            // <xs:complexType name="ScopesType">
            scopesType.ContentModel = scopesTypeContent;

            schema.Items.Add(scopesType);
        }
Example #11
0
		private ComplexType ToComplexType (Element el)
		{
			QName name = el.SchemaTypeName;
			XmlSchemaType type = el.SchemaType;

			// 1. element type is complex.
			ComplexType ct = type as ComplexType;
			if (ct != null)
				return ct;

			// 2. reference to global complexType.
			XmlSchemaType globalType = schemas.GlobalTypes [name]
				as XmlSchemaType;
			ct = globalType as ComplexType;
			if (ct != null)
				return ct;

			ct = new ComplexType ();
			el.SchemaType = ct;
			el.SchemaTypeName = QName.Empty;

			// 3. base type name is xs:anyType or no specification.
			// <xs:complexType />
			if (name == QNameAnyType)
				return ct;
			else if (type == null && name == QName.Empty)
				return ct;

			SimpleModel sc = new SimpleModel ();
			ct.ContentModel = sc;

			// 4. type is simpleType
			//    -> extension of existing simple type.
			SimpleType st = type as SimpleType;
			if (st != null) {
				SimpleRst scr = new SimpleRst ();
				scr.BaseType = st;
				sc.Content = scr;
				return ct;
			}

			SimpleExt sce = new SimpleExt ();
			sc.Content = sce;

			// 5. type name points to primitive type
			//    -> simple extension of a primitive type
			st = XmlSchemaType.GetBuiltInSimpleType (name);
			if (st != null) {
				sce.BaseTypeName = name;
				return ct;
			}

			// 6. type name points to global simpleType.
			st = globalType as SimpleType;
			if (st != null) {
				sce.BaseTypeName = name;
				return ct;
			}

			throw Error (el, "Unexpected schema component that contains simpleTypeName that could not be resolved.");
		}
        XmlSchemaAttribute FindAttribute(XmlSchemaSimpleContent simpleContent, string name)
        {
            XmlSchemaAttribute matchedAttribute = null;

              XmlSchemaSimpleContentExtension extension = simpleContent.Content as XmlSchemaSimpleContentExtension;
              XmlSchemaSimpleContentRestriction restriction = simpleContent.Content as XmlSchemaSimpleContentRestriction;

              if (extension != null)
              {
            matchedAttribute = FindAttribute(extension, name);
              }
              else if (restriction != null)
              {
            //Do Nothing ... for now
              }

              return matchedAttribute;
        }
		XmlCompletionDataCollection GetAttributeCompletionData(XmlSchemaSimpleContent simpleContent)
		{
			XmlCompletionDataCollection data = new XmlCompletionDataCollection();
						
			XmlSchemaSimpleContentExtension extension = simpleContent.Content as XmlSchemaSimpleContentExtension;
			if (extension != null) {
				data.AddRange(GetAttributeCompletionData(extension));
			}
			
			return data;
		}		
Example #14
0
		public virtual void Check (ConformanceCheckContext ctx, XmlSchemaSimpleContent value) {}
Example #15
0
        private static string ProcessSchemaSimpleContent(XmlSchemaSimpleContent schemaContent)
        {
            StringBuilder result = new StringBuilder();
            result.Append(ProcessAnnotation(schemaContent.Annotation));

            if (schemaContent.Content is XmlSchemaSimpleContentExtension)
                result.Append(ProcessSchemaSimpleContentExtension((XmlSchemaSimpleContentExtension)schemaContent.Content));
            else
                result.AppendLine(string.Format("Unsupport content extension: {0}", schemaContent.Content));
            return result.ToString();
        }
 private void Write36_XmlSchemaSimpleContent(XmlSchemaSimpleContent o)
 {
     if (o != null)
     {
         this.WriteStartElement("simpleContent");
         this.WriteAttribute("id", "", o.Id);
         this.WriteAttributes(o.UnhandledAttributes, o);
         this.Write5_XmlSchemaAnnotation(o.Annotation);
         if (o.Content is XmlSchemaSimpleContentRestriction)
         {
             this.Write40_XmlSchemaSimpleContentRestriction((XmlSchemaSimpleContentRestriction) o.Content);
         }
         else if (o.Content is XmlSchemaSimpleContentExtension)
         {
             this.Write38_XmlSchemaSimpleContentExtension((XmlSchemaSimpleContentExtension) o.Content);
         }
         this.WriteEndElement();
     }
 }
Example #17
0
        IEnumerable<XObject> CreateProtoSimpleContent(XmlSchemaSimpleContent simpleContent, XmlSchemaType baseType) {
            var content = simpleContent.Content as XmlSchemaSimpleContentExtension;

            if (baseType != null) {
                foreach (var x in CreateProtoAnyType(baseType)) {
                    yield return x;
                }
            }

            foreach (XmlSchemaAttribute attr in content.Attributes) {
                if (attr.Use == XmlSchemaUse.Required) {
                    var name = attr.QualifiedName.ToXName();
                    string retres = null;
                    if (attr.FixedValue != null) {
                        throw new Exception("not implemented");
                    } else if (attr.DefaultValue != null) {
                        retres = attr.DefaultValue;
                    } else {
                        retres = CreateProtoSimpleType(attr.AttributeSchemaType);
                    }
                    yield return new XAttribute(name, retres);
                }
            }
        }
 protected virtual void Visit(XmlSchemaSimpleContent content)
 {
     Traverse(content.Content);
 }
 internal XmlSchemaObject Clone(XmlSchema parentSchema)
 {
     XmlSchemaComplexType type = (XmlSchemaComplexType) base.MemberwiseClone();
     if (type.ContentModel != null)
     {
         XmlSchemaSimpleContent contentModel = type.ContentModel as XmlSchemaSimpleContent;
         if (contentModel != null)
         {
             XmlSchemaSimpleContent content2 = (XmlSchemaSimpleContent) contentModel.Clone();
             XmlSchemaSimpleContentExtension content = contentModel.Content as XmlSchemaSimpleContentExtension;
             if (content != null)
             {
                 XmlSchemaSimpleContentExtension extension2 = (XmlSchemaSimpleContentExtension) content.Clone();
                 extension2.BaseTypeName = content.BaseTypeName.Clone();
                 extension2.SetAttributes(CloneAttributes(content.Attributes));
                 content2.Content = extension2;
             }
             else
             {
                 XmlSchemaSimpleContentRestriction restriction = (XmlSchemaSimpleContentRestriction) contentModel.Content;
                 XmlSchemaSimpleContentRestriction restriction2 = (XmlSchemaSimpleContentRestriction) restriction.Clone();
                 restriction2.BaseTypeName = restriction.BaseTypeName.Clone();
                 restriction2.SetAttributes(CloneAttributes(restriction.Attributes));
                 content2.Content = restriction2;
             }
             type.ContentModel = content2;
         }
         else
         {
             XmlSchemaComplexContent content3 = (XmlSchemaComplexContent) type.ContentModel;
             XmlSchemaComplexContent content4 = (XmlSchemaComplexContent) content3.Clone();
             XmlSchemaComplexContentExtension extension3 = content3.Content as XmlSchemaComplexContentExtension;
             if (extension3 != null)
             {
                 XmlSchemaComplexContentExtension extension4 = (XmlSchemaComplexContentExtension) extension3.Clone();
                 extension4.BaseTypeName = extension3.BaseTypeName.Clone();
                 extension4.SetAttributes(CloneAttributes(extension3.Attributes));
                 if (HasParticleRef(extension3.Particle, parentSchema))
                 {
                     extension4.Particle = CloneParticle(extension3.Particle, parentSchema);
                 }
                 content4.Content = extension4;
             }
             else
             {
                 XmlSchemaComplexContentRestriction restriction3 = content3.Content as XmlSchemaComplexContentRestriction;
                 XmlSchemaComplexContentRestriction restriction4 = (XmlSchemaComplexContentRestriction) restriction3.Clone();
                 restriction4.BaseTypeName = restriction3.BaseTypeName.Clone();
                 restriction4.SetAttributes(CloneAttributes(restriction3.Attributes));
                 if (HasParticleRef(restriction4.Particle, parentSchema))
                 {
                     restriction4.Particle = CloneParticle(restriction4.Particle, parentSchema);
                 }
                 content4.Content = restriction4;
             }
             type.ContentModel = content4;
         }
     }
     else
     {
         if (HasParticleRef(type.Particle, parentSchema))
         {
             type.Particle = CloneParticle(type.Particle, parentSchema);
         }
         type.SetAttributes(CloneAttributes(type.Attributes));
     }
     type.ClearCompiledState();
     return type;
 }
Example #20
0
        private void BuildSimpleContent(XmlSchemaSimpleContent simpleContent, CodeTypeDeclaration codeType)
        {
            // SimpleContent can contain Extension, Restriction or Annotation(ignored) elements
            if (simpleContent.Content is XmlSchemaSimpleContentExtension)
            {
                // Extension has a base type and can contain attribute, anyAttribute,
                // attributeGroup(not supported) or annotation(ignored) elements

                // Get the extension type
                XmlSchemaSimpleContentExtension extension = (XmlSchemaSimpleContentExtension)simpleContent.Content;

                // Add Attributes
                foreach (XmlSchemaAttribute attribute in extension.Attributes)
                {
                    WriteAttributeField(attribute, codeType, attribute.DefaultValue);
                }

                // Add anyAttribute if not null
                if (extension.AnyAttribute != null)
                    WriteAnyAttribute(extension.AnyAttribute, codeType);
            }
            else if (simpleContent.Content.GetType() == typeof(XmlSchemaSimpleContentRestriction))
            {
                // Restriction has a base attribute and can contain attribute, anyAttribute, simpleType, attributeGroup(not supported) or annotation(ignored) elements
                // and facets
                XmlSchemaSimpleContentRestriction restriction = (XmlSchemaSimpleContentRestriction)simpleContent.Content;

                // Add Attributes
                foreach (XmlSchemaAttribute attribute in restriction.Attributes)
                {
                    WriteAttributeField(attribute, codeType, attribute.DefaultValue);
                }

                // need to add restriction facet processing
                foreach (XmlSchemaFacet facet in restriction.Facets)
                {
                }

                // Add anyAttribute if not null
                if (restriction.AnyAttribute != null)
                    WriteAnyAttribute(restriction.AnyAttribute, codeType);
            }
        }
Example #21
0
        void ExportTypeMembers(XmlSchemaComplexType type, MemberMapping[] members, string name, string ns, bool hasSimpleContent, bool openModel) {
            XmlSchemaGroupBase seq = new XmlSchemaSequence();
            TypeMapping textMapping = null;
            
            for (int i = 0; i < members.Length; i++) {
                MemberMapping member = members[i];
                if (member.Ignore)
                    continue;
                if (member.Text != null) {
                    if (textMapping != null) {
                        throw new InvalidOperationException(Res.GetString(Res.XmlIllegalMultipleText, name));
                    }
                    textMapping = member.Text.Mapping;
                }
                if (member.Elements.Length > 0) {
                    bool repeats = member.TypeDesc.IsArrayLike && 
                        !(member.Elements.Length == 1 && member.Elements[0].Mapping is ArrayMapping);

                    bool valueTypeOptional = member.CheckSpecified != SpecifiedAccessor.None || member.CheckShouldPersist;
                    ExportElementAccessors(seq, member.Elements, repeats, valueTypeOptional, ns);
                }
            }

            if (seq.Items.Count > 0) {
                if (type.ContentModel != null) {
                    if (type.ContentModel.Content is XmlSchemaComplexContentRestriction)
                        ((XmlSchemaComplexContentRestriction)type.ContentModel.Content).Particle = seq;
                    else if (type.ContentModel.Content is XmlSchemaComplexContentExtension)
                        ((XmlSchemaComplexContentExtension)type.ContentModel.Content).Particle = seq;
                    else
                        throw new InvalidOperationException(Res.GetString(Res.XmlInvalidContent, type.ContentModel.Content.GetType().Name));
                }
                else {
                    type.Particle = seq;
                }
            }
            if (textMapping != null) {
                if (hasSimpleContent) {
                    if (textMapping is PrimitiveMapping && seq.Items.Count == 0) {
                        PrimitiveMapping pm = (PrimitiveMapping)textMapping;
                        if (pm.IsList) {
                            type.IsMixed = true;
                        }
                        else {
                            if (pm.IsAnonymousType) {
                                throw new InvalidOperationException(Res.GetString(Res.XmlAnonymousBaseType, textMapping.TypeDesc.Name, pm.TypeDesc.Name, "AnonymousType", "false"));
                            }
                            // Create simpleContent
                            XmlSchemaSimpleContent model = new XmlSchemaSimpleContent();
                            XmlSchemaSimpleContentExtension ex = new XmlSchemaSimpleContentExtension();
                            model.Content = ex;
                            type.ContentModel = model;
                            ex.BaseTypeName = ExportPrimitiveMapping(pm, ns);
                        }
                    }
                }
                else {
                    type.IsMixed = true;
                }
            }
            bool anyAttribute = false;
            for (int i = 0; i < members.Length; i++) {
                AttributeAccessor accessor = members[i].Attribute;
                if (accessor != null) {
                    ExportAttributeAccessor(type, members[i].Attribute, members[i].CheckSpecified != SpecifiedAccessor.None || members[i].CheckShouldPersist, ns);
                    if (members[i].Attribute.Any)
                        anyAttribute = true;
                }
            }
            if (openModel && !anyAttribute) {
                AttributeAccessor any = new AttributeAccessor();
                any.Any = true;
                ExportAttributeAccessor(type, any, false, ns);
            }
        }
 internal void ProcessAttributes(ref XmlSchemaElement xse, XmlSchemaType effectiveSchemaType, bool bCreatingNewType, XmlSchema parentSchema)
 {
     XmlSchemaObjectCollection attributesInInstance = new XmlSchemaObjectCollection();
     XmlSchemaComplexType ct = effectiveSchemaType as XmlSchemaComplexType;
     do
     {
         if (this.xtr.NamespaceURI == "http://www.w3.org/2001/XMLSchema")
         {
             throw new XmlSchemaInferenceException("SchInf_schema", 0, 0);
         }
         if (this.xtr.NamespaceURI == "http://www.w3.org/2000/xmlns/")
         {
             if (this.xtr.Prefix == "xmlns")
             {
                 this.NamespaceManager.AddNamespace(this.xtr.LocalName, this.xtr.Value);
             }
         }
         else if (this.xtr.NamespaceURI == "http://www.w3.org/2001/XMLSchema-instance")
         {
             string localName = this.xtr.LocalName;
             if (localName != "nil")
             {
                 if (((localName != "type") && (localName != "schemaLocation")) && (localName != "noNamespaceSchemaLocation"))
                 {
                     throw new XmlSchemaInferenceException("Sch_NotXsiAttribute", localName);
                 }
             }
             else
             {
                 xse.IsNillable = true;
             }
         }
         else
         {
             if ((ct == null) || (ct == XmlSchemaComplexType.AnyType))
             {
                 ct = new XmlSchemaComplexType();
                 xse.SchemaType = ct;
             }
             XmlSchemaAttribute item = null;
             if (((effectiveSchemaType != null) && (effectiveSchemaType.Datatype != null)) && !xse.SchemaTypeName.IsEmpty)
             {
                 XmlSchemaSimpleContent content = new XmlSchemaSimpleContent();
                 ct.ContentModel = content;
                 XmlSchemaSimpleContentExtension extension = new XmlSchemaSimpleContentExtension();
                 content.Content = extension;
                 extension.BaseTypeName = xse.SchemaTypeName;
                 extension.LineNumber = xse.LineNumber;
                 xse.LineNumber = 0;
                 xse.SchemaTypeName = XmlQualifiedName.Empty;
             }
             if (ct.ContentModel != null)
             {
                 XmlSchemaSimpleContentExtension extension2 = this.CheckSimpleContentExtension(ct);
                 item = this.AddAttribute(this.xtr.LocalName, this.xtr.Prefix, this.xtr.NamespaceURI, this.xtr.Value, bCreatingNewType, parentSchema, extension2.Attributes, ct.AttributeUses);
             }
             else
             {
                 item = this.AddAttribute(this.xtr.LocalName, this.xtr.Prefix, this.xtr.NamespaceURI, this.xtr.Value, bCreatingNewType, parentSchema, ct.Attributes, ct.AttributeUses);
             }
             if (item != null)
             {
                 attributesInInstance.Add(item);
             }
         }
     }
     while (this.xtr.MoveToNextAttribute());
     if (!bCreatingNewType && (ct != null))
     {
         this.MakeExistingAttributesOptional(ct, attributesInInstance);
     }
 }
Example #23
0
        void WriteSimpleContent(XmlTextWriter xtw, XmlSchemaSimpleContent content)
        {
            XmlSchemaSimpleContentExtension ext = content.Content as XmlSchemaSimpleContentExtension;
            if (ext != null)
                WriteAttributes(xtw, ext.Attributes, ext.AnyAttribute);

            XmlQualifiedName qname = GetContentBaseType(content.Content);
            xtw.WriteString(GetLiteral(FindBuiltInType(qname)));
        }
Example #24
0
 private static void AddElementAndType(XmlSchema schema, string baseXsdType, string ns)
 {
     XmlSchemaElement item = new XmlSchemaElement();
     item.Name = baseXsdType;
     item.SchemaTypeName = new XmlQualifiedName(baseXsdType, ns);
     schema.Items.Add(item);
     XmlSchemaComplexType type = new XmlSchemaComplexType();
     type.Name = baseXsdType;
     XmlSchemaSimpleContent content = new XmlSchemaSimpleContent();
     type.ContentModel = content;
     XmlSchemaSimpleContentExtension extension = new XmlSchemaSimpleContentExtension();
     extension.BaseTypeName = new XmlQualifiedName(baseXsdType, "http://www.w3.org/2001/XMLSchema");
     content.Content = extension;
     schema.Items.Add(type);
 }
Example #25
0
		private void ToEmptiableSimpleContent (
			SimpleModel sm, bool isNew)
		{
			SimpleExt se = sm.Content as SimpleExt;
			if (se != null)
				se.BaseTypeName = QNameString;
			else {
				SimpleRst sr = sm.Content
					as SimpleRst;
				if (sr == null)
					throw Error (sm, "Invalid simple content model was passed.");
				sr.BaseTypeName = QNameString;
				sr.BaseType = null;
			}
		}
Example #26
0
        private void InferTextContent(Element el, bool isNew)
        {
            string value = source.ReadString();

            if (el.SchemaType == null)
            {
                if (el.SchemaTypeName == QName.Empty)
                {
                    // no type information -> infer type
                    if (isNew)
                    {
                        el.SchemaTypeName =
                            InferSimpleType(
                                value);
                    }
                    else
                    {
                        el.SchemaTypeName =
                            QNameString;
                    }
                    return;
                }
                switch (el.SchemaTypeName.Namespace)
                {
                case XmlSchema.Namespace:
                case XdtNamespace:
                    // existing primitive type
                    el.SchemaTypeName = InferMergedType(
                        value, el.SchemaTypeName);
                    break;

                default:
                    ComplexType ct = schemas.GlobalTypes [
                        el.SchemaTypeName]
                                     as ComplexType;
                    // If it is complex, then just set
                    // mixed='true' (type cannot be set.)
                    // If it is simple, then we cannot
                    // make sure that string value is
                    // valid. So just set as xs:string.
                    if (ct != null)
                    {
                        MarkAsMixed(ct);
                    }
                    else
                    {
                        el.SchemaTypeName = QNameString;
                    }
                    break;
                }
                return;
            }
            // simpleType
            SimpleType st = el.SchemaType as SimpleType;

            if (st != null)
            {
                // If simple, then (described above)
                el.SchemaType     = null;
                el.SchemaTypeName = QNameString;
                return;
            }

            // complexType
            ComplexType ect = el.SchemaType as ComplexType;

            SimpleModel sm = ect.ContentModel as SimpleModel;

            if (sm == null)
            {
                // - ComplexContent
                MarkAsMixed(ect);
                return;
            }

            // - SimpleContent
            SimpleExt se = sm.Content as SimpleExt;

            if (se != null)
            {
                se.BaseTypeName = InferMergedType(value,
                                                  se.BaseTypeName);
            }
            SimpleRst sr = sm.Content as SimpleRst;

            if (sr != null)
            {
                sr.BaseTypeName = InferMergedType(value,
                                                  sr.BaseTypeName);
                sr.BaseType = null;
            }
        }
		void ImportSimpleContent (XmlQualifiedName typeQName, XmlTypeMapping map, XmlSchemaSimpleContent content, CodeIdentifiers classIds, bool isMixed)
		{
			ClassMap cmap = (ClassMap)map.ObjectMap;
			
			XmlQualifiedName qname = GetContentBaseType (content.Content);

			XmlTypeMapMemberElement member = new XmlTypeMapMemberElement ();
			member.Name = classIds.AddUnique("Value", member);
			member.TypeData = FindBuiltInType (qname);
			member.ElementInfo.Add (CreateTextElementInfo (typeQName.Namespace, member, member.TypeData));
			member.IsXmlTextCollector = true;
			cmap.AddMember (member);

			XmlSchemaSimpleContentExtension ext = content.Content as XmlSchemaSimpleContentExtension;
			if (ext != null)
				ImportAttributes (typeQName, cmap, ext.Attributes, ext.AnyAttribute, classIds);
		}
 internal void InferElement(XmlSchemaElement xse, bool bCreatingNewType, XmlSchema parentSchema)
 {
     bool isEmptyElement = this.xtr.IsEmptyElement;
     int lastUsedSeqItem = -1;
     Hashtable hashtable = new Hashtable();
     XmlSchemaType effectiveSchemaType = this.GetEffectiveSchemaType(xse, bCreatingNewType);
     XmlSchemaComplexType ct = effectiveSchemaType as XmlSchemaComplexType;
     if (this.xtr.MoveToFirstAttribute())
     {
         this.ProcessAttributes(ref xse, effectiveSchemaType, bCreatingNewType, parentSchema);
     }
     else if (!bCreatingNewType && (ct != null))
     {
         this.MakeExistingAttributesOptional(ct, null);
     }
     if ((ct == null) || (ct == XmlSchemaComplexType.AnyType))
     {
         ct = xse.SchemaType as XmlSchemaComplexType;
     }
     if (isEmptyElement)
     {
         if (!bCreatingNewType)
         {
             if (ct == null)
             {
                 if (!xse.SchemaTypeName.IsEmpty)
                 {
                     xse.LineNumber = 0x40000;
                     xse.SchemaTypeName = ST_string;
                 }
             }
             else if (ct.Particle != null)
             {
                 ct.Particle.MinOccurs = 0M;
             }
             else if (ct.ContentModel != null)
             {
                 XmlSchemaSimpleContentExtension extension = this.CheckSimpleContentExtension(ct);
                 extension.BaseTypeName = ST_string;
                 extension.LineNumber = 0x40000;
             }
         }
         else
         {
             xse.LineNumber = 0x40000;
         }
     }
     else
     {
         bool flag2 = false;
         do
         {
             this.xtr.Read();
             if (this.xtr.NodeType == XmlNodeType.Whitespace)
             {
                 flag2 = true;
             }
             if (this.xtr.NodeType == XmlNodeType.EntityReference)
             {
                 throw new XmlSchemaInferenceException("SchInf_entity", 0, 0);
             }
         }
         while ((!this.xtr.EOF && (this.xtr.NodeType != XmlNodeType.EndElement)) && (((this.xtr.NodeType != XmlNodeType.CDATA) && (this.xtr.NodeType != XmlNodeType.Element)) && (this.xtr.NodeType != XmlNodeType.Text)));
         if (this.xtr.NodeType == XmlNodeType.EndElement)
         {
             if (flag2)
             {
                 if (ct != null)
                 {
                     if (ct.ContentModel != null)
                     {
                         XmlSchemaSimpleContentExtension extension2 = this.CheckSimpleContentExtension(ct);
                         extension2.BaseTypeName = ST_string;
                         extension2.LineNumber = 0x40000;
                     }
                     else if (bCreatingNewType)
                     {
                         XmlSchemaSimpleContent content = new XmlSchemaSimpleContent();
                         ct.ContentModel = content;
                         XmlSchemaSimpleContentExtension simpleContentExtension = new XmlSchemaSimpleContentExtension();
                         content.Content = simpleContentExtension;
                         this.MoveAttributes(ct, simpleContentExtension, bCreatingNewType);
                         simpleContentExtension.BaseTypeName = ST_string;
                         simpleContentExtension.LineNumber = 0x40000;
                     }
                     else
                     {
                         ct.IsMixed = true;
                     }
                 }
                 else
                 {
                     xse.SchemaTypeName = ST_string;
                     xse.LineNumber = 0x40000;
                 }
             }
             if (bCreatingNewType)
             {
                 xse.LineNumber = 0x40000;
             }
             else if (ct != null)
             {
                 if (ct.Particle != null)
                 {
                     ct.Particle.MinOccurs = 0M;
                 }
                 else if (ct.ContentModel != null)
                 {
                     XmlSchemaSimpleContentExtension extension4 = this.CheckSimpleContentExtension(ct);
                     extension4.BaseTypeName = ST_string;
                     extension4.LineNumber = 0x40000;
                 }
             }
             else if (!xse.SchemaTypeName.IsEmpty)
             {
                 xse.LineNumber = 0x40000;
                 xse.SchemaTypeName = ST_string;
             }
         }
         else
         {
             int num2 = 0;
             bool flag3 = false;
             while (!this.xtr.EOF && (this.xtr.NodeType != XmlNodeType.EndElement))
             {
                 bool flag4 = false;
                 num2++;
                 if ((this.xtr.NodeType == XmlNodeType.Text) || (this.xtr.NodeType == XmlNodeType.CDATA))
                 {
                     if (ct != null)
                     {
                         if (ct.Particle != null)
                         {
                             ct.IsMixed = true;
                             if (num2 == 1)
                             {
                                 do
                                 {
                                     this.xtr.Read();
                                 }
                                 while (!this.xtr.EOF && ((((this.xtr.NodeType == XmlNodeType.CDATA) || (this.xtr.NodeType == XmlNodeType.Text)) || ((this.xtr.NodeType == XmlNodeType.Comment) || (this.xtr.NodeType == XmlNodeType.ProcessingInstruction))) || (((this.xtr.NodeType == XmlNodeType.Whitespace) || (this.xtr.NodeType == XmlNodeType.SignificantWhitespace)) || (this.xtr.NodeType == XmlNodeType.XmlDeclaration))));
                                 flag4 = true;
                                 if (this.xtr.NodeType == XmlNodeType.EndElement)
                                 {
                                     ct.Particle.MinOccurs = 0M;
                                 }
                             }
                         }
                         else if (ct.ContentModel != null)
                         {
                             XmlSchemaSimpleContentExtension extension5 = this.CheckSimpleContentExtension(ct);
                             if ((this.xtr.NodeType == XmlNodeType.Text) && (num2 == 1))
                             {
                                 int iTypeFlags = -1;
                                 if (xse.Parent == null)
                                 {
                                     iTypeFlags = extension5.LineNumber;
                                 }
                                 else
                                 {
                                     iTypeFlags = GetSchemaType(extension5.BaseTypeName);
                                     xse.Parent = null;
                                 }
                                 extension5.BaseTypeName = this.RefineSimpleType(this.xtr.Value, ref iTypeFlags);
                                 extension5.LineNumber = iTypeFlags;
                             }
                             else
                             {
                                 extension5.BaseTypeName = ST_string;
                                 extension5.LineNumber = 0x40000;
                             }
                         }
                         else
                         {
                             XmlSchemaSimpleContent content2 = new XmlSchemaSimpleContent();
                             ct.ContentModel = content2;
                             XmlSchemaSimpleContentExtension extension6 = new XmlSchemaSimpleContentExtension();
                             content2.Content = extension6;
                             this.MoveAttributes(ct, extension6, bCreatingNewType);
                             if (this.xtr.NodeType == XmlNodeType.Text)
                             {
                                 int num4;
                                 if (!bCreatingNewType)
                                 {
                                     num4 = 0x40000;
                                 }
                                 else
                                 {
                                     num4 = -1;
                                 }
                                 extension6.BaseTypeName = this.RefineSimpleType(this.xtr.Value, ref num4);
                                 extension6.LineNumber = num4;
                             }
                             else
                             {
                                 extension6.BaseTypeName = ST_string;
                                 extension6.LineNumber = 0x40000;
                             }
                         }
                     }
                     else if (num2 > 1)
                     {
                         xse.SchemaTypeName = ST_string;
                         xse.LineNumber = 0x40000;
                     }
                     else
                     {
                         int lineNumber = -1;
                         if (bCreatingNewType)
                         {
                             if (this.xtr.NodeType == XmlNodeType.Text)
                             {
                                 xse.SchemaTypeName = this.RefineSimpleType(this.xtr.Value, ref lineNumber);
                                 xse.LineNumber = lineNumber;
                             }
                             else
                             {
                                 xse.SchemaTypeName = ST_string;
                                 xse.LineNumber = 0x40000;
                             }
                         }
                         else if (this.xtr.NodeType == XmlNodeType.Text)
                         {
                             if (xse.Parent == null)
                             {
                                 lineNumber = xse.LineNumber;
                             }
                             else
                             {
                                 lineNumber = GetSchemaType(xse.SchemaTypeName);
                                 if ((lineNumber == -1) && (xse.LineNumber == 0x40000))
                                 {
                                     lineNumber = 0x40000;
                                 }
                                 xse.Parent = null;
                             }
                             xse.SchemaTypeName = this.RefineSimpleType(this.xtr.Value, ref lineNumber);
                             xse.LineNumber = lineNumber;
                         }
                         else
                         {
                             xse.SchemaTypeName = ST_string;
                             xse.LineNumber = 0x40000;
                         }
                     }
                 }
                 else if (this.xtr.NodeType == XmlNodeType.Element)
                 {
                     XmlQualifiedName key = new XmlQualifiedName(this.xtr.LocalName, this.xtr.NamespaceURI);
                     bool setMaxoccurs = false;
                     if (hashtable.Contains(key))
                     {
                         setMaxoccurs = true;
                     }
                     else
                     {
                         hashtable.Add(key, null);
                     }
                     if (ct == null)
                     {
                         ct = new XmlSchemaComplexType();
                         xse.SchemaType = ct;
                         if (!xse.SchemaTypeName.IsEmpty)
                         {
                             ct.IsMixed = true;
                             xse.SchemaTypeName = XmlQualifiedName.Empty;
                         }
                     }
                     if (ct.ContentModel != null)
                     {
                         XmlSchemaSimpleContentExtension scExtension = this.CheckSimpleContentExtension(ct);
                         this.MoveAttributes(scExtension, ct);
                         ct.ContentModel = null;
                         ct.IsMixed = true;
                         if (ct.Particle != null)
                         {
                             throw new XmlSchemaInferenceException("SchInf_particle", 0, 0);
                         }
                         ct.Particle = new XmlSchemaSequence();
                         flag3 = true;
                         this.AddElement(this.xtr.LocalName, this.xtr.Prefix, this.xtr.NamespaceURI, parentSchema, ((XmlSchemaSequence) ct.Particle).Items, -1);
                         lastUsedSeqItem = 0;
                         if (!bCreatingNewType)
                         {
                             ct.Particle.MinOccurs = 0M;
                         }
                     }
                     else if (ct.Particle == null)
                     {
                         ct.Particle = new XmlSchemaSequence();
                         flag3 = true;
                         this.AddElement(this.xtr.LocalName, this.xtr.Prefix, this.xtr.NamespaceURI, parentSchema, ((XmlSchemaSequence) ct.Particle).Items, -1);
                         if (!bCreatingNewType)
                         {
                             ((XmlSchemaSequence) ct.Particle).MinOccurs = 0M;
                         }
                         lastUsedSeqItem = 0;
                     }
                     else
                     {
                         bool bParticleChanged = false;
                         this.FindMatchingElement(bCreatingNewType || flag3, this.xtr, ct, ref lastUsedSeqItem, ref bParticleChanged, parentSchema, setMaxoccurs);
                     }
                 }
                 else if (this.xtr.NodeType == XmlNodeType.Text)
                 {
                     if (ct == null)
                     {
                         throw new XmlSchemaInferenceException("SchInf_ct", 0, 0);
                     }
                     ct.IsMixed = true;
                 }
                 do
                 {
                     if (this.xtr.NodeType == XmlNodeType.EntityReference)
                     {
                         throw new XmlSchemaInferenceException("SchInf_entity", 0, 0);
                     }
                     if (!flag4)
                     {
                         this.xtr.Read();
                     }
                     else
                     {
                         flag4 = false;
                     }
                 }
                 while ((!this.xtr.EOF && (this.xtr.NodeType != XmlNodeType.EndElement)) && (((this.xtr.NodeType != XmlNodeType.CDATA) && (this.xtr.NodeType != XmlNodeType.Element)) && (this.xtr.NodeType != XmlNodeType.Text)));
             }
             if (lastUsedSeqItem != -1)
             {
                 while (++lastUsedSeqItem < ((XmlSchemaSequence) ct.Particle).Items.Count)
                 {
                     if (((XmlSchemaSequence) ct.Particle).Items[lastUsedSeqItem].GetType() != typeof(XmlSchemaElement))
                     {
                         throw new XmlSchemaInferenceException("SchInf_seq", 0, 0);
                     }
                     XmlSchemaElement element = (XmlSchemaElement) ((XmlSchemaSequence) ct.Particle).Items[lastUsedSeqItem];
                     element.MinOccurs = 0M;
                 }
             }
         }
     }
 }
Example #29
0
        private ComplexType ToComplexType(Element el)
        {
            QName         name = el.SchemaTypeName;
            XmlSchemaType type = el.SchemaType;

            // 1. element type is complex.
            ComplexType ct = type as ComplexType;

            if (ct != null)
            {
                return(ct);
            }

            // 2. reference to global complexType.
            XmlSchemaType globalType = schemas.GlobalTypes [name]
                                       as XmlSchemaType;

            ct = globalType as ComplexType;
            if (ct != null)
            {
                return(ct);
            }

            ct                = new ComplexType();
            el.SchemaType     = ct;
            el.SchemaTypeName = QName.Empty;

            // 3. base type name is xs:anyType or no specification.
            // <xs:complexType />
            if (name == QNameAnyType)
            {
                return(ct);
            }
            else if (type == null && name == QName.Empty)
            {
                return(ct);
            }

            SimpleModel sc = new SimpleModel();

            ct.ContentModel = sc;

            // 4. type is simpleType
            //    -> extension of existing simple type.
            SimpleType st = type as SimpleType;

            if (st != null)
            {
                SimpleRst scr = new SimpleRst();
                scr.BaseType = st;
                sc.Content   = scr;
                return(ct);
            }

            SimpleExt sce = new SimpleExt();

            sc.Content = sce;

            // 5. type name points to primitive type
            //    -> simple extension of a primitive type
            st = XmlSchemaType.GetBuiltInSimpleType(name);
            if (st != null)
            {
                sce.BaseTypeName = name;
                return(ct);
            }

            // 6. type name points to global simpleType.
            st = globalType as SimpleType;
            if (st != null)
            {
                sce.BaseTypeName = name;
                return(ct);
            }

            throw Error(el, "Unexpected schema component that contains simpleTypeName that could not be resolved.");
        }
Example #30
0
		void ExportClassSchema (XmlTypeMapping map)
		{
			if (IsMapExported (map)) return;
			SetMapExported (map);
			
			if (map.TypeData.Type == typeof(object))
			{
				foreach (XmlTypeMapping dmap in map.DerivedTypes)
					if (dmap.TypeData.SchemaType == SchemaTypes.Class) ExportClassSchema (dmap);
				return;
			}

			XmlSchema schema = GetSchema (map.XmlTypeNamespace);
			XmlSchemaComplexType stype = new XmlSchemaComplexType ();
			stype.Name = map.XmlType;
			schema.Items.Add (stype);

			ClassMap cmap = (ClassMap)map.ObjectMap;

			if (cmap.HasSimpleContent)
			{
				XmlSchemaSimpleContent simple = new XmlSchemaSimpleContent ();
				stype.ContentModel = simple;
				XmlSchemaSimpleContentExtension ext = new XmlSchemaSimpleContentExtension ();
				simple.Content = ext;
				XmlSchemaSequence particle;
				XmlSchemaAnyAttribute anyAttribute;
				ExportMembersMapSchema (schema, cmap, map.BaseMap, ext.Attributes, out particle, out anyAttribute);
				ext.AnyAttribute = anyAttribute;
				if (map.BaseMap == null)
					ext.BaseTypeName = cmap.SimpleContentBaseType;
				else {
					ext.BaseTypeName = new XmlQualifiedName (map.BaseMap.XmlType, map.BaseMap.XmlTypeNamespace);
					ImportNamespace (schema, map.BaseMap.XmlTypeNamespace);
					ExportClassSchema (map.BaseMap);
				}
			}
			else if (map.BaseMap != null && map.BaseMap.IncludeInSchema)
			{
				XmlSchemaComplexContent cstype = new XmlSchemaComplexContent ();
				XmlSchemaComplexContentExtension ext = new XmlSchemaComplexContentExtension ();
				ext.BaseTypeName = new XmlQualifiedName (map.BaseMap.XmlType, map.BaseMap.XmlTypeNamespace);
				cstype.Content = ext;
				stype.ContentModel = cstype;

				XmlSchemaSequence particle;
				XmlSchemaAnyAttribute anyAttribute;
				ExportMembersMapSchema (schema, cmap, map.BaseMap, ext.Attributes, out particle, out anyAttribute);
				ext.Particle = particle;
				ext.AnyAttribute = anyAttribute;
				stype.IsMixed = HasMixedContent (map);
				cstype.IsMixed = BaseHasMixedContent (map);

				ImportNamespace (schema, map.BaseMap.XmlTypeNamespace);
				ExportClassSchema (map.BaseMap);
			}
			else
			{
				XmlSchemaSequence particle;
				XmlSchemaAnyAttribute anyAttribute;
				ExportMembersMapSchema (schema, cmap, map.BaseMap, stype.Attributes, out particle, out anyAttribute);
				stype.Particle = particle;
				stype.AnyAttribute = anyAttribute;
				stype.IsMixed = cmap.XmlTextCollector != null;
			}
			
			foreach (XmlTypeMapping dmap in map.DerivedTypes)
				if (dmap.TypeData.SchemaType == SchemaTypes.Class) ExportClassSchema (dmap);
		}
		void GetAttributeCompletionData (XmlCompletionDataList data, XmlSchemaSimpleContent simpleContent)
		{
			var extension = simpleContent.Content as XmlSchemaSimpleContentExtension;
			if (extension != null)
				GetAttributeCompletionData (data, extension);
		}
Example #32
0
        private static void AddElementAndType(XmlSchema schema, string baseXsdType, string ns) {
            // name the element and soap-encoding type the same as the base XSD type
            XmlSchemaElement el = new XmlSchemaElement();
            el.Name = baseXsdType;
            el.SchemaTypeName = new XmlQualifiedName(baseXsdType, ns);
            schema.Items.Add(el);

            XmlSchemaComplexType type = new XmlSchemaComplexType();
            type.Name = baseXsdType;
            XmlSchemaSimpleContent model = new XmlSchemaSimpleContent();
            type.ContentModel = model;

            XmlSchemaSimpleContentExtension ex = new XmlSchemaSimpleContentExtension();
            ex.BaseTypeName = new XmlQualifiedName(baseXsdType, XmlSchema.Namespace);
            model.Content = ex;
            schema.Items.Add(type);
        }
 private void ExportTypeMembers(XmlSchemaComplexType type, MemberMapping[] members, string name, string ns, bool hasSimpleContent, bool openModel)
 {
     XmlSchemaGroupBase group = new XmlSchemaSequence();
     TypeMapping mapping = null;
     for (int i = 0; i < members.Length; i++)
     {
         MemberMapping mapping2 = members[i];
         if (!mapping2.Ignore)
         {
             if (mapping2.Text != null)
             {
                 if (mapping != null)
                 {
                     throw new InvalidOperationException(Res.GetString("XmlIllegalMultipleText", new object[] { name }));
                 }
                 mapping = mapping2.Text.Mapping;
             }
             if (mapping2.Elements.Length > 0)
             {
                 bool repeats = mapping2.TypeDesc.IsArrayLike && ((mapping2.Elements.Length != 1) || !(mapping2.Elements[0].Mapping is ArrayMapping));
                 bool valueTypeOptional = (mapping2.CheckSpecified != SpecifiedAccessor.None) || mapping2.CheckShouldPersist;
                 this.ExportElementAccessors(group, mapping2.Elements, repeats, valueTypeOptional, ns);
             }
         }
     }
     if (group.Items.Count > 0)
     {
         if (type.ContentModel != null)
         {
             if (type.ContentModel.Content is XmlSchemaComplexContentRestriction)
             {
                 ((XmlSchemaComplexContentRestriction) type.ContentModel.Content).Particle = group;
             }
             else
             {
                 if (!(type.ContentModel.Content is XmlSchemaComplexContentExtension))
                 {
                     throw new InvalidOperationException(Res.GetString("XmlInvalidContent", new object[] { type.ContentModel.Content.GetType().Name }));
                 }
                 ((XmlSchemaComplexContentExtension) type.ContentModel.Content).Particle = group;
             }
         }
         else
         {
             type.Particle = group;
         }
     }
     if (mapping != null)
     {
         if (hasSimpleContent)
         {
             if ((mapping is PrimitiveMapping) && (group.Items.Count == 0))
             {
                 PrimitiveMapping mapping3 = (PrimitiveMapping) mapping;
                 if (mapping3.IsList)
                 {
                     type.IsMixed = true;
                 }
                 else
                 {
                     if (mapping3.IsAnonymousType)
                     {
                         throw new InvalidOperationException(Res.GetString("XmlAnonymousBaseType", new object[] { mapping.TypeDesc.Name, mapping3.TypeDesc.Name, "AnonymousType", "false" }));
                     }
                     XmlSchemaSimpleContent content = new XmlSchemaSimpleContent();
                     XmlSchemaSimpleContentExtension extension = new XmlSchemaSimpleContentExtension();
                     content.Content = extension;
                     type.ContentModel = content;
                     extension.BaseTypeName = this.ExportPrimitiveMapping(mapping3, ns);
                 }
             }
         }
         else
         {
             type.IsMixed = true;
         }
     }
     bool flag3 = false;
     for (int j = 0; j < members.Length; j++)
     {
         if (members[j].Attribute != null)
         {
             this.ExportAttributeAccessor(type, members[j].Attribute, (members[j].CheckSpecified != SpecifiedAccessor.None) || members[j].CheckShouldPersist, ns);
             if (members[j].Attribute.Any)
             {
                 flag3 = true;
             }
         }
     }
     if (openModel && !flag3)
     {
         AttributeAccessor accessor = new AttributeAccessor {
             Any = true
         };
         this.ExportAttributeAccessor(type, accessor, false, ns);
     }
 }
Example #34
0
		//<simpleContent 
		//  id = ID 
		//  {any attributes with non-schema namespace . . .}>
		//  Content: (annotation?, (restriction | extension))
		//</simpleContent>
		internal static XmlSchemaSimpleContent Read(XmlSchemaReader reader, ValidationEventHandler h)
		{
			XmlSchemaSimpleContent simple = new XmlSchemaSimpleContent();
			reader.MoveToElement();

			if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
			{
				error(h,"Should not happen :1: XmlSchemaComplexContent.Read, name="+reader.Name,null);
				reader.SkipToEnd();
				return null;
			}

			simple.LineNumber = reader.LineNumber;
			simple.LinePosition = reader.LinePosition;
			simple.SourceUri = reader.BaseURI;

			while(reader.MoveToNextAttribute())
			{
				if(reader.Name == "id")
				{
					simple.Id = reader.Value;
				}
				else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
				{
					error(h,reader.Name + " is not a valid attribute for simpleContent",null);
				}
				else
				{
					XmlSchemaUtil.ReadUnhandledAttribute(reader,simple);
				}
			}
			
			reader.MoveToElement();
			if(reader.IsEmptyElement)
				return simple;
			//Content: (annotation?, (restriction | extension))
			int level = 1;
			while(reader.ReadNextElement())
			{
				if(reader.NodeType == XmlNodeType.EndElement)
				{
					if(reader.LocalName != xmlname)
						error(h,"Should not happen :2: XmlSchemaSimpleContent.Read, name="+reader.Name,null);
					break;
				}
				if(level <= 1 && reader.LocalName == "annotation")
				{
					level = 2; //Only one annotation
					XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
					if(annotation != null)
						simple.Annotation = annotation;
					continue;
				}
				if(level <=2)
				{
					if(reader.LocalName == "restriction")
					{
						level = 3;
						XmlSchemaSimpleContentRestriction restriction = XmlSchemaSimpleContentRestriction.Read(reader,h);
						if(restriction != null)
							simple.content = restriction;
						continue;
					}
					if(reader.LocalName == "extension")
					{
						level = 3;
						XmlSchemaSimpleContentExtension extension = XmlSchemaSimpleContentExtension.Read(reader,h);
						if(extension != null)
							simple.content = extension;
						continue;
					}
				}
				reader.RaiseInvalidElementError();
			}
			return simple;
		}
Example #35
0
 private void SetContainer(State state, object container) {
     switch (state) {
         case State.Root:
             break;
         case State.Schema:
             break;
         case State.Annotation:
             this.annotation = (XmlSchemaAnnotation)container;
             break;
         case State.Include:
             this.include = (XmlSchemaInclude)container;
             break;
         case State.Import:
             this.import = (XmlSchemaImport)container;
             break;
         case State.Element:
             this.element = (XmlSchemaElement)container;
             break;
         case State.Attribute:
             this.attribute = (XmlSchemaAttribute)container;
             break;
         case State.AttributeGroup:
             this.attributeGroup = (XmlSchemaAttributeGroup)container;
             break;
         case State.AttributeGroupRef:
             this.attributeGroupRef = (XmlSchemaAttributeGroupRef)container;
             break;
         case State.AnyAttribute:
             this.anyAttribute = (XmlSchemaAnyAttribute)container;
             break;
         case State.Group:
             this.group = (XmlSchemaGroup)container;
             break;
         case State.GroupRef:
             this.groupRef = (XmlSchemaGroupRef)container;
             break;
         case State.All:
             this.all = (XmlSchemaAll)container;
             break;
         case State.Choice:
             this.choice = (XmlSchemaChoice)container;
             break;
         case State.Sequence:
             this.sequence = (XmlSchemaSequence)container;
             break;
         case State.Any:
             this.anyElement = (XmlSchemaAny)container;
             break;
         case State.Notation:
             this.notation = (XmlSchemaNotation)container;
             break;
         case State.SimpleType:
             this.simpleType = (XmlSchemaSimpleType)container;
             break;
         case State.ComplexType:
             this.complexType = (XmlSchemaComplexType)container;
             break;
         case State.ComplexContent:
             this.complexContent = (XmlSchemaComplexContent)container;
             break;
         case State.ComplexContentExtension:
             this.complexContentExtension = (XmlSchemaComplexContentExtension)container;
             break;
         case State.ComplexContentRestriction:
             this.complexContentRestriction = (XmlSchemaComplexContentRestriction)container;
             break;
         case State.SimpleContent:
             this.simpleContent = (XmlSchemaSimpleContent)container;
             break;
         case State.SimpleContentExtension:
             this.simpleContentExtension = (XmlSchemaSimpleContentExtension)container;
             break;
         case State.SimpleContentRestriction:
             this.simpleContentRestriction = (XmlSchemaSimpleContentRestriction)container;
             break;
         case State.SimpleTypeUnion:
             this.simpleTypeUnion = (XmlSchemaSimpleTypeUnion)container;
             break;
         case State.SimpleTypeList:
             this.simpleTypeList = (XmlSchemaSimpleTypeList)container;
             break;
         case State.SimpleTypeRestriction:
             this.simpleTypeRestriction = (XmlSchemaSimpleTypeRestriction)container;
             break;
         case State.Unique:
         case State.Key:
         case State.KeyRef:
             this.identityConstraint = (XmlSchemaIdentityConstraint)container;
             break;
         case State.Selector:
         case State.Field:
             this.xpath = (XmlSchemaXPath)container;
             break;
         case State.MinExclusive:
         case State.MinInclusive:
         case State.MaxExclusive:
         case State.MaxInclusive:
         case State.TotalDigits:
         case State.FractionDigits:
         case State.Length:
         case State.MinLength:
         case State.MaxLength:
         case State.Enumeration:
         case State.Pattern:
         case State.WhiteSpace:
             this.facet = (XmlSchemaFacet)container;
             break;
         case State.AppInfo:
             this.appInfo = (XmlSchemaAppInfo)container;
             break;
         case State.Documentation:
             this.documentation = (XmlSchemaDocumentation)container;
             break;
         case State.Redefine:
             this.redefine = (XmlSchemaRedefine)container;
             break;
         default:
             Debug.Assert(false, "State is " + state);
             break;
     }
 }
Example #36
0
        internal XmlSchemaObject Clone(XmlSchema?parentSchema)
        {
            XmlSchemaComplexType complexType = (XmlSchemaComplexType)MemberwiseClone();

            //Deep clone the QNames as these will be updated on chameleon includes
            if (complexType.ContentModel != null)
            { //simpleContent or complexContent
                XmlSchemaSimpleContent?simpleContent = complexType.ContentModel as XmlSchemaSimpleContent;
                if (simpleContent != null)
                {
                    XmlSchemaSimpleContent newSimpleContent = (XmlSchemaSimpleContent)simpleContent.Clone();

                    XmlSchemaSimpleContentExtension?simpleExt = simpleContent.Content as XmlSchemaSimpleContentExtension;
                    if (simpleExt != null)
                    {
                        XmlSchemaSimpleContentExtension newSimpleExt = (XmlSchemaSimpleContentExtension)simpleExt.Clone();
                        newSimpleExt.BaseTypeName = simpleExt.BaseTypeName.Clone();
                        newSimpleExt.SetAttributes(CloneAttributes(simpleExt.Attributes));
                        newSimpleContent.Content = newSimpleExt;
                    }
                    else
                    { //simpleContent.Content is XmlSchemaSimpleContentRestriction
                        XmlSchemaSimpleContentRestriction simpleRest    = (XmlSchemaSimpleContentRestriction)simpleContent.Content !;
                        XmlSchemaSimpleContentRestriction newSimpleRest = (XmlSchemaSimpleContentRestriction)simpleRest.Clone();
                        newSimpleRest.BaseTypeName = simpleRest.BaseTypeName.Clone();
                        newSimpleRest.SetAttributes(CloneAttributes(simpleRest.Attributes));
                        newSimpleContent.Content = newSimpleRest;
                    }

                    complexType.ContentModel = newSimpleContent;
                }
                else
                { // complexType.ContentModel is XmlSchemaComplexContent
                    XmlSchemaComplexContent complexContent    = (XmlSchemaComplexContent)complexType.ContentModel;
                    XmlSchemaComplexContent newComplexContent = (XmlSchemaComplexContent)complexContent.Clone();

                    XmlSchemaComplexContentExtension?complexExt = complexContent.Content as XmlSchemaComplexContentExtension;
                    if (complexExt != null)
                    {
                        XmlSchemaComplexContentExtension newComplexExt = (XmlSchemaComplexContentExtension)complexExt.Clone();
                        newComplexExt.BaseTypeName = complexExt.BaseTypeName.Clone();
                        newComplexExt.SetAttributes(CloneAttributes(complexExt.Attributes));
                        if (HasParticleRef(complexExt.Particle, parentSchema))
                        {
                            newComplexExt.Particle = CloneParticle(complexExt.Particle, parentSchema);
                        }
                        newComplexContent.Content = newComplexExt;
                    }
                    else
                    { // complexContent.Content is XmlSchemaComplexContentRestriction
                        XmlSchemaComplexContentRestriction complexRest    = (complexContent.Content as XmlSchemaComplexContentRestriction) !;
                        XmlSchemaComplexContentRestriction newComplexRest = (XmlSchemaComplexContentRestriction)complexRest.Clone();
                        newComplexRest.BaseTypeName = complexRest.BaseTypeName.Clone();
                        newComplexRest.SetAttributes(CloneAttributes(complexRest.Attributes));
                        if (HasParticleRef(newComplexRest.Particle, parentSchema))
                        {
                            newComplexRest.Particle = CloneParticle(newComplexRest.Particle, parentSchema);
                        }

                        newComplexContent.Content = newComplexRest;
                    }

                    complexType.ContentModel = newComplexContent;
                }
            }
            else
            { //equals XmlSchemaComplexContent with baseType is anyType
                if (HasParticleRef(complexType.Particle, parentSchema))
                {
                    complexType.Particle = CloneParticle(complexType.Particle, parentSchema);
                }
                complexType.SetAttributes(CloneAttributes(complexType.Attributes));
            }
            complexType.ClearCompiledState();
            return(complexType);
        }
 void Write36_XmlSchemaSimpleContent(XmlSchemaSimpleContent o) {
     if ((object)o == null) return;
     WriteStartElement("simpleContent");
     
     WriteAttribute(@"id", @"", ((System.String)o.@Id));
     WriteAttributes((XmlAttribute[])o.@UnhandledAttributes, o);
     Write5_XmlSchemaAnnotation((XmlSchemaAnnotation)o.@Annotation);
     if (o.@Content is XmlSchemaSimpleContentRestriction) {
         Write40_XmlSchemaSimpleContentRestriction((XmlSchemaSimpleContentRestriction)o.@Content);
     }
     else if (o.@Content is XmlSchemaSimpleContentExtension) {
         Write38_XmlSchemaSimpleContentExtension((XmlSchemaSimpleContentExtension)o.@Content);
     }
     WriteEndElement();
 }
Example #38
0
 XmlQualifiedName ExportStructMapping(StructMapping mapping, string ns, XmlSchemaElement element) {
     if (mapping.TypeDesc.IsRoot) {
         needToExportRoot = true;
         return XmlQualifiedName.Empty;
     }
     if (mapping.IsAnonymousType) {
         if (references[mapping] != null)
             throw new InvalidOperationException(Res.GetString(Res.XmlCircularReference2, mapping.TypeDesc.Name, "AnonymousType", "false"));
         references[mapping] = mapping;
     }
     XmlSchemaComplexType type = (XmlSchemaComplexType)types[mapping];
     if (type == null) {
         if (!mapping.IncludeInSchema) throw new InvalidOperationException(Res.GetString(Res.XmlCannotIncludeInSchema, mapping.TypeDesc.Name));
         CheckForDuplicateType(mapping, mapping.Namespace);
         type = new XmlSchemaComplexType();
         if (!mapping.IsAnonymousType) {
             type.Name = mapping.TypeName;
             AddSchemaItem(type, mapping.Namespace, ns);
             types.Add(mapping, type);
         }
         type.IsAbstract = mapping.TypeDesc.IsAbstract;
         bool openModel = mapping.IsOpenModel;
         if (mapping.BaseMapping != null && mapping.BaseMapping.IncludeInSchema) {
             if (mapping.BaseMapping.IsAnonymousType) {
                 throw new InvalidOperationException(Res.GetString(Res.XmlAnonymousBaseType, mapping.TypeDesc.Name, mapping.BaseMapping.TypeDesc.Name, "AnonymousType", "false"));
             }
             if (mapping.HasSimpleContent) {
                 XmlSchemaSimpleContent model = new XmlSchemaSimpleContent();
                 XmlSchemaSimpleContentExtension extension = new XmlSchemaSimpleContentExtension();
                 extension.BaseTypeName = ExportStructMapping(mapping.BaseMapping, mapping.Namespace, null);
                 model.Content = extension;
                 type.ContentModel = model;
             }
             else {
                 XmlSchemaComplexContentExtension extension = new XmlSchemaComplexContentExtension();
                 extension.BaseTypeName = ExportStructMapping(mapping.BaseMapping, mapping.Namespace, null);
                 XmlSchemaComplexContent model = new XmlSchemaComplexContent();
                 model.Content = extension;
                 model.IsMixed = XmlSchemaImporter.IsMixed((XmlSchemaComplexType)types[mapping.BaseMapping]);
                 type.ContentModel = model;
             }
             openModel = false;
         }
         ExportTypeMembers(type, mapping.Members, mapping.TypeName, mapping.Namespace, mapping.HasSimpleContent, openModel);
         ExportDerivedMappings(mapping);
         if (mapping.XmlnsMember != null) {
             AddXmlnsAnnotation(type, mapping.XmlnsMember.Name);
         }
     }
     else {
         AddSchemaImport(mapping.Namespace, ns);
     }
     if (mapping.IsAnonymousType) {
         references[mapping] = null;
         if (element != null) 
             element.SchemaType = type;
         return XmlQualifiedName.Empty;
     }
     else {
         XmlQualifiedName qname = new XmlQualifiedName(type.Name, mapping.Namespace);
         if (element != null) element.SchemaTypeName = qname;
         return qname;
     }
 }
Example #39
0
            /// <summary>
            /// Sets type of the xse based on the currently read element.
            /// If the type is already set, verifies that it matches the instance and if not, updates the type to validate the instance.
            /// </summary>
            /// <param name="xse">XmlSchemaElement corresponding to the element just read by the xtr XmlTextReader</param>
            /// <param name="bCreatingNewType">true if the type is newly created, false if the type already existed and matches the current element name</param>
            /// <param name="nsContext">namespaceURI of the parent element. Used to distinguish if ref= should be used when parent is in different ns than child.</param>
            internal void InferElement(XmlSchemaElement xse, bool bCreatingNewType, XmlSchema parentSchema)
            {
            
                bool bEmptyElement = xtr.IsEmptyElement;
                int lastUsedSeqItem = -1;

                Hashtable table = new Hashtable();
                XmlSchemaType schemaType = GetEffectiveSchemaType(xse, bCreatingNewType);
                XmlSchemaComplexType ct = schemaType as XmlSchemaComplexType;

                //infer type based on content of the current element
                if (xtr.MoveToFirstAttribute())
                {
                    ProcessAttributes(ref xse, schemaType, bCreatingNewType, parentSchema);
                }
                else
                {
                    if (!bCreatingNewType && ct != null) 
                    {   //if type already exists and can potentially have attributes
                        MakeExistingAttributesOptional(ct, null);
                    }
                }
                if (ct == null || ct == XmlSchemaComplexType.AnyType) { //It was null or simple type, after processing attributes, this might have been set
                    ct = xse.SchemaType as XmlSchemaComplexType;
                }
                //xse's type is set either to complex type if attributes exist or null
                if (bEmptyElement)  //<element attr="3232" />
                {
                    if (!bCreatingNewType)
                    {
                        if (null != ct)
                        {
                            if (null!= ct.Particle )

                            {
                                ct.Particle.MinOccurs = 0;
                            }
                            else if (null != ct.ContentModel)
                            {
                                XmlSchemaSimpleContentExtension sce = CheckSimpleContentExtension(ct);
                                sce.BaseTypeName = ST_string;
                                sce.LineNumber = TF_string;
                            }
                        }
                        else if (!xse.SchemaTypeName.IsEmpty)
                        {
                            xse.LineNumber = TF_string;
                            xse.SchemaTypeName = ST_string;
                        }
                    }
                    else
                    {
                        xse.LineNumber = TF_string;
                        //xse.SchemaTypeName = ST_string; //My change
                    }
                    return; //We are done processing this element - all attributes are already added
                }
                bool bWhiteSpace = false;
                do
                { 
                    xtr.Read();
                    if (xtr.NodeType == XmlNodeType.Whitespace) 
                    {
                        bWhiteSpace = true;
                    }
                    if (xtr.NodeType == XmlNodeType.EntityReference) 
                    {
                        throw new XmlSchemaInferenceException(Res.SchInf_entity, 0, 0);
                    }
                } while( (!xtr.EOF) && (xtr.NodeType != XmlNodeType.EndElement) && (xtr.NodeType != XmlNodeType.CDATA)&&(xtr.NodeType != XmlNodeType.Element)&&(xtr.NodeType != XmlNodeType.Text) );

                if (xtr.NodeType == XmlNodeType.EndElement)
                {
                    if (bWhiteSpace) {
                        if (ct != null)
                        {
                            if (null != ct.ContentModel)
                            {
                                XmlSchemaSimpleContentExtension sce = CheckSimpleContentExtension(ct);
                                sce.BaseTypeName = ST_string;
                                sce.LineNumber = TF_string;
                            }
                            else if (bCreatingNewType)
                            {
                                //attributes exist, but both Particle and ContentModel == null - this must be complex type with simpleContent extension
                                XmlSchemaSimpleContent sc = new XmlSchemaSimpleContent();
                                ct.ContentModel = sc;
                                XmlSchemaSimpleContentExtension sce = new XmlSchemaSimpleContentExtension();
                                sc.Content = sce;

                                MoveAttributes(ct, sce, bCreatingNewType);
                                
                                sce.BaseTypeName = ST_string;
                                sce.LineNumber = TF_string;
                            }
                            else
                                ct.IsMixed = true;
                        } 
                        else
                        {
                            xse.SchemaTypeName = ST_string;
                            xse.LineNumber = TF_string;
                        }
                    }
                    if (bCreatingNewType) 
                    {
                        xse.LineNumber = TF_string;
                        //xse.SchemaTypeName = ST_string; //my change
                    }
                    else
                    {
                        if (null != ct)
                        {
                            if (null!= ct.Particle) 
                            {
                                ct.Particle.MinOccurs = 0;
                            }
                            else if (null != ct.ContentModel)
                            {
                                XmlSchemaSimpleContentExtension sce = CheckSimpleContentExtension(ct);
                                sce.BaseTypeName = ST_string;
                                sce.LineNumber = TF_string;
                            }
                        }
                        else if (!xse.SchemaTypeName.IsEmpty)
                        {
                            xse.LineNumber = TF_string;
                            xse.SchemaTypeName = ST_string;
                        }
                    }

                    return; //<element attr="232"></element>
                }
                int iChildNumber = 0;
                bool bCreatingNewSequence = false;
                while (!xtr.EOF && (xtr.NodeType != XmlNodeType.EndElement))
                {
                    bool bNextNodeAlreadyRead = false;  //In some cases we have to look ahead one node. If true means that we did look ahead.
                    iChildNumber++;
                    if ((xtr.NodeType == XmlNodeType.Text) || (xtr.NodeType == XmlNodeType.CDATA) ) //node can be simple type, complex with simple content or complex with mixed content
                    {
                        if (null != ct)
                        {
                            if (null != ct.Particle)
                            {
                                ct.IsMixed = true;
                                if (iChildNumber==1)
                                {
                                    //if this is the only child and other elements do not follow, we must set particle minOccurs="0"
                                    do{ xtr.Read();} while( (!xtr.EOF) && ((xtr.NodeType == XmlNodeType.CDATA)||(xtr.NodeType == XmlNodeType.Text) ||  (xtr.NodeType == XmlNodeType.Comment) || (xtr.NodeType == XmlNodeType.ProcessingInstruction) || (xtr.NodeType == XmlNodeType.Whitespace) || (xtr.NodeType == XmlNodeType.SignificantWhitespace) || (xtr.NodeType == XmlNodeType.XmlDeclaration)));
                                    bNextNodeAlreadyRead = true;
                                    if (xtr.NodeType == XmlNodeType.EndElement)
                                        ct.Particle.MinOccurs=decimal.Zero;
                                }
                            }
                            else if (null!=ct.ContentModel)
                            {   //complexType with simpleContent
                                XmlSchemaSimpleContentExtension sce = CheckSimpleContentExtension(ct);
                                if ((xtr.NodeType == XmlNodeType.Text) && (iChildNumber==1))
                                {
                                    int SimpleType = -1;
                                    if (xse.Parent == null)
                                    {
                                        SimpleType = sce.LineNumber; // we use LineNumber to represent valid type flags
                                    }
                                    else
                                    {
                                        SimpleType = GetSchemaType(sce.BaseTypeName);
                                        xse.Parent = null;
                                    }
                                    sce.BaseTypeName = RefineSimpleType(xtr.Value, ref SimpleType);
                                    sce.LineNumber = SimpleType; // we use LineNumber to represent valid type flags
                                }
                                else
                                {
                                    sce.BaseTypeName = ST_string;
                                    sce.LineNumber = TF_string;
                                }
                            }
                            else
                            {
                                //attributes exist, but both Particle and ContentModel == null - this must be complex type with simpleContent extension
                                XmlSchemaSimpleContent sc = new XmlSchemaSimpleContent();
                                ct.ContentModel = sc;
                                XmlSchemaSimpleContentExtension sce = new XmlSchemaSimpleContentExtension();
                                sc.Content = sce;

                                MoveAttributes(ct, sce, bCreatingNewType);

                                if (xtr.NodeType == XmlNodeType.Text)
                                {
                                    int TypeFlags;
                                    if(!bCreatingNewType)
                                        //previously this was empty element
                                        TypeFlags=TF_string;
                                    else
                                        TypeFlags = -1;
                                    sce.BaseTypeName = RefineSimpleType(xtr.Value, ref TypeFlags);
                                    sce.LineNumber = TypeFlags; // we use LineNumber to store flags of valid types
                                }
                                else
                                {
                                    sce.BaseTypeName = ST_string;
                                    sce.LineNumber = TF_string;
                                }
                                
                            }
                        }
                        else
                        {   //node is currently empty or with SimpleType
                            //node will become simple type
                            if (iChildNumber>1)
                            {
                                //more than one consecutive text nodes probably with PI in between
                                xse.SchemaTypeName = ST_string;
                                xse.LineNumber = TF_string;// we use LineNumber to store flags of valid types
                            }
                            else
                            {
                                int TypeFlags = -1;
                                if (bCreatingNewType)
                                    if (xtr.NodeType == XmlNodeType.Text)
                                    {
                                        xse.SchemaTypeName = RefineSimpleType(xtr.Value, ref TypeFlags);
                                        xse.LineNumber = TypeFlags; // we use LineNumber to store flags of valid types
                                    }
                                    else
                                    {
                                        xse.SchemaTypeName = ST_string;
                                        xse.LineNumber = TF_string;// we use LineNumber to store flags of valid types
                                    }
                                else if (xtr.NodeType == XmlNodeType.Text)
                                {
                                    if (xse.Parent == null)
                                    {
                                        TypeFlags = xse.LineNumber;
                                    }
                                    else
                                    {
                                        TypeFlags = GetSchemaType(xse.SchemaTypeName);
                                        if (TypeFlags == -1 && xse.LineNumber == TF_string) { //Since schemaTypeName is not set for empty elements (<e></e>)
                                            TypeFlags = TF_string;
                                        }
                                        xse.Parent = null;
                                    }
                                    xse.SchemaTypeName = RefineSimpleType(xtr.Value, ref TypeFlags);    //simple type
                                    xse.LineNumber = TypeFlags; // we use LineNumber to store flags of valid types
                                }
                                else
                                {
                                    xse.SchemaTypeName = ST_string;
                                    xse.LineNumber = TF_string;// we use LineNumber to store flags of valid types
                                }
                            }
                        } 
                    }
                    else if (xtr.NodeType == XmlNodeType.Element)
                    {
                        XmlQualifiedName qname = new XmlQualifiedName(xtr.LocalName, xtr.NamespaceURI);
                        bool Maxoccursflag = false;
                        if (table.Contains(qname)) 
                        {
                            Maxoccursflag = true;
                        }
                        else 
                        {
                            table.Add(qname, null);
                        }
                        if (ct==null)
                        { //untill now the element was empty or SimpleType - it now becomes complex type
                            ct = new XmlSchemaComplexType();
                            xse.SchemaType = ct;
                            if (!xse.SchemaTypeName.IsEmpty) //
                            {
                                ct.IsMixed=true;
                                xse.SchemaTypeName = XmlQualifiedName.Empty;
                            }
                        }
                        if (ct.ContentModel !=null)
                        {   //type was previously identified as simple content extension - we need to convert it to sequence
                            XmlSchemaSimpleContentExtension sce = CheckSimpleContentExtension(ct);
                            MoveAttributes(sce, ct);
                            ct.ContentModel = null;
                            ct.IsMixed = true;
                            if (ct.Particle != null)
                                throw new XmlSchemaInferenceException(Res.SchInf_particle, 0, 0);
                            ct.Particle = new XmlSchemaSequence();
                            bCreatingNewSequence = true;
                            XmlSchemaElement subelement = AddElement(xtr.LocalName, xtr.Prefix, xtr.NamespaceURI, parentSchema,((XmlSchemaSequence)ct.Particle).Items, -1);
                            lastUsedSeqItem = 0;
                            if (!bCreatingNewType)
                                ct.Particle.MinOccurs=0;    //previously this was simple type so subelements did not exist
                        }
                        else if (ct.Particle == null)
                        {
                            ct.Particle = new XmlSchemaSequence();
                            bCreatingNewSequence = true;
                            XmlSchemaElement subelement = AddElement(xtr.LocalName, xtr.Prefix, xtr.NamespaceURI, parentSchema,((XmlSchemaSequence)ct.Particle).Items, -1);
                            if (!bCreatingNewType)
                            {
                                ((XmlSchemaSequence)ct.Particle).MinOccurs = decimal.Zero;
                                //  subelement.MinOccurs = decimal.Zero;
                            }
                    
                            lastUsedSeqItem = 0;
                        }
                        else
                        {
                            bool bParticleChanged = false;
                            XmlSchemaElement subelement = FindMatchingElement(bCreatingNewType || bCreatingNewSequence, xtr, ct, ref lastUsedSeqItem, ref bParticleChanged, parentSchema, Maxoccursflag);
                        }
                    }
                    else if (xtr.NodeType == XmlNodeType.Text)
                    {
                        if (ct==null)
                            throw new XmlSchemaInferenceException(Res.SchInf_ct, 0, 0);
                        ct.IsMixed = true;
                    }
                    do
                    { 
                        if (xtr.NodeType == XmlNodeType.EntityReference)
                        {
                            throw new XmlSchemaInferenceException(Res.SchInf_entity, 0, 0);

                        }
                        if (!bNextNodeAlreadyRead)
                        {
                            xtr.Read();
                        }
                        else
                        {
                            bNextNodeAlreadyRead = false;
                        }
                    } while( (!xtr.EOF) && (xtr.NodeType != XmlNodeType.EndElement) && (xtr.NodeType != XmlNodeType.CDATA)&&(xtr.NodeType != XmlNodeType.Element)&&(xtr.NodeType != XmlNodeType.Text));
                }
                if (lastUsedSeqItem != -1)
                {
                    //Verify if all elements in a sequence exist, if not set MinOccurs=0
                    while (++lastUsedSeqItem < ((XmlSchemaSequence)ct.Particle).Items.Count)
                    {
                        if (((XmlSchemaSequence)ct.Particle).Items[lastUsedSeqItem].GetType() != typeof (XmlSchemaElement))
                            throw new XmlSchemaInferenceException(Res.SchInf_seq, 0 , 0);
                        XmlSchemaElement subElement = (XmlSchemaElement) ((XmlSchemaSequence)ct.Particle).Items[lastUsedSeqItem];
                        subElement.MinOccurs = 0;
                    }
                }

            }   
Example #40
0
        //<simpleContent
        //  id = ID
        //  {any attributes with non-schema namespace . . .}>
        //  Content: (annotation?, (restriction | extension))
        //</simpleContent>
        internal static XmlSchemaSimpleContent Read(XmlSchemaReader reader, ValidationEventHandler h)
        {
            XmlSchemaSimpleContent simple = new XmlSchemaSimpleContent();

            reader.MoveToElement();

            if (reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
            {
                error(h, "Should not happen :1: XmlSchemaComplexContent.Read, name=" + reader.Name, null);
                reader.SkipToEnd();
                return(null);
            }

            simple.LineNumber   = reader.LineNumber;
            simple.LinePosition = reader.LinePosition;
            simple.SourceUri    = reader.BaseURI;

            while (reader.MoveToNextAttribute())
            {
                if (reader.Name == "id")
                {
                    simple.Id = reader.Value;
                }
                else if ((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
                {
                    error(h, reader.Name + " is not a valid attribute for simpleContent", null);
                }
                else
                {
                    XmlSchemaUtil.ReadUnhandledAttribute(reader, simple);
                }
            }

            reader.MoveToElement();
            if (reader.IsEmptyElement)
            {
                return(simple);
            }
            //Content: (annotation?, (restriction | extension))
            int level = 1;

            while (reader.ReadNextElement())
            {
                if (reader.NodeType == XmlNodeType.EndElement)
                {
                    if (reader.LocalName != xmlname)
                    {
                        error(h, "Should not happen :2: XmlSchemaSimpleContent.Read, name=" + reader.Name, null);
                    }
                    break;
                }
                if (level <= 1 && reader.LocalName == "annotation")
                {
                    level = 2; //Only one annotation
                    XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader, h);
                    if (annotation != null)
                    {
                        simple.Annotation = annotation;
                    }
                    continue;
                }
                if (level <= 2)
                {
                    if (reader.LocalName == "restriction")
                    {
                        level = 3;
                        XmlSchemaSimpleContentRestriction restriction = XmlSchemaSimpleContentRestriction.Read(reader, h);
                        if (restriction != null)
                        {
                            simple.content = restriction;
                        }
                        continue;
                    }
                    if (reader.LocalName == "extension")
                    {
                        level = 3;
                        XmlSchemaSimpleContentExtension extension = XmlSchemaSimpleContentExtension.Read(reader, h);
                        if (extension != null)
                        {
                            simple.content = extension;
                        }
                        continue;
                    }
                }
                reader.RaiseInvalidElementError();
            }
            return(simple);
        }