This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[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
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29 using System;\r
30 using System.Xml;\r
31 using System.Xml.Serialization;\r
32 \r
33 namespace System.Xml.Schema\r
34 {\r
35         /// <summary>\r
36         /// Summary description for XmlSchemaComplexContentRestriction.\r
37         /// </summary>\r
38         public class XmlSchemaComplexContentRestriction : XmlSchemaContent\r
39         {\r
40                 private XmlSchemaAnyAttribute any;\r
41                 private XmlSchemaObjectCollection attributes;\r
42                 private XmlQualifiedName baseTypeName;\r
43                 private XmlSchemaParticle particle;\r
44                 const string xmlname = "restriction";\r
45 \r
46                 public XmlSchemaComplexContentRestriction()\r
47                 {\r
48                         baseTypeName = XmlQualifiedName.Empty;\r
49                         attributes       = new XmlSchemaObjectCollection();\r
50                 }\r
51 \r
52                 [System.Xml.Serialization.XmlAttribute("base")]\r
53                 public XmlQualifiedName BaseTypeName \r
54                 {\r
55                         get{ return  baseTypeName; }\r
56                         set{ baseTypeName = value; }\r
57                 }\r
58 \r
59                 [XmlElement("group",typeof(XmlSchemaGroupRef),Namespace=XmlSchema.Namespace)]\r
60                 [XmlElement("all",typeof(XmlSchemaAll),Namespace=XmlSchema.Namespace)]\r
61                 [XmlElement("choice",typeof(XmlSchemaChoice),Namespace=XmlSchema.Namespace)]\r
62                 [XmlElement("sequence",typeof(XmlSchemaSequence),Namespace=XmlSchema.Namespace)]\r
63                 public XmlSchemaParticle Particle \r
64                 {\r
65                         get{ return  particle; }\r
66                         set{ particle = value; }\r
67                 }\r
68 \r
69                 [XmlElement("attribute",typeof(XmlSchemaAttribute),Namespace=XmlSchema.Namespace)]\r
70                 [XmlElement("attributeGroup",typeof(XmlSchemaAttributeGroupRef),Namespace=XmlSchema.Namespace)]\r
71                 public XmlSchemaObjectCollection Attributes \r
72                 {\r
73                         get{ return attributes; }\r
74                 }\r
75 \r
76                 [XmlElement("anyAttribute",Namespace=XmlSchema.Namespace)]\r
77                 public XmlSchemaAnyAttribute AnyAttribute \r
78                 {\r
79                         get{ return  any; }\r
80                         set{ any = value; }\r
81                 }\r
82 \r
83                 // internal properties\r
84                 internal override bool IsExtension {\r
85                         get { return false; }\r
86                 }\r
87 \r
88                 /// <remarks>\r
89                 /// 1. base must be present\r
90                 /// </remarks>\r
91                 internal override int Compile(ValidationEventHandler h, XmlSchema schema)\r
92                 {\r
93                         // If this is already compiled this time, simply skip.\r
94                         if (this.IsComplied (schema.CompilationId))\r
95                                 return 0;\r
96 \r
97                         if (this.isRedefinedComponent) {\r
98                                 if (Annotation != null)\r
99                                         Annotation.isRedefinedComponent = true;\r
100                                 if (AnyAttribute != null)\r
101                                         AnyAttribute.isRedefinedComponent = true;\r
102                                 foreach (XmlSchemaObject obj in Attributes)\r
103                                         obj.isRedefinedComponent = true;\r
104                                 if (Particle != null)\r
105                                         Particle.isRedefinedComponent = true;\r
106                         }\r
107 \r
108                         if(BaseTypeName == null || BaseTypeName.IsEmpty)\r
109                         {\r
110                                 error(h, "base must be present, as a QName");\r
111                         }\r
112                         else if(!XmlSchemaUtil.CheckQName(BaseTypeName))\r
113                                 error(h,"BaseTypeName is not a valid XmlQualifiedName");\r
114 \r
115                         if(this.AnyAttribute != null)\r
116                         {\r
117                                 errorCount += AnyAttribute.Compile(h, schema);\r
118                         }\r
119 \r
120                         foreach(XmlSchemaObject obj in Attributes)\r
121                         {\r
122                                 if(obj is XmlSchemaAttribute)\r
123                                 {\r
124                                         XmlSchemaAttribute attr = (XmlSchemaAttribute) obj;\r
125                                         errorCount += attr.Compile(h, schema);\r
126                                 }\r
127                                 else if(obj is XmlSchemaAttributeGroupRef)\r
128                                 {\r
129                                         XmlSchemaAttributeGroupRef atgrp = (XmlSchemaAttributeGroupRef) obj;\r
130                                         errorCount += atgrp.Compile(h, schema);\r
131                                 }\r
132                                 else\r
133                                         error(h,obj.GetType() +" is not valid in this place::ComplexContentRestriction");\r
134                         }\r
135                         \r
136                         if(Particle != null)\r
137                         {\r
138                                 if(Particle is XmlSchemaGroupRef)\r
139                                 {\r
140                                         errorCount += ((XmlSchemaGroupRef)Particle).Compile(h, schema);\r
141                                 }\r
142                                 else if(Particle is XmlSchemaAll)\r
143                                 {\r
144                                         errorCount += ((XmlSchemaAll)Particle).Compile(h, schema);\r
145                                 }\r
146                                 else if(Particle is XmlSchemaChoice)\r
147                                 {\r
148                                         errorCount += ((XmlSchemaChoice)Particle).Compile(h, schema);\r
149                                 }\r
150                                 else if(Particle is XmlSchemaSequence)\r
151                                 {\r
152                                         errorCount += ((XmlSchemaSequence)Particle).Compile(h, schema);\r
153                                 }\r
154                                 else\r
155                                         error (h, "Particle of a restriction is limited only to group, sequence, choice and all.");\r
156                         }\r
157                         \r
158                         XmlSchemaUtil.CompileID(Id,this, schema.IDCollection,h);\r
159 \r
160                         this.CompilationId = schema.CompilationId;\r
161                         return errorCount;\r
162                 }\r
163                 \r
164                 internal override XmlQualifiedName GetBaseTypeName ()\r
165                 {\r
166                         return baseTypeName;\r
167                 }\r
168 \r
169                 internal override XmlSchemaParticle GetParticle ()\r
170                 {\r
171                         return particle;\r
172                 }\r
173 \r
174                 internal override int Validate (ValidationEventHandler h, XmlSchema schema)\r
175                 {\r
176                         return errorCount;\r
177                 }\r
178 \r
179                 //<restriction\r
180                 //  base = QName\r
181                 //  id = ID\r
182                 //  {any attributes with non-schema namespace . . .}>\r
183                 //  Content: (annotation?, ((group | all | choice | sequence)?, ((attribute | attributeGroup)*, anyAttribute?)))\r
184                 //</restriction>\r
185                 internal static XmlSchemaComplexContentRestriction Read(XmlSchemaReader reader, ValidationEventHandler h)\r
186                 {\r
187                         XmlSchemaComplexContentRestriction restriction = new XmlSchemaComplexContentRestriction();\r
188                         reader.MoveToElement();\r
189 \r
190                         if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)\r
191                         {\r
192                                 error(h,"Should not happen :1: XmlSchemaComplexContentRestriction.Read, name="+reader.Name,null);\r
193                                 reader.Skip();\r
194                                 return null;\r
195                         }\r
196 \r
197                         restriction.LineNumber = reader.LineNumber;\r
198                         restriction.LinePosition = reader.LinePosition;\r
199                         restriction.SourceUri = reader.BaseURI;\r
200 \r
201                         while(reader.MoveToNextAttribute())\r
202                         {\r
203                                 if(reader.Name == "base")\r
204                                 {\r
205                                         Exception innerex;\r
206                                         restriction.baseTypeName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);\r
207                                         if(innerex != null)\r
208                                                 error(h, reader.Value + " is not a valid value for base attribute",innerex);\r
209                                 }\r
210                                 else if(reader.Name == "id")\r
211                                 {\r
212                                         restriction.Id = reader.Value;\r
213                                 }\r
214                                 else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)\r
215                                 {\r
216                                         error(h,reader.Name + " is not a valid attribute for restriction",null);\r
217                                 }\r
218                                 else\r
219                                 {\r
220                                         XmlSchemaUtil.ReadUnhandledAttribute(reader,restriction);\r
221                                 }\r
222                         }\r
223                         \r
224                         reader.MoveToElement();\r
225                         if(reader.IsEmptyElement)\r
226                                 return restriction;\r
227                         //Content: 1. annotation?, \r
228                         //                      (2.(group | all | choice | sequence)?, (3.(attribute | attributeGroup)*, 4.anyAttribute?)))\r
229                         int level = 1;\r
230                         while(reader.ReadNextElement())\r
231                         {\r
232                                 if(reader.NodeType == XmlNodeType.EndElement)\r
233                                 {\r
234                                         if(reader.LocalName != xmlname)\r
235                                                 error(h,"Should not happen :2: XmlSchemaComplexContentRestriction.Read, name="+reader.Name,null);\r
236                                         break;\r
237                                 }\r
238                                 if(level <= 1 && reader.LocalName == "annotation")\r
239                                 {\r
240                                         level = 2; //Only one annotation\r
241                                         XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);\r
242                                         if(annotation != null)\r
243                                                 restriction.Annotation = annotation;\r
244                                         continue;\r
245                                 }\r
246                                 if(level <= 2)\r
247                                 {\r
248                                         if(reader.LocalName == "group")\r
249                                         {\r
250                                                 level = 3;\r
251                                                 XmlSchemaGroupRef group = XmlSchemaGroupRef.Read(reader,h);\r
252                                                 if(group != null)\r
253                                                         restriction.particle = group;\r
254                                                 continue;\r
255                                         }\r
256                                         if(reader.LocalName == "all")\r
257                                         {\r
258                                                 level = 3;\r
259                                                 XmlSchemaAll all = XmlSchemaAll.Read(reader,h);\r
260                                                 if(all != null)\r
261                                                         restriction.particle = all;\r
262                                                 continue;\r
263                                         }\r
264                                         if(reader.LocalName == "choice")\r
265                                         {\r
266                                                 level = 3;\r
267                                                 XmlSchemaChoice choice = XmlSchemaChoice.Read(reader,h);\r
268                                                 if(choice != null)\r
269                                                         restriction.particle = choice;\r
270                                                 continue;\r
271                                         }\r
272                                         if(reader.LocalName == "sequence")\r
273                                         {\r
274                                                 level = 3;\r
275                                                 XmlSchemaSequence sequence = XmlSchemaSequence.Read(reader,h);\r
276                                                 if(sequence != null)\r
277                                                         restriction.particle = sequence;\r
278                                                 continue;\r
279                                         }\r
280                                 }\r
281                                 if(level <= 3)\r
282                                 {\r
283                                         if(reader.LocalName == "attribute")\r
284                                         {\r
285                                                 level = 3;\r
286                                                 XmlSchemaAttribute attr = XmlSchemaAttribute.Read(reader,h);\r
287                                                 if(attr != null)\r
288                                                         restriction.Attributes.Add(attr);\r
289                                                 continue;\r
290                                         }\r
291                                         if(reader.LocalName == "attributeGroup")\r
292                                         {\r
293                                                 level = 3;\r
294                                                 XmlSchemaAttributeGroupRef attr = XmlSchemaAttributeGroupRef.Read(reader,h);\r
295                                                 if(attr != null)\r
296                                                         restriction.attributes.Add(attr);\r
297                                                 continue;\r
298                                         }\r
299                                 }\r
300                                 if(level <= 4 && reader.LocalName == "anyAttribute")\r
301                                 {\r
302                                         level = 5;\r
303                                         XmlSchemaAnyAttribute anyattr = XmlSchemaAnyAttribute.Read(reader,h);\r
304                                         if(anyattr != null)\r
305                                                 restriction.AnyAttribute = anyattr;\r
306                                         continue;\r
307                                 }\r
308                                 reader.RaiseInvalidElementError();\r
309                         }\r
310                         return restriction;\r
311                 }\r
312         }\r
313 }\r