2 // XmlSchemaInference.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C)2004 Novell Inc.
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.Collections;
36 using System.Xml.Schema;
38 using QName = System.Xml.XmlQualifiedName;
39 using Form = System.Xml.Schema.XmlSchemaForm;
40 using Use = System.Xml.Schema.XmlSchemaUse;
41 using SOMList = System.Xml.Schema.XmlSchemaObjectCollection;
42 using SOMObject = System.Xml.Schema.XmlSchemaObject;
43 using Element = System.Xml.Schema.XmlSchemaElement;
44 using Attr = System.Xml.Schema.XmlSchemaAttribute;
45 using AttrGroup = System.Xml.Schema.XmlSchemaAttributeGroup;
46 using AttrGroupRef = System.Xml.Schema.XmlSchemaAttributeGroupRef;
47 using SimpleType = System.Xml.Schema.XmlSchemaSimpleType;
48 using ComplexType = System.Xml.Schema.XmlSchemaComplexType;
49 using SimpleModel = System.Xml.Schema.XmlSchemaSimpleContent;
50 using SimpleExt = System.Xml.Schema.XmlSchemaSimpleContentExtension;
51 using SimpleRst = System.Xml.Schema.XmlSchemaSimpleContentRestriction;
52 using ComplexModel = System.Xml.Schema.XmlSchemaComplexContent;
53 using ComplexExt = System.Xml.Schema.XmlSchemaComplexContentExtension;
54 using ComplexRst = System.Xml.Schema.XmlSchemaComplexContentRestriction;
55 using SimpleTypeRst = System.Xml.Schema.XmlSchemaSimpleTypeRestriction;
56 using SimpleList = System.Xml.Schema.XmlSchemaSimpleTypeList;
57 using SimpleUnion = System.Xml.Schema.XmlSchemaSimpleTypeUnion;
58 using SchemaFacet = System.Xml.Schema.XmlSchemaFacet;
59 using LengthFacet = System.Xml.Schema.XmlSchemaLengthFacet;
60 using MinLengthFacet = System.Xml.Schema.XmlSchemaMinLengthFacet;
61 using Particle = System.Xml.Schema.XmlSchemaParticle;
62 using Sequence = System.Xml.Schema.XmlSchemaSequence;
63 using Choice = System.Xml.Schema.XmlSchemaChoice;
66 namespace System.Xml.Schema
68 [MonoTODO ("merge primitive types; infer gYearMonth too; in some cases sequence should contain element whose minOccurs=0 (no obvious rules right now); reject some non-supported schema components")]
69 public class XmlSchemaInference
71 public enum InferenceOption {
76 InferenceOption occurrence = InferenceOption.Restricted;
77 InferenceOption typeInference = InferenceOption.Restricted;
79 public XmlSchemaInference ()
83 public InferenceOption Occurrence {
84 get { return occurrence; }
85 set { occurrence = value; }
88 public InferenceOption TypeInference {
89 get { return TypeInference; }
90 set { typeInference = value; }
93 public XmlSchemaSet InferSchema (XmlReader xmlReader)
95 return InferSchema (xmlReader, new XmlSchemaSet ());
98 public XmlSchemaSet InferSchema (XmlReader xmlReader,
101 return XsdInference.Process (xmlReader, schemas,
102 occurrence == InferenceOption.Relaxed,
103 typeInference == InferenceOption.Relaxed);
109 public static XmlSchemaSet Process (XmlReader xmlReader,
110 XmlSchemaSet schemas,
112 bool laxTypeInference)
114 XsdInference impl = new XsdInference (xmlReader,
115 schemas, laxOccurence, laxTypeInference);
120 public const string NamespaceXml =
121 "http://www.w3.org/XML/1998/namespace";
123 public const string NamespaceXmlns =
124 "http://www.w3.org/2000/xmlns/";
126 public const string XdtNamespace =
127 "http://www.w3.org/2003/11/xpath-datatypes";
129 static readonly QName QNameString = new QName (
130 "string", XmlSchema.Namespace);
132 static readonly QName QNameBoolean = new QName (
133 "boolean", XmlSchema.Namespace);
135 static readonly QName QNameAnyType = new QName (
136 "anyType", XmlSchema.Namespace);
138 static readonly QName QNameByte = new QName (
139 "byte", XmlSchema.Namespace);
141 static readonly QName QNameUByte = new QName (
142 "unsignedByte", XmlSchema.Namespace);
144 static readonly QName QNameShort = new QName (
145 "short", XmlSchema.Namespace);
147 static readonly QName QNameUShort = new QName (
148 "unsignedShort", XmlSchema.Namespace);
150 static readonly QName QNameInt = new QName (
151 "int", XmlSchema.Namespace);
153 static readonly QName QNameUInt = new QName (
154 "unsignedInt", XmlSchema.Namespace);
156 static readonly QName QNameLong = new QName (
157 "long", XmlSchema.Namespace);
159 static readonly QName QNameULong = new QName (
160 "unsignedLong", XmlSchema.Namespace);
162 static readonly QName QNameDecimal = new QName (
163 "decimal", XmlSchema.Namespace);
165 static readonly QName QNameUDecimal = new QName (
166 "unsignedDecimal", XmlSchema.Namespace);
168 static readonly QName QNameDouble = new QName (
169 "double", XmlSchema.Namespace);
171 static readonly QName QNameFloat = new QName (
172 "float", XmlSchema.Namespace);
174 static readonly QName QNameDateTime = new QName (
175 "dateTime", XmlSchema.Namespace);
177 static readonly QName QNameDuration = new QName (
178 "duration", XmlSchema.Namespace);
181 XmlSchemaSet schemas;
183 bool laxTypeInference;
185 Hashtable newElements = new Hashtable ();
186 Hashtable newAttributes = new Hashtable ();
188 private XsdInference (XmlReader xmlReader,
189 XmlSchemaSet schemas,
191 bool laxTypeInference)
193 this.source = xmlReader;
194 this.schemas = schemas;
195 this.laxOccurence = laxOccurence;
196 this.laxTypeInference = laxTypeInference;
201 // XmlSchemaSet need to be compiled.
204 // move to top-level element
205 source.MoveToContent ();
206 int depth = source.Depth;
207 if (source.NodeType != XmlNodeType.Element)
208 throw new ArgumentException ("Argument XmlReader content is expected to be an element.");
210 QName qname = new QName (source.LocalName,
211 source.NamespaceURI);
212 Element el = GetGlobalElement (qname);
214 el = CreateGlobalElement (qname);
215 InferElement (el, qname.Namespace, true);
218 InferElement (el, qname.Namespace, false);
221 private void IncludeXmlAttributes ()
223 if (schemas.Schemas (NamespaceXml).Count == 0)
224 // FIXME: do it from resources.
225 schemas.Add (NamespaceXml,
226 "http://www.w3.org/2001/xml.xsd");
229 private void InferElement (Element el, string ns, bool isNew)
231 // Quick check for reference to another definition
232 // (i.e. element ref='...' that should be redirected)
233 if (el.RefName != QName.Empty) {
234 Element body = GetGlobalElement (el.RefName);
236 body = CreateElement (el.RefName);
237 InferElement (body, ns, true);
240 InferElement (body, ns, isNew);
245 if (source.MoveToFirstAttribute ()) {
246 InferAttributes (el, ns, isNew);
247 source.MoveToElement ();
251 if (source.IsEmptyElement) {
252 InferAsEmptyElement (el, ns, isNew);
254 source.MoveToContent ();
257 InferContent (el, ns, isNew);
258 source.ReadEndElement ();
260 if (el.SchemaType == null &&
261 el.SchemaTypeName == QName.Empty)
262 el.SchemaTypeName = QNameString;
265 #region Attribute Inference
267 private Hashtable CollectAttrTable (SOMList attList)
269 // get attribute definition table.
270 Hashtable table = new Hashtable ();
271 foreach (XmlSchemaObject obj in attList) {
272 Attr attr = obj as Attr;
274 throw Error (obj, String.Format ("Attribute inference only supports direct attribute definition. {0} is not supported.", obj.GetType ()));
275 if (attr.RefName != QName.Empty)
276 table.Add (attr.RefName, attr);
278 table.Add (new QName (attr.Name, ""),
284 private void InferAttributes (Element el, string ns, bool isNew)
286 // Now this element is going to have complexType.
287 // It currently not, then we have to replace it.
288 ComplexType ct = null;
289 SOMList attList = null;
290 Hashtable table = null;
293 switch (source.NamespaceURI) {
295 if (schemas.Schemas (
296 NamespaceXml) .Count == 0)
297 IncludeXmlAttributes ();
299 case XmlSchema.InstanceNamespace:
300 if (source.LocalName == "nil")
301 el.IsNillable = true;
302 // all other xsi:* atts are ignored
308 ct = ToComplexType (el);
309 attList = GetAttributes (ct);
310 table = CollectAttrTable (attList);
312 QName attrName = new QName (
313 source.LocalName, source.NamespaceURI);
314 Attr attr = table [attrName] as Attr;
316 attList.Add (InferNewAttribute (
319 table.Remove (attrName);
320 if (attr.RefName != null &&
321 attr.RefName != QName.Empty)
322 continue; // just a reference
323 InferMergedAttribute (attr);
325 } while (source.MoveToNextAttribute ());
327 // mark all attr definitions that did not appear
330 foreach (Attr attr in table.Values)
331 attr.Use = Use.Optional;
334 private XmlSchemaAttribute InferNewAttribute (
335 QName attrName, bool isNewTypeDefinition)
338 bool mergedRequired = false;
339 if (attrName.Namespace.Length > 0) {
340 // global attribute; might be already defined.
341 attr = GetGlobalAttribute (attrName) as Attr;
343 attr = CreateGlobalAttribute (attrName);
344 attr.SchemaTypeName =
345 InferSimpleType (source.Value);
347 InferMergedAttribute (attr);
349 attr.Use == Use.Required;
352 attr.RefName = attrName;
356 attr.Name = attrName.Name;
357 attr.SchemaTypeName =
358 InferSimpleType (source.Value);
361 (isNewTypeDefinition || mergedRequired))
362 attr.Use = Use.Required;
364 attr.Use = Use.Optional;
369 // validate string value agains attr and
370 // if invalid, then relax the type.
371 private void InferMergedAttribute (Attr attr)
373 attr.SchemaTypeName = InferMergedType (source.Value,
374 attr.SchemaTypeName);
375 attr.SchemaType = null;
378 private QName InferMergedType (string value, QName typeName)
380 // examine value against specified type and
381 // if unacceptable, then return a relaxed type.
383 SimpleType st = XmlSchemaType.GetBuiltInSimpleType (
385 if (st == null) // non-primitive type => see above.
389 st.Datatype.ParseValue (value,
391 source as IXmlNamespaceResolver);
394 st = st.BaseXmlSchemaType as XmlSchemaSimpleType;
395 typeName = st != null ? st.QualifiedName : QName.Empty;
397 } while (typeName != QName.Empty);
401 private SOMList GetAttributes (ComplexType ct)
403 if (ct.ContentModel == null)
404 return ct.Attributes;
406 SimpleModel sc = ct.ContentModel as SimpleModel;
408 SimpleExt sce = sc.Content as SimpleExt;
410 return sce.Attributes;
411 SimpleRst scr = sc.Content as SimpleRst;
413 return scr.Attributes;
415 throw Error (sc, "Invalid simple content model.");
417 ComplexModel cc = ct.ContentModel as ComplexModel;
419 ComplexExt cce = cc.Content as ComplexExt;
421 return cce.Attributes;
422 ComplexRst ccr = cc.Content as ComplexRst;
424 return ccr.Attributes;
426 throw Error (cc, "Invalid simple content model.");
428 throw Error (cc, "Invalid complexType. Should not happen.");
431 private ComplexType ToComplexType (Element el)
433 QName name = el.SchemaTypeName;
434 XmlSchemaType type = el.SchemaType;
436 // 1. element type is complex.
437 ComplexType ct = type as ComplexType;
441 // 2. reference to global complexType.
442 XmlSchemaType globalType = schemas.GlobalTypes [name]
444 ct = globalType as ComplexType;
448 ct = new ComplexType ();
450 el.SchemaTypeName = QName.Empty;
452 // 3. base type name is xs:anyType or no specification.
453 // <xs:complexType />
454 if (name == QNameAnyType)
456 else if (type == null && name == QName.Empty)
459 SimpleModel sc = new SimpleModel ();
460 ct.ContentModel = sc;
462 // 4. type is simpleType
463 // -> extension of existing simple type.
464 SimpleType st = type as SimpleType;
466 SimpleRst scr = new SimpleRst ();
472 SimpleExt sce = new SimpleExt ();
475 // 5. type name points to primitive type
476 // -> simple extension of a primitive type
477 st = XmlSchemaType.GetBuiltInSimpleType (name);
479 sce.BaseTypeName = name;
483 // 6. type name points to global simpleType.
484 st = globalType as SimpleType;
486 sce.BaseTypeName = name;
490 throw Error (el, "Unexpected schema component that contains simpleTypeName that could not be resolved.");
497 private void InferAsEmptyElement (Element el, string ns,
500 ComplexType ct = el.SchemaType as ComplexType;
503 ct.ContentModel as SimpleModel;
505 ToEmptiableSimpleContent (sm, isNew);
509 ComplexModel cm = ct.ContentModel
512 ToEmptiableComplexContent (cm, isNew);
516 if (ct.Particle != null)
517 ct.Particle.MinOccurs = 0;
520 SimpleType st = el.SchemaType as SimpleType;
522 st = MakeBaseTypeAsEmptiable (st);
523 switch (st.QualifiedName.Namespace) {
524 case XmlSchema.Namespace:
526 el.SchemaTypeName = st.QualifiedName;
535 private SimpleType MakeBaseTypeAsEmptiable (SimpleType st)
537 switch (st.QualifiedName.Namespace) {
538 case XmlSchema.Namespace:
540 // If a primitive type
541 return XmlSchemaType.GetBuiltInSimpleType (
544 SimpleTypeRst str = st.Content as SimpleTypeRst;
547 foreach (SchemaFacet f in str.Facets) {
548 if (f is LengthFacet ||
549 f is MinLengthFacet) {
551 al = new ArrayList ();
555 foreach (SchemaFacet f in al)
556 str.Facets.Remove (f);
557 if (str.BaseType != null)
559 MakeBaseTypeAsEmptiable (st);
561 // It might have a reference to an
562 // external simple type, but there is
563 // no assurance that any of those
564 // external types allow an empty
565 // string. So just set base type as
567 str.BaseTypeName = QNameString;
568 } // union/list can have empty string value.
573 private void ToEmptiableSimpleContent (
574 SimpleModel sm, bool isNew)
576 SimpleExt se = sm.Content as SimpleExt;
578 se.BaseTypeName = QNameString;
580 SimpleRst sr = sm.Content
583 throw Error (sm, "Invalid simple content model was passed.");
584 sr.BaseTypeName = QNameString;
589 private void ToEmptiableComplexContent (
590 ComplexModel cm, bool isNew)
592 ComplexExt ce = cm.Content
595 if (ce.Particle != null)
596 ce.Particle.MinOccurs = 0;
597 else if (ce.BaseTypeName != null &&
598 ce.BaseTypeName != QName.Empty &&
599 ce.BaseTypeName != QNameAnyType)
600 throw Error (ce, "Complex type content extension has a reference to an external component that is not supported.");
603 ComplexRst cr = cm.Content
606 throw Error (cm, "Invalid complex content model was passed.");
607 if (cr.Particle != null)
608 cr.Particle.MinOccurs = 0;
609 else if (cr.BaseTypeName != null &&
610 cr.BaseTypeName != QName.Empty &&
611 cr.BaseTypeName != QNameAnyType)
612 throw Error (cr, "Complex type content extension has a reference to an external component that is not supported.");
616 private void InferContent (Element el, string ns, bool isNew)
619 source.MoveToContent ();
620 switch (source.NodeType) {
621 case XmlNodeType.EndElement:
622 InferAsEmptyElement (el, ns, isNew);
624 case XmlNodeType.Element:
625 InferComplexContent (el, ns, isNew);
627 case XmlNodeType.Text:
628 case XmlNodeType.CDATA:
629 case XmlNodeType.SignificantWhitespace:
630 InferTextContent (el, isNew);
631 source.MoveToContent ();
632 if (source.NodeType == XmlNodeType.Element)
633 goto case XmlNodeType.Element;
635 case XmlNodeType.Whitespace:
636 InferContent (el, ns, isNew); // skip and retry
641 private void InferComplexContent (Element el, string ns,
644 ComplexType ct = ToComplexType (el);
645 ToComplexContentType (ct);
648 bool consumed = false;
651 switch (source.NodeType) {
652 case XmlNodeType.Element:
653 Sequence s = PopulateSequence (ct);
654 Choice c = s.Items.Count > 0 ?
655 s.Items [0] as Choice :
660 ProcessSequence (ct, s, ns,
664 source.MoveToContent ();
666 case XmlNodeType.Text:
667 case XmlNodeType.CDATA:
668 case XmlNodeType.SignificantWhitespace:
670 source.ReadString ();
671 source.MoveToContent ();
673 case XmlNodeType.EndElement:
675 case XmlNodeType.None:
676 throw new NotImplementedException ("Internal Error: Should not happen.");
681 private void InferTextContent (Element el, bool isNew)
683 string value = source.ReadString ();
684 if (el.SchemaType == null) {
685 if (el.SchemaTypeName == QName.Empty) {
686 // no type information -> infer type
696 switch (el.SchemaTypeName.Namespace) {
697 case XmlSchema.Namespace:
699 // existing primitive type
700 el.SchemaTypeName = InferMergedType (
701 value, el.SchemaTypeName);
704 ComplexType ct = schemas.GlobalTypes [
707 // If it is complex, then just set
708 // mixed='true' (type cannot be set.)
709 // If it is simple, then we cannot
710 // make sure that string value is
711 // valid. So just set as xs:string.
715 el.SchemaTypeName = QNameString;
721 SimpleType st = el.SchemaType as SimpleType;
723 // If simple, then (described above)
724 el.SchemaType = null;
725 el.SchemaTypeName = QNameString;
730 ComplexType ect = el.SchemaType as ComplexType;
732 SimpleModel sm = ect.ContentModel as SimpleModel;
740 SimpleExt se = sm.Content as SimpleExt;
742 se.BaseTypeName = InferMergedType (value,
744 SimpleRst sr = sm.Content as SimpleRst;
746 sr.BaseTypeName = InferMergedType (value,
752 private void MarkAsMixed (ComplexType ct)
754 ComplexModel cm = ct.ContentModel as ComplexModel;
765 private void ProcessLax (Choice c, string ns)
767 foreach (Particle p in c.Items) {
768 Element el = p as Element;
770 throw Error (c, String.Format ("Target schema item contains unacceptable particle {0}. Only element is allowed here."));
771 if (ElementMatches (el, ns)) {
772 InferElement (el, ns, false);
776 // append a new element particle to lax term.
777 Element nel = new Element ();
778 if (source.NamespaceURI == ns)
779 nel.Name = source.LocalName;
781 nel.RefName = new QName (source.LocalName,
782 source.NamespaceURI);
783 InferElement (nel, source.NamespaceURI, true);
787 private bool ElementMatches (Element el, string ns)
789 bool matches = false;
790 if (el.RefName != QName.Empty) {
791 if (el.RefName.Name == source.LocalName &&
792 el.RefName.Namespace ==
796 else if (el.Name == source.LocalName &&
797 ns == source.NamespaceURI)
802 private void ProcessSequence (ComplexType ct, Sequence s,
803 string ns, ref int position, ref bool consumed,
806 for (int i = 0; i < position; i++) {
807 Element iel = s.Items [i] as Element;
808 if (ElementMatches (iel, ns)) {
809 // Sequence element type violation
810 // might happen (might not, but we
811 // cannot backtrack here). So switch
812 // to sequence of choice* here.
813 ProcessLax (ToSequenceOfChoice (s), ns);
818 if (s.Items.Count <= position) {
819 QName name = new QName (source.LocalName,
820 source.NamespaceURI);
821 Element nel = CreateElement (name);
822 InferElement (nel, ns, true);
823 if (ns == name.Namespace)
826 Element re = new Element ();
833 Element el = s.Items [position] as Element;
835 throw Error (s, String.Format ("Target complex type content sequence has an unacceptable type of particle {0}", s.Items [position]));
836 bool matches = ElementMatches (el, ns);
839 el.MaxOccursString = "unbounded";
840 InferElement (el, source.NamespaceURI, false);
841 source.MoveToContent ();
842 switch (source.NodeType) {
843 case XmlNodeType.None:
844 if (source.NodeType ==
846 goto case XmlNodeType.Element;
847 else if (source.NodeType ==
848 XmlNodeType.EndElement)
849 goto case XmlNodeType.EndElement;
851 case XmlNodeType.Element:
852 ProcessSequence (ct, s, ns, ref position,
853 ref consumed, isNew);
855 case XmlNodeType.Text:
856 case XmlNodeType.CDATA:
857 case XmlNodeType.SignificantWhitespace:
859 source.ReadString ();
860 goto case XmlNodeType.None;
861 case XmlNodeType.Whitespace:
862 source.ReadString ();
863 goto case XmlNodeType.None;
864 case XmlNodeType.EndElement:
875 ProcessSequence (ct, s, ns,
876 ref position, ref consumed,
880 ProcessLax (ToSequenceOfChoice (s), ns);
884 // Note that it does not return the changed sequence.
885 private Choice ToSequenceOfChoice (Sequence s)
887 Choice c = new Choice ();
890 c.MaxOccursString = "unbounded";
891 foreach (Particle p in s.Items)
898 // It makes complexType not to have Simple content model.
899 private void ToComplexContentType (ComplexType type)
901 SimpleModel sm = type.ContentModel as SimpleModel;
905 SOMList atts = GetAttributes (type);
906 foreach (SOMObject o in atts)
907 type.Attributes.Add (o);
908 // FIXME: need to copy AnyAttribute.
909 // (though not considered right now)
910 type.ContentModel = null;
914 private Sequence PopulateSequence (ComplexType ct)
916 Particle p = PopulateParticle (ct);
917 Sequence s = p as Sequence;
921 throw Error (ct, String.Format ("Target complexType contains unacceptable type of particle {0}", p));
924 private Sequence CreateSequence ()
926 Sequence s = new Sequence ();
932 private Particle PopulateParticle (ComplexType ct)
934 if (ct.ContentModel == null) {
935 if (ct.Particle == null)
936 ct.Particle = CreateSequence ();
939 ComplexModel cm = ct.ContentModel as ComplexModel;
941 ComplexExt ce = cm.Content as ComplexExt;
943 if (ce.Particle == null)
944 ce.Particle = CreateSequence ();
947 ComplexRst cr = cm.Content as ComplexRst;
949 if (cr.Particle == null)
950 cr.Particle = CreateSequence ();
954 throw Error (ct, "Schema inference internal error. The complexType should have been converted to have a complex content.");
961 // primitive type inference.
962 // When running lax type inference, it just returns xs:string.
963 private QName InferSimpleType (string value)
965 if (laxTypeInference)
969 // 0 and 1 are not infered as byte unlike MS.XSDInfer
977 long dec = XmlConvert.ToInt64 (value);
978 if (byte.MinValue <= dec && dec <= byte.MaxValue)
980 if (sbyte.MinValue <= dec && dec <= sbyte.MaxValue)
982 if (ushort.MinValue <= dec && dec <= ushort.MaxValue)
984 if (short.MinValue <= dec && dec <= short.MaxValue)
986 if (uint.MinValue <= dec && dec <= uint.MaxValue)
988 if (int.MinValue <= dec && dec <= int.MaxValue)
991 } catch (Exception) {
994 XmlConvert.ToUInt64 (value);
996 } catch (Exception) {
999 XmlConvert.ToDecimal (value);
1000 return QNameDecimal;
1001 } catch (Exception) {
1004 double dbl = XmlConvert.ToDouble (value);
1005 if (float.MinValue <= dbl &&
1006 dbl <= float.MaxValue)
1010 } catch (Exception) {
1013 // FIXME: also try DateTimeSerializationMode
1015 XmlConvert.ToDateTime (value);
1016 return QNameDateTime;
1017 } catch (Exception) {
1020 XmlConvert.ToTimeSpan (value);
1021 return QNameDuration;
1022 } catch (Exception) {
1033 private Element GetGlobalElement (QName name)
1035 Element el = newElements [name] as Element;
1037 el = schemas.GlobalElements [name] as Element;
1041 private Attr GetGlobalAttribute (QName name)
1043 Attr a = newElements [name] as Attr;
1045 a = schemas.GlobalAttributes [name] as Attr;
1049 private Element CreateElement (QName name)
1051 Element el = new Element ();
1052 el.Name = name.Name;
1056 private Element CreateGlobalElement (QName name)
1058 Element el = CreateElement (name);
1059 XmlSchema schema = PopulateSchema (name.Namespace);
1060 schema.Items.Add (el);
1061 newElements.Add (name, el);
1065 private Attr CreateGlobalAttribute (QName name)
1067 Attr attr = new Attr ();
1068 XmlSchema schema = PopulateSchema (name.Namespace);
1069 attr.Name = name.Name;
1070 schema.Items.Add (attr);
1071 newAttributes.Add (name, attr);
1075 // Note that the return value never assures that all the
1076 // components in the parameter ns must reside in it.
1077 private XmlSchema PopulateSchema (string ns)
1079 ICollection list = schemas.Schemas (ns);
1080 if (list.Count > 0) {
1081 IEnumerator e = list.GetEnumerator ();
1083 return (XmlSchema) e.Current;
1085 XmlSchema s = new XmlSchema ();
1086 if (ns != null && ns.Length > 0)
1087 s.TargetNamespace = ns;
1088 s.ElementFormDefault = Form.Qualified;
1089 s.AttributeFormDefault = Form.Unqualified;
1094 private XmlSchemaInferenceException Error (
1095 XmlSchemaObject sourceObj,
1098 // This override is mainly for schema component error.
1099 return Error (sourceObj, false, message);
1102 private XmlSchemaInferenceException Error (
1103 XmlSchemaObject sourceObj,
1107 string msg = String.Concat (
1110 String.Format (". Related schema component is {0}",
1111 sourceObj.SourceUri,
1112 sourceObj.LineNumber,
1113 sourceObj.LinePosition) :
1116 String.Format (". {0}", source.BaseURI) :
1119 IXmlLineInfo li = source as IXmlLineInfo;
1120 if (useReader && li != null)
1121 return new XmlSchemaInferenceException (
1122 msg, null, li.LineNumber,
1125 return new XmlSchemaInferenceException (msg);