/mcs/class/referencesource/System.Xml/System/Xml/Schema/Inference/Infer.cs
C# | 2509 lines | 2265 code | 100 blank | 144 comment | 756 complexity | 8717547022fa98fcaf2dfcfb99dd04a2 MD5 | raw file
Possible License(s): GPL-2.0, CC-BY-SA-3.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, LGPL-2.1, Unlicense, Apache-2.0
Large files files are truncated, but you can click here to view the full file
- //------------------------------------------------------------------------------
- // <copyright file="Infer.cs" company="Microsoft">
- // Copyright (c) Microsoft Corporation. All rights reserved.
- // </copyright>
- // <owner current="true" primary="true">Microsoft</owner>
- // <owner current="false" primary="false">Microsoft</owner>
- //------------------------------------------------------------------------------
- using System;
- using System.IO;
- using System.Xml;
- using System.Collections;
- using System.Diagnostics;
- using System.Globalization;
- using System.Security.Permissions;
- namespace System.Xml.Schema
-
- {
- /// <include file='doc\Infer.uex' path='docs/doc[@for="Infer"]/*' />
- /// <summary>
- /// Infer class serves for infering XML Schema from given XML instance document.
- /// </summary>
- public sealed class XmlSchemaInference
- {
- internal static XmlQualifiedName ST_boolean = new XmlQualifiedName("boolean", XmlSchema.Namespace);
- internal static XmlQualifiedName ST_byte = new XmlQualifiedName("byte", XmlSchema.Namespace);
- internal static XmlQualifiedName ST_unsignedByte = new XmlQualifiedName("unsignedByte", XmlSchema.Namespace);
- internal static XmlQualifiedName ST_short = new XmlQualifiedName("short", XmlSchema.Namespace);
- internal static XmlQualifiedName ST_unsignedShort = new XmlQualifiedName("unsignedShort", XmlSchema.Namespace);
- internal static XmlQualifiedName ST_int = new XmlQualifiedName("int", XmlSchema.Namespace);
- internal static XmlQualifiedName ST_unsignedInt = new XmlQualifiedName("unsignedInt", XmlSchema.Namespace);
- internal static XmlQualifiedName ST_long = new XmlQualifiedName("long", XmlSchema.Namespace);
- internal static XmlQualifiedName ST_unsignedLong = new XmlQualifiedName("unsignedLong", XmlSchema.Namespace);
- internal static XmlQualifiedName ST_integer = new XmlQualifiedName("integer", XmlSchema.Namespace);
- internal static XmlQualifiedName ST_decimal = new XmlQualifiedName("decimal", XmlSchema.Namespace);
- internal static XmlQualifiedName ST_float = new XmlQualifiedName("float", XmlSchema.Namespace);
- internal static XmlQualifiedName ST_double = new XmlQualifiedName("double", XmlSchema.Namespace);
- internal static XmlQualifiedName ST_duration = new XmlQualifiedName("duration", XmlSchema.Namespace);
- internal static XmlQualifiedName ST_dateTime = new XmlQualifiedName("dateTime", XmlSchema.Namespace);
- internal static XmlQualifiedName ST_time = new XmlQualifiedName("time", XmlSchema.Namespace);
- internal static XmlQualifiedName ST_date = new XmlQualifiedName("date", XmlSchema.Namespace);
- internal static XmlQualifiedName ST_gYearMonth = new XmlQualifiedName("gYearMonth", XmlSchema.Namespace);
- internal static XmlQualifiedName ST_string = new XmlQualifiedName("string", XmlSchema.Namespace);
- internal static XmlQualifiedName ST_anySimpleType = new XmlQualifiedName("anySimpleType", XmlSchema.Namespace);
- internal static XmlQualifiedName[] SimpleTypes =
- {
- ST_boolean,
- ST_byte,
- ST_unsignedByte,
- ST_short,
- ST_unsignedShort,
- ST_int,
- ST_unsignedInt,
- ST_long,
- ST_unsignedLong,
- ST_integer,
- ST_decimal,
- ST_float,
- ST_double,
- ST_duration,
- ST_dateTime,
- ST_time,
- ST_date,
- ST_gYearMonth,
- ST_string
- };
- internal const short HC_ST_boolean = 0;
- internal const short HC_ST_byte = 1;
- internal const short HC_ST_unsignedByte = 2;
- internal const short HC_ST_short = 3;
- internal const short HC_ST_unsignedShort = 4;
- internal const short HC_ST_int = 5;
- internal const short HC_ST_unsignedInt = 6;
- internal const short HC_ST_long = 7;
- internal const short HC_ST_unsignedLong = 8;
- internal const short HC_ST_integer = 9;
- internal const short HC_ST_decimal = 10;
- internal const short HC_ST_float = 11;
- internal const short HC_ST_double = 12;
- internal const short HC_ST_duration = 13;
- internal const short HC_ST_dateTime = 14;
- internal const short HC_ST_time = 15;
- internal const short HC_ST_date = 16;
- internal const short HC_ST_gYearMonth = 17;
- internal const short HC_ST_string = 18;
- internal const short HC_ST_Count = HC_ST_string +1;
-
-
- internal const int TF_boolean = 1<<HC_ST_boolean;
- internal const int TF_byte = 1<<HC_ST_byte;
- internal const int TF_unsignedByte = 1<<HC_ST_unsignedByte;
- internal const int TF_short = 1<<HC_ST_short;
- internal const int TF_unsignedShort = 1<<HC_ST_unsignedShort;
- internal const int TF_int = 1<<HC_ST_int;
- internal const int TF_unsignedInt = 1<<HC_ST_unsignedInt;
- internal const int TF_long = 1<<HC_ST_long;
- internal const int TF_unsignedLong = 1<<HC_ST_unsignedLong;
- internal const int TF_integer = 1<<HC_ST_integer;
- internal const int TF_decimal = 1<<HC_ST_decimal;
- internal const int TF_float = 1<<HC_ST_float;
- internal const int TF_double = 1<<HC_ST_double;
- internal const int TF_duration = 1<<HC_ST_duration;
- internal const int TF_dateTime = 1<<HC_ST_dateTime;
- internal const int TF_time = 1<<HC_ST_time;
- internal const int TF_date = 1<<HC_ST_date;
- internal const int TF_gYearMonth = 1<<HC_ST_gYearMonth;
- internal const int TF_string = 1<<HC_ST_string;
- XmlSchema rootSchema = null; //(XmlSchema) xsc[TargetNamespace];
- private XmlSchemaSet schemaSet;
- private XmlReader xtr;
- private NameTable nametable;
- private string TargetNamespace;
- private XmlNamespaceManager NamespaceManager;
- //private Hashtable schemas; //contains collection of schemas before they get added to the XmlSchemaSet xsc
- //private bool bRefine = false; //indicates if we are going to infer or refine schema when InferSchema is called
- ArrayList schemaList;
- InferenceOption occurrence = InferenceOption.Restricted;
- InferenceOption typeInference = InferenceOption.Restricted;
- /* internal struct ReplaceList
- {
- internal XmlSchemaObjectCollection col;
- internal int position;
- internal ReplaceList(XmlSchemaObjectCollection col, int position)
- {
- this.col = col;
- this.position = position;
- }
- }*/
- /// <include file='doc\Infer.uex' path='docs/doc[@for="InferenceOption"]/*' />
- public enum InferenceOption
- {
- /// <include file='doc\Infer.uex' path='docs/doc[@for="InferenceOption.Restricted"]/*' />
- Restricted,
- /// <include file='doc\Infer.uex' path='docs/doc[@for="InferenceOption.Relaxed"]/*' />
- Relaxed
- };
- /// <include file='doc\Infer.uex' path='docs/doc[@for="Infer.Occurrence"]/*' />
- public InferenceOption Occurrence
- {
- set
- {
- this.occurrence = value;
- }
- get
- {
- return this.occurrence;
- }
- }
-
- /// <include file='doc\Infer.uex' path='docs/doc[@for="Infer.TypeInference"]/*' />
- public InferenceOption TypeInference
- {
- set
- {
- this.typeInference = value;
- }
- get
- {
- return this.typeInference;
- }
- }
- /// <include file='doc\Infer.uex' path='docs/doc[@for="Infer.Infer"]/*' />
- public XmlSchemaInference()
- {
- this.nametable = new NameTable();
- this.NamespaceManager = new XmlNamespaceManager(nametable);
- this.NamespaceManager.AddNamespace("xs", XmlSchema.Namespace);
- this.schemaList = new ArrayList();
- }
- /// <include file='doc\Infer.uex' path='docs/doc[@for="Infer.InferSchema"]/*' />
- public XmlSchemaSet InferSchema(XmlReader instanceDocument)
- {
- return InferSchema1(instanceDocument, new XmlSchemaSet(nametable));
- }
- /// <include file='doc\Infer.uex' path='docs/doc[@for="Infer.InferSchema1"]/*' />
- public XmlSchemaSet InferSchema(XmlReader instanceDocument, XmlSchemaSet schemas)
- {
- if (schemas == null)
- {
- schemas = new XmlSchemaSet(nametable);
- }
- return InferSchema1(instanceDocument, schemas);
- }
- internal XmlSchemaSet InferSchema1(XmlReader instanceDocument, XmlSchemaSet schemas)
- {
- if (instanceDocument == null)
- {
- throw new ArgumentNullException("instanceDocument");
- }
- this.rootSchema = null;
- xtr = instanceDocument;
- schemas.Compile();
- this.schemaSet = schemas;
- //schemas = new Hashtable();
- //while(xtr.Read())
-
- while (xtr.NodeType != XmlNodeType.Element && xtr.Read()) ;
-
-
- if (xtr.NodeType == XmlNodeType.Element)
- {
- //Create and process the root element
- TargetNamespace = xtr.NamespaceURI;
- if ( xtr.NamespaceURI == XmlSchema.Namespace)
- {
- throw new XmlSchemaInferenceException(Res.SchInf_schema, 0, 0);
- }
- XmlSchemaElement xse = null;
- foreach (XmlSchemaElement elem in schemas.GlobalElements.Values)
- {
- if (elem.Name == xtr.LocalName && elem.QualifiedName.Namespace == xtr.NamespaceURI)
- {
- rootSchema = elem.Parent as XmlSchema;
- xse = elem;
- break;
- }
- }
-
- if (rootSchema == null)
- {
- //rootSchema = CreateXmlSchema(xtr.NamespaceURI);
- xse = AddElement(xtr.LocalName, xtr.Prefix, xtr.NamespaceURI, null, null, -1);
- }
- else
- {
- //bRefine = true;
- InferElement(xse, false, rootSchema);
- }
-
- /* foreach (ReplaceList listItem in schemaList)
- {
- if (listItem.position < listItem.col.Count)
- {
- XmlSchemaElement particle = listItem.col[listItem.position] as XmlSchemaElement;
- if (particle != null && (particle.RefName.Namespace == XmlSchema.Namespace))
- {
- XmlSchemaAny any = new XmlSchemaAny();
- if (particle.MaxOccurs != 1)
- {
- any.MaxOccurs = particle.MaxOccurs;
- }
- if (particle.MinOccurs != 1)
- {
- any.MinOccurs = particle.MinOccurs;
- }
- any.ProcessContents = XmlSchemaContentProcessing.Skip;
- any.MinOccurs = decimal.Zero;
- any.Namespace = particle.RefName.Namespace;
- listItem.col[listItem.position] = any;
- }
- }
- }*/
- foreach(String prefix in this.NamespaceManager)
- {
- if (!prefix.Equals("xml") && !prefix.Equals("xmlns"))
- {
- String ns = this.NamespaceManager.LookupNamespace(this.nametable.Get(prefix));
- if (ns.Length != 0) { //Do not add xmlns=""
- rootSchema.Namespaces.Add(prefix, ns);
- }
- }
- }
- Debug.Assert(this.rootSchema != null, "rootSchema is null");
- schemas.Reprocess(rootSchema);
- schemas.Compile();
- //break;
- }
- else
- {
- throw new XmlSchemaInferenceException(Res.SchInf_NoElement, 0, 0);
- }
- return schemas;
- }
- private XmlSchemaAttribute AddAttribute(string localName, string prefix, string childURI, string attrValue, bool bCreatingNewType, XmlSchema parentSchema, XmlSchemaObjectCollection addLocation, XmlSchemaObjectTable compiledAttributes)
- {
- if (childURI == XmlSchema.Namespace)
- {
- throw new XmlSchemaInferenceException(Res.SchInf_schema, 0, 0);
- }
- XmlSchemaAttribute xsa = null;
- int AttributeType = -1;
- XmlSchemaAttribute returnedAttribute = null; //this value will change to attributeReference if childURI!= parentURI
- XmlSchema xs = null;
- bool add = true;
- Debug.Assert(compiledAttributes != null); //AttributeUses is never null
- // First we need to look into the already compiled attributes
- // (they come from the schemaset which we got on input)
- // If there are none or we don't find it there, then we must search the list of attributes
- // where we are going to add a new one (if it doesn't exist).
- // This is necessary to avoid adding duplicate attribute declarations.
- ICollection searchCollectionPrimary, searchCollectionSecondary;
- if (compiledAttributes.Count > 0) {
- searchCollectionPrimary = compiledAttributes.Values;
- searchCollectionSecondary = addLocation;
- }
- else {
- searchCollectionPrimary = addLocation;
- searchCollectionSecondary = null;
- }
- if (childURI == XmlReservedNs.NsXml)
- {
- XmlSchemaAttribute attributeReference = null;
- //see if the reference exists
- attributeReference = FindAttributeRef(searchCollectionPrimary, localName, childURI);
- if (attributeReference == null && searchCollectionSecondary != null) {
- attributeReference = FindAttributeRef(searchCollectionSecondary, localName, childURI);
- }
- if (attributeReference == null)
- {
- attributeReference = new XmlSchemaAttribute();
- attributeReference.RefName = new XmlQualifiedName(localName, childURI);
- if (bCreatingNewType && this.Occurrence == InferenceOption.Restricted)
- {
- attributeReference.Use = XmlSchemaUse.Required;
- }
- else
- {
- attributeReference.Use = XmlSchemaUse.Optional;
- }
- addLocation.Add(attributeReference);
- }
- returnedAttribute = attributeReference;
- }
- else
- {
- if (childURI.Length == 0)
- {
- xs = parentSchema;
- add = false;
- }
- else if (childURI != null && !schemaSet.Contains(childURI))
- {
- /*if (parentSchema.AttributeFormDefault = XmlSchemaForm.Unqualified && childURI.Length == 0)
- {
- xs = parentSchema;
- add = false;
- break;
- }*/
- xs = new XmlSchema();
- xs.AttributeFormDefault = XmlSchemaForm.Unqualified;
- xs.ElementFormDefault = XmlSchemaForm.Qualified;
- if (childURI.Length != 0)
- xs.TargetNamespace = childURI;
- //schemas.Add(childURI, xs);
- this.schemaSet.Add(xs);
- if (prefix.Length != 0 && String.Compare(prefix, "xml", StringComparison.OrdinalIgnoreCase) != 0)
- NamespaceManager.AddNamespace(prefix, childURI);
- }
- else
- {
- ArrayList col = this.schemaSet.Schemas(childURI) as ArrayList;
- if (col != null && col.Count > 0)
- {
- xs = col[0] as XmlSchema;
- }
- }
- if (childURI.Length != 0) //
- {
- XmlSchemaAttribute attributeReference = null;
- //see if the reference exists
- attributeReference = FindAttributeRef(searchCollectionPrimary, localName, childURI);
- if (attributeReference == null & searchCollectionSecondary != null) {
- attributeReference = FindAttributeRef(searchCollectionSecondary, localName, childURI);
- }
- if (attributeReference == null)
- {
- attributeReference = new XmlSchemaAttribute();
- attributeReference.RefName = new XmlQualifiedName(localName, childURI);
- if (bCreatingNewType && this.Occurrence == InferenceOption.Restricted)
- {
- attributeReference.Use = XmlSchemaUse.Required;
- }
- else
- {
- attributeReference.Use = XmlSchemaUse.Optional;
- }
- addLocation.Add(attributeReference);
- }
- returnedAttribute = attributeReference;
- //see if the attribute exists on the global level
- xsa = FindAttribute(xs.Items, localName);
- if (xsa == null)
- {
- xsa = new XmlSchemaAttribute();
- xsa.Name = localName;
- xsa.SchemaTypeName = RefineSimpleType(attrValue, ref AttributeType);
- xsa.LineNumber = AttributeType; //we use LineNumber to store flags of valid types
- xs.Items.Add(xsa);
- }
- else
- {
- if (xsa.Parent == null)
- {
- AttributeType = xsa.LineNumber; // we use LineNumber to store flags of valid types
- }
- else
- {
- AttributeType = GetSchemaType(xsa.SchemaTypeName);
- xsa.Parent = null;
- }
- xsa.SchemaTypeName = RefineSimpleType(attrValue, ref AttributeType);
- xsa.LineNumber = AttributeType; // we use LineNumber to store flags of valid types
- }
- }
- else
- {
- xsa = FindAttribute(searchCollectionPrimary, localName);
- if (xsa == null && searchCollectionSecondary != null) {
- xsa = FindAttribute(searchCollectionSecondary, localName);
- }
- if (xsa == null)
- {
- xsa = new XmlSchemaAttribute();
- xsa.Name = localName;
- xsa.SchemaTypeName = RefineSimpleType(attrValue, ref AttributeType);
- xsa.LineNumber = AttributeType; // we use LineNumber to store flags of valid types
- if (bCreatingNewType && this.Occurrence == InferenceOption.Restricted)
- xsa.Use = XmlSchemaUse.Required;
- else
- xsa.Use = XmlSchemaUse.Optional;
- addLocation.Add(xsa);
- if (xs.AttributeFormDefault != XmlSchemaForm.Unqualified)
- {
- xsa.Form = XmlSchemaForm.Unqualified;
- }
- }
- else
- {
- if (xsa.Parent == null)
- {
- AttributeType = xsa.LineNumber; // we use LineNumber to store flags of valid types
- }
- else
- {
- AttributeType = GetSchemaType(xsa.SchemaTypeName);
- xsa.Parent = null;
- }
- xsa.SchemaTypeName = RefineSimpleType(attrValue, ref AttributeType);
- xsa.LineNumber = AttributeType; // we use LineNumber to store flags of valid types
- }
- returnedAttribute = xsa;
- }
- }
- string nullString = null;
- if (add && childURI != parentSchema.TargetNamespace)
- {
- for (int i = 0; i < parentSchema.Includes.Count; ++i)
- {
- XmlSchemaImport import = parentSchema.Includes[i] as XmlSchemaImport;
- if (import == null)
- {
- continue;
- }
- if (import.Namespace == childURI)
- {
- add = false;
- }
- }
- if (add)
- {
- XmlSchemaImport import = new XmlSchemaImport();
- import.Schema = xs;
- if (childURI.Length != 0)
- {
- nullString = childURI;
- }
- import.Namespace = nullString;
- parentSchema.Includes.Add(import);
- }
- }
- return returnedAttribute;
- }
- private XmlSchema CreateXmlSchema(string targetNS)
- {
- Debug.Assert(targetNS == null || targetNS.Length > 0 , "targetns for schema is empty");
- XmlSchema xs = new XmlSchema();
- xs.AttributeFormDefault = XmlSchemaForm.Unqualified;
- xs.ElementFormDefault = XmlSchemaForm.Qualified;
- xs.TargetNamespace = targetNS;
- this.schemaSet.Add(xs);
- return xs;
- }
- private XmlSchemaElement AddElement(string localName, string prefix, string childURI, XmlSchema parentSchema, XmlSchemaObjectCollection addLocation, int positionWithinCollection)
- {
- if (childURI == XmlSchema.Namespace)
- {
- throw new XmlSchemaInferenceException(Res.SchInf_schema, 0, 0);
- }
- XmlSchemaElement xse = null;
- XmlSchemaElement returnedElement = xse; //this value will change to elementReference if childURI!= parentURI
- XmlSchema xs = null;
- bool bCreatingNewType = true;
- if (childURI == String.Empty)
- {
- childURI = null;
- }
- // The new element belongs to the same ns as parent and addlocation is not null
- if (parentSchema != null && childURI == parentSchema.TargetNamespace)
- {
- xse = new XmlSchemaElement();
- xse.Name = localName;
- xs = parentSchema;
- if (xs.ElementFormDefault != XmlSchemaForm.Qualified && addLocation != null)
- {
- xse.Form = XmlSchemaForm.Qualified;
- }
- }
- else if (schemaSet.Contains(childURI))
- {
- xse = this.FindGlobalElement(childURI, localName, out xs);
- if (xse == null)
- {
- ArrayList col = this.schemaSet.Schemas(childURI)as ArrayList;
- if (col != null && col.Count > 0 )
- {
- xs = col[0] as XmlSchema;
- }
- xse = new XmlSchemaElement();
- xse.Name = localName;
- xs.Items.Add(xse);
- }
- else
- bCreatingNewType = false;
- }
- else
- {
- xs = CreateXmlSchema(childURI);
- if (prefix.Length!=0)
- NamespaceManager.AddNamespace(prefix, childURI);
- xse=new XmlSchemaElement();
- xse.Name = localName;
- xs.Items.Add(xse); //add global element declaration only when creating new schema
- }
- if (parentSchema == null)
- {
- parentSchema = xs;
- this.rootSchema = parentSchema;
- }
- if (childURI != parentSchema.TargetNamespace )
- {
- bool add = true;
- for (int i = 0; i < parentSchema.Includes.Count; ++i)
- {
- XmlSchemaImport import = parentSchema.Includes[i] as XmlSchemaImport;
- if (import == null)
- {
- continue;
- }
- //Debug.WriteLine(import.Schema.TargetNamespace);
-
- if (import.Namespace == childURI)
- {
- add = false;
- }
- }
- if (add)
- {
- XmlSchemaImport import = new XmlSchemaImport();
- import.Schema = xs;
- import.Namespace = childURI;
- parentSchema.Includes.Add(import);
- }
- }
- returnedElement = xse;
- if (addLocation != null)
- {
- if (childURI == parentSchema.TargetNamespace )
- {
- if (this.Occurrence == InferenceOption.Relaxed /*&& parentSchema.Items != addLocation*/)
- {
- xse.MinOccurs = 0;
- }
- if (positionWithinCollection == -1)
- {
- positionWithinCollection = addLocation.Add(xse);
- }
- else
- {
- addLocation.Insert(positionWithinCollection, xse);
- }
- }
- else
- {
- XmlSchemaElement elementReference = new XmlSchemaElement();
- elementReference.RefName = new XmlQualifiedName(localName, childURI);
- if (this.Occurrence == InferenceOption.Relaxed)
- {
- elementReference.MinOccurs = 0;
- }
- if (positionWithinCollection == -1)
- {
- positionWithinCollection = addLocation.Add(elementReference);
- }
- else
- {
- addLocation.Insert(positionWithinCollection, elementReference);
- }
- returnedElement = elementReference;
- /* if (childURI == XmlSchema.Namespace)
- {
- schemaList.Add(new ReplaceList(addLocation, positionWithinCollection));
- }*/
- }
- }
-
- InferElement(xse, bCreatingNewType, xs);
-
- return returnedElement;
- }
- /// <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;
- }
- }
- }
-
- private XmlSchemaSimpleContentExtension CheckSimpleContentExtension(XmlSchemaComplexType ct) {
- XmlSchemaSimpleContent sc = ct.C…
Large files files are truncated, but you can click here to view the full file