2002-06-14 Dwivedi, Ajay kumar <adwiv@yahoo.com>
[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("anyAttribute",Namespace="http://www.w3.org/2001/XMLSchema")]\r
37                 public XmlSchemaAnyAttribute AnyAttribute \r
38                 {\r
39                         get{ return  any; }\r
40                         set{ any = value; }\r
41                 }\r
42 \r
43                 [XmlElement("attribute",typeof(XmlSchemaAttribute),Namespace="http://www.w3.org/2001/XMLSchema")]\r
44                 [XmlElement("attributeGroup",typeof(XmlSchemaAttributeGroupRef),Namespace="http://www.w3.org/2001/XMLSchema")]\r
45                 public XmlSchemaObjectCollection Attributes \r
46                 {\r
47                         get{ return attributes; }\r
48                 }\r
49 \r
50                 [XmlElement("simpleType",Namespace="http://www.w3.org/2001/XMLSchema")]\r
51                 public XmlSchemaSimpleType BaseType \r
52                 { \r
53                         get{ return  baseType; } \r
54                         set{ baseType = value; } \r
55                 }\r
56                 \r
57                 \r
58                 [XmlElement("minExclusive",typeof(XmlSchemaMinExclusiveFacet),Namespace="http://www.w3.org/2001/XMLSchema")]\r
59                 [XmlElement("minInclusive",typeof(XmlSchemaMinInclusiveFacet),Namespace="http://www.w3.org/2001/XMLSchema")] \r
60                 [XmlElement("maxExclusive",typeof(XmlSchemaMaxExclusiveFacet),Namespace="http://www.w3.org/2001/XMLSchema")]\r
61                 [XmlElement("maxInclusive",typeof(XmlSchemaMaxInclusiveFacet),Namespace="http://www.w3.org/2001/XMLSchema")]\r
62                 [XmlElement("totalDigits",typeof(XmlSchemaTotalDigitsFacet),Namespace="http://www.w3.org/2001/XMLSchema")]\r
63                 [XmlElement("fractionDigits",typeof(XmlSchemaFractionDigitsFacet),Namespace="http://www.w3.org/2001/XMLSchema")]\r
64                 [XmlElement("length",typeof(XmlSchemaLengthFacet),Namespace="http://www.w3.org/2001/XMLSchema")]\r
65                 [XmlElement("minLength",typeof(XmlSchemaMinLengthFacet),Namespace="http://www.w3.org/2001/XMLSchema")]\r
66                 [XmlElement("maxLength",typeof(XmlSchemaMaxLengthFacet),Namespace="http://www.w3.org/2001/XMLSchema")]\r
67                 [XmlElement("enumeration",typeof(XmlSchemaEnumerationFacet),Namespace="http://www.w3.org/2001/XMLSchema")]\r
68                 [XmlElement("whiteSpace",typeof(XmlSchemaWhiteSpaceFacet),Namespace="http://www.w3.org/2001/XMLSchema")]\r
69                 [XmlElement("pattern",typeof(XmlSchemaPatternFacet),Namespace="http://www.w3.org/2001/XMLSchema")]\r
70                 public XmlSchemaObjectCollection Facets \r
71                 { \r
72                         get{ return facets; } \r
73                 }\r
74                 ///<remarks>\r
75                 /// 1. Base must be present and a QName\r
76                 ///</remarks>\r
77                 [MonoTODO]\r
78                 internal int Compile(ValidationEventHandler h, XmlSchemaInfo info)\r
79                 {\r
80                         if(BaseTypeName == null || BaseTypeName.IsEmpty)\r
81                         {\r
82                                 error(h, "base must be present and a QName");\r
83                         }\r
84                         else if(!XmlSchemaUtil.CheckQName(BaseTypeName))\r
85                                 error(h,"BaseTypeName must be a QName");\r
86 \r
87                         if(BaseType != null)\r
88                         {\r
89                                 errorCount += BaseType.Compile(h,info);\r
90                         }\r
91 \r
92                         if(this.AnyAttribute != null)\r
93                         {\r
94                                 errorCount += AnyAttribute.Compile(h,info);\r
95                         }\r
96 \r
97                         foreach(XmlSchemaObject obj in Attributes)\r
98                         {\r
99                                 if(obj is XmlSchemaAttribute)\r
100                                 {\r
101                                         XmlSchemaAttribute attr = (XmlSchemaAttribute) obj;\r
102                                         errorCount += attr.Compile(h,info);\r
103                                 }\r
104                                 else if(obj is XmlSchemaAttributeGroupRef)\r
105                                 {\r
106                                         XmlSchemaAttributeGroupRef atgrp = (XmlSchemaAttributeGroupRef) obj;\r
107                                         errorCount += atgrp.Compile(h,info);\r
108                                 }\r
109                                 else\r
110                                         error(h,obj.GetType() +" is not valid in this place::SimpleContentRestriction");\r
111                         }\r
112                         \r
113                         //TODO: Compile Facets: Looks like they are a part of datatypes. So we'll do them with the datatypes\r
114 \r
115                         \r
116                         XmlSchemaUtil.CompileID(Id,this,info.IDCollection,h);\r
117                         return errorCount;\r
118                 }\r
119                 \r
120                 [MonoTODO]\r
121                 internal int Validate(ValidationEventHandler h)\r
122                 {\r
123                         return errorCount;\r
124                 }\r
125 \r
126                 //<restriction \r
127                 //base = QName \r
128                 //id = ID \r
129                 //{any attributes with non-schema namespace . . .}>\r
130                 //Content: (annotation?, (simpleType?, (minExclusive | minInclusive | maxExclusive | maxInclusive | totalDigits | fractionDigits | length | minLength | maxLength | enumeration | whiteSpace | pattern)*)?, ((attribute | attributeGroup)*, anyAttribute?))\r
131                 //</restriction>\r
132                 internal static XmlSchemaSimpleContentRestriction Read(XmlSchemaReader reader, ValidationEventHandler h)\r
133                 {\r
134                         XmlSchemaSimpleContentRestriction restriction = new XmlSchemaSimpleContentRestriction();\r
135                         reader.MoveToElement();\r
136 \r
137                         if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)\r
138                         {\r
139                                 error(h,"Should not happen :1: XmlSchemaComplexContentRestriction.Read, name="+reader.Name,null);\r
140                                 reader.SkipToEnd();\r
141                                 return null;\r
142                         }\r
143 \r
144                         restriction.LineNumber = reader.LineNumber;\r
145                         restriction.LinePosition = reader.LinePosition;\r
146                         restriction.SourceUri = reader.BaseURI;\r
147 \r
148                         while(reader.MoveToNextAttribute())\r
149                         {\r
150                                 if(reader.Name == "base")\r
151                                 {\r
152                                         Exception innerex;\r
153                                         restriction.baseTypeName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);\r
154                                         if(innerex != null)\r
155                                                 error(h, reader.Value + " is not a valid value for base attribute",innerex);\r
156                                 }\r
157                                 else if(reader.Name == "id")\r
158                                 {\r
159                                         restriction.Id = reader.Value;\r
160                                 }\r
161                                 else if(reader.NamespaceURI == "" || reader.NamespaceURI == XmlSchema.Namespace)\r
162                                 {\r
163                                         error(h,reader.Name + " is not a valid attribute for restriction",null);\r
164                                 }\r
165                                 else\r
166                                 {\r
167                                         if(reader.Prefix == "xmlns")\r
168                                                 restriction.Namespaces.Add(reader.LocalName, reader.Value);\r
169                                         else if(reader.Name == "xmlns")\r
170                                                 restriction.Namespaces.Add("",reader.Value);\r
171                                         //TODO: Add to Unhandled attributes\r
172                                 }\r
173                         }\r
174                         \r
175                         reader.MoveToElement();\r
176                         if(reader.IsEmptyElement)\r
177                                 return restriction;\r
178                         \r
179                         //Content:  1.annotation?, \r
180                         //                  2.simpleType?, \r
181                         //                      3.(minExclusive |...| enumeration | whiteSpace | pattern)*, \r
182                         //                      4.(attribute | attributeGroup)*, \r
183                         //                      5.anyAttribute?\r
184                         int level = 1;\r
185                         while(reader.ReadNextElement())\r
186                         {\r
187                                 if(reader.NodeType == XmlNodeType.EndElement)\r
188                                 {\r
189                                         if(reader.LocalName != xmlname)\r
190                                                 error(h,"Should not happen :2: XmlSchemaSimpleContentRestriction.Read, name="+reader.Name,null);\r
191                                         break;\r
192                                 }\r
193                                 if(level <= 1 && reader.LocalName == "annotation")\r
194                                 {\r
195                                         level = 2; //Only one annotation\r
196                                         XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);\r
197                                         if(annotation != null)\r
198                                                 restriction.Annotation = annotation;\r
199                                         continue;\r
200                                 }\r
201                                 if(level <=2 && reader.LocalName == "simpleType")\r
202                                 {\r
203                                         level = 3;\r
204                                         XmlSchemaSimpleType stype = XmlSchemaSimpleType.Read(reader,h);\r
205                                         if(stype != null)\r
206                                                 restriction.baseType = stype;\r
207                                         continue;\r
208                                 }\r
209                                 if(level <= 3)\r
210                                 {\r
211                                         if(reader.LocalName == "minExclusive")\r
212                                         {\r
213                                                 level = 3;\r
214                                                 XmlSchemaMinExclusiveFacet minex = XmlSchemaMinExclusiveFacet.Read(reader,h);\r
215                                                 if(minex != null)\r
216                                                         restriction.facets.Add(minex);\r
217                                                 continue;\r
218                                         }\r
219                                         else if(reader.LocalName == "minInclusive")\r
220                                         {\r
221                                                 level = 3;\r
222                                                 XmlSchemaMinInclusiveFacet mini = XmlSchemaMinInclusiveFacet.Read(reader,h);\r
223                                                 if(mini != null)\r
224                                                         restriction.facets.Add(mini);\r
225                                                 continue;\r
226                                         }\r
227                                         else if(reader.LocalName == "maxExclusive")\r
228                                         {\r
229                                                 level = 3;\r
230                                                 XmlSchemaMaxExclusiveFacet maxex = XmlSchemaMaxExclusiveFacet.Read(reader,h);\r
231                                                 if(maxex != null)\r
232                                                         restriction.facets.Add(maxex);\r
233                                                 continue;\r
234                                         }\r
235                                         else if(reader.LocalName == "maxInclusive")\r
236                                         {\r
237                                                 level = 3;\r
238                                                 XmlSchemaMaxInclusiveFacet maxi = XmlSchemaMaxInclusiveFacet.Read(reader,h);\r
239                                                 if(maxi != null)\r
240                                                         restriction.facets.Add(maxi);\r
241                                                 continue;\r
242                                         }\r
243                                         else if(reader.LocalName == "totalDigits")\r
244                                         {\r
245                                                 level = 3;\r
246                                                 XmlSchemaTotalDigitsFacet total = XmlSchemaTotalDigitsFacet.Read(reader,h);\r
247                                                 if(total != null)\r
248                                                         restriction.facets.Add(total);\r
249                                                 continue;\r
250                                         }\r
251                                         else if(reader.LocalName == "fractionDigits")\r
252                                         {\r
253                                                 level = 3;\r
254                                                 XmlSchemaFractionDigitsFacet fraction = XmlSchemaFractionDigitsFacet.Read(reader,h);\r
255                                                 if(fraction != null)\r
256                                                         restriction.facets.Add(fraction);\r
257                                                 continue;\r
258                                         }\r
259                                         else if(reader.LocalName == "length")\r
260                                         {\r
261                                                 level = 3;\r
262                                                 XmlSchemaLengthFacet length = XmlSchemaLengthFacet.Read(reader,h);\r
263                                                 if(length != null)\r
264                                                         restriction.facets.Add(length);\r
265                                                 continue;\r
266                                         }\r
267                                         else if(reader.LocalName == "minLength")\r
268                                         {\r
269                                                 level = 3;\r
270                                                 XmlSchemaMinLengthFacet minlen = XmlSchemaMinLengthFacet.Read(reader,h);\r
271                                                 if(minlen != null)\r
272                                                         restriction.facets.Add(minlen);\r
273                                                 continue;\r
274                                         }\r
275                                         else if(reader.LocalName == "maxLength")\r
276                                         {\r
277                                                 level = 3;\r
278                                                 XmlSchemaMaxLengthFacet maxlen = XmlSchemaMaxLengthFacet.Read(reader,h);\r
279                                                 if(maxlen != null)\r
280                                                         restriction.facets.Add(maxlen);\r
281                                                 continue;\r
282                                         }\r
283                                         else if(reader.LocalName == "enumeration")\r
284                                         {\r
285                                                 level = 3;\r
286                                                 XmlSchemaEnumerationFacet enumeration = XmlSchemaEnumerationFacet.Read(reader,h);\r
287                                                 if(enumeration != null)\r
288                                                         restriction.facets.Add(enumeration);\r
289                                                 continue;\r
290                                         }\r
291                                         else if(reader.LocalName == "whiteSpace")\r
292                                         {\r
293                                                 level = 3;\r
294                                                 XmlSchemaWhiteSpaceFacet ws = XmlSchemaWhiteSpaceFacet.Read(reader,h);\r
295                                                 if(ws != null)\r
296                                                         restriction.facets.Add(ws);\r
297                                                 continue;\r
298                                         }\r
299                                         else if(reader.LocalName == "pattern")\r
300                                         {\r
301                                                 level = 3;\r
302                                                 XmlSchemaPatternFacet pattern = XmlSchemaPatternFacet.Read(reader,h);\r
303                                                 if(pattern != null)\r
304                                                         restriction.facets.Add(pattern);\r
305                                                 continue;\r
306                                         }\r
307                                 }\r
308                                 if(level <= 4)\r
309                                 {\r
310                                         if(reader.LocalName == "attribute")\r
311                                         {\r
312                                                 level = 4;\r
313                                                 XmlSchemaAttribute attr = XmlSchemaAttribute.Read(reader,h);\r
314                                                 if(attr != null)\r
315                                                         restriction.Attributes.Add(attr);\r
316                                                 continue;\r
317                                         }\r
318                                         if(reader.LocalName == "attributeGroup")\r
319                                         {\r
320                                                 level = 4;\r
321                                                 XmlSchemaAttributeGroupRef attr = XmlSchemaAttributeGroupRef.Read(reader,h);\r
322                                                 if(attr != null)\r
323                                                         restriction.attributes.Add(attr);\r
324                                                 continue;\r
325                                         }\r
326                                 }\r
327                                 if(level <= 5 && reader.LocalName == "anyAttribute")\r
328                                 {\r
329                                         level = 6;\r
330                                         XmlSchemaAnyAttribute anyattr = XmlSchemaAnyAttribute.Read(reader,h);\r
331                                         if(anyattr != null)\r
332                                                 restriction.AnyAttribute = anyattr;\r
333                                         continue;\r
334                                 }\r
335                                 reader.RaiseInvalidElementError();\r
336                         }\r
337                         return restriction;\r
338                 }\r
339 \r
340         }\r
341 }\r