1 // Author: Dwivedi, Ajay kumar
\r
4 using System.Xml.Serialization;
\r
7 namespace System.Xml.Schema
\r
10 /// Summary description for XmlSchemaSimpleType.
\r
12 public class XmlSchemaSimpleType : XmlSchemaType
\r
14 private XmlSchemaSimpleTypeContent content;
\r
16 internal bool islocal = true; // Assuming local means we have to specify islocal=false only in XmlSchema
\r
17 private static string xmlname = "simpleType";
\r
19 public XmlSchemaSimpleType()
\r
23 [XmlElement("restriction",typeof(XmlSchemaSimpleTypeRestriction),Namespace=XmlSchema.Namespace)]
\r
24 [XmlElement("list",typeof(XmlSchemaSimpleTypeList),Namespace=XmlSchema.Namespace)]
\r
25 [XmlElement("union",typeof(XmlSchemaSimpleTypeUnion),Namespace=XmlSchema.Namespace)]
\r
26 public XmlSchemaSimpleTypeContent Content
\r
28 get{ return content; }
\r
29 set{ content = value; }
\r
33 /// For a simple Type:
\r
34 /// 1. Content must be present
\r
35 /// 2. id if present, must have be a valid ID
\r
36 /// a) If the simpletype is local
\r
37 /// 1- are from <xs:complexType name="simpleType"> and <xs:complexType name="localSimpleType">
\r
38 /// 1. name is prohibited
\r
39 /// 2. final is prohibited
\r
40 /// b) If the simpletype is toplevel
\r
41 /// 1- are from <xs:complexType name="simpleType"> and <xs:complexType name="topLevelSimpleType">
\r
42 /// 1. name is required, type must be NCName
\r
43 /// 2. Content is required
\r
44 /// 3. final can have values : #all | (list | union | restriction)
\r
45 /// 4. If final is set, finalResolved is same as final (but within the values of b.3)
\r
46 /// 5. If final is not set, the finalDefault of the schema (ie. only #all and restriction)
\r
47 /// 6. Base type is:
\r
48 /// 4.1 If restriction is chosen,the base type of restriction or elements
\r
49 /// 4.2 otherwise simple ur-type
\r
52 internal int Compile(ValidationEventHandler h, XmlSchema schema)
\r
54 // If this is already compiled this time, simply skip.
\r
55 if (this.IsComplied (schema.CompilationId))
\r
60 if(this.islocal) // a
\r
62 if(this.Name != null) // a.1
\r
63 error(h,"Name is prohibited in a local simpletype");
\r
64 if(this.Final != XmlSchemaDerivationMethod.None) //a.2
\r
65 error(h,"Final is prohibited in a local simpletype");
\r
69 if(this.Name == null) //b.1
\r
70 error(h,"Name is required in top level simpletype");
\r
71 else if(!XmlSchemaUtil.CheckNCName(this.Name)) // b.1.2
\r
72 error(h,"name attribute of a simpleType must be NCName");
\r
74 this.qName = new XmlQualifiedName(this.Name,schema.TargetNamespace);
\r
76 //NOTE: Although the FinalResolved can be Empty, it is not a valid value for Final
\r
77 //DEVIATION: If an error occurs, the finaldefault is always consulted. This deviates
\r
78 // from the way MS implementation works.
\r
79 switch(this.Final) //b.3, b.4
\r
81 case XmlSchemaDerivationMethod.All:
\r
82 this.finalResolved = XmlSchemaDerivationMethod.All;
\r
84 case XmlSchemaDerivationMethod.List:
\r
85 this.finalResolved = XmlSchemaDerivationMethod.List;
\r
87 case XmlSchemaDerivationMethod.Union:
\r
88 this.finalResolved = XmlSchemaDerivationMethod.Union;
\r
90 case XmlSchemaDerivationMethod.Restriction:
\r
91 this.finalResolved = XmlSchemaDerivationMethod.Restriction;
\r
94 error(h,"The value of final attribute is not valid for simpleType");
\r
95 goto case XmlSchemaDerivationMethod.None;
\r
96 // use assignment from finaldefault on schema.
\r
97 case XmlSchemaDerivationMethod.None: // b.5
\r
98 XmlSchemaDerivationMethod flags =
\r
99 (XmlSchemaDerivationMethod.Restriction | XmlSchemaDerivationMethod.List |
\r
100 XmlSchemaDerivationMethod.Extension | XmlSchemaDerivationMethod.Union );
\r
101 if(schema.FinalDefault == XmlSchemaDerivationMethod.All)
\r
102 finalResolved = XmlSchemaDerivationMethod.All;
\r
103 else if(schema.FinalDefault == XmlSchemaDerivationMethod.None)
\r
104 finalResolved = flags;
\r
106 finalResolved = schema.FinalDefault & flags;
\r
111 XmlSchemaUtil.CompileID(Id,this,schema.IDCollection,h);
\r
113 if(this.Content == null) //a.3,b.2
\r
114 error(h,"Content is required in a simpletype");
\r
115 else if(Content is XmlSchemaSimpleTypeRestriction)
\r
117 errorCount += ((XmlSchemaSimpleTypeRestriction)Content).Compile(h,schema);
\r
119 else if(Content is XmlSchemaSimpleTypeList)
\r
121 errorCount += ((XmlSchemaSimpleTypeList)Content).Compile(h,schema);
\r
123 else if(Content is XmlSchemaSimpleTypeUnion)
\r
125 errorCount += ((XmlSchemaSimpleTypeUnion)Content).Compile(h,schema);
\r
127 this.CompilationId = schema.CompilationId;
\r
132 internal int Validate(ValidationEventHandler h, XmlSchema schema)
\r
141 // final = (#all | (list | union | restriction))
\r
144 // {any attributes with non-schema namespace . . .}>
\r
145 // Content: (annotation?, (restriction | list | union))
\r
147 internal static XmlSchemaSimpleType Read(XmlSchemaReader reader, ValidationEventHandler h)
\r
149 XmlSchemaSimpleType stype = new XmlSchemaSimpleType();
\r
150 reader.MoveToElement();
\r
152 if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
\r
154 error(h,"Should not happen :1: XmlSchemaGroup.Read, name="+reader.Name,null);
\r
159 stype.LineNumber = reader.LineNumber;
\r
160 stype.LinePosition = reader.LinePosition;
\r
161 stype.SourceUri = reader.BaseURI;
\r
163 while(reader.MoveToNextAttribute())
\r
165 if(reader.Name == "final")
\r
168 stype.Final = XmlSchemaUtil.ReadDerivationAttribute(reader, out innerex, "final");
\r
169 if(innerex != null)
\r
170 error(h, "some invalid values not a valid value for final", innerex);
\r
172 else if(reader.Name == "id")
\r
174 stype.Id = reader.Value;
\r
176 else if(reader.Name == "name")
\r
178 stype.Name = reader.Value;
\r
180 else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
\r
182 error(h,reader.Name + " is not a valid attribute for simpleType",null);
\r
186 XmlSchemaUtil.ReadUnhandledAttribute(reader,stype);
\r
190 reader.MoveToElement();
\r
191 if(reader.IsEmptyElement)
\r
194 // Content: (annotation?, (restriction | list | union))
\r
196 while(reader.ReadNextElement())
\r
198 if(reader.NodeType == XmlNodeType.EndElement)
\r
200 if(reader.LocalName != xmlname)
\r
201 error(h,"Should not happen :2: XmlSchemaSimpleType.Read, name="+reader.Name,null);
\r
204 if(level <= 1 && reader.LocalName == "annotation")
\r
206 level = 2; //Only one annotation
\r
207 XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
\r
208 if(annotation != null)
\r
209 stype.Annotation = annotation;
\r
214 if(reader.LocalName == "restriction")
\r
217 XmlSchemaSimpleTypeRestriction restriction = XmlSchemaSimpleTypeRestriction.Read(reader,h);
\r
218 if(restriction != null)
\r
219 stype.content = restriction;
\r
222 if(reader.LocalName == "list")
\r
225 XmlSchemaSimpleTypeList list = XmlSchemaSimpleTypeList.Read(reader,h);
\r
227 stype.content = list;
\r
230 if(reader.LocalName == "union")
\r
233 XmlSchemaSimpleTypeUnion union = XmlSchemaSimpleTypeUnion.Read(reader,h);
\r
235 stype.content = union;
\r
239 reader.RaiseInvalidElementError();
\r