31e658d2e941030dc042a58da7eb88cc70b212a1
[mono.git] / mcs / class / System.XML / System.Xml.Schema / XmlSchemaSimpleContentRestriction.cs
1 // Author: Dwivedi, Ajay kumar\r
2 //            Adwiv@Yahoo.com\r
3
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining
6 // a copy of this software and associated documentation files (the
7 // "Software"), to deal in the Software without restriction, including
8 // without limitation the rights to use, copy, modify, merge, publish,
9 // distribute, sublicense, and/or sell copies of the Software, and to
10 // permit persons to whom the Software is furnished to do so, subject to
11 // the following conditions:
12 // 
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
15 // 
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 //
24 using System;\r
25 using System.Xml;\r
26 using System.Xml.Serialization;\r
27 \r
28 namespace System.Xml.Schema\r
29 {\r
30         /// <summary>\r
31         /// Summary description for XmlSchemaSimpleContentRestriction.\r
32         /// </summary>\r
33         public class XmlSchemaSimpleContentRestriction : XmlSchemaContent\r
34         {\r
35                 \r
36                 private XmlSchemaAnyAttribute any;\r
37                 private XmlSchemaObjectCollection attributes;\r
38                 private XmlSchemaSimpleType baseType;\r
39                 private XmlQualifiedName baseTypeName;\r
40                 private XmlSchemaObjectCollection facets;\r
41                 const string xmlname = "restriction";\r
42 \r
43                 public XmlSchemaSimpleContentRestriction()\r
44                 {\r
45                         baseTypeName = XmlQualifiedName.Empty;\r
46                         attributes       = new XmlSchemaObjectCollection();\r
47                         facets           = new XmlSchemaObjectCollection();\r
48                 }\r
49 \r
50                 [System.Xml.Serialization.XmlAttribute("base")]\r
51                 public XmlQualifiedName BaseTypeName \r
52                 {\r
53                         get{ return  baseTypeName; }\r
54                         set{ baseTypeName = value; }\r
55                 }\r
56 \r
57                 [XmlElement("simpleType", Type=typeof (XmlSchemaSimpleType))]\r
58                 public XmlSchemaSimpleType BaseType \r
59                 { \r
60                         get{ return  baseType; } \r
61                         set{ baseType = value; } \r
62                 }\r
63                 \r
64                 [XmlElement("minExclusive",typeof(XmlSchemaMinExclusiveFacet))]\r
65                 [XmlElement("minInclusive",typeof(XmlSchemaMinInclusiveFacet))] \r
66                 [XmlElement("maxExclusive",typeof(XmlSchemaMaxExclusiveFacet))]\r
67                 [XmlElement("maxInclusive",typeof(XmlSchemaMaxInclusiveFacet))]\r
68                 [XmlElement("totalDigits",typeof(XmlSchemaTotalDigitsFacet))]\r
69                 [XmlElement("fractionDigits",typeof(XmlSchemaFractionDigitsFacet))]\r
70                 [XmlElement("length",typeof(XmlSchemaLengthFacet))]\r
71                 [XmlElement("minLength",typeof(XmlSchemaMinLengthFacet))]\r
72                 [XmlElement("maxLength",typeof(XmlSchemaMaxLengthFacet))]\r
73                 [XmlElement("enumeration",typeof(XmlSchemaEnumerationFacet))]\r
74                 [XmlElement("whiteSpace",typeof(XmlSchemaWhiteSpaceFacet))]\r
75                 [XmlElement("pattern",typeof(XmlSchemaPatternFacet))]\r
76                 public XmlSchemaObjectCollection Facets \r
77                 { \r
78                         get{ return facets; } \r
79                 }\r
80 \r
81                 [XmlElement("attribute",typeof(XmlSchemaAttribute))]\r
82                 [XmlElement("attributeGroup",typeof(XmlSchemaAttributeGroupRef))]\r
83                 public XmlSchemaObjectCollection Attributes \r
84                 {\r
85                         get{ return attributes; }\r
86                 }\r
87 \r
88                 [XmlElement("anyAttribute")]\r
89                 public XmlSchemaAnyAttribute AnyAttribute \r
90                 {\r
91                         get{ return  any; }\r
92                         set{ any = value; }\r
93                 }\r
94                 \r
95                 // internal properties\r
96                 internal override bool IsExtension {\r
97                         get { return false; }\r
98                 }\r
99 \r
100                 ///<remarks>\r
101                 /// 1. Base must be present and a QName\r
102                 ///</remarks>\r
103                 internal override int Compile(ValidationEventHandler h, XmlSchema schema)\r
104                 {\r
105                         // If this is already compiled this time, simply skip.\r
106                         if (CompilationId == schema.CompilationId)\r
107                                 return 0;\r
108 #if NET_2_0\r
109                         if (BaseType != null)\r
110                                 BaseType.Parent = this;\r
111                         if (AnyAttribute != null)\r
112                                 AnyAttribute.Parent = this;\r
113                         foreach (XmlSchemaObject obj in Attributes)\r
114                                 obj.Parent = this;\r
115 #endif\r
116 \r
117                         if (this.isRedefinedComponent) {\r
118                                 if (Annotation != null)\r
119                                         Annotation.isRedefinedComponent = true;\r
120                                 if (AnyAttribute != null)\r
121                                         AnyAttribute.isRedefinedComponent = true;\r
122                                 foreach (XmlSchemaObject obj in Attributes)\r
123                                         obj.isRedefinedComponent = true;\r
124                         }\r
125 \r
126                         if(BaseTypeName == null || BaseTypeName.IsEmpty)\r
127                         {\r
128                                 error(h, "base must be present, as a QName");\r
129                         }\r
130                         else if(!XmlSchemaUtil.CheckQName(BaseTypeName))\r
131                                 error(h,"BaseTypeName must be a QName");\r
132 \r
133                         if(BaseType != null)\r
134                         {\r
135                                 errorCount += BaseType.Compile(h,schema);\r
136                         }\r
137 \r
138                         if(this.AnyAttribute != null)\r
139                         {\r
140                                 errorCount += AnyAttribute.Compile(h,schema);\r
141                         }\r
142 \r
143                         foreach(XmlSchemaObject obj in Attributes)\r
144                         {\r
145                                 if(obj is XmlSchemaAttribute)\r
146                                 {\r
147                                         XmlSchemaAttribute attr = (XmlSchemaAttribute) obj;\r
148                                         errorCount += attr.Compile(h,schema);\r
149                                 }\r
150                                 else if(obj is XmlSchemaAttributeGroupRef)\r
151                                 {\r
152                                         XmlSchemaAttributeGroupRef atgrp = (XmlSchemaAttributeGroupRef) obj;\r
153                                         errorCount += atgrp.Compile(h,schema);\r
154                                 }\r
155                                 else\r
156                                         error(h,obj.GetType() +" is not valid in this place::SimpleContentRestriction");\r
157                         }\r
158                         \r
159                         //TODO: Compile Facets: Looks like they are a part of datatypes. So we'll do them with the datatypes\r
160 \r
161                         \r
162                         XmlSchemaUtil.CompileID(Id,this,schema.IDCollection,h);\r
163                         this.CompilationId = schema.CompilationId;\r
164                         return errorCount;\r
165                 }\r
166                 \r
167                 internal override XmlQualifiedName GetBaseTypeName ()\r
168                 {\r
169                         return baseTypeName;\r
170                 }\r
171 \r
172                 internal override XmlSchemaParticle GetParticle ()\r
173                 {\r
174                         return null;\r
175                 }\r
176 \r
177                 internal override int Validate(ValidationEventHandler h, XmlSchema schema)\r
178                 {\r
179                         if (IsValidated (schema.ValidationId))\r
180                                 return errorCount;\r
181 \r
182                         if (baseType != null) {\r
183                                 baseType.Validate (h, schema);\r
184                                 actualBaseSchemaType = baseType;\r
185                         }\r
186                         else if (baseTypeName != XmlQualifiedName.Empty) {\r
187                                 XmlSchemaType st = schema.SchemaTypes [baseTypeName] as XmlSchemaType;\r
188                                 if (st != null) {\r
189                                         st.Validate (h, schema);\r
190                                         actualBaseSchemaType = st;\r
191                                 } else if (baseTypeName == XmlSchemaComplexType.AnyTypeName) {\r
192                                         actualBaseSchemaType = XmlSchemaComplexType.AnyType;
193                                 } else if (XmlSchemaUtil.IsBuiltInDatatypeName (baseTypeName)) {\r
194                                         actualBaseSchemaType = XmlSchemaDatatype.FromName (baseTypeName);\r
195                                         if (actualBaseSchemaType == null)\r
196                                                 error (h, "Invalid schema datatype name is specified.");\r
197                                 }\r
198                                 // otherwise, it might be missing sub components.\r
199                                 else if (!schema.IsNamespaceAbsent (baseTypeName.Namespace))\r
200                                         error (h, "Referenced base schema type " + baseTypeName + " was not found in the corresponding schema.");\r
201                         }\r
202 \r
203                         ValidationId = schema.ValidationId;\r
204                         return errorCount;\r
205                 }\r
206 \r
207                 //<restriction \r
208                 //base = QName \r
209                 //id = ID \r
210                 //{any attributes with non-schema namespace . . .}>\r
211                 //Content: (annotation?, (simpleType?, (minExclusive | minInclusive | maxExclusive | maxInclusive | totalDigits | fractionDigits | length | minLength | maxLength | enumeration | whiteSpace | pattern)*)?, ((attribute | attributeGroup)*, anyAttribute?))\r
212                 //</restriction>\r
213                 internal static XmlSchemaSimpleContentRestriction Read(XmlSchemaReader reader, ValidationEventHandler h)\r
214                 {\r
215                         XmlSchemaSimpleContentRestriction restriction = new XmlSchemaSimpleContentRestriction();\r
216                         reader.MoveToElement();\r
217 \r
218                         if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)\r
219                         {\r
220                                 error(h,"Should not happen :1: XmlSchemaComplexContentRestriction.Read, name="+reader.Name,null);\r
221                                 reader.SkipToEnd();\r
222                                 return null;\r
223                         }\r
224 \r
225                         restriction.LineNumber = reader.LineNumber;\r
226                         restriction.LinePosition = reader.LinePosition;\r
227                         restriction.SourceUri = reader.BaseURI;\r
228 \r
229                         while(reader.MoveToNextAttribute())\r
230                         {\r
231                                 if(reader.Name == "base")\r
232                                 {\r
233                                         Exception innerex;\r
234                                         restriction.baseTypeName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);\r
235                                         if(innerex != null)\r
236                                                 error(h, reader.Value + " is not a valid value for base attribute",innerex);\r
237                                 }\r
238                                 else if(reader.Name == "id")\r
239                                 {\r
240                                         restriction.Id = reader.Value;\r
241                                 }\r
242                                 else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)\r
243                                 {\r
244                                         error(h,reader.Name + " is not a valid attribute for restriction",null);\r
245                                 }\r
246                                 else\r
247                                 {\r
248                                         XmlSchemaUtil.ReadUnhandledAttribute(reader,restriction);\r
249                                 }\r
250                         }\r
251                         \r
252                         reader.MoveToElement();\r
253                         if(reader.IsEmptyElement)\r
254                                 return restriction;\r
255                         \r
256                         //Content:  1.annotation?, \r
257                         //                  2.simpleType?, \r
258                         //                      3.(minExclusive |...| enumeration | whiteSpace | pattern)*, \r
259                         //                      4.(attribute | attributeGroup)*, \r
260                         //                      5.anyAttribute?\r
261                         int level = 1;\r
262                         while(reader.ReadNextElement())\r
263                         {\r
264                                 if(reader.NodeType == XmlNodeType.EndElement)\r
265                                 {\r
266                                         if(reader.LocalName != xmlname)\r
267                                                 error(h,"Should not happen :2: XmlSchemaSimpleContentRestriction.Read, name="+reader.Name,null);\r
268                                         break;\r
269                                 }\r
270                                 if(level <= 1 && reader.LocalName == "annotation")\r
271                                 {\r
272                                         level = 2; //Only one annotation\r
273                                         XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);\r
274                                         if(annotation != null)\r
275                                                 restriction.Annotation = annotation;\r
276                                         continue;\r
277                                 }\r
278                                 if(level <=2 && reader.LocalName == "simpleType")\r
279                                 {\r
280                                         level = 3;\r
281                                         XmlSchemaSimpleType stype = XmlSchemaSimpleType.Read(reader,h);\r
282                                         if(stype != null)\r
283                                                 restriction.baseType = stype;\r
284                                         continue;\r
285                                 }\r
286                                 if(level <= 3)\r
287                                 {\r
288                                         if(reader.LocalName == "minExclusive")\r
289                                         {\r
290                                                 level = 3;\r
291                                                 XmlSchemaMinExclusiveFacet minex = XmlSchemaMinExclusiveFacet.Read(reader,h);\r
292                                                 if(minex != null)\r
293                                                         restriction.facets.Add(minex);\r
294                                                 continue;\r
295                                         }\r
296                                         else if(reader.LocalName == "minInclusive")\r
297                                         {\r
298                                                 level = 3;\r
299                                                 XmlSchemaMinInclusiveFacet mini = XmlSchemaMinInclusiveFacet.Read(reader,h);\r
300                                                 if(mini != null)\r
301                                                         restriction.facets.Add(mini);\r
302                                                 continue;\r
303                                         }\r
304                                         else if(reader.LocalName == "maxExclusive")\r
305                                         {\r
306                                                 level = 3;\r
307                                                 XmlSchemaMaxExclusiveFacet maxex = XmlSchemaMaxExclusiveFacet.Read(reader,h);\r
308                                                 if(maxex != null)\r
309                                                         restriction.facets.Add(maxex);\r
310                                                 continue;\r
311                                         }\r
312                                         else if(reader.LocalName == "maxInclusive")\r
313                                         {\r
314                                                 level = 3;\r
315                                                 XmlSchemaMaxInclusiveFacet maxi = XmlSchemaMaxInclusiveFacet.Read(reader,h);\r
316                                                 if(maxi != null)\r
317                                                         restriction.facets.Add(maxi);\r
318                                                 continue;\r
319                                         }\r
320                                         else if(reader.LocalName == "totalDigits")\r
321                                         {\r
322                                                 level = 3;\r
323                                                 XmlSchemaTotalDigitsFacet total = XmlSchemaTotalDigitsFacet.Read(reader,h);\r
324                                                 if(total != null)\r
325                                                         restriction.facets.Add(total);\r
326                                                 continue;\r
327                                         }\r
328                                         else if(reader.LocalName == "fractionDigits")\r
329                                         {\r
330                                                 level = 3;\r
331                                                 XmlSchemaFractionDigitsFacet fraction = XmlSchemaFractionDigitsFacet.Read(reader,h);\r
332                                                 if(fraction != null)\r
333                                                         restriction.facets.Add(fraction);\r
334                                                 continue;\r
335                                         }\r
336                                         else if(reader.LocalName == "length")\r
337                                         {\r
338                                                 level = 3;\r
339                                                 XmlSchemaLengthFacet length = XmlSchemaLengthFacet.Read(reader,h);\r
340                                                 if(length != null)\r
341                                                         restriction.facets.Add(length);\r
342                                                 continue;\r
343                                         }\r
344                                         else if(reader.LocalName == "minLength")\r
345                                         {\r
346                                                 level = 3;\r
347                                                 XmlSchemaMinLengthFacet minlen = XmlSchemaMinLengthFacet.Read(reader,h);\r
348                                                 if(minlen != null)\r
349                                                         restriction.facets.Add(minlen);\r
350                                                 continue;\r
351                                         }\r
352                                         else if(reader.LocalName == "maxLength")\r
353                                         {\r
354                                                 level = 3;\r
355                                                 XmlSchemaMaxLengthFacet maxlen = XmlSchemaMaxLengthFacet.Read(reader,h);\r
356                                                 if(maxlen != null)\r
357                                                         restriction.facets.Add(maxlen);\r
358                                                 continue;\r
359                                         }\r
360                                         else if(reader.LocalName == "enumeration")\r
361                                         {\r
362                                                 level = 3;\r
363                                                 XmlSchemaEnumerationFacet enumeration = XmlSchemaEnumerationFacet.Read(reader,h);\r
364                                                 if(enumeration != null)\r
365                                                         restriction.facets.Add(enumeration);\r
366                                                 continue;\r
367                                         }\r
368                                         else if(reader.LocalName == "whiteSpace")\r
369                                         {\r
370                                                 level = 3;\r
371                                                 XmlSchemaWhiteSpaceFacet ws = XmlSchemaWhiteSpaceFacet.Read(reader,h);\r
372                                                 if(ws != null)\r
373                                                         restriction.facets.Add(ws);\r
374                                                 continue;\r
375                                         }\r
376                                         else if(reader.LocalName == "pattern")\r
377                                         {\r
378                                                 level = 3;\r
379                                                 XmlSchemaPatternFacet pattern = XmlSchemaPatternFacet.Read(reader,h);\r
380                                                 if(pattern != null)\r
381                                                         restriction.facets.Add(pattern);\r
382                                                 continue;\r
383                                         }\r
384                                 }\r
385                                 if(level <= 4)\r
386                                 {\r
387                                         if(reader.LocalName == "attribute")\r
388                                         {\r
389                                                 level = 4;\r
390                                                 XmlSchemaAttribute attr = XmlSchemaAttribute.Read(reader,h);\r
391                                                 if(attr != null)\r
392                                                         restriction.Attributes.Add(attr);\r
393                                                 continue;\r
394                                         }\r
395                                         if(reader.LocalName == "attributeGroup")\r
396                                         {\r
397                                                 level = 4;\r
398                                                 XmlSchemaAttributeGroupRef attr = XmlSchemaAttributeGroupRef.Read(reader,h);\r
399                                                 if(attr != null)\r
400                                                         restriction.attributes.Add(attr);\r
401                                                 continue;\r
402                                         }\r
403                                 }\r
404                                 if(level <= 5 && reader.LocalName == "anyAttribute")\r
405                                 {\r
406                                         level = 6;\r
407                                         XmlSchemaAnyAttribute anyattr = XmlSchemaAnyAttribute.Read(reader,h);\r
408                                         if(anyattr != null)\r
409                                                 restriction.AnyAttribute = anyattr;\r
410                                         continue;\r
411                                 }\r
412                                 reader.RaiseInvalidElementError();\r
413                         }\r
414                         return restriction;\r
415                 }\r
416 \r
417         }\r
418 }\r