| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520 |
- //------------------------------------------------------------------------------
- // <copyright file="XMLSchema.cs" company="Microsoft">
- // Copyright (c) Microsoft Corporation. All rights reserved.
- // </copyright>
- // <owner current="true" primary="true">[....]</owner>
- // <owner current="true" primary="false">[....]</owner>
- //------------------------------------------------------------------------------
- namespace System.Data {
- using System;
- using System.Data.Common;
- using System.Xml;
- using System.Xml.Schema;
- using System.Diagnostics;
- using System.Collections;
- using System.Collections.Generic;
- using System.Globalization;
- using System.ComponentModel;
- using System.Security;
- using System.Security.Permissions;
- internal class XMLSchema {
- internal static TypeConverter GetConverter(Type type) {
- HostProtectionAttribute protAttrib = new HostProtectionAttribute();
- protAttrib.SharedState = true;
- CodeAccessPermission permission = (CodeAccessPermission)protAttrib.CreatePermission();
- permission.Assert();
- try {
- return TypeDescriptor.GetConverter(type);
- }
- finally {
- CodeAccessPermission.RevertAssert();
- }
- }
- internal static void SetProperties(Object instance, XmlAttributeCollection attrs) {
- // This is called from both XSD and XDR schemas.
- // Do we realy need it in XSD ???
- for (int i = 0; i < attrs.Count; i++) {
- if (attrs[i].NamespaceURI == Keywords.MSDNS) {
- string name = attrs[i].LocalName;
- string value = attrs[i].Value;
- if (name == "DefaultValue" || name == "RemotingFormat")
- continue;
- // Webdata 97925, skipp expressions, we will handle them after SetProperties (in xdrschema)
- if (name == "Expression" && instance is DataColumn)
- continue;
- PropertyDescriptor pd = TypeDescriptor.GetProperties(instance)[name];
- if (pd != null) {
- // Standard property
- Type type = pd.PropertyType;
- TypeConverter converter = XMLSchema.GetConverter(type);
- object propValue;
- if (converter.CanConvertFrom(typeof(string))) {
- propValue = converter.ConvertFromString(value);
- }else if (type == typeof(Type)) {
- propValue = DataStorage.GetType(value);
- }else if (type == typeof(CultureInfo)) {
- propValue = new CultureInfo(value);
- }else {
- throw ExceptionBuilder.CannotConvert(value,type.FullName);
- }
- pd.SetValue(instance, propValue);
- }
- }
- }
- }// SetProperties
- internal static bool FEqualIdentity(XmlNode node, String name, String ns) {
- if (node != null && node.LocalName == name && node.NamespaceURI == ns)
- return true;
- return false;
- }
- internal static bool GetBooleanAttribute(XmlElement element, String attrName, String attrNS, bool defVal) {
- string value = element.GetAttribute(attrName, attrNS);
- if (value == null || value.Length == 0) {
- return defVal;
- }
- if ((value == Keywords.TRUE) || (value == Keywords.ONE_DIGIT)){
- return true;
- }
- if ((value == Keywords.FALSE) || (value == Keywords.ZERO_DIGIT)){
- return false;
- }
- // Error processing:
- throw ExceptionBuilder.InvalidAttributeValue(attrName, value);
- }
- internal static string GenUniqueColumnName(string proposedName, DataTable table) {
- if (table.Columns.IndexOf(proposedName) >= 0) {
- for (int i=0; i <= table.Columns.Count; i++) {
- string tempName = proposedName + "_" + (i).ToString(CultureInfo.InvariantCulture);
- if (table.Columns.IndexOf(tempName) >= 0) {
- continue;
- }
- else {
- return tempName;
- }
- }
- }
- return proposedName;
- }
- }
- internal sealed class ConstraintTable {
- public DataTable table;
- public XmlSchemaIdentityConstraint constraint;
- public ConstraintTable(DataTable t, XmlSchemaIdentityConstraint c) {
- table = t;
- constraint = c;
- }
- }
- internal sealed class XSDSchema : XMLSchema
- {
- XmlSchemaSet _schemaSet = null;
- XmlSchemaElement dsElement = null;
- DataSet _ds = null;
- String _schemaName = null;
- private ArrayList ColumnExpressions;
- private Hashtable ConstraintNodes;
- private ArrayList RefTables;
- private ArrayList complexTypes;
- XmlSchemaObjectCollection annotations;
- XmlSchemaObjectCollection elements;
- Hashtable attributes;
- Hashtable elementsTable;
- Hashtable attributeGroups;
- Hashtable schemaTypes;
- Hashtable expressions;
- Dictionary <DataTable, List<DataTable>> tableDictionary;
- Hashtable udSimpleTypes;
- Hashtable existingSimpleTypeMap;
-
- private bool fromInference = false;
-
- internal bool FromInference {
- get {
- return fromInference;
- }
- set {
- fromInference = value;
- }
- }
- private void CollectElementsAnnotations(XmlSchema schema){
- ArrayList schemaList = new ArrayList();
- CollectElementsAnnotations(schema, schemaList);
- schemaList.Clear();
- }
-
- private void CollectElementsAnnotations(XmlSchema schema, ArrayList schemaList){
- if (schemaList.Contains(schema)) {
- return;
- }
- schemaList.Add(schema);
- foreach(object item in schema.Items) {
- if (item is XmlSchemaAnnotation) {
- annotations.Add((XmlSchemaAnnotation)item);
- }
- if (item is XmlSchemaElement) {
- XmlSchemaElement elem = (XmlSchemaElement)item;
- elements.Add(elem);
- elementsTable[elem.QualifiedName] = elem;
- }
- if (item is XmlSchemaAttribute) {
- XmlSchemaAttribute attr = (XmlSchemaAttribute)item;
- attributes[attr.QualifiedName] = attr;
- }
- if (item is XmlSchemaAttributeGroup) {
- XmlSchemaAttributeGroup attr = (XmlSchemaAttributeGroup)item;
- attributeGroups[attr.QualifiedName] = attr;
- }
- if (item is XmlSchemaType) {
- string MSDATATargetNamespace = null;
- if (item is XmlSchemaSimpleType) {
- MSDATATargetNamespace = XSDSchema.GetMsdataAttribute((XmlSchemaType)item, Keywords.TARGETNAMESPACE);
- }
-
- XmlSchemaType type = (XmlSchemaType)item;
- schemaTypes[type.QualifiedName] = type;
- // Webdata 92054 if we have a User Defined simple type, cache it so later we may need for mapping
- // meanwhile more convinient solution would be to directly use schemaTypes, but it would be more complex to handle
- XmlSchemaSimpleType xmlSimpleType = (item as XmlSchemaSimpleType );
- if (xmlSimpleType != null) {
- if (udSimpleTypes == null) {
- udSimpleTypes = new Hashtable();
- }
- udSimpleTypes[type.QualifiedName.ToString()] = xmlSimpleType;
- DataColumn dc = (DataColumn)existingSimpleTypeMap[type.QualifiedName.ToString()];
- // Assumption is that our simple type qualified name ihas the same output as XmlSchemaSimpleType type.QualifiedName.ToString()
- SimpleType tmpSimpleType = (dc != null)? dc.SimpleType: null;
- //
- if (tmpSimpleType != null) {
- SimpleType tmpDataSimpleType = new SimpleType(xmlSimpleType);
- string errorStr = tmpSimpleType.HasConflictingDefinition(tmpDataSimpleType);
- if (errorStr.Length != 0) {
- throw ExceptionBuilder.InvalidDuplicateNamedSimpleTypeDelaration(tmpDataSimpleType.SimpleTypeQualifiedName, errorStr);
- }
- }
-
- }
- }
- }
- foreach(XmlSchemaExternal include in schema.Includes) {
- if (include is XmlSchemaImport)
- continue;
- if (include.Schema != null) {
- CollectElementsAnnotations(include.Schema, schemaList);
- }
- }
- }
- internal static string QualifiedName(string name) {
- int iStart = name.IndexOf(':');
- if (iStart == -1)
- return Keywords.XSD_PREFIXCOLON + name;
- else
- return name;
- }
- internal static void SetProperties(Object instance, XmlAttribute[] attrs) {
- // This is called from both XSD and XDR schemas.
- // Do we realy need it in XSD ???
- if (attrs == null)
- return;
- for (int i = 0; i < attrs.Length; i++) {
- if (attrs[i].NamespaceURI == Keywords.MSDNS) {
- string name = attrs[i].LocalName;
- string value = attrs[i].Value;
- if (name == "DefaultValue" || name == "Ordinal" || name == "Locale" || name == "RemotingFormat")
- continue;
- if (name == "Expression" && instance is DataColumn) // Webdata 97925: we will handle columnexpressions at HandleColumnExpression
- continue;
- if (name == "DataType") {
- DataColumn col = instance as DataColumn;
- if (col != null) {
- col.DataType = DataStorage.GetType(value);
- }
- continue;
- }
- PropertyDescriptor pd = TypeDescriptor.GetProperties(instance)[name];
- if (pd != null) {
- // Standard property
- Type type = pd.PropertyType;
- TypeConverter converter = XMLSchema.GetConverter(type);
- object propValue;
- if (converter.CanConvertFrom(typeof(string))) {
- propValue = converter.ConvertFromString(value);
- }else if (type == typeof(Type)) {
- propValue = Type.GetType(value);
- }else if (type == typeof(CultureInfo)) {
- propValue = new CultureInfo(value);
- }else {
- throw ExceptionBuilder.CannotConvert(value,type.FullName);
- }
- pd.SetValue(instance, propValue);
- }
- }
- }
- }// SetProperties
- private static void SetExtProperties(Object instance, XmlAttribute[] attrs) {
- PropertyCollection props = null;
- if (attrs == null)
- return;
- for (int i = 0; i < attrs.Length; i++) {
- if (attrs[i].NamespaceURI == Keywords.MSPROPNS) {
- if(props == null) {
- object val = TypeDescriptor.GetProperties(instance)["ExtendedProperties"].GetValue(instance);
- Debug.Assert(val is PropertyCollection, "We can set values only for classes that have ExtendedProperties");
- props = (PropertyCollection) val;
- }
- string propName = XmlConvert.DecodeName(attrs[i].LocalName);
- if (instance is ForeignKeyConstraint) {
- if (propName.StartsWith(Keywords.MSD_FK_PREFIX, StringComparison.Ordinal))
- propName = propName.Substring(3);
- else
- continue;
- }
- if ((instance is DataRelation) && (propName.StartsWith(Keywords.MSD_REL_PREFIX, StringComparison.Ordinal))) {
- propName = propName.Substring(4);
- }
- else if ((instance is DataRelation) && (propName.StartsWith(Keywords.MSD_FK_PREFIX, StringComparison.Ordinal))) {
- continue;
- }
- props.Add(propName, attrs[i].Value);
- }
- }
- }// SetExtProperties
- private void HandleColumnExpression(Object instance, XmlAttribute[] attrs) {
- if (attrs == null)
- return;
- DataColumn dc = instance as DataColumn;
- Debug.Assert(dc != null, "HandleColumnExpression is supposed to be called for DataColumn");
- if (dc != null ) {
- for (int i = 0; i < attrs.Length; i++) {
- if (attrs[i].NamespaceURI == Keywords.MSDNS) {
- if (attrs[i].LocalName == "Expression"){
- if (this.expressions == null)
- this.expressions = new Hashtable();
- this.expressions[dc] = attrs[i].Value;
- ColumnExpressions.Add(dc);
- break;
- }
- }
- }
- }
- }
-
- internal static String GetMsdataAttribute(XmlSchemaAnnotated node, String ln) {
- XmlAttribute[] nodeAttributes = node.UnhandledAttributes;
- if (nodeAttributes!=null)
- for(int i=0; i<nodeAttributes.Length;i++)
- if (nodeAttributes[i].LocalName == ln && nodeAttributes[i].NamespaceURI == Keywords.MSDNS)
- return nodeAttributes[i].Value;
- return null;
- }
-
- private static void SetExtProperties(Object instance, XmlAttributeCollection attrs) {
- PropertyCollection props = null;
- for (int i = 0; i < attrs.Count; i++) {
- if (attrs[i].NamespaceURI == Keywords.MSPROPNS) {
- if(props == null) {
- object val = TypeDescriptor.GetProperties(instance)["ExtendedProperties"].GetValue(instance);
- Debug.Assert(val is PropertyCollection, "We can set values only for classes that have ExtendedProperties");
- props = (PropertyCollection) val;
- }
- string propName = XmlConvert.DecodeName(attrs[i].LocalName);
- props.Add(propName, attrs[i].Value);
- }
- }
- }// SetExtProperties
- internal void HandleRefTableProperties(ArrayList RefTables, XmlSchemaElement element) {
- string typeName = GetInstanceName(element);
- DataTable table = _ds.Tables.GetTable(XmlConvert.DecodeName(typeName), element.QualifiedName.Namespace);
- Debug.Assert(table != null, "ref table should have been already created");
- SetProperties(table, element.UnhandledAttributes);
- SetExtProperties(table, element.UnhandledAttributes);
- }
-
- internal void HandleRelation(XmlElement node, bool fNested) {
- string strName;
- string parentName;
- string childName;
- string [] parentNames;
- string [] childNames;
- string value;
- bool fCreateConstraints = false; //if we have a relation,
- //we do not have constraints
- DataRelationCollection rels = _ds.Relations;
- DataRelation relation;
- DataColumn [] parentKey;
- DataColumn [] childKey;
- DataTable parent;
- DataTable child;
- int keyLength;
- strName = XmlConvert.DecodeName(node.GetAttribute(Keywords.NAME));
- for (int i = 0; i < rels.Count; ++i) {
- if (0 == String.Compare(rels[i].RelationName, strName, StringComparison.Ordinal))
- return;
- }
- parentName = node.GetAttribute(Keywords.MSD_PARENT, Keywords.MSDNS);
- if (parentName == null || parentName.Length==0)
- throw ExceptionBuilder.RelationParentNameMissing(strName);
- parentName = XmlConvert.DecodeName(parentName);
-
- childName = node.GetAttribute(Keywords.MSD_CHILD, Keywords.MSDNS);
- if (childName == null || childName.Length==0)
- throw ExceptionBuilder.RelationChildNameMissing(strName);
- childName = XmlConvert.DecodeName(childName);
- value = node.GetAttribute(Keywords.MSD_PARENTKEY, Keywords.MSDNS);
- if (value == null || value.Length==0)
- throw ExceptionBuilder.RelationTableKeyMissing(strName);
- parentNames = value.TrimEnd(null).Split(new char[] {Keywords.MSD_KEYFIELDSEP, Keywords.MSD_KEYFIELDOLDSEP});
- value = node.GetAttribute(Keywords.MSD_CHILDKEY, Keywords.MSDNS);
- if (value == null || value.Length==0)
- throw ExceptionBuilder.RelationChildKeyMissing(strName);
- childNames = value.TrimEnd(null).Split(new char[] {Keywords.MSD_KEYFIELDSEP, Keywords.MSD_KEYFIELDOLDSEP});
- keyLength = parentNames.Length;
- if (keyLength != childNames.Length)
- throw ExceptionBuilder.MismatchKeyLength();
- parentKey = new DataColumn[keyLength];
- childKey = new DataColumn[keyLength];
- string parentNs = node.GetAttribute(Keywords.MSD_PARENTTABLENS, Keywords.MSDNS);
- string childNs = node.GetAttribute(Keywords.MSD_CHILDTABLENS, Keywords.MSDNS);
-
- parent = _ds.Tables.GetTableSmart(parentName,parentNs);
- if (parent == null)
- throw ExceptionBuilder.ElementTypeNotFound(parentName);
- child = _ds.Tables.GetTableSmart(childName,childNs);
- if (child == null)
- throw ExceptionBuilder.ElementTypeNotFound(childName);
- for (int i = 0; i < keyLength; i++) {
- parentKey[i] = parent.Columns[XmlConvert.DecodeName(parentNames[i])];
- if (parentKey[i] == null)
- throw ExceptionBuilder.ElementTypeNotFound(parentNames[i]);
- childKey[i] = child.Columns[XmlConvert.DecodeName(childNames[i])];
- if (childKey[i] == null)
- throw ExceptionBuilder.ElementTypeNotFound(childNames[i]);
- }
- relation = new DataRelation(strName, parentKey, childKey, fCreateConstraints);
- relation.Nested = fNested;
- SetExtProperties(relation, node.Attributes);
- _ds.Relations.Add(relation);
- if (FromInference && relation.Nested) {
- tableDictionary[relation.ParentTable].Add(relation.ChildTable);
- }
- }
- private bool HasAttributes(XmlSchemaObjectCollection attributes){
- foreach (XmlSchemaObject so in attributes) {
- if (so is XmlSchemaAttribute) {
- return true;
- }
- if (so is XmlSchemaAttributeGroup) {
- return true;
- }
- if (so is XmlSchemaAttributeGroupRef) {
- return true;
- }
- }
- return false;
- }
- private bool IsDatasetParticle(XmlSchemaParticle pt){
- XmlSchemaObjectCollection items = GetParticleItems(pt);
-
- if (items == null)
- return false; // empty element, threat it as table
-
- bool isChoice = this.FromInference && (pt is XmlSchemaChoice);// currently we add this support for choice, just for inference
- foreach (XmlSchemaAnnotated el in items){
- if (el is XmlSchemaElement) {
- // pushing max occur of choice element to its imidiate children of type xs:elements
- if (isChoice && pt.MaxOccurs > Decimal.One && (((XmlSchemaElement)el).SchemaType is XmlSchemaComplexType)) // we know frominference condition
- ((XmlSchemaElement)el).MaxOccurs = pt.MaxOccurs;
- if (((XmlSchemaElement)el).RefName.Name.Length != 0) {
- if (!FromInference || (((XmlSchemaElement)el).MaxOccurs != Decimal.One && !(((XmlSchemaElement)el).SchemaType is XmlSchemaComplexType)))
- continue;
- }
- if (!IsTable ((XmlSchemaElement)el))
- return false;
- continue;
- }
-
- if (el is XmlSchemaParticle) {
- if (!IsDatasetParticle((XmlSchemaParticle)el))
- return false;
- }
- }
- return true;
- }
-
- private int DatasetElementCount(XmlSchemaObjectCollection elements) {
- int nCount = 0;
- foreach(XmlSchemaElement XmlElement in elements) {
- if (GetBooleanAttribute(XmlElement, Keywords.MSD_ISDATASET, /*default:*/ false)) {
- nCount++;
- }
- }
- return nCount;
- }
- private XmlSchemaElement FindDatasetElement(XmlSchemaObjectCollection elements) {
- foreach(XmlSchemaElement XmlElement in elements) {
- if (GetBooleanAttribute(XmlElement, Keywords.MSD_ISDATASET, /*default:*/ false))
- return XmlElement;
- }
- if ((elements.Count == 1) || (this.FromInference && elements.Count > 0)) { //let's see if this element looks like a DataSet
- XmlSchemaElement node = (XmlSchemaElement)elements[0];
- if (!GetBooleanAttribute(node, Keywords.MSD_ISDATASET, /*default:*/ true))
- return null;
- XmlSchemaComplexType ct = node.SchemaType as XmlSchemaComplexType;
- if (ct == null)
- return null;
- while (ct != null) {
- if (HasAttributes(ct.Attributes))
- return null;
- if (ct.ContentModel is XmlSchemaSimpleContent){
- XmlSchemaAnnotated cContent = ((XmlSchemaSimpleContent) (ct.ContentModel)).Content;
- if (cContent is XmlSchemaSimpleContentExtension) {
- XmlSchemaSimpleContentExtension ccExtension = ((XmlSchemaSimpleContentExtension) cContent );
- if (HasAttributes(ccExtension.Attributes))
- return null;
- } else {
- XmlSchemaSimpleContentRestriction ccRestriction = ((XmlSchemaSimpleContentRestriction) cContent );
- if (HasAttributes(ccRestriction.Attributes))
- return null;
- }
- }
-
-
- XmlSchemaParticle particle = GetParticle(ct);
- if (particle != null) {
- if (!IsDatasetParticle(particle))
- return null; // it's a table
- }
- if (ct.BaseXmlSchemaType is XmlSchemaComplexType)
- ct = (XmlSchemaComplexType)ct.BaseXmlSchemaType;
- else
- break;
- }
- //if we are here there all elements are tables
- return node;
- }
- return null;
- }
- public void LoadSchema(XmlSchemaSet schemaSet , DataTable dt) {
- if (dt.DataSet != null)
- LoadSchema(schemaSet, dt.DataSet);
- }
- public void LoadSchema(XmlSchemaSet schemaSet , DataSet ds) { //Element schemaRoot, DataSet ds) {
- ConstraintNodes = new Hashtable();
- RefTables = new ArrayList();
- ColumnExpressions = new ArrayList();
- complexTypes = new ArrayList();
- bool setRootNStoDataSet = false;
- bool newDataSet = (ds.Tables.Count == 0);
- if (schemaSet == null) //
- return;
- _schemaSet = schemaSet;
- _ds = ds;
- ds.fIsSchemaLoading = true;
- foreach (XmlSchema schemaRoot in schemaSet.Schemas()) {
- _schemaName = schemaRoot.Id;
- if (_schemaName == null ||_schemaName.Length == 0) {
- _schemaName = "NewDataSet";
- }
- ds.DataSetName = XmlConvert.DecodeName(_schemaName);
- string ns = schemaRoot.TargetNamespace;
- if (ds.namespaceURI == null || ds.namespaceURI.Length == 0) {// set just one time, for backward compatibility
- ds.namespaceURI = (ns== null)? string.Empty : ns; // see fx\Data\XDO\ReadXml\SchemaM2.xml for more info
- }
- break; // we just need to take Name and NS from first schema [V1.0 & v1.1 semantics]
- }
-
- annotations = new XmlSchemaObjectCollection();
- elements = new XmlSchemaObjectCollection();
- elementsTable = new Hashtable();
- attributes = new Hashtable();
- attributeGroups = new Hashtable();
- schemaTypes = new Hashtable();
- tableDictionary = new Dictionary <DataTable, List<DataTable>>() ;
- existingSimpleTypeMap = new Hashtable();
- foreach(DataTable dt in ds.Tables){
- foreach(DataColumn dc in dt.Columns) {
- if (dc.SimpleType!= null && dc.SimpleType.Name != null && dc.SimpleType.Name.Length != 0) {
- existingSimpleTypeMap[dc.SimpleType.SimpleTypeQualifiedName] = dc;
- // existingSimpleTypeMap[dc.SimpleType.SimpleTypeQualifiedName] = dc.SimpleType;
- }
- }
- }
- foreach (XmlSchema schemaRoot in schemaSet.Schemas())
- CollectElementsAnnotations(schemaRoot);
- dsElement = FindDatasetElement(elements);
- if (dsElement != null) {
- string mainName = GetStringAttribute (dsElement, Keywords.MSD_MAINDATATABLE, "");
- if (null != mainName) {
- ds.MainTableName = XmlConvert.DecodeName (mainName);
- }
- }
- else {
- if (this.FromInference) {
- ds.fTopLevelTable = true; // Backward compatability: for inference, if we do not read DataSet element
- }
- // we should not write it also
- setRootNStoDataSet = true;
- //incase of Root is not mapped to DataSet and is mapped to DataTable instead; to be backward compatable
- // we need to set the Namespace of Root to DataSet's namespace also(it would be NS of First DataTable in collection)
- }
- List<XmlQualifiedName> qnames = new List<XmlQualifiedName>();
-
- if (ds != null && ds.UseDataSetSchemaOnly) {
- int dataSetElementCount = DatasetElementCount(elements);
- if (dataSetElementCount == 0) {
- throw ExceptionBuilder.IsDataSetAttributeMissingInSchema();
- }
- else if (dataSetElementCount > 1) {
- throw ExceptionBuilder.TooManyIsDataSetAtributeInSchema();
- }
- XmlSchemaComplexType ct = (XmlSchemaComplexType) FindTypeNode(dsElement);
- if (ct.Particle != null) {
- XmlSchemaObjectCollection items = GetParticleItems(ct.Particle);
- if (items != null) {
- foreach (XmlSchemaAnnotated el in items){
- XmlSchemaElement sel = el as XmlSchemaElement;
- if (null != sel) {
- if(sel.RefName.Name.Length != 0 ) {
- qnames.Add(sel.QualifiedName);
- }
- }
- }
- }
- }
-
- }
- // Walk all the top level Element tags.
- foreach (XmlSchemaElement element in elements) {
- if (element == dsElement)
- continue;
- if (ds != null && ds.UseDataSetSchemaOnly && dsElement != null) {
- if (dsElement.Parent != element.Parent) {
- if (!qnames.Contains(element.QualifiedName)) {
- continue;
- }
- }
- }
- String typeName = GetInstanceName(element);
- if (RefTables.Contains(element.QualifiedName.Namespace +":"+ typeName)) {
- HandleRefTableProperties(RefTables, element);
- continue;
- }
- DataTable table = HandleTable(element);
- }
-
- if (dsElement!=null)
- HandleDataSet(dsElement, newDataSet);
- foreach (XmlSchemaAnnotation annotation in annotations) {
- HandleRelations(annotation, false);
- }
- //just add Expressions, at this point and if ColumnExpressions.Count > 0, this.expressions should not be null
- for(int i=0; i<ColumnExpressions.Count; i++) {
- DataColumn dc = ((DataColumn)(ColumnExpressions[i]));
- dc.Expression = (string) this.expressions[dc];
- }
- foreach (DataTable dt in ds.Tables) {
- if (dt.NestedParentRelations.Length == 0 && dt.Namespace == ds.Namespace) {
- DataRelationCollection childRelations = dt.ChildRelations;// WebData 113251 if we nulling NS of parent table,
- for (int j = 0; j < childRelations.Count; j++) { // we need to do the same thing for nested child tables as they
- if (childRelations[j].Nested && dt.Namespace == childRelations[j].ChildTable.Namespace) { // take NS from Parent table
- childRelations[j].ChildTable.tableNamespace = null;
- }
- }
- dt.tableNamespace = null;
- }
- }
- DataTable tmpTable = ds.Tables[ds.DataSetName, ds.Namespace];
- if ( tmpTable != null) // this fix is done to support round-trip problem in case if there is one table with same name and NS
- tmpTable.fNestedInDataset = true;
- // this fix is for backward compatability with old inference engine
- if (this.FromInference && ds.Tables.Count == 0 && String.Compare(ds.DataSetName, "NewDataSet", StringComparison.Ordinal) == 0)
- ds.DataSetName = XmlConvert.DecodeName(((XmlSchemaElement)elements[0]).Name);
-
- ds.fIsSchemaLoading = false; //reactivate column computations
- //for backward compatability; we need to set NS of Root Element to DataSet, if root already does not mapped to dataSet
- if (setRootNStoDataSet) {
- if (ds.Tables.Count > 0) { // if there is table, take first one's NS
- ds.Namespace = ds.Tables[0].Namespace;
- ds.Prefix = ds.Tables[0].Prefix;
- }
- else {// otherwise, take TargetNS from first schema
- Debug.Assert(schemaSet.Count == 1, "there should be one schema");
- foreach (XmlSchema schemaRoot in schemaSet.Schemas()) { // we should have 1 schema
- ds.Namespace = schemaRoot.TargetNamespace;
- }
- }
- }
- }
- private void HandleRelations(XmlSchemaAnnotation ann, bool fNested) {
- foreach (object __items in ann.Items)
- if (__items is XmlSchemaAppInfo) {
- XmlNode[] relations = ((XmlSchemaAppInfo) __items).Markup;
- for (int i = 0; i<relations.Length; i++)
- if (FEqualIdentity(relations[i], Keywords.MSD_RELATION, Keywords.MSDNS))
- HandleRelation((XmlElement)relations[i], fNested);
- }
- }
-
- internal XmlSchemaObjectCollection GetParticleItems(XmlSchemaParticle pt){
- if (pt is XmlSchemaSequence)
- return ((XmlSchemaSequence)pt).Items;
- if (pt is XmlSchemaAll)
- return ((XmlSchemaAll)pt).Items;
- if (pt is XmlSchemaChoice)
- return ((XmlSchemaChoice)pt).Items;
- if (pt is XmlSchemaAny)
- return null;
- // the code below is a little hack for the SOM behavior
- if (pt is XmlSchemaElement) {
- XmlSchemaObjectCollection Items = new XmlSchemaObjectCollection();
- Items.Add(pt);
- return Items;
- }
- if (pt is XmlSchemaGroupRef)
- return GetParticleItems( ((XmlSchemaGroupRef)pt).Particle );
- // should never get here.
- return null;
- }
- internal void HandleParticle(XmlSchemaParticle pt, DataTable table, ArrayList tableChildren, bool isBase){
- XmlSchemaObjectCollection items = GetParticleItems(pt);
-
- if (items == null)
- return;
- foreach (XmlSchemaAnnotated item in items){
- XmlSchemaElement el = item as XmlSchemaElement;
- if (el != null) {
- if (this.FromInference && pt is XmlSchemaChoice && pt.MaxOccurs > Decimal.One && (el.SchemaType is XmlSchemaComplexType))
- ((XmlSchemaElement)el).MaxOccurs = pt.MaxOccurs;
-
- DataTable child = null;
- // to decide if element is our table, we need to match both name and ns
- // 286043 - SQL BU Defect Tracking
- if (((el.Name == null) && (el.RefName.Name == table.EncodedTableName && el.RefName.Namespace == table.Namespace)) ||
- (IsTable(el) && el.Name == table.TableName)) {
- if (this.FromInference) {
- child = HandleTable ((XmlSchemaElement)el);
- Debug.Assert(child == table, "table not the same");
- }
- else {
- child = table;
- }
- }
- else {
- child = HandleTable ((XmlSchemaElement)el);
- if (child == null && this.FromInference && el.Name == table.TableName) {
- child = table;
- }
- }
-
- if (child==null) {
- if (!this.FromInference || el.Name != table.TableName) {// check is required to support 1.1 inference behavior
- HandleElementColumn((XmlSchemaElement)el, table, isBase);
- }
- }
- else {
- DataRelation relation = null;
- if (el.Annotation != null)
- HandleRelations(el.Annotation, true);
- DataRelationCollection childRelations = table.ChildRelations;
- for (int j = 0; j < childRelations.Count; j++) {
- if (!childRelations[j].Nested)
- continue;
- if (child == childRelations[j].ChildTable)
- relation = childRelations[j];
- }
- if (relation == null) {
- tableChildren.Add(child);// how about prefix for this?
- if(this.FromInference && table.UKColumnPositionForInference == -1) { // this is done for Inference
- int ukColumnPosition = -1;
- foreach(DataColumn dc in table.Columns) {
- if (dc.ColumnMapping == MappingType.Element)
- ukColumnPosition++;
- }
- table.UKColumnPositionForInference = ukColumnPosition + 1; // since it starts from
- }
- }
- }
- }
- else {
- HandleParticle((XmlSchemaParticle)item, table, tableChildren, isBase);
- }
- }
- return ;
- }
- internal void HandleAttributes(XmlSchemaObjectCollection attributes, DataTable table, bool isBase) {
- foreach (XmlSchemaObject so in attributes) {
- if (so is XmlSchemaAttribute) {
- HandleAttributeColumn((XmlSchemaAttribute) so, table, isBase);
- }
- else { // XmlSchemaAttributeGroupRef
- XmlSchemaAttributeGroupRef groupRef = so as XmlSchemaAttributeGroupRef;
- XmlSchemaAttributeGroup schemaGroup = attributeGroups[groupRef.RefName] as XmlSchemaAttributeGroup;
- if (schemaGroup!=null) {
- HandleAttributeGroup(schemaGroup, table, isBase);
- }
- }
- }
- }
- private void HandleAttributeGroup(XmlSchemaAttributeGroup attributeGroup, DataTable table, bool isBase) {
- foreach (XmlSchemaObject obj in attributeGroup.Attributes) {
- if (obj is XmlSchemaAttribute) {
- HandleAttributeColumn((XmlSchemaAttribute) obj, table, isBase);
- }
- else { // XmlSchemaAttributeGroupRef
- XmlSchemaAttributeGroupRef attributeGroupRef = (XmlSchemaAttributeGroupRef)obj;
- XmlSchemaAttributeGroup attributeGroupResolved;
- if (attributeGroup.RedefinedAttributeGroup != null && attributeGroupRef.RefName == new XmlQualifiedName(attributeGroup.Name, attributeGroupRef.RefName.Namespace)) {
- attributeGroupResolved = (XmlSchemaAttributeGroup)attributeGroup.RedefinedAttributeGroup;
- }
- else {
- attributeGroupResolved = (XmlSchemaAttributeGroup)attributeGroups[attributeGroupRef.RefName];
- }
- if (attributeGroupResolved != null) {
- HandleAttributeGroup(attributeGroupResolved, table, isBase);
- }
- }
- }
- }
- internal void HandleComplexType(XmlSchemaComplexType ct, DataTable table, ArrayList tableChildren, bool isNillable){
- if (complexTypes.Contains(ct))
- throw ExceptionBuilder.CircularComplexType(ct.Name);
- bool isBase = false;
- complexTypes.Add(ct);
- if (ct.ContentModel != null) {
- /*
- HandleParticle(ct.CompiledParticle, table, tableChildren, isBase);
- foreach (XmlSchemaAttribute s in ct.Attributes){
- HandleAttributeColumn(s, table, isBase);
- }
- */
-
- if (ct.ContentModel is XmlSchemaComplexContent) {
- XmlSchemaAnnotated cContent = ((XmlSchemaComplexContent) (ct.ContentModel)).Content;
- if (cContent is XmlSchemaComplexContentExtension) {
- XmlSchemaComplexContentExtension ccExtension = ((XmlSchemaComplexContentExtension) cContent );
- if (!(ct.BaseXmlSchemaType is XmlSchemaComplexType && this.FromInference))
- HandleAttributes(ccExtension.Attributes, table, isBase);
- if (ct.BaseXmlSchemaType is XmlSchemaComplexType) {
- HandleComplexType((XmlSchemaComplexType)ct.BaseXmlSchemaType, table, tableChildren, isNillable);
- }
- else {
- Debug.Assert(ct.BaseXmlSchemaType is XmlSchemaSimpleType, "Expected SimpleType or ComplexType");
- if (ccExtension.BaseTypeName.Namespace != Keywords.XSDNS){
- // this is UDSimpleType, pass Qualified name of type
- HandleSimpleContentColumn(ccExtension.BaseTypeName.ToString(), table, isBase, ct.ContentModel.UnhandledAttributes, isNillable);
- }
- else { // it is built in type
- HandleSimpleContentColumn(ccExtension.BaseTypeName.Name, table, isBase, ct.ContentModel.UnhandledAttributes, isNillable);
- }
- }
- if (ccExtension.Particle != null)
- HandleParticle(ccExtension.Particle, table, tableChildren, isBase);
- if (ct.BaseXmlSchemaType is XmlSchemaComplexType && this.FromInference)
- HandleAttributes(ccExtension.Attributes, table, isBase);
- } else {
- Debug.Assert(cContent is XmlSchemaComplexContentRestriction, "Expected complexContent extension or restriction");
- XmlSchemaComplexContentRestriction ccRestriction = ((XmlSchemaComplexContentRestriction) cContent );
- if (!this.FromInference)
- HandleAttributes(ccRestriction.Attributes, table, isBase);
- if (ccRestriction.Particle != null)
- HandleParticle(ccRestriction.Particle, table, tableChildren, isBase);
- if (this.FromInference)
- HandleAttributes(ccRestriction.Attributes, table, isBase);
- }
- } else {
- Debug.Assert(ct.ContentModel is XmlSchemaSimpleContent, "expected simpleContent or complexContent");
- XmlSchemaAnnotated cContent = ((XmlSchemaSimpleContent) (ct.ContentModel)).Content;
- if (cContent is XmlSchemaSimpleContentExtension) {
- XmlSchemaSimpleContentExtension ccExtension = ((XmlSchemaSimpleContentExtension) cContent );
- HandleAttributes(ccExtension.Attributes, table, isBase);
- if (ct.BaseXmlSchemaType is XmlSchemaComplexType) {
- HandleComplexType((XmlSchemaComplexType)ct.BaseXmlSchemaType, table, tableChildren, isNillable);
- }
- else {
- Debug.Assert(ct.BaseXmlSchemaType is XmlSchemaSimpleType, "Expected SimpleType or ComplexType");
- HandleSimpleTypeSimpleContentColumn((XmlSchemaSimpleType)ct.BaseXmlSchemaType, ccExtension.BaseTypeName.Name, table, isBase, ct.ContentModel.UnhandledAttributes, isNillable);
- }
- } else {
- Debug.Assert(cContent is XmlSchemaSimpleContentRestriction, "Expected SimpleContent extension or restriction");
- XmlSchemaSimpleContentRestriction ccRestriction = ((XmlSchemaSimpleContentRestriction) cContent );
- HandleAttributes(ccRestriction.Attributes, table, isBase);
- }
- }
- }
- else {
- isBase = true;
- if (!this.FromInference)
- HandleAttributes(ct.Attributes, table, isBase);
- if (ct.Particle != null)
- HandleParticle(ct.Particle, table, tableChildren, isBase);
- if (this.FromInference) {
- HandleAttributes(ct.Attributes, table, isBase);
- if (isNillable) // this is for backward compatability to support xsi:Nill=true
- HandleSimpleContentColumn("string", table, isBase, null, isNillable);
- }
- }
- complexTypes.Remove(ct);
- }
- internal XmlSchemaParticle GetParticle(XmlSchemaComplexType ct){
- if (ct.ContentModel != null) {
- if (ct.ContentModel is XmlSchemaComplexContent) {
- XmlSchemaAnnotated cContent = ((XmlSchemaComplexContent) (ct.ContentModel)).Content;
- if (cContent is XmlSchemaComplexContentExtension) {
- return ((XmlSchemaComplexContentExtension) cContent ).Particle;
- } else {
- Debug.Assert(cContent is XmlSchemaComplexContentRestriction, "Expected complexContent extension or restriction");
- return ((XmlSchemaComplexContentRestriction) cContent ).Particle;
- }
- } else {
- Debug.Assert(ct.ContentModel is XmlSchemaSimpleContent, "expected simpleContent or complexContent");
- return null;
- }
-
- }
- else {
- return ct.Particle;
- }
- }
- internal DataColumn FindField(DataTable table, string field) {
- bool attribute = false;
- String colName = field;
-
- if (field.StartsWith("@", StringComparison.Ordinal)) {
- attribute = true;
- colName = field.Substring(1);
- }
- String [] split = colName.Split(':');
- colName = split [split.Length - 1];
- colName = XmlConvert.DecodeName(colName);
- DataColumn col = table.Columns[colName];
- if (col == null )
- throw ExceptionBuilder.InvalidField(field);
- bool _attribute = (col.ColumnMapping == MappingType.Attribute) || (col.ColumnMapping == MappingType.Hidden);
-
- if (_attribute != attribute)
- throw ExceptionBuilder.InvalidField(field);
-
- return col;
- }
- internal DataColumn[] BuildKey(XmlSchemaIdentityConstraint keyNode, DataTable table){
- ArrayList keyColumns = new ArrayList();
-
- foreach (XmlSchemaXPath node in keyNode.Fields) {
- keyColumns.Add(FindField(table, node.XPath));
- }
-
- DataColumn [] key = new DataColumn[keyColumns.Count];
- keyColumns.CopyTo(key, 0);
-
- return key;
- }
-
- internal bool GetBooleanAttribute(XmlSchemaAnnotated element, String attrName, bool defVal) {
- string value = GetMsdataAttribute(element, attrName);
- if (value == null || value.Length == 0) {
- return defVal;
- }
- if ((value == Keywords.TRUE) || (value == Keywords.ONE_DIGIT)){
- return true;
- }
- if ((value == Keywords.FALSE) || (value == Keywords.ZERO_DIGIT)){
- return false;
- }
- // Error processing:
- throw ExceptionBuilder.InvalidAttributeValue(attrName, value);
- }
- internal String GetStringAttribute(XmlSchemaAnnotated element, String attrName, String defVal) {
- string value = GetMsdataAttribute(element, attrName);
- if (value == null || value.Length == 0) {
- return defVal;
- }
- return value;
- }
-
- /*
- <key name="fk">
- <selector>../Customers</selector>
- <field>ID</field>
- </key>
- <keyref refer="fk">
- <selector>.</selector>
- <field>CustID</field>
- </keyref>
- */
- internal static AcceptRejectRule TranslateAcceptRejectRule( string strRule ) {
- if (strRule == "Cascade")
- return AcceptRejectRule.Cascade;
- else if (strRule == "None")
- return AcceptRejectRule.None;
- else
- return ForeignKeyConstraint.AcceptRejectRule_Default;
- }
- internal static Rule TranslateRule( string strRule ) {
- if (strRule == "Cascade")
- return Rule.Cascade;
- else if (strRule == "None")
- return Rule.None;
- else if (strRule == "SetDefault")
- return Rule.SetDefault;
- else if (strRule == "SetNull")
- return Rule.SetNull;
- else
- return ForeignKeyConstraint.Rule_Default;
- }
- internal void HandleKeyref(XmlSchemaKeyref keyref) {
- string refer = XmlConvert.DecodeName(keyref.Refer.Name); // check here!!!
- string name = XmlConvert.DecodeName(keyref.Name);
- name = GetStringAttribute( keyref, "ConstraintName", /*default:*/ name);
-
- // we do not process key defined outside the current node
-
- String tableName = GetTableName(keyref);
- string tableNs = GetMsdataAttribute(keyref,Keywords.MSD_TABLENS);
-
- DataTable table = _ds.Tables.GetTableSmart(tableName,tableNs);
-
- if (table == null)
- return;
- if (refer == null || refer.Length == 0)
- throw ExceptionBuilder.MissingRefer(name);
-
- ConstraintTable key = (ConstraintTable) ConstraintNodes[refer];
- if (key == null) {
- throw ExceptionBuilder.InvalidKey(name);
- }
- DataColumn[] pKey = BuildKey(key.constraint, key.table);
- DataColumn[] fKey = BuildKey(keyref, table);
- ForeignKeyConstraint fkc = null;
- if (GetBooleanAttribute(keyref, Keywords.MSD_CONSTRAINTONLY, /*default:*/ false)) {
- int iExisting = fKey[0].Table.Constraints.InternalIndexOf(name);
- if (iExisting > -1) {
- if (fKey[0].Table.Constraints[iExisting].ConstraintName != name)
- iExisting = -1;
- }
-
- if (iExisting < 0) {
- fkc = new ForeignKeyConstraint( name, pKey, fKey );
- fKey[0].Table.Constraints.Add(fkc);
- }
- }
- else {
- string relName = XmlConvert.DecodeName(GetStringAttribute( keyref, Keywords.MSD_RELATIONNAME, keyref.Name));
- if (relName == null || relName.Length == 0)
- relName = name;
- int iExisting = fKey[0].Table.DataSet.Relations.InternalIndexOf(relName);
- if (iExisting > -1) {
- if (fKey[0].Table.DataSet.Relations[iExisting].RelationName != relName)
- iExisting = -1;
- }
- DataRelation relation = null;
- if (iExisting < 0) {
- relation = new DataRelation(relName, pKey, fKey);
- SetExtProperties(relation, keyref.UnhandledAttributes);
- pKey[0].Table.DataSet.Relations.Add(relation);
- if (FromInference && relation.Nested) {
- if (tableDictionary.ContainsKey(relation.ParentTable)) {
- tableDictionary[relation.ParentTable].Add(relation.ChildTable);
- }
- }
-
- fkc = relation.ChildKeyConstraint;
- fkc.ConstraintName = name;
- }
- else {
- relation = fKey[0].Table.DataSet.Relations[iExisting];
- }
- if (GetBooleanAttribute(keyref, Keywords.MSD_ISNESTED, /*default:*/ false)) {
- relation.Nested = true;
- }
- }
- string acceptRejectRule = GetMsdataAttribute(keyref, Keywords.MSD_ACCEPTREJECTRULE);
- string updateRule = GetMsdataAttribute(keyref, Keywords.MSD_UPDATERULE);
- string deleteRule = GetMsdataAttribute(keyref, Keywords.MSD_DELETERULE);
- if (fkc != null) {
- if (acceptRejectRule != null)
- fkc.AcceptRejectRule = TranslateAcceptRejectRule(acceptRejectRule);
- if (updateRule != null)
- fkc.UpdateRule = TranslateRule(updateRule);
- if (deleteRule != null)
- fkc.DeleteRule = TranslateRule(deleteRule);
- SetExtProperties(fkc, keyref.UnhandledAttributes);
- }
- }
- internal void HandleConstraint(XmlSchemaIdentityConstraint keyNode){
- String name = null;
-
- name = XmlConvert.DecodeName(keyNode.Name);
- if (name==null || name.Length==0)
- throw ExceptionBuilder.MissingAttribute(Keywords.NAME);
- if (ConstraintNodes.ContainsKey(name))
- throw ExceptionBuilder.DuplicateConstraintRead(name);
- // we do not process key defined outside the current node
- String tableName = GetTableName(keyNode);
- string tableNs = GetMsdataAttribute(keyNode,Keywords.MSD_TABLENS);
-
- DataTable table = _ds.Tables.GetTableSmart(tableName,tableNs);
- if (table == null)
- return;
- ConstraintNodes.Add(name, new ConstraintTable(table, keyNode));
- bool fPrimaryKey = GetBooleanAttribute(keyNode, Keywords.MSD_PRIMARYKEY, /*default:*/ false);
- name = GetStringAttribute(keyNode, "ConstraintName", /*default:*/ name);
- DataColumn[] key = BuildKey(keyNode, table);
- if (0 < key.Length) {
- UniqueConstraint found = (UniqueConstraint) key[0].Table.Constraints.FindConstraint(new UniqueConstraint(name, key));
- if (found == null) {
- key[0].Table.Constraints.Add(name, key, fPrimaryKey);
- SetExtProperties(key[0].Table.Constraints[name], keyNode.UnhandledAttributes);
- }
- else {
- key = found.ColumnsReference;
- SetExtProperties(found, keyNode.UnhandledAttributes);
- if (fPrimaryKey)
- key[0].Table.PrimaryKey = key;
- }
- if (keyNode is XmlSchemaKey) {
- for (int i=0; i<key.Length; i++)
- key[i].AllowDBNull = false;
- }
- }
- }
- internal DataTable InstantiateSimpleTable(XmlSchemaElement node) {
- DataTable table;
- String typeName = XmlConvert.DecodeName(GetInstanceName(node));
- String _TableUri;
-
- _TableUri = node.QualifiedName.Namespace;
- table = _ds.Tables.GetTable(typeName, _TableUri);
- //
- /* if (table!=null) {
- throw ExceptionBuilder.DuplicateDeclaration(typeName);
- }
- */
- if (!FromInference && table!=null) {
- throw ExceptionBuilder.DuplicateDeclaration(typeName);
- }
- if (table == null) {
- table = new DataTable( typeName);
- table.Namespace = _TableUri;
- // If msdata:targetNamespace node on element, then use it to override table.Namespace.
- table.Namespace = GetStringAttribute(node, "targetNamespace", _TableUri);
-
- if (!this.FromInference) {
- table.MinOccurs = node.MinOccurs;
- table.MaxOccurs = node.MaxOccurs;
- }
- else {
- string prefix = GetPrefix(_TableUri);
- if (prefix != null)
- table.Prefix = prefix;
- }
- SetProperties(table, node.UnhandledAttributes);
- SetExtProperties(table, node.UnhandledAttributes);
- }
- XmlSchemaComplexType ct = node.SchemaType as XmlSchemaComplexType;
- // We assume node.ElementSchemaType.BaseSchemaType to be null for
- // <xs:element name="foo"/> and not null for <xs:element name="foo" type="xs:string"/>
- bool isSimpleContent = ((node.ElementSchemaType.BaseXmlSchemaType != null) ||(ct != null && ct.ContentModel is XmlSchemaSimpleContent));
- if (!FromInference ||(isSimpleContent && table.Columns.Count == 0 )) {// for inference backward compatability
- HandleElementColumn(node, table, false);
- string colName;
-
- if (this.FromInference) {
- int i = 0;
- colName = typeName + "_Text";
- while (table.Columns[colName] != null)
- colName = colName + i++;
- }
- else {
- colName = typeName + "_Column";
- }
- table.Columns[0].ColumnName = colName;
- table.Columns[0].ColumnMapping = MappingType.SimpleContent;
- }
-
- if (!this.FromInference ||_ds.Tables.GetTable(typeName, _TableUri) == null) { // for inference; special case: add table if doesnot exists in collection
- _ds.Tables.Add(table);
- if (this.FromInference) {
- tableDictionary.Add(table, new List<DataTable>());
- }
- }
- // handle all the unique and key constraints related to this table
- if ((dsElement != null) && (dsElement.Constraints!=null)) {
- foreach (XmlSchemaIdentityConstraint key in dsElement.Constraints) {
- if (key is XmlSchemaKeyref)
- continue;
- if (GetTableName(key) == table.TableName)
- HandleConstraint(key);
- }
- }
- table.fNestedInDataset = false;
- return (table);
- }
- internal string GetInstanceName(XmlSchemaAnnotated node) {
- string instanceName = null;
- Debug.Assert( (node is XmlSchemaElement) || (node is XmlSchemaAttribute), "GetInstanceName should only be called on attribute or elements");
- if (node is XmlSchemaElement) {
- XmlSchemaElement el = (XmlSchemaElement) node;
- instanceName = el.Name != null ? el.Name : el.RefName.Name;
- }
- else if (node is XmlSchemaAttribute) {
- XmlSchemaAttribute el = (XmlSchemaAttribute) node;
- instanceName = el.Name != null ? el.Name : el.RefName.Name;
- }
- Debug.Assert((instanceName != null) && (instanceName.Length != 0), "instanceName cannot be null or empty. There's an error in the XSD compiler");
- return instanceName;
- }
- // Sequences of handling Elements, Attributes and Text-only column should be the same as in InferXmlSchema
- internal DataTable InstantiateTable(XmlSchemaElement node, XmlSchemaComplexType typeNode, bool isRef) {
- DataTable table;
- String typeName = GetInstanceName(node);
- ArrayList tableChildren = new ArrayList();
-
- String _TableUri;
-
- _TableUri = node.QualifiedName.Namespace;
- table = _ds.Tables.GetTable(XmlConvert.DecodeName(typeName), _TableUri);
- // TOD: Do not do this fix
- // if (table == null && node.RefName.IsEmpty && !IsTopLevelElement(node) && _TableUri != null && _TableUri.Length > 0) {
- // _TableUri = null; // it means form="qualified", so child element inherits namespace. [....]
- // }
-
- if (!FromInference || (FromInference && table == null))
- {
- if (table!=null)
- {
- if (isRef)
- return table;
- else
- throw ExceptionBuilder.DuplicateDeclaration(typeName);
- }
-
- if (isRef)
- RefTables.Add(_TableUri+":"+typeName);
- table = new DataTable(XmlConvert.DecodeName(typeName) );
- table.TypeName = node.SchemaTypeName;
- table.Namespace = _TableUri;
- table.Namespace = GetStringAttribute(node, "targetNamespace", _TableUri);
- //table.Prefix = node.Prefix;
- String value= GetStringAttribute(typeNode, Keywords.MSD_CASESENSITIVE, "") ;
- if (value.Length ==0)
- {
- value= GetStringAttribute(node, Keywords.MSD_CASESENSITIVE, "") ;
- }
- if (0 < value.Length) {
- if ((value == Keywords.TRUE) || (value == "True"))
- table.CaseSensitive = true;
- if ((value == Keywords.FALSE) || (value == "False"))
- table.CaseSensitive = false;
- }
- value = GetMsdataAttribute(node, Keywords.MSD_LOCALE);
- if (null != value) { // set by user
- if (0 < value.Length) {
- // <... msdata:Locale="en-US"/>
- table.Locale = new CultureInfo(value);
- }
- else {
- // everett bug behavior before <... msdata:Locale=""/> inherit from DataSet
- table.Locale = CultureInfo.InvariantCulture;
- }
- }
-
- // else inherit from DataSet, not set by user
-
- if (!this.FromInference)
- {
- table.MinOccurs = node.MinOccurs;
- table.MaxOccurs = node.MaxOccurs;
- }
- else
- {
- string prefix = GetPrefix(_TableUri);
- if (prefix != null)
- table.Prefix = prefix;
- }
- _ds.Tables.Add(table);
- if (FromInference) {
- tableDictionary.Add(table, new List<DataTable>());
- }
- }
-
- HandleComplexType(typeNode, table, tableChildren, node.IsNillable);
- for (int i=0; i < table.Columns.Count ; i++)
- table.Columns[i].SetOrdinalInternal(i);
- /*
- if (xmlContent == XmlContent.Mixed) {
- string textColumn = GenUniqueColumnName(table.TableName+ "_Text", table);
- table.XmlText = new DataColumn(textColumn, typeof(string), null, MappingType.Text);
- } */
- SetProperties(table, node.UnhandledAttributes);
- SetExtProperties(table, node.UnhandledAttributes);
- // handle all the unique and key constraints related to this table
- if ((dsElement != null) && (dsElement.Constraints!=null)) {
- foreach (XmlSchemaIdentityConstraint key in dsElement.Constraints) {
- if (key is XmlSchemaKeyref)
- continue;
- if (GetTableName(key) == table.TableName) {
- // respect the NS if it is specified for key, otherwise just go with table name check
- if( GetTableNamespace(key) == table.Namespace || GetTableNamespace(key) == null)
- HandleConstraint(key);
- /* if (GetTableNamespace(key) != null) {
- if (GetTableNamespace(key) == table.Namespace)
- HandleConstraint(key);
- }
- else {
- HandleConstraint(key);
- }
- */
- }
- }
- }
- foreach(DataTable _tableChild in tableChildren) {
- if (_tableChild != table && table.Namespace == _tableChild.Namespace)
- _tableChild.tableNamespace = null;
- if ((dsElement != null) && (dsElement.Constraints!=null)) {
- foreach (XmlSchemaIdentityConstraint key in dsElement.Constraints) {
- XmlSchemaKeyref keyref = key as XmlSchemaKeyref;
- if (keyref == null)
- continue;
- bool isNested = GetBooleanAttribute(keyref, Keywords.MSD_ISNESTED, /*default:*/ false);
- if (!isNested)
- continue;
- if (GetTableName(keyref) == _tableChild.TableName) {
- if (_tableChild.DataSet.Tables.InternalIndexOf(_tableChild.TableName) < -1) { // if we have multiple tables with the same name
- if (GetTableNamespace(keyref) == _tableChild.Namespace) {
- HandleKeyref(keyref);
- }
- }
- else {
- HandleKeyref(keyref);
- }
- }
- }
- }
- DataRelation relation = null;
- DataRelationCollection childRelations = table.ChildRelations;
- for (int j = 0; j < childRelations.Count; j++) {
- if (!childRelations[j].Nested)
- continue;
- if (_tableChild == childRelations[j].ChildTable)
- relation = childRelations[j];
- }
- if (relation!=null)
- continue;
- DataColumn parentKey;
- if (this.FromInference) {
- int position = table.UKColumnPositionForInference;// we keep posiotion of unique key column here, for inference
- if (position == -1)
- foreach (DataColumn dc in table.Columns) {
- if (dc.ColumnMapping == MappingType.Attribute) {
- position = dc.Ordinal;
- break;
- }
- }
- parentKey = table.AddUniqueKey(position);
- }
- else {
- parentKey = table.AddUniqueKey();
- }
- // foreign key in the child table
- DataColumn childKey = _tableChild.AddForeignKey(parentKey);
- // when we add unique key, we do set prefix; but for Fk we do not do . So for backward compatability
- if (this.FromInference)
- childKey.Prefix = _tableChild.Prefix;
- // childKey.Prefix = GetPrefix(childKey.Namespace);
- // create relationship
- // setup relationship between parent and this table
- relation = new DataRelation(table.TableName + "_" + _tableChild.TableName, parentKey, childKey, true);
- relation.Nested = true;
- _tableChild.DataSet.Relations.Add(relation);
- if (FromInference && relation.Nested) {
- if (tableDictionary.ContainsKey(relation.ParentTable)) {
- tableDictionary[relation.ParentTable].Add(relation.ChildTable);
- }
- }
- }
- return (table);
- }
- private sealed class NameType : IComparable {
- public readonly String name;
- public readonly Type type;
- public NameType(String n, Type t) {
- name = n;
- type = t;
- }
- public int CompareTo(object obj) { return String.Compare(name, (string)obj, StringComparison.Ordinal); }
- };
- public static Type XsdtoClr(string xsdTypeName) {
- #if DEBUG
- for(int i = 1; i < mapNameTypeXsd.Length; ++i) {
- Debug.Assert((mapNameTypeXsd[i-1].CompareTo(mapNameTypeXsd[i].name)) < 0, "incorrect sorting " + mapNameTypeXsd[i].name);
- }
- #endif
- int index = Array.BinarySearch(mapNameTypeXsd, xsdTypeName);
- if (index < 0) {
- throw ExceptionBuilder.UndefinedDatatype(xsdTypeName);
- }
- return mapNameTypeXsd[index].type;
- }
-
- // XSD spec: http://www.w3.org/TR/xmlschema-2/
- // April: http://www.w3.org/TR/2000/WD-xmlschema-2-20000407/datatypes.html
- // Fabr: http://www.w3.org/TR/2000/WD-xmlschema-2-20000225/
- private static readonly NameType[] mapNameTypeXsd = {
- new NameType("ENTITIES" , typeof(string) ), /* XSD Apr */
- new NameType("ENTITY" , typeof(string) ), /* XSD Apr */
- new NameType("ID" , typeof(string) ), /* XSD Apr */
- new NameType("IDREF" , typeof(string) ), /* XSD Apr */
- new NameType("IDREFS" , typeof(string) ), /* XSD Apr */
- new NameType("NCName" , typeof(string) ), /* XSD Apr */
- new NameType("NMTOKEN" , typeof(string) ), /* XSD Apr */
- new NameType("NMTOKENS" , typeof(string) ), /* XSD Apr */
- new NameType("NOTATION" , typeof(string) ), /* XSD Apr */
- new NameType("Name" , typeof(string) ), /* XSD Apr */
- new NameType("QName" , typeof(string) ), /* XSD Apr */
- new NameType("anyType" , typeof(System.Object) ), /* XSD Apr */
- new NameType("anyURI" , typeof(System.Uri) ), /* XSD Apr */
- new NameType("base64Binary" , typeof(Byte[]) ), /* XSD Apr : abstruct */
- new NameType("boolean" , typeof(bool) ), /* XSD Apr */
- new NameType("byte" , typeof(SByte) ), /* XSD Apr */
- new NameType("date" , typeof(DateTime)), /* XSD Apr */
- new NameType("dateTime" , typeof(DateTime)), /* XSD Apr */
- new NameType("decimal" , typeof(decimal) ), /* XSD 2001 [....] */
- new NameType("double" , typeof(double) ), /* XSD Apr */
- new NameType("duration" , typeof(TimeSpan)), /* XSD Apr */
- new NameType("float" , typeof(Single) ), /* XSD Apr */
- new NameType("gDay" , typeof(DateTime)), /* XSD Apr */
- new NameType("gMonth" , typeof(DateTime)), /* XSD Apr */
- new NameType("gMonthDay" , typeof(DateTime)), /* XSD Apr */
- new NameType("gYear" , typeof(DateTime)), /* XSD Apr */
- new NameType("gYearMonth" , typeof(DateTime)), /* XSD Apr */
- new NameType("hexBinary" , typeof(Byte[]) ), /* XSD Apr : abstruct */
- new NameType("int" , typeof(Int32) ), /* XSD Apr */
- new NameType("integer" , typeof(Int64) ), /* XSD Apr */ // <xs:element name="" msdata:DataType="System.Numerics.BigInteger" type="xs:integer" minOccurs="0" />
- new NameType("language" , typeof(string) ), /* XSD Apr */
- new NameType("long" , typeof(Int64) ), /* XSD Apr */
- new NameType("negativeInteger" , typeof(Int64) ), /* XSD Apr */
- new NameType("nonNegativeInteger" , typeof(UInt64) ), /* XSD Apr */
- new NameType("nonPositiveInteger" , typeof(Int64) ), /* XSD Apr */
- new NameType("normalizedString" , typeof(string) ), /* XSD Apr */
- new NameType("positiveInteger" , typeof(UInt64) ), /* XSD Apr */
- new NameType("short" , typeof(Int16) ), /* XSD Apr */
- new NameType("string" , typeof(string) ), /* XSD Apr */
- new NameType("time" , typeof(DateTime)), /* XSD Apr */
- new NameType("unsignedByte" , typeof(Byte) ), /* XSD Apr */
- new NameType("unsignedInt" , typeof(UInt32) ), /* XSD Apr */
- new NameType("unsignedLong" , typeof(UInt64) ), /* XSD Apr */
- new NameType("unsignedShort" , typeof(UInt16) ), /* XSD Apr */
- };
- private static NameType FindNameType(string name) {
- #if DEBUG
- for(int i = 1; i < mapNameTypeXsd.Length; ++i) {
- Debug.Assert((mapNameTypeXsd[i-1].CompareTo(mapNameTypeXsd[i].name)) < 0, "incorrect sorting " + mapNameTypeXsd[i].name);
- }
- #endif
- int index = Array.BinarySearch(mapNameTypeXsd, name);
- if (index < 0) {
- throw ExceptionBuilder.UndefinedDatatype(name);
- }
- return mapNameTypeXsd[index];
- }
- // input param dt is a "qName" for UDSimpleType else it assumes it's a XSD builtin simpleType
- private Type ParseDataType(string dt) {
- if (!IsXsdType(dt)) {
- if (udSimpleTypes != null) {
- XmlSchemaSimpleType simpleType = (XmlSchemaSimpleType) udSimpleTypes[dt];
- if (simpleType == null) { // it is not named simple type, it is not XSD type, it should be unsupported type like xs:token
- throw ExceptionBuilder.UndefinedDatatype(dt);
- }
- SimpleType rootType = new SimpleType(simpleType);
- while (rootType.BaseSimpleType != null) {
- rootType = rootType.BaseSimpleType;
- }
- return ParseDataType(rootType.BaseType);
- }
- }
- NameType nt = FindNameType(dt);
- return nt.type;
- }
- /* later we may need such a function
- private Boolean IsUDSimpleType(string qname) {
- if (udSimpleTypes == null)
- return false;
- return (udSimpleTypes.Contains(qname));
- }
- */
- internal static Boolean IsXsdType(string name) {
- #if DEBUG
- for(int i = 1; i < mapNameTypeXsd.Length; ++i) {
- Debug.Assert((mapNameTypeXsd[i-1].CompareTo(mapNameTypeXsd[i].name)) < 0, "incorrect sorting " + mapNameTypeXsd[i].name);
- }
- #endif
- int index = Array.BinarySearch(mapNameTypeXsd, name);
- if (index < 0) {
- #if DEBUG
- // Let's check that we realy don't have this name:
- foreach (NameType nt in mapNameTypeXsd) {
- Debug.Assert(nt.name != name, "FindNameType('" + name + "') -- failed. Existed name not found");
- }
- #endif
- return false;
- }
- Debug.Assert(mapNameTypeXsd[index].name == name, "FindNameType('" + name + "') -- failed. Wrong name found");
- return true;
- }
- internal XmlSchemaAnnotated FindTypeNode(XmlSchemaAnnotated node) {
- // this function is returning null
- // if the typeNode for node is in the XSD namespace.
-
- XmlSchemaAttribute attr = node as XmlSchemaAttribute;
- XmlSchemaElement el = node as XmlSchemaElement;
- bool isAttr = false;
- if (attr != null) {
- isAttr = true;
- }
- String _type = isAttr ? attr.SchemaTypeName.Name : el.SchemaTypeName.Name;
- String _typeNs = isAttr ? attr.SchemaTypeName.Namespace : el.SchemaTypeName.Namespace;
- if (_typeNs == Keywords.XSDNS)
- return null;
- XmlSchemaAnnotated typeNode;
- if (_type == null || _type.Length == 0) {
- _type = isAttr ? attr.RefName.Name : el.RefName.Name;
- if (_type == null || _type.Length == 0)
- typeNode = (XmlSchemaAnnotated) (isAttr ? attr.SchemaType : el.SchemaType);
- else
- typeNode = isAttr ? FindTypeNode((XmlSchemaAnnotated)attributes[attr.RefName]) :FindTypeNode((XmlSchemaAnnotated)elementsTable[el.RefName]);
- }
- else
- typeNode = (XmlSchemaAnnotated)schemaTypes[isAttr ? ((XmlSchemaAttribute)node).SchemaTypeName : ((XmlSchemaElement)node).SchemaTypeName];
- return typeNode;
- }
- internal void HandleSimpleTypeSimpleContentColumn(XmlSchemaSimpleType typeNode, string strType, DataTable table, bool isBase, XmlAttribute[] attrs, bool isNillable){
- // disallow multiple simple content columns for the table
- if(FromInference && table.XmlText != null) { // backward compatability for inference
- return;
- }
- Type type = null;
- SimpleType xsdType = null;
-
- // if (typeNode.QualifiedName.Namespace != Keywords.XSDNS) { // this means UDSimpleType
- if (typeNode.QualifiedName.Name != null && typeNode.QualifiedName.Name.Length != 0 && typeNode.QualifiedName.Namespace != Keywords.XSDNS) { // this means UDSimpleType
- xsdType = new SimpleType(typeNode);
- strType = typeNode.QualifiedName.ToString(); // use qualifed name
- type = ParseDataType(typeNode.QualifiedName.ToString());
- }
- else {// previous code V 1.1
- XmlSchemaSimpleType ancestor = typeNode.BaseXmlSchemaType as XmlSchemaSimpleType;
- if ((ancestor != null) && (ancestor.QualifiedName.Namespace != Keywords.XSDNS)) {
- xsdType = new SimpleType(typeNode);
- SimpleType rootType = xsdType;
- while (rootType.BaseSimpleType != null) {
- rootType = rootType.BaseSimpleType;
- }
- type = ParseDataType(rootType.BaseType);
- strType = xsdType.Name;
- }
- else {
- type = ParseDataType(strType);
- }
- }
-
- DataColumn column;
- string colName;
- if (this.FromInference) {
- int i = 0;
- colName = table.TableName + "_Text";
- while (table.Columns[colName] != null) {
- colName = colName + i++;
- }
- }
- else
- colName = table.TableName + "_text";
- string columnName = colName;
- bool isToAdd = true;
- if ((!isBase) && (table.Columns.Contains(columnName, true))){
- column = table.Columns[columnName];
- isToAdd = false;
- }
- else {
- column = new DataColumn(columnName, type, null, MappingType.SimpleContent);
- }
- SetProperties(column, attrs);
- HandleColumnExpression(column, attrs);
- SetExtProperties(column, attrs);
- String tmp = (-1).ToString(CultureInfo.CurrentCulture);
- string defValue = null;
- //try to see if attributes contain allownull
- column.AllowDBNull = isNillable;
- if(attrs!=null)
- for(int i=0; i< attrs.Length;i++) {
- if ( attrs[i].LocalName == Keywords.MSD_ALLOWDBNULL && attrs[i].NamespaceURI == Keywords.MSDNS)
- if ( attrs[i].Value == Keywords.FALSE)
- column.AllowDBNull = false;
- if ( attrs[i].LocalName == Keywords.MSD_ORDINAL && attrs[i].NamespaceURI == Keywords.MSDNS)
- tmp = attrs[i].Value;
- if ( attrs[i].LocalName == Keywords.MSD_DEFAULTVALUE && attrs[i].NamespaceURI == Keywords.MSDNS)
- defValue = attrs[i].Value;
- }
- int ordinal = (int)Convert.ChangeType(tmp, typeof(int), null);
-
- //SetExtProperties(column, attr.UnhandledAttributes);
- if ((column.Expression!=null)&&(column.Expression.Length!=0)) {
- ColumnExpressions.Add(column);
- }
- // Update XSD type to point to simple types actual namespace instead of normalized default namespace in case of remoting
- if (xsdType != null && xsdType.Name != null && xsdType.Name.Length > 0) {
- if (XSDSchema.GetMsdataAttribute(typeNode, Keywords.TARGETNAMESPACE)!= null) {
- column.XmlDataType = xsdType.SimpleTypeQualifiedName;
- }
- }
- else {
- column.XmlDataType = strType;
- }
- column.SimpleType = xsdType;
- //column.Namespace = typeNode.SourceUri;
- if (isToAdd) {
- if (this.FromInference) {
- column.Prefix = GetPrefix(table.Namespace);
- column.AllowDBNull = true;
- }
- if(ordinal>-1 && ordinal<table.Columns.Count)
- table.Columns.AddAt(ordinal, column);
- else
- table.Columns.Add(column);
- }
- if (defValue != null)
- try {
- column.DefaultValue = column.ConvertXmlToObject(defValue);
- }
- catch (System.FormatException) {
- throw ExceptionBuilder.CannotConvert(defValue, type.FullName);
- }
- }
- internal void HandleSimpleContentColumn(String strType, DataTable table, bool isBase, XmlAttribute[] attrs, bool isNillable){
- // for Named Simple type support : We should not recieved anything here other than string.
- // there can not be typed simple content
- // disallow multiple simple content columns for the table
- if(this.FromInference && table.XmlText != null) // backward compatability for inference
- return;
-
- Type type = null;
- if (strType == null) {
- return;
- }
- type = ParseDataType(strType); // we pass it correctly when we call the method, no need to special check.
- DataColumn column;
- string colName;
- if (this.FromInference) {
- int i = 0;
- colName = table.TableName + "_Text";
- while (table.Columns[colName] != null) {
- colName = colName + i++;
- }
- }
- else
- colName = table.TableName + "_text";
- string columnName = colName;
- bool isToAdd = true;
- if ((!isBase) && (table.Columns.Contains(columnName, true))){
- column = table.Columns[columnName];
- isToAdd = false;
- }
- else {
- column = new DataColumn(columnName, type, null, MappingType.SimpleContent);
- }
-
- SetProperties(column, attrs);
- HandleColumnExpression(column, attrs);
- SetExtProperties(column, attrs);
- String tmp = (-1).ToString(CultureInfo.CurrentCulture);
- string defValue = null;
- //try to see if attributes contain allownull
- column.AllowDBNull = isNillable;
- if(attrs!=null)
- for(int i=0; i< attrs.Length;i++) {
- if ( attrs[i].LocalName == Keywords.MSD_ALLOWDBNULL && attrs[i].NamespaceURI == Keywords.MSDNS)
- if ( attrs[i].Value == Keywords.FALSE)
- column.AllowDBNull = false;
- if ( attrs[i].LocalName == Keywords.MSD_ORDINAL && attrs[i].NamespaceURI == Keywords.MSDNS)
- tmp = attrs[i].Value;
- if ( attrs[i].LocalName == Keywords.MSD_DEFAULTVALUE && attrs[i].NamespaceURI == Keywords.MSDNS)
- defValue = attrs[i].Value;
- }
- int ordinal = (int)Convert.ChangeType(tmp, typeof(int), null);
-
- //SetExtProperties(column, attr.UnhandledAttributes);
- if ((column.Expression!=null)&&(column.Expression.Length!=0)) {
- ColumnExpressions.Add(column);
- }
- column.XmlDataType = strType;
- column.SimpleType = null;
- //column.Namespace = typeNode.SourceUri;
- if(this.FromInference)
- column.Prefix = GetPrefix(column.Namespace);
- if (isToAdd) {
- if (this.FromInference) // move this setting to SetProperties
- column.AllowDBNull = true;
- if(ordinal>-1 && ordinal<table.Columns.Count)
- table.Columns.AddAt(ordinal, column);
- else
- table.Columns.Add(column);
- }
- if (defValue != null)
- try {
- column.DefaultValue = column.ConvertXmlToObject(defValue);
- }
- catch (System.FormatException) {
- throw ExceptionBuilder.CannotConvert(defValue, type.FullName);
- }
- }
- internal void HandleAttributeColumn(XmlSchemaAttribute attrib, DataTable table, bool isBase){
- Type type = null;
- XmlSchemaAttribute attr = attrib.Name != null ? attrib : (XmlSchemaAttribute) attributes[attrib.RefName];
- XmlSchemaAnnotated typeNode = FindTypeNode(attr);
- String strType = null;
- SimpleType xsdType = null;
- if (typeNode == null) {
- strType = attr.SchemaTypeName.Name;
- if (Common.ADP.IsEmpty(strType)) {
- strType = "";
- type = typeof(string);
- }
- else {
- if (attr.SchemaTypeName.Namespace != Keywords.XSDNS) // it is UD Simple Type, can it be?
- //
- type = ParseDataType(attr.SchemaTypeName.ToString());
- else
- type = ParseDataType(attr.SchemaTypeName.Name);
- }
- }
- else if (typeNode is XmlSchemaSimpleType) {
- //
- XmlSchemaSimpleType node = typeNode as XmlSchemaSimpleType;
- xsdType = new SimpleType(node);
- if (node.QualifiedName.Name != null && node.QualifiedName.Name.Length != 0 && node.QualifiedName.Namespace != Keywords.XSDNS) { // this means UDSimpleType
- strType = node.QualifiedName.ToString(); // use qualifed name
- type = ParseDataType(node.QualifiedName.ToString());// search with QName
- }
- else {
- type = ParseDataType(xsdType.BaseType);
- strType = xsdType.Name;
- if(xsdType.Length == 1 && type == typeof(string)) {
- type = typeof(Char);
- }
- }
- }
- else if (typeNode is XmlSchemaElement) {
- strType = ((XmlSchemaElement)typeNode).SchemaTypeName.Name;
- type = ParseDataType(strType);
- }
- else {
- if (typeNode.Id == null)
- throw ExceptionBuilder.DatatypeNotDefined();
- else
- throw ExceptionBuilder.UndefinedDatatype(typeNode.Id);
- }
- DataColumn column;
- string columnName = XmlConvert.DecodeName(GetInstanceName(attr));
- bool isToAdd = true;
- if ((!isBase || FromInference) && (table.Columns.Contains(columnName, true))) {
- column = table.Columns[columnName];
- isToAdd = false;
- if (FromInference) { // for backward compatability with old inference
- // throw eception if same column is being aded with different mapping
- if (column.ColumnMapping != MappingType.Attribute)
- throw ExceptionBuilder.ColumnTypeConflict(column.ColumnName);
- // in previous inference , if we have incoming column with different NS, we think as different column and
- //while adding , since there is no NS concept for datacolumn, we used to throw exception
- // simulate the same behavior.
- if ((Common.ADP.IsEmpty(attrib.QualifiedName.Namespace) && Common.ADP.IsEmpty(column._columnUri)) || // backward compatability :SQL BU DT 310912
- (string.Compare(attrib.QualifiedName.Namespace, column.Namespace, StringComparison.Ordinal) == 0))
- {
- return; // backward compatability
- }
- column = new DataColumn(columnName, type, null, MappingType.Attribute); // this is to fix issue with Exception we used to throw for old inference engine if column
- //exists with different namespace; while adding it to columncollection
- isToAdd = true;
- }
- }
- else {
- column = new DataColumn(columnName, type, null, MappingType.Attribute);
- }
- SetProperties(column, attr.UnhandledAttributes);
- HandleColumnExpression(column, attr.UnhandledAttributes);
- SetExtProperties(column, attr.UnhandledAttributes);
- if ((column.Expression!=null)&&(column.Expression.Length!=0)) {
- ColumnExpressions.Add(column);
- }
- if (xsdType != null && xsdType.Name != null && xsdType.Name.Length > 0) {
- if (XSDSchema.GetMsdataAttribute(typeNode, Keywords.TARGETNAMESPACE)!= null) {
- column.XmlDataType = xsdType.SimpleTypeQualifiedName;
- }
- }
- else {
- column.XmlDataType = strType;
- }
- column.SimpleType = xsdType;
- column.AllowDBNull = !(attrib.Use == XmlSchemaUse.Required);
- column.Namespace = attrib.QualifiedName.Namespace;
- column.Namespace = GetStringAttribute(attrib, "targetNamespace", column.Namespace);
- if (isToAdd) {
- if (this.FromInference) { // move this setting to SetProperties
- column.AllowDBNull = true;
- column.Prefix = GetPrefix(column.Namespace);
- }
- table.Columns.Add(column);
- }
- if (attrib.Use == XmlSchemaUse.Prohibited) {
- column.ColumnMapping = MappingType.Hidden;
-
- column.AllowDBNull = GetBooleanAttribute(attr, Keywords.MSD_ALLOWDBNULL, true) ;
- String defValue = GetMsdataAttribute(attr, Keywords.MSD_DEFAULTVALUE);
- if (defValue != null)
- try {
- column.DefaultValue = column.ConvertXmlToObject(defValue);
- }
- catch (System.FormatException) {
- throw ExceptionBuilder.CannotConvert(defValue, type.FullName);
- }
- }
- // XDR [....] change
- string strDefault = (attrib.Use == XmlSchemaUse.Required) ? GetMsdataAttribute(attr, Keywords.MSD_DEFAULTVALUE) : attr.DefaultValue;
- if ((attr.Use == XmlSchemaUse.Optional) && (strDefault == null ))
- strDefault = attr.FixedValue;
-
- if (strDefault != null)
- try {
- column.DefaultValue = column.ConvertXmlToObject(strDefault);
- }
- catch (System.FormatException) {
- throw ExceptionBuilder.CannotConvert(strDefault, type.FullName);
- }
- }
- internal void HandleElementColumn(XmlSchemaElement elem, DataTable table, bool isBase){
- Type type = null;
- XmlSchemaElement el = elem.Name != null ? elem : (XmlSchemaElement) elementsTable[elem.RefName];
- if (el == null) // it's possible due to some XSD compiler optimizations
- return; // do nothing
-
- XmlSchemaAnnotated typeNode = FindTypeNode(el);
- String strType = null;
- SimpleType xsdType = null;
- if (typeNode == null) {
- strType = el.SchemaTypeName.Name;
- if (Common.ADP.IsEmpty(strType)) {
- strType = "";
- type = typeof(string);
- }
- else {
- type = ParseDataType(el.SchemaTypeName.Name);
- }
- }
- else if (typeNode is XmlSchemaSimpleType) {
- //
- XmlSchemaSimpleType simpleTypeNode = typeNode as XmlSchemaSimpleType;
- xsdType = new SimpleType(simpleTypeNode);
- // ((XmlSchemaSimpleType)typeNode).Name != null && ((XmlSchemaSimpleType)typeNode).Name.Length != 0 check is for annonymos simple type,
- // it should be user defined Named simple type
- if (((XmlSchemaSimpleType)typeNode).Name != null && ((XmlSchemaSimpleType)typeNode).Name.Length != 0 && ((XmlSchemaSimpleType)typeNode).QualifiedName.Namespace != Keywords.XSDNS) {
- string targetNamespace = XSDSchema.GetMsdataAttribute(typeNode, Keywords.TARGETNAMESPACE);
- strType = ((XmlSchemaSimpleType)typeNode).QualifiedName.ToString(); // use qualifed name
- type = ParseDataType(strType);
- }
- else {
- simpleTypeNode = (xsdType.XmlBaseType!= null && xsdType.XmlBaseType.Namespace != Keywords.XSDNS) ?
- schemaTypes[xsdType.XmlBaseType] as XmlSchemaSimpleType :
- null;
- while (simpleTypeNode != null) {
- xsdType.LoadTypeValues(simpleTypeNode);
- simpleTypeNode = (xsdType.XmlBaseType!= null && xsdType.XmlBaseType.Namespace != Keywords.XSDNS) ?
- schemaTypes[xsdType.XmlBaseType] as XmlSchemaSimpleType :
- null;
- }
- type = ParseDataType(xsdType.BaseType);
- strType = xsdType.Name;
- if(xsdType.Length == 1 && type == typeof(string)) {
- type = typeof(Char);
- }
- }
- }
- else if (typeNode is XmlSchemaElement) { // theoratically no named simpletype should come here
- strType = ((XmlSchemaElement)typeNode).SchemaTypeName.Name;
- type = ParseDataType(strType);
- }
- else if (typeNode is XmlSchemaComplexType) {
- if (ADP.IsEmpty(XSDSchema.GetMsdataAttribute(elem, Keywords.MSD_DATATYPE))) {
- throw ExceptionBuilder.DatatypeNotDefined();
- }
- else {
- type = typeof(object);
- }
- }
- else {
- if (typeNode.Id == null)
- throw ExceptionBuilder.DatatypeNotDefined();
- else
- throw ExceptionBuilder.UndefinedDatatype(typeNode.Id);
- }
- DataColumn column;
- string columnName = XmlConvert.DecodeName(GetInstanceName(el));
- bool isToAdd = true;
- if (((!isBase) || FromInference) && (table.Columns.Contains(columnName, true))) {
- column = table.Columns[columnName];
- isToAdd = false;
-
- if (FromInference) { // for backward compatability with old inference
- if (column.ColumnMapping != MappingType.Element)
- throw ExceptionBuilder.ColumnTypeConflict(column.ColumnName);
- // in previous inference , if we have incoming column with different NS, we think as different column and
- //while adding , since there is no NS concept for datacolumn, we used to throw exception
- // simulate the same behavior.
- if ((Common.ADP.IsEmpty(elem.QualifiedName.Namespace) && Common.ADP.IsEmpty(column._columnUri)) || // backward compatability :SQL BU DT 310912
- (string.Compare(elem.QualifiedName.Namespace, column.Namespace, StringComparison.Ordinal) == 0))
- {
- return; // backward compatability
- }
- column = new DataColumn(columnName, type, null, MappingType.Element);// this is to fix issue with Exception we used to throw for old inference engine if column
- //exists with different namespace; while adding it to columncollection
- isToAdd = true;
- }
- }
- else {
- column = new DataColumn(columnName, type, null, MappingType.Element);
- }
- SetProperties(column, el.UnhandledAttributes);
- HandleColumnExpression(column, el.UnhandledAttributes);
- SetExtProperties(column, el.UnhandledAttributes);
- if (!Common.ADP.IsEmpty(column.Expression)) {
- ColumnExpressions.Add(column);
- }
- // Update XSD type to point to simple types actual namespace instead of normalized default namespace in case of remoting
- if (xsdType != null && xsdType.Name != null && xsdType.Name.Length > 0) {
- if (XSDSchema.GetMsdataAttribute(typeNode, Keywords.TARGETNAMESPACE)!= null) {
- column.XmlDataType = xsdType.SimpleTypeQualifiedName;
- }
- }
- else {
- column.XmlDataType = strType;
- }
- column.SimpleType = xsdType;
- column.AllowDBNull = this.FromInference ||(elem.MinOccurs == 0 ) || elem.IsNillable;
- if (!elem.RefName.IsEmpty || elem.QualifiedName.Namespace != table.Namespace)
- { // if ref element (or in diferent NS) it is global element, so form MUST BE Qualified
- column.Namespace = elem.QualifiedName.Namespace;
- column.Namespace = GetStringAttribute(el, "targetNamespace", column.Namespace);
- }
- else { // it is local, hence check for 'form' on local element, if not specified, check for 'elemenfformdefault' on schema element
- if (elem.Form == XmlSchemaForm.Unqualified) {
- column.Namespace = String.Empty;
- }
- else if (elem.Form == XmlSchemaForm.None) {
- XmlSchemaObject e = (XmlSchemaObject)elem.Parent;
- while (e.Parent != null) {
- e = e.Parent;
- }
- if (((XmlSchema)e).ElementFormDefault == XmlSchemaForm.Unqualified) {
- column.Namespace = String.Empty;
- }
- }
- else {
- column.Namespace = elem.QualifiedName.Namespace;
- column.Namespace = GetStringAttribute(el, "targetNamespace", column.Namespace);
- }
- }
- String tmp = GetStringAttribute(elem, Keywords.MSD_ORDINAL, (-1).ToString(CultureInfo.CurrentCulture));
- int ordinal = (int)Convert.ChangeType(tmp, typeof(int), null);
- if(isToAdd) {
- if(ordinal>-1 && ordinal<table.Columns.Count)
- table.Columns.AddAt(ordinal, column);
- else
- table.Columns.Add(column);
- }
- if (column.Namespace == table.Namespace)
- column._columnUri = null; // to not raise a column change namespace again
- if(this.FromInference) {// search for prefix after adding to table, so NS has its final value, and
- column.Prefix = GetPrefix(column.Namespace); // it can inherit its NS from DataTable, if it is null
- }
- string strDefault = el.DefaultValue;
- if (strDefault != null )
- try {
- column.DefaultValue = column.ConvertXmlToObject(strDefault);
- }
- catch (System.FormatException) {
- throw ExceptionBuilder.CannotConvert(strDefault, type.FullName);
- }
- }
- internal void HandleDataSet(XmlSchemaElement node, bool isNewDataSet) {
- string dsName = node.Name;
- string dsNamespace = node.QualifiedName.Namespace;
- int initialTableCount = _ds.Tables.Count; // just use for inference backward compatablity
- List<DataTable> tableSequenceList = new List<DataTable>();
- String value = GetMsdataAttribute(node, Keywords.MSD_LOCALE);
- if (null != value) { // set by user
- if (0 != value.Length) {
- // <... msdata:Locale="en-US"/>
- _ds.Locale = new CultureInfo(value);
- }
- else {
- // everett bug behavior before <... msdata:Locale=""/> becoming CultureInfo(0x409)
- _ds.Locale = CultureInfo.InvariantCulture;
- }
- }
- else { // not set by user
- // MSD_LOCALE overrides MSD_USECURRENTLOCALE
- if (GetBooleanAttribute(node, Keywords.MSD_USECURRENTLOCALE, false)) {
- _ds.SetLocaleValue(CultureInfo.CurrentCulture, false);
- }
- else {
- // everett behavior before <... msdata:UseCurrentLocale="true"/>
- _ds.SetLocaleValue(new CultureInfo(0x409), false);
- }
- }
-
- // reuse variable
- value = GetMsdataAttribute(node, Keywords.MSD_DATASETNAME);
- if (value!=null && value.Length != 0) {
- dsName = value;
- }
- value = GetMsdataAttribute(node, Keywords.MSD_DATASETNAMESPACE);
- if (value!=null && value.Length != 0) {
- dsNamespace = value;
- }
- SetProperties(_ds, node.UnhandledAttributes);
- SetExtProperties(_ds, node.UnhandledAttributes);
- if (dsName != null && dsName.Length != 0)
- _ds.DataSetName = XmlConvert.DecodeName(dsName);
- // _ds.Namespace = node.QualifiedName.Namespace;
- _ds.Namespace = dsNamespace;
- if (this.FromInference)
- _ds.Prefix = GetPrefix(_ds.Namespace);
- XmlSchemaComplexType ct = (XmlSchemaComplexType) FindTypeNode(node);
- if (ct.Particle != null) {
- XmlSchemaObjectCollection items = GetParticleItems(ct.Particle);
-
- if (items == null) {
- return;
- }
- foreach (XmlSchemaAnnotated el in items){
- if (el is XmlSchemaElement) {
- if(((XmlSchemaElement)el).RefName.Name.Length != 0) {
- if (!FromInference) {
- continue;
- }
- else {
- DataTable tempTable = _ds.Tables.GetTable(XmlConvert.DecodeName(GetInstanceName((XmlSchemaElement)el)), node.QualifiedName.Namespace);
- if (tempTable != null) {
- tableSequenceList.Add(tempTable); // if ref table is created, add it
- }
- bool isComplexTypeOrValidElementType = false;
- if (node.ElementSchemaType != null || !(((XmlSchemaElement)el).SchemaType is XmlSchemaComplexType)) {
- isComplexTypeOrValidElementType = true;
- }
- // bool isComplexTypeOrValidElementType = (node.ElementType != null || !(((XmlSchemaElement)el).SchemaType is XmlSchemaComplexType));
- if ((((XmlSchemaElement)el).MaxOccurs != Decimal.One ) && (!isComplexTypeOrValidElementType)) {
- continue;
- }
- }
- }
- DataTable child = HandleTable ((XmlSchemaElement)el);
- if (child!=null) {
- child.fNestedInDataset = true;
- }
- if (FromInference) {
- tableSequenceList.Add(child);
- }
- }
- else if (el is XmlSchemaChoice){ // should we check for inference?
- XmlSchemaObjectCollection choiceItems = ((XmlSchemaChoice)el).Items;
- if (choiceItems == null)
- continue;
- foreach (XmlSchemaAnnotated choiceEl in choiceItems) {
- if (choiceEl is XmlSchemaElement) {
- if (((XmlSchemaParticle)el).MaxOccurs > Decimal.One && (((XmlSchemaElement)choiceEl).SchemaType is XmlSchemaComplexType)) // amir
- ((XmlSchemaElement)choiceEl).MaxOccurs = ((XmlSchemaParticle)el).MaxOccurs;
- if ((((XmlSchemaElement)choiceEl).RefName.Name.Length != 0) && (!FromInference && ((XmlSchemaElement)choiceEl).MaxOccurs != Decimal.One && !(((XmlSchemaElement)choiceEl).SchemaType is XmlSchemaComplexType)))
- continue;
- DataTable child = HandleTable ((XmlSchemaElement)choiceEl);
- if (FromInference) {
- tableSequenceList.Add(child);
- }
- if (child != null)
- {
- child.fNestedInDataset = true;
- }
- }
- }
- }
- }
- }
- // Handle the non-nested keyref constraints
- if (node.Constraints != null) {
- foreach (XmlSchemaIdentityConstraint key in node.Constraints) {
- XmlSchemaKeyref keyref = key as XmlSchemaKeyref;
- if (keyref == null)
- continue;
- bool isNested = GetBooleanAttribute(keyref, Keywords.MSD_ISNESTED, /*default:*/ false);
- if (isNested)
- continue;
- HandleKeyref(keyref);
- }
- }
- if (FromInference && isNewDataSet) {
- List<DataTable> _tableList = new List<DataTable>(_ds.Tables.Count);
- foreach(DataTable dt in tableSequenceList) {
- AddTablesToList(_tableList, dt);
- }
- _ds.Tables.ReplaceFromInference(_tableList); // replace the list with the one in correct order: BackWard compatability for inference
- }
- }
- private void AddTablesToList(List<DataTable> tableList, DataTable dt) { // kind of depth _first travarsal
- if (!tableList.Contains(dt)) {
- tableList.Add(dt);
- foreach(DataTable childTable in tableDictionary[dt]) {
- AddTablesToList(tableList, childTable);
- }
- }
- }
-
- private string GetPrefix(string ns) {
- if (ns == null)
- return null;
- foreach (XmlSchema schemaRoot in _schemaSet.Schemas()) {
- XmlQualifiedName[] qualifiedNames = schemaRoot.Namespaces.ToArray();
- for(int i = 0; i < qualifiedNames.Length; i++) {
- if (qualifiedNames[i].Namespace == ns)
- return qualifiedNames[i].Name;
- }
- }
- return null;
- }
- private string GetNamespaceFromPrefix(string prefix) {
- if ((prefix == null) ||(prefix.Length == 0))
- return null;
- foreach (XmlSchema schemaRoot in _schemaSet.Schemas()) {
- XmlQualifiedName[] qualifiedNames = schemaRoot.Namespaces.ToArray();
- for(int i = 0; i < qualifiedNames.Length; i++) {
- if (qualifiedNames[i].Name == prefix)
- return qualifiedNames[i].Namespace;
- }
- }
- return null;
- }
- private String GetTableNamespace(XmlSchemaIdentityConstraint key) {
- string xpath = key.Selector.XPath;
- string [] split = xpath.Split('/');
- string prefix =string.Empty;
-
- string QualifiedTableName = split[split.Length - 1]; //get the last string after '/' and ':'
-
- if ((QualifiedTableName == null) || (QualifiedTableName.Length == 0))
- throw ExceptionBuilder.InvalidSelector(xpath);
- if (QualifiedTableName.IndexOf(':') != -1)
- prefix = QualifiedTableName.Substring(0, QualifiedTableName.IndexOf(':'));
- else
- return GetMsdataAttribute(key, Keywords.MSD_TABLENS);
- prefix = XmlConvert.DecodeName(prefix );
- return GetNamespaceFromPrefix(prefix);
- }
-
- private String GetTableName(XmlSchemaIdentityConstraint key) {
- string xpath = key.Selector.XPath;
- string [] split = xpath.Split('/',':');
- String tableName = split[split.Length - 1]; //get the last string after '/' and ':'
-
- if ((tableName == null) || (tableName.Length == 0))
- throw ExceptionBuilder.InvalidSelector(xpath);
-
- tableName = XmlConvert.DecodeName(tableName);
- return tableName;
- }
-
- internal bool IsTable(XmlSchemaElement node) {
- if (node.MaxOccurs == decimal.Zero)
- return false;
- XmlAttribute[] attribs = node.UnhandledAttributes;
- if (attribs != null) {
- for(int i = 0; i < attribs.Length; i++) {
- XmlAttribute attrib = attribs[i];
- if (attrib.LocalName == Keywords.MSD_DATATYPE &&
- attrib.Prefix == Keywords.MSD &&
- attrib.NamespaceURI == Keywords.MSDNS)
- return false;
- }
- }
- Object typeNode = FindTypeNode(node);
- if ( (node.MaxOccurs > decimal.One) && typeNode == null ){
- return true;
- }
-
- if ((typeNode==null) || !(typeNode is XmlSchemaComplexType)) {
- return false;
- }
- XmlSchemaComplexType ctNode = (XmlSchemaComplexType) typeNode;
- if (ctNode.IsAbstract)
- throw ExceptionBuilder.CannotInstantiateAbstract(node.Name);
- return true;
- }
- // internal bool IsTopLevelElement (XmlSchemaElement node) {
- // return (elements.IndexOf(node) != -1);
- // }
- internal DataTable HandleTable(XmlSchemaElement node) {
-
- if (!IsTable(node))
- return null;
-
- Object typeNode = FindTypeNode(node);
- if ( (node.MaxOccurs > decimal.One) && typeNode == null ){
- return InstantiateSimpleTable(node);
- }
-
- DataTable table = InstantiateTable(node,(XmlSchemaComplexType)typeNode, (node.RefName != null)); // this is wrong , correct check should be node.RefName.IsEmpty
- table.fNestedInDataset = false;
- return table;
- }
- }
- internal sealed class XmlIgnoreNamespaceReader : XmlNodeReader {
- private List<string> namespacesToIgnore;
- //
- // Constructor
- //
- internal XmlIgnoreNamespaceReader(XmlDocument xdoc, string[] namespacesToIgnore) : base(xdoc) {
- this.namespacesToIgnore = new List<string>(namespacesToIgnore);
- }
- //
- // XmlReader implementation
- //
- public override bool MoveToFirstAttribute() {
- if (base.MoveToFirstAttribute()) {
- if ( namespacesToIgnore.Contains(this.NamespaceURI) ||
- (this.NamespaceURI == Keywords.XML_XMLNS && this.LocalName != "lang")) { //try next one
- return MoveToNextAttribute();
- }
- else {
- return true;
- }
- }
- return false;
- }
- public override bool MoveToNextAttribute() {
- bool moved, flag;
- do {
- moved = false;
- flag = false;
- if (base.MoveToNextAttribute()) {
- moved = true;
- if ( namespacesToIgnore.Contains(this.NamespaceURI) ||
- (this.NamespaceURI == Keywords.XML_XMLNS && this.LocalName != "lang")) {
- flag = true;
- }
- }
- } while(flag);
- return moved;
- }
- }
- }
|