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="http://www.w3.org/2001/XMLSchema")]
\r
24 [XmlElement("list",typeof(XmlSchemaSimpleTypeList),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
25 [XmlElement("union",typeof(XmlSchemaSimpleTypeUnion),Namespace="http://www.w3.org/2001/XMLSchema")]
\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, XmlSchemaInfo info)
\r
56 if(this.islocal) // a
\r
58 if(this.Name != null) // a.1
\r
59 error(h,"Name is prohibited in a local simpletype");
\r
60 if(this.Final != XmlSchemaDerivationMethod.None) //a.2
\r
61 error(h,"Final is prohibited in a local simpletype");
\r
65 if(this.Name == null) //b.1
\r
66 error(h,"Name is required in top level simpletype");
\r
67 else if(!XmlSchemaUtil.CheckNCName(this.Name)) // b.1.2
\r
68 error(h,"name attribute of a simpleType must be NCName");
\r
70 this.qName = new XmlQualifiedName(this.Name,info.TargetNamespace);
\r
72 //NOTE: Although the FinalResolved can be Empty, it is not a valid value for Final
\r
73 //DEVIATION: If an error occurs, the finaldefault is always consulted. This deviates
\r
74 // from the way MS implementation works.
\r
75 switch(this.Final) //b.3, b.4
\r
77 case XmlSchemaDerivationMethod.All:
\r
78 this.finalResolved = XmlSchemaDerivationMethod.All;
\r
80 case XmlSchemaDerivationMethod.List:
\r
81 this.finalResolved = XmlSchemaDerivationMethod.List;
\r
83 case XmlSchemaDerivationMethod.Union:
\r
84 this.finalResolved = XmlSchemaDerivationMethod.Union;
\r
86 case XmlSchemaDerivationMethod.Restriction:
\r
87 this.finalResolved = XmlSchemaDerivationMethod.Restriction;
\r
90 error(h,"The value of final attribute is not valid for simpleType");
\r
91 goto case XmlSchemaDerivationMethod.None;
\r
92 // use assignment from finaldefault on schema.
\r
93 case XmlSchemaDerivationMethod.None: // b.5
\r
94 if(info.FinalDefault == XmlSchemaDerivationMethod.All)
\r
95 finalResolved = XmlSchemaDerivationMethod.All;
\r
97 finalResolved = info.FinalDefault & (XmlSchemaDerivationMethod.Restriction | XmlSchemaDerivationMethod.List |
\r
98 XmlSchemaDerivationMethod.Extension | XmlSchemaDerivationMethod.Union );
\r
103 XmlSchemaUtil.CompileID(Id,this,info.IDCollection,h);
\r
105 if(this.Content == null) //a.3,b.2
\r
106 error(h,"Content is required in a simpletype");
\r
107 else if(Content is XmlSchemaSimpleTypeRestriction)
\r
109 errorCount += ((XmlSchemaSimpleTypeRestriction)Content).Compile(h,info);
\r
111 else if(Content is XmlSchemaSimpleTypeList)
\r
113 errorCount += ((XmlSchemaSimpleTypeList)Content).Compile(h,info);
\r
115 else if(Content is XmlSchemaSimpleTypeUnion)
\r
117 errorCount += ((XmlSchemaSimpleTypeUnion)Content).Compile(h,info);
\r
123 internal int Validate(ValidationEventHandler h)
\r
129 // final = (#all | (list | union | restriction))
\r
132 // {any attributes with non-schema namespace . . .}>
\r
133 // Content: (annotation?, (restriction | list | union))
\r
135 internal static XmlSchemaSimpleType Read(XmlSchemaReader reader, ValidationEventHandler h)
\r
137 XmlSchemaSimpleType stype = new XmlSchemaSimpleType();
\r
138 reader.MoveToElement();
\r
140 if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
\r
142 error(h,"Should not happen :1: XmlSchemaGroup.Read, name="+reader.Name,null);
\r
147 stype.LineNumber = reader.LineNumber;
\r
148 stype.LinePosition = reader.LinePosition;
\r
149 stype.SourceUri = reader.BaseURI;
\r
151 while(reader.MoveToNextAttribute())
\r
153 if(reader.Name == "final")
\r
156 stype.Final = XmlSchemaUtil.ReadDerivationAttribute(reader, out innerex, "final");
\r
157 if(innerex != null)
\r
158 error(h, "some invalid values not a valid value for final", innerex);
\r
160 else if(reader.Name == "id")
\r
162 stype.Id = reader.Value;
\r
164 else if(reader.Name == "name")
\r
166 stype.Name = reader.Value;
\r
168 else if(reader.NamespaceURI == "" || reader.NamespaceURI == XmlSchema.Namespace)
\r
170 error(h,reader.Name + " is not a valid attribute for simpleType",null);
\r
174 if(reader.Prefix == "xmlns")
\r
175 stype.Namespaces.Add(reader.LocalName, reader.Value);
\r
176 else if(reader.Name == "xmlns")
\r
177 stype.Namespaces.Add("",reader.Value);
\r
178 //TODO: Add to Unhandled attributes
\r
182 reader.MoveToElement();
\r
183 if(reader.IsEmptyElement)
\r
186 // Content: (annotation?, (restriction | list | union))
\r
188 while(reader.ReadNextElement())
\r
190 if(reader.NodeType == XmlNodeType.EndElement)
\r
192 if(reader.LocalName != xmlname)
\r
193 error(h,"Should not happen :2: XmlSchemaSimpleType.Read, name="+reader.Name,null);
\r
196 if(level <= 1 && reader.LocalName == "annotation")
\r
198 level = 2; //Only one annotation
\r
199 XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
\r
200 if(annotation != null)
\r
201 stype.Annotation = annotation;
\r
206 if(reader.LocalName == "restriction")
\r
209 XmlSchemaSimpleTypeRestriction restriction = XmlSchemaSimpleTypeRestriction.Read(reader,h);
\r
210 if(restriction != null)
\r
211 stype.content = restriction;
\r
214 if(reader.LocalName == "list")
\r
217 XmlSchemaSimpleTypeList list = XmlSchemaSimpleTypeList.Read(reader,h);
\r
219 stype.content = list;
\r
222 if(reader.LocalName == "union")
\r
225 XmlSchemaSimpleTypeUnion union = XmlSchemaSimpleTypeUnion.Read(reader,h);
\r
227 stype.content = union;
\r
231 reader.RaiseInvalidElementError();
\r