0e8116a9caa33b1f0a364ca4f73c477a396a5b56
[mono.git] / mcs / class / System.XML / System.Xml.Schema / XmlSchemaComplexContentRestriction.cs
1 //\r
2 // System.Xml.Schema.XmlSchemaComplexContentRestriction.cs\r
3 //\r
4 // Author:\r
5 //      Dwivedi, Ajay kumar  Adwiv@Yahoo.com\r
6 //      Atsushi Enomoto  ginga@kit.hi-ho.ne.jp\r
7 //\r
8 using System;\r
9 using System.Xml;\r
10 using System.Xml.Serialization;\r
11 \r
12 namespace System.Xml.Schema\r
13 {\r
14         /// <summary>\r
15         /// Summary description for XmlSchemaComplexContentRestriction.\r
16         /// </summary>\r
17         public class XmlSchemaComplexContentRestriction : XmlSchemaContent\r
18         {\r
19                 private XmlSchemaAnyAttribute any;\r
20                 private XmlSchemaObjectCollection attributes;\r
21                 private XmlQualifiedName baseTypeName;\r
22                 private XmlSchemaParticle particle;\r
23                 private static string xmlname = "restriction";\r
24 \r
25                 public XmlSchemaComplexContentRestriction()\r
26                 {\r
27                         baseTypeName = XmlQualifiedName.Empty;\r
28                         attributes       = new XmlSchemaObjectCollection();\r
29                 }\r
30 \r
31                 [System.Xml.Serialization.XmlAttribute("base")]\r
32                 public XmlQualifiedName BaseTypeName \r
33                 {\r
34                         get{ return  baseTypeName; }\r
35                         set{ baseTypeName = value; }\r
36                 }\r
37 \r
38                 [XmlElement("group",typeof(XmlSchemaGroupRef),Namespace=XmlSchema.Namespace)]\r
39                 [XmlElement("all",typeof(XmlSchemaAll),Namespace=XmlSchema.Namespace)]\r
40                 [XmlElement("choice",typeof(XmlSchemaChoice),Namespace=XmlSchema.Namespace)]\r
41                 [XmlElement("sequence",typeof(XmlSchemaSequence),Namespace=XmlSchema.Namespace)]\r
42                 public XmlSchemaParticle Particle \r
43                 {\r
44                         get{ return  particle; }\r
45                         set{ particle = value; }\r
46                 }\r
47 \r
48                 [XmlElement("attribute",typeof(XmlSchemaAttribute),Namespace=XmlSchema.Namespace)]\r
49                 [XmlElement("attributeGroup",typeof(XmlSchemaAttributeGroupRef),Namespace=XmlSchema.Namespace)]\r
50                 public XmlSchemaObjectCollection Attributes \r
51                 {\r
52                         get{ return attributes; }\r
53                 }\r
54 \r
55                 [XmlElement("anyAttribute",Namespace=XmlSchema.Namespace)]\r
56                 public XmlSchemaAnyAttribute AnyAttribute \r
57                 {\r
58                         get{ return  any; }\r
59                         set{ any = value; }\r
60                 }\r
61 \r
62                 // internal properties\r
63                 internal override bool IsExtension {\r
64                         get { return false; }\r
65                 }\r
66 \r
67                 /// <remarks>\r
68                 /// 1. base must be present\r
69                 /// </remarks>\r
70                 internal override int Compile(ValidationEventHandler h, XmlSchema schema)\r
71                 {\r
72                         // If this is already compiled this time, simply skip.\r
73                         if (this.IsComplied (schema.CompilationId))\r
74                                 return 0;\r
75 \r
76                         if (this.isRedefinedComponent) {\r
77                                 if (Annotation != null)\r
78                                         Annotation.isRedefinedComponent = true;\r
79                                 if (AnyAttribute != null)\r
80                                         AnyAttribute.isRedefinedComponent = true;\r
81                                 foreach (XmlSchemaObject obj in Attributes)\r
82                                         obj.isRedefinedComponent = true;\r
83                                 if (Particle != null)\r
84                                         Particle.isRedefinedComponent = true;\r
85                         }\r
86 \r
87                         if(BaseTypeName == null || BaseTypeName.IsEmpty)\r
88                         {\r
89                                 error(h, "base must be present, as a QName");\r
90                         }\r
91                         else if(!XmlSchemaUtil.CheckQName(BaseTypeName))\r
92                                 error(h,"BaseTypeName is not a valid XmlQualifiedName");\r
93 \r
94                         if(this.AnyAttribute != null)\r
95                         {\r
96                                 errorCount += AnyAttribute.Compile(h, schema);\r
97                         }\r
98 \r
99                         foreach(XmlSchemaObject obj in Attributes)\r
100                         {\r
101                                 if(obj is XmlSchemaAttribute)\r
102                                 {\r
103                                         XmlSchemaAttribute attr = (XmlSchemaAttribute) obj;\r
104                                         errorCount += attr.Compile(h, schema);\r
105                                 }\r
106                                 else if(obj is XmlSchemaAttributeGroupRef)\r
107                                 {\r
108                                         XmlSchemaAttributeGroupRef atgrp = (XmlSchemaAttributeGroupRef) obj;\r
109                                         errorCount += atgrp.Compile(h, schema);\r
110                                 }\r
111                                 else\r
112                                         error(h,obj.GetType() +" is not valid in this place::ComplexContentRestriction");\r
113                         }\r
114                         \r
115                         if(Particle != null)\r
116                         {\r
117                                 if(Particle is XmlSchemaGroupRef)\r
118                                 {\r
119                                         errorCount += ((XmlSchemaGroupRef)Particle).Compile(h, schema);\r
120                                 }\r
121                                 else if(Particle is XmlSchemaAll)\r
122                                 {\r
123                                         errorCount += ((XmlSchemaAll)Particle).Compile(h, schema);\r
124                                 }\r
125                                 else if(Particle is XmlSchemaChoice)\r
126                                 {\r
127                                         errorCount += ((XmlSchemaChoice)Particle).Compile(h, schema);\r
128                                 }\r
129                                 else if(Particle is XmlSchemaSequence)\r
130                                 {\r
131                                         errorCount += ((XmlSchemaSequence)Particle).Compile(h, schema);\r
132                                 }\r
133                                 else\r
134                                         error (h, "Particle of a restriction is limited only to group, sequence, choice and all.");\r
135                         }\r
136                         \r
137                         XmlSchemaUtil.CompileID(Id,this, schema.IDCollection,h);\r
138 \r
139                         this.CompilationId = schema.CompilationId;\r
140                         return errorCount;\r
141                 }\r
142                 \r
143                 internal override XmlQualifiedName GetBaseTypeName ()\r
144                 {\r
145                         return baseTypeName;\r
146                 }\r
147 \r
148                 internal override XmlSchemaParticle GetParticle ()\r
149                 {\r
150                         return particle;\r
151                 }\r
152 \r
153                 internal override int Validate (ValidationEventHandler h, XmlSchema schema)\r
154                 {\r
155                         return errorCount;\r
156                 }\r
157 \r
158                 //<restriction\r
159                 //  base = QName\r
160                 //  id = ID\r
161                 //  {any attributes with non-schema namespace . . .}>\r
162                 //  Content: (annotation?, ((group | all | choice | sequence)?, ((attribute | attributeGroup)*, anyAttribute?)))\r
163                 //</restriction>\r
164                 internal static XmlSchemaComplexContentRestriction Read(XmlSchemaReader reader, ValidationEventHandler h)\r
165                 {\r
166                         XmlSchemaComplexContentRestriction restriction = new XmlSchemaComplexContentRestriction();\r
167                         reader.MoveToElement();\r
168 \r
169                         if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)\r
170                         {\r
171                                 error(h,"Should not happen :1: XmlSchemaComplexContentRestriction.Read, name="+reader.Name,null);\r
172                                 reader.Skip();\r
173                                 return null;\r
174                         }\r
175 \r
176                         restriction.LineNumber = reader.LineNumber;\r
177                         restriction.LinePosition = reader.LinePosition;\r
178                         restriction.SourceUri = reader.BaseURI;\r
179 \r
180                         while(reader.MoveToNextAttribute())\r
181                         {\r
182                                 if(reader.Name == "base")\r
183                                 {\r
184                                         Exception innerex;\r
185                                         restriction.baseTypeName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);\r
186                                         if(innerex != null)\r
187                                                 error(h, reader.Value + " is not a valid value for base attribute",innerex);\r
188                                 }\r
189                                 else if(reader.Name == "id")\r
190                                 {\r
191                                         restriction.Id = reader.Value;\r
192                                 }\r
193                                 else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)\r
194                                 {\r
195                                         error(h,reader.Name + " is not a valid attribute for restriction",null);\r
196                                 }\r
197                                 else\r
198                                 {\r
199                                         XmlSchemaUtil.ReadUnhandledAttribute(reader,restriction);\r
200                                 }\r
201                         }\r
202                         \r
203                         reader.MoveToElement();\r
204                         if(reader.IsEmptyElement)\r
205                                 return restriction;\r
206                         //Content: 1. annotation?, \r
207                         //                      (2.(group | all | choice | sequence)?, (3.(attribute | attributeGroup)*, 4.anyAttribute?)))\r
208                         int level = 1;\r
209                         while(reader.ReadNextElement())\r
210                         {\r
211                                 if(reader.NodeType == XmlNodeType.EndElement)\r
212                                 {\r
213                                         if(reader.LocalName != xmlname)\r
214                                                 error(h,"Should not happen :2: XmlSchemaComplexContentRestriction.Read, name="+reader.Name,null);\r
215                                         break;\r
216                                 }\r
217                                 if(level <= 1 && reader.LocalName == "annotation")\r
218                                 {\r
219                                         level = 2; //Only one annotation\r
220                                         XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);\r
221                                         if(annotation != null)\r
222                                                 restriction.Annotation = annotation;\r
223                                         continue;\r
224                                 }\r
225                                 if(level <= 2)\r
226                                 {\r
227                                         if(reader.LocalName == "group")\r
228                                         {\r
229                                                 level = 3;\r
230                                                 XmlSchemaGroupRef group = XmlSchemaGroupRef.Read(reader,h);\r
231                                                 if(group != null)\r
232                                                         restriction.particle = group;\r
233                                                 continue;\r
234                                         }\r
235                                         if(reader.LocalName == "all")\r
236                                         {\r
237                                                 level = 3;\r
238                                                 XmlSchemaAll all = XmlSchemaAll.Read(reader,h);\r
239                                                 if(all != null)\r
240                                                         restriction.particle = all;\r
241                                                 continue;\r
242                                         }\r
243                                         if(reader.LocalName == "choice")\r
244                                         {\r
245                                                 level = 3;\r
246                                                 XmlSchemaChoice choice = XmlSchemaChoice.Read(reader,h);\r
247                                                 if(choice != null)\r
248                                                         restriction.particle = choice;\r
249                                                 continue;\r
250                                         }\r
251                                         if(reader.LocalName == "sequence")\r
252                                         {\r
253                                                 level = 3;\r
254                                                 XmlSchemaSequence sequence = XmlSchemaSequence.Read(reader,h);\r
255                                                 if(sequence != null)\r
256                                                         restriction.particle = sequence;\r
257                                                 continue;\r
258                                         }\r
259                                 }\r
260                                 if(level <= 3)\r
261                                 {\r
262                                         if(reader.LocalName == "attribute")\r
263                                         {\r
264                                                 level = 3;\r
265                                                 XmlSchemaAttribute attr = XmlSchemaAttribute.Read(reader,h);\r
266                                                 if(attr != null)\r
267                                                         restriction.Attributes.Add(attr);\r
268                                                 continue;\r
269                                         }\r
270                                         if(reader.LocalName == "attributeGroup")\r
271                                         {\r
272                                                 level = 3;\r
273                                                 XmlSchemaAttributeGroupRef attr = XmlSchemaAttributeGroupRef.Read(reader,h);\r
274                                                 if(attr != null)\r
275                                                         restriction.attributes.Add(attr);\r
276                                                 continue;\r
277                                         }\r
278                                 }\r
279                                 if(level <= 4 && reader.LocalName == "anyAttribute")\r
280                                 {\r
281                                         level = 5;\r
282                                         XmlSchemaAnyAttribute anyattr = XmlSchemaAnyAttribute.Read(reader,h);\r
283                                         if(anyattr != null)\r
284                                                 restriction.AnyAttribute = anyattr;\r
285                                         continue;\r
286                                 }\r
287                                 reader.RaiseInvalidElementError();\r
288                         }\r
289                         return restriction;\r
290                 }\r
291         }\r
292 }\r