2003-10-01 Atsushi Enomoto <ginga@kit.hi-ho.ne.jp>
[mono.git] / mcs / class / System.XML / System.Xml.Schema / XmlSchemaSimpleContentRestriction.cs
1 // Author: Dwivedi, Ajay kumar\r
2 //            Adwiv@Yahoo.com\r
3 using System;\r
4 using System.Xml;\r
5 using System.Xml.Serialization;\r
6 \r
7 namespace System.Xml.Schema\r
8 {\r
9         /// <summary>\r
10         /// Summary description for XmlSchemaSimpleContentRestriction.\r
11         /// </summary>\r
12         public class XmlSchemaSimpleContentRestriction : XmlSchemaContent\r
13         {\r
14                 \r
15                 private XmlSchemaAnyAttribute any;\r
16                 private XmlSchemaObjectCollection attributes;\r
17                 private XmlSchemaSimpleType baseType;\r
18                 private XmlQualifiedName baseTypeName;\r
19                 private XmlSchemaObjectCollection facets;\r
20                 private static string xmlname = "restriction";\r
21 \r
22                 public XmlSchemaSimpleContentRestriction()\r
23                 {\r
24                         baseTypeName = XmlQualifiedName.Empty;\r
25                         attributes       = new XmlSchemaObjectCollection();\r
26                         facets           = new XmlSchemaObjectCollection();\r
27                 }\r
28 \r
29                 [System.Xml.Serialization.XmlAttribute("base")]\r
30                 public XmlQualifiedName BaseTypeName \r
31                 {\r
32                         get{ return  baseTypeName; }\r
33                         set{ baseTypeName = value; }\r
34                 }\r
35 \r
36                 [XmlElement("simpleType",Namespace=XmlSchema.Namespace)]\r
37                 public XmlSchemaSimpleType BaseType \r
38                 { \r
39                         get{ return  baseType; } \r
40                         set{ baseType = value; } \r
41                 }\r
42                 \r
43                 [XmlElement("minExclusive",typeof(XmlSchemaMinExclusiveFacet),Namespace=XmlSchema.Namespace)]\r
44                 [XmlElement("minInclusive",typeof(XmlSchemaMinInclusiveFacet),Namespace=XmlSchema.Namespace)] \r
45                 [XmlElement("maxExclusive",typeof(XmlSchemaMaxExclusiveFacet),Namespace=XmlSchema.Namespace)]\r
46                 [XmlElement("maxInclusive",typeof(XmlSchemaMaxInclusiveFacet),Namespace=XmlSchema.Namespace)]\r
47                 [XmlElement("totalDigits",typeof(XmlSchemaTotalDigitsFacet),Namespace=XmlSchema.Namespace)]\r
48                 [XmlElement("fractionDigits",typeof(XmlSchemaFractionDigitsFacet),Namespace=XmlSchema.Namespace)]\r
49                 [XmlElement("length",typeof(XmlSchemaLengthFacet),Namespace=XmlSchema.Namespace)]\r
50                 [XmlElement("minLength",typeof(XmlSchemaMinLengthFacet),Namespace=XmlSchema.Namespace)]\r
51                 [XmlElement("maxLength",typeof(XmlSchemaMaxLengthFacet),Namespace=XmlSchema.Namespace)]\r
52                 [XmlElement("enumeration",typeof(XmlSchemaEnumerationFacet),Namespace=XmlSchema.Namespace)]\r
53                 [XmlElement("whiteSpace",typeof(XmlSchemaWhiteSpaceFacet),Namespace=XmlSchema.Namespace)]\r
54                 [XmlElement("pattern",typeof(XmlSchemaPatternFacet),Namespace=XmlSchema.Namespace)]\r
55                 public XmlSchemaObjectCollection Facets \r
56                 { \r
57                         get{ return facets; } \r
58                 }\r
59 \r
60                 [XmlElement("attribute",typeof(XmlSchemaAttribute),Namespace=XmlSchema.Namespace)]\r
61                 [XmlElement("attributeGroup",typeof(XmlSchemaAttributeGroupRef),Namespace=XmlSchema.Namespace)]\r
62                 public XmlSchemaObjectCollection Attributes \r
63                 {\r
64                         get{ return attributes; }\r
65                 }\r
66 \r
67                 [XmlElement("anyAttribute",Namespace=XmlSchema.Namespace)]\r
68                 public XmlSchemaAnyAttribute AnyAttribute \r
69                 {\r
70                         get{ return  any; }\r
71                         set{ any = value; }\r
72                 }\r
73                 \r
74                 ///<remarks>\r
75                 /// 1. Base must be present and a QName\r
76                 ///</remarks>\r
77                 [MonoTODO]\r
78                 internal override int Compile(ValidationEventHandler h, XmlSchema schema)\r
79                 {\r
80                         // If this is already compiled this time, simply skip.\r
81                         if (this.IsComplied (schema.CompilationId))\r
82                                 return 0;\r
83 \r
84                         if (this.isRedefinedComponent) {\r
85                                 if (Annotation != null)\r
86                                         Annotation.isRedefinedComponent = true;\r
87                                 if (AnyAttribute != null)\r
88                                         AnyAttribute.isRedefinedComponent = true;\r
89                                 foreach (XmlSchemaObject obj in Attributes)\r
90                                         obj.isRedefinedComponent = true;\r
91                         }\r
92 \r
93                         if(BaseTypeName == null || BaseTypeName.IsEmpty)\r
94                         {\r
95                                 error(h, "base must be present, as a QName");\r
96                         }\r
97                         else if(!XmlSchemaUtil.CheckQName(BaseTypeName))\r
98                                 error(h,"BaseTypeName must be a QName");\r
99 \r
100                         if(BaseType != null)\r
101                         {\r
102                                 errorCount += BaseType.Compile(h,schema);\r
103                         }\r
104 \r
105                         if(this.AnyAttribute != null)\r
106                         {\r
107                                 errorCount += AnyAttribute.Compile(h,schema);\r
108                         }\r
109 \r
110                         foreach(XmlSchemaObject obj in Attributes)\r
111                         {\r
112                                 if(obj is XmlSchemaAttribute)\r
113                                 {\r
114                                         XmlSchemaAttribute attr = (XmlSchemaAttribute) obj;\r
115                                         errorCount += attr.Compile(h,schema);\r
116                                 }\r
117                                 else if(obj is XmlSchemaAttributeGroupRef)\r
118                                 {\r
119                                         XmlSchemaAttributeGroupRef atgrp = (XmlSchemaAttributeGroupRef) obj;\r
120                                         errorCount += atgrp.Compile(h,schema);\r
121                                 }\r
122                                 else\r
123                                         error(h,obj.GetType() +" is not valid in this place::SimpleContentRestriction");\r
124                         }\r
125                         \r
126                         //TODO: Compile Facets: Looks like they are a part of datatypes. So we'll do them with the datatypes\r
127 \r
128                         \r
129                         XmlSchemaUtil.CompileID(Id,this,schema.IDCollection,h);\r
130                         this.CompilationId = schema.CompilationId;\r
131                         return errorCount;\r
132                 }\r
133                 \r
134                 [MonoTODO]\r
135                 internal override int Validate(ValidationEventHandler h, XmlSchema schema)\r
136                 {\r
137                         if (IsValidated (schema.ValidationId))\r
138                                 return errorCount;\r
139 \r
140                         if (baseType != null) {\r
141                                 baseType.Validate (h, schema);\r
142                                 actualBaseSchemaType = baseType;\r
143                         }\r
144                         else if (baseTypeName != XmlQualifiedName.Empty) {\r
145                                 XmlSchemaType st = schema.SchemaTypes [baseTypeName] as XmlSchemaType;\r
146                                 if (st != null) {\r
147                                         st.Validate (h, schema);\r
148                                         actualBaseSchemaType = st;\r
149                                 } else if (baseTypeName == XmlSchemaComplexType.AnyTypeName) {\r
150                                         actualBaseSchemaType = XmlSchemaComplexType.AnyType;
151                                 } else if (baseTypeName.Namespace == XmlSchema.Namespace) {\r
152                                         actualBaseSchemaType = XmlSchemaDatatype.FromName (baseTypeName);\r
153                                         if (actualBaseSchemaType == null)\r
154                                                 error (h, "Invalid schema datatype name is specified.");\r
155                                 }\r
156                                 // otherwise, it might be missing sub components.\r
157                                 else if (!schema.missedSubComponents)// && schema.Schemas [baseTypeName.Namespace] != null)\r
158                                         error (h, "Referenced base schema type " + baseTypeName + " was not found in the corresponding schema.");\r
159                         }\r
160 \r
161                         ValidationId = schema.ValidationId;\r
162                         return errorCount;\r
163                 }\r
164 \r
165                 //<restriction \r
166                 //base = QName \r
167                 //id = ID \r
168                 //{any attributes with non-schema namespace . . .}>\r
169                 //Content: (annotation?, (simpleType?, (minExclusive | minInclusive | maxExclusive | maxInclusive | totalDigits | fractionDigits | length | minLength | maxLength | enumeration | whiteSpace | pattern)*)?, ((attribute | attributeGroup)*, anyAttribute?))\r
170                 //</restriction>\r
171                 internal static XmlSchemaSimpleContentRestriction Read(XmlSchemaReader reader, ValidationEventHandler h)\r
172                 {\r
173                         XmlSchemaSimpleContentRestriction restriction = new XmlSchemaSimpleContentRestriction();\r
174                         reader.MoveToElement();\r
175 \r
176                         if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)\r
177                         {\r
178                                 error(h,"Should not happen :1: XmlSchemaComplexContentRestriction.Read, name="+reader.Name,null);\r
179                                 reader.SkipToEnd();\r
180                                 return null;\r
181                         }\r
182 \r
183                         restriction.LineNumber = reader.LineNumber;\r
184                         restriction.LinePosition = reader.LinePosition;\r
185                         restriction.SourceUri = reader.BaseURI;\r
186 \r
187                         while(reader.MoveToNextAttribute())\r
188                         {\r
189                                 if(reader.Name == "base")\r
190                                 {\r
191                                         Exception innerex;\r
192                                         restriction.baseTypeName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);\r
193                                         if(innerex != null)\r
194                                                 error(h, reader.Value + " is not a valid value for base attribute",innerex);\r
195                                 }\r
196                                 else if(reader.Name == "id")\r
197                                 {\r
198                                         restriction.Id = reader.Value;\r
199                                 }\r
200                                 else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)\r
201                                 {\r
202                                         error(h,reader.Name + " is not a valid attribute for restriction",null);\r
203                                 }\r
204                                 else\r
205                                 {\r
206                                         XmlSchemaUtil.ReadUnhandledAttribute(reader,restriction);\r
207                                 }\r
208                         }\r
209                         \r
210                         reader.MoveToElement();\r
211                         if(reader.IsEmptyElement)\r
212                                 return restriction;\r
213                         \r
214                         //Content:  1.annotation?, \r
215                         //                  2.simpleType?, \r
216                         //                      3.(minExclusive |...| enumeration | whiteSpace | pattern)*, \r
217                         //                      4.(attribute | attributeGroup)*, \r
218                         //                      5.anyAttribute?\r
219                         int level = 1;\r
220                         while(reader.ReadNextElement())\r
221                         {\r
222                                 if(reader.NodeType == XmlNodeType.EndElement)\r
223                                 {\r
224                                         if(reader.LocalName != xmlname)\r
225                                                 error(h,"Should not happen :2: XmlSchemaSimpleContentRestriction.Read, name="+reader.Name,null);\r
226                                         break;\r
227                                 }\r
228                                 if(level <= 1 && reader.LocalName == "annotation")\r
229                                 {\r
230                                         level = 2; //Only one annotation\r
231                                         XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);\r
232                                         if(annotation != null)\r
233                                                 restriction.Annotation = annotation;\r
234                                         continue;\r
235                                 }\r
236                                 if(level <=2 && reader.LocalName == "simpleType")\r
237                                 {\r
238                                         level = 3;\r
239                                         XmlSchemaSimpleType stype = XmlSchemaSimpleType.Read(reader,h);\r
240                                         if(stype != null)\r
241                                                 restriction.baseType = stype;\r
242                                         continue;\r
243                                 }\r
244                                 if(level <= 3)\r
245                                 {\r
246                                         if(reader.LocalName == "minExclusive")\r
247                                         {\r
248                                                 level = 3;\r
249                                                 XmlSchemaMinExclusiveFacet minex = XmlSchemaMinExclusiveFacet.Read(reader,h);\r
250                                                 if(minex != null)\r
251                                                         restriction.facets.Add(minex);\r
252                                                 continue;\r
253                                         }\r
254                                         else if(reader.LocalName == "minInclusive")\r
255                                         {\r
256                                                 level = 3;\r
257                                                 XmlSchemaMinInclusiveFacet mini = XmlSchemaMinInclusiveFacet.Read(reader,h);\r
258                                                 if(mini != null)\r
259                                                         restriction.facets.Add(mini);\r
260                                                 continue;\r
261                                         }\r
262                                         else if(reader.LocalName == "maxExclusive")\r
263                                         {\r
264                                                 level = 3;\r
265                                                 XmlSchemaMaxExclusiveFacet maxex = XmlSchemaMaxExclusiveFacet.Read(reader,h);\r
266                                                 if(maxex != null)\r
267                                                         restriction.facets.Add(maxex);\r
268                                                 continue;\r
269                                         }\r
270                                         else if(reader.LocalName == "maxInclusive")\r
271                                         {\r
272                                                 level = 3;\r
273                                                 XmlSchemaMaxInclusiveFacet maxi = XmlSchemaMaxInclusiveFacet.Read(reader,h);\r
274                                                 if(maxi != null)\r
275                                                         restriction.facets.Add(maxi);\r
276                                                 continue;\r
277                                         }\r
278                                         else if(reader.LocalName == "totalDigits")\r
279                                         {\r
280                                                 level = 3;\r
281                                                 XmlSchemaTotalDigitsFacet total = XmlSchemaTotalDigitsFacet.Read(reader,h);\r
282                                                 if(total != null)\r
283                                                         restriction.facets.Add(total);\r
284                                                 continue;\r
285                                         }\r
286                                         else if(reader.LocalName == "fractionDigits")\r
287                                         {\r
288                                                 level = 3;\r
289                                                 XmlSchemaFractionDigitsFacet fraction = XmlSchemaFractionDigitsFacet.Read(reader,h);\r
290                                                 if(fraction != null)\r
291                                                         restriction.facets.Add(fraction);\r
292                                                 continue;\r
293                                         }\r
294                                         else if(reader.LocalName == "length")\r
295                                         {\r
296                                                 level = 3;\r
297                                                 XmlSchemaLengthFacet length = XmlSchemaLengthFacet.Read(reader,h);\r
298                                                 if(length != null)\r
299                                                         restriction.facets.Add(length);\r
300                                                 continue;\r
301                                         }\r
302                                         else if(reader.LocalName == "minLength")\r
303                                         {\r
304                                                 level = 3;\r
305                                                 XmlSchemaMinLengthFacet minlen = XmlSchemaMinLengthFacet.Read(reader,h);\r
306                                                 if(minlen != null)\r
307                                                         restriction.facets.Add(minlen);\r
308                                                 continue;\r
309                                         }\r
310                                         else if(reader.LocalName == "maxLength")\r
311                                         {\r
312                                                 level = 3;\r
313                                                 XmlSchemaMaxLengthFacet maxlen = XmlSchemaMaxLengthFacet.Read(reader,h);\r
314                                                 if(maxlen != null)\r
315                                                         restriction.facets.Add(maxlen);\r
316                                                 continue;\r
317                                         }\r
318                                         else if(reader.LocalName == "enumeration")\r
319                                         {\r
320                                                 level = 3;\r
321                                                 XmlSchemaEnumerationFacet enumeration = XmlSchemaEnumerationFacet.Read(reader,h);\r
322                                                 if(enumeration != null)\r
323                                                         restriction.facets.Add(enumeration);\r
324                                                 continue;\r
325                                         }\r
326                                         else if(reader.LocalName == "whiteSpace")\r
327                                         {\r
328                                                 level = 3;\r
329                                                 XmlSchemaWhiteSpaceFacet ws = XmlSchemaWhiteSpaceFacet.Read(reader,h);\r
330                                                 if(ws != null)\r
331                                                         restriction.facets.Add(ws);\r
332                                                 continue;\r
333                                         }\r
334                                         else if(reader.LocalName == "pattern")\r
335                                         {\r
336                                                 level = 3;\r
337                                                 XmlSchemaPatternFacet pattern = XmlSchemaPatternFacet.Read(reader,h);\r
338                                                 if(pattern != null)\r
339                                                         restriction.facets.Add(pattern);\r
340                                                 continue;\r
341                                         }\r
342                                 }\r
343                                 if(level <= 4)\r
344                                 {\r
345                                         if(reader.LocalName == "attribute")\r
346                                         {\r
347                                                 level = 4;\r
348                                                 XmlSchemaAttribute attr = XmlSchemaAttribute.Read(reader,h);\r
349                                                 if(attr != null)\r
350                                                         restriction.Attributes.Add(attr);\r
351                                                 continue;\r
352                                         }\r
353                                         if(reader.LocalName == "attributeGroup")\r
354                                         {\r
355                                                 level = 4;\r
356                                                 XmlSchemaAttributeGroupRef attr = XmlSchemaAttributeGroupRef.Read(reader,h);\r
357                                                 if(attr != null)\r
358                                                         restriction.attributes.Add(attr);\r
359                                                 continue;\r
360                                         }\r
361                                 }\r
362                                 if(level <= 5 && reader.LocalName == "anyAttribute")\r
363                                 {\r
364                                         level = 6;\r
365                                         XmlSchemaAnyAttribute anyattr = XmlSchemaAnyAttribute.Read(reader,h);\r
366                                         if(anyattr != null)\r
367                                                 restriction.AnyAttribute = anyattr;\r
368                                         continue;\r
369                                 }\r
370                                 reader.RaiseInvalidElementError();\r
371                         }\r
372                         return restriction;\r
373                 }\r
374 \r
375         }\r
376 }\r