copying the latest Sys.Web.Services from trunk.
[mono.git] / mcs / class / System.XML / System.Xml.Schema / XmlSchemaComplexContentExtension.cs
1 //\r
2 // System.Xml.Schema.XmlSchemaComplexContentExtension.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 XmlSchemaComplexContentExtension.\r
37         /// </summary>\r
38         public class XmlSchemaComplexContentExtension : 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 = "extension";\r
45 \r
46                 public XmlSchemaComplexContentExtension()\r
47                 {\r
48                         attributes = new XmlSchemaObjectCollection();\r
49                         baseTypeName = XmlQualifiedName.Empty;\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))]\r
60                 [XmlElement("all",typeof(XmlSchemaAll))]\r
61                 [XmlElement("choice",typeof(XmlSchemaChoice))]\r
62                 [XmlElement("sequence",typeof(XmlSchemaSequence))]\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))]\r
70                 [XmlElement("attributeGroup",typeof(XmlSchemaAttributeGroupRef))]\r
71                 public XmlSchemaObjectCollection Attributes \r
72                 {\r
73                         get{ return attributes; }\r
74                 }\r
75 \r
76                 [XmlElement("anyAttribute")]\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 true; }\r
86                 }\r
87 \r
88                 /// <remarks>\r
89                 /// </remarks>\r
90                 internal override int Compile(ValidationEventHandler h, XmlSchema schema)\r
91                 {\r
92                         // If this is already compiled this time, simply skip.\r
93                         if (CompilationId == schema.CompilationId)\r
94                                 return 0;\r
95 \r
96 #if NET_2_0\r
97                         if (Particle != null)\r
98                                 Particle.Parent = this;\r
99                         if (AnyAttribute != null)\r
100                                 AnyAttribute.Parent = this;\r
101                         foreach (XmlSchemaObject obj in Attributes)\r
102                                 obj.Parent = this;\r
103 #endif\r
104 \r
105                         if (this.isRedefinedComponent) {\r
106                                 if (Annotation != null)\r
107                                         Annotation.isRedefinedComponent = true;\r
108                                 if (AnyAttribute != null)\r
109                                         AnyAttribute.isRedefinedComponent  = true;\r
110                                 foreach (XmlSchemaObject obj in Attributes)\r
111                                         obj.isRedefinedComponent  = true;\r
112                                 if (Particle != null)\r
113                                         Particle.isRedefinedComponent  = true;\r
114                         }\r
115 \r
116                         if(BaseTypeName == null || BaseTypeName.IsEmpty)\r
117                         {\r
118                                 error(h, "base must be present, as a QName");\r
119                         }\r
120                         else if(!XmlSchemaUtil.CheckQName(BaseTypeName))\r
121                                 error(h,"BaseTypeName is not a valid XmlQualifiedName");\r
122                         \r
123                         if(this.AnyAttribute != null)\r
124                         {\r
125                                 errorCount += AnyAttribute.Compile(h, schema);\r
126                         }\r
127 \r
128                         foreach(XmlSchemaObject obj in Attributes)\r
129                         {\r
130                                 if(obj is XmlSchemaAttribute)\r
131                                 {\r
132                                         XmlSchemaAttribute attr = (XmlSchemaAttribute) obj;\r
133                                         errorCount += attr.Compile(h, schema);\r
134                                 }\r
135                                 else if(obj is XmlSchemaAttributeGroupRef)\r
136                                 {\r
137                                         XmlSchemaAttributeGroupRef atgrp = (XmlSchemaAttributeGroupRef) obj;\r
138                                         errorCount += atgrp.Compile(h, schema);\r
139                                 }\r
140                                 else\r
141                                         error(h,obj.GetType() +" is not valid in this place::ComplexConetnetExtension");\r
142                         }\r
143                         \r
144                         if(Particle != null)\r
145                         {\r
146                                 if(Particle is XmlSchemaGroupRef)\r
147                                 {\r
148                                         errorCount += ((XmlSchemaGroupRef)Particle).Compile(h, schema);\r
149                                 }\r
150                                 else if(Particle is XmlSchemaAll)\r
151                                 {\r
152                                         errorCount += ((XmlSchemaAll)Particle).Compile(h, schema);\r
153                                 }\r
154                                 else if(Particle is XmlSchemaChoice)\r
155                                 {\r
156                                         errorCount += ((XmlSchemaChoice)Particle).Compile(h, schema);\r
157                                 }\r
158                                 else if(Particle is XmlSchemaSequence)\r
159                                 {\r
160                                         errorCount += ((XmlSchemaSequence)Particle).Compile(h, schema);\r
161                                 }\r
162                                 else\r
163                                         error (h, "Particle of a restriction is limited only to group, sequence, choice and all.");\r
164                         }\r
165                         \r
166                         XmlSchemaUtil.CompileID(Id,this, schema.IDCollection,h);\r
167 \r
168                         this.CompilationId = schema.CompilationId;\r
169                         return errorCount;\r
170                 }\r
171                 \r
172                 internal override XmlQualifiedName GetBaseTypeName ()\r
173                 {\r
174                         return baseTypeName;\r
175                 }\r
176 \r
177                 internal override XmlSchemaParticle GetParticle ()\r
178                 {\r
179                         return particle;\r
180                 }\r
181 \r
182                 internal override int Validate(ValidationEventHandler h, XmlSchema schema)\r
183                 {\r
184                         if (IsValidated (schema.ValidationId))\r
185                                 return errorCount;\r
186 \r
187                         if (AnyAttribute != null)\r
188                                 errorCount += AnyAttribute.Validate (h, schema);\r
189                         foreach (XmlSchemaObject attrObj in Attributes)\r
190                                 errorCount += attrObj.Validate (h, schema);\r
191                         if (Particle != null)\r
192                                 errorCount += Particle.Validate (h, schema);\r
193 \r
194                         ValidationId = schema.ValidationId;\r
195                         return errorCount;\r
196                 }\r
197                 //<extension\r
198                 //  base = QName\r
199                 //  id = ID\r
200                 //  {any attributes with non-schema namespace . . .}>\r
201                 //  Content: (annotation?, ((group | all | choice | sequence)?, ((attribute | attributeGroup)*, anyAttribute?)))\r
202                 //</extension>\r
203                 internal static XmlSchemaComplexContentExtension Read(XmlSchemaReader reader, ValidationEventHandler h)\r
204                 {\r
205                         XmlSchemaComplexContentExtension extension = new XmlSchemaComplexContentExtension();\r
206                         reader.MoveToElement();\r
207 \r
208                         if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)\r
209                         {\r
210                                 error(h,"Should not happen :1: XmlSchemaComplexContentExtension.Read, name="+reader.Name,null);\r
211                                 reader.Skip();\r
212                                 return null;\r
213                         }\r
214 \r
215                         extension.LineNumber = reader.LineNumber;\r
216                         extension.LinePosition = reader.LinePosition;\r
217                         extension.SourceUri = reader.BaseURI;\r
218 \r
219                         while(reader.MoveToNextAttribute())\r
220                         {\r
221                                 if(reader.Name == "base")\r
222                                 {\r
223                                         Exception innerex;\r
224                                         extension.baseTypeName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);\r
225                                         if(innerex != null)\r
226                                                 error(h, reader.Value + " is not a valid value for base attribute",innerex);\r
227                                 }\r
228                                 else if(reader.Name == "id")\r
229                                 {\r
230                                         extension.Id = reader.Value;\r
231                                 }\r
232                                 else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)\r
233                                 {\r
234                                         error(h,reader.Name + " is not a valid attribute for extension",null);\r
235                                 }\r
236                                 else\r
237                                 {\r
238                                         XmlSchemaUtil.ReadUnhandledAttribute(reader,extension);\r
239                                 }\r
240                         }\r
241                         \r
242                         reader.MoveToElement();\r
243                         if(reader.IsEmptyElement)\r
244                                 return extension;\r
245                         //Content: 1. annotation?, \r
246                         //                      (2.(group | all | choice | sequence)?, (3.(attribute | attributeGroup)*, 4.anyAttribute?)))\r
247                         int level = 1;\r
248                         while(reader.ReadNextElement())\r
249                         {\r
250                                 if(reader.NodeType == XmlNodeType.EndElement)\r
251                                 {\r
252                                         if(reader.LocalName != xmlname)\r
253                                                 error(h,"Should not happen :2: XmlSchemaComplexContentExtension.Read, name="+reader.Name,null);\r
254                                         break;\r
255                                 }\r
256                                 if(level <= 1 && reader.LocalName == "annotation")\r
257                                 {\r
258                                         level = 2; //Only one annotation\r
259                                         XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);\r
260                                         if(annotation != null)\r
261                                                 extension.Annotation = annotation;\r
262                                         continue;\r
263                                 }\r
264                                 if(level <= 2)\r
265                                 {\r
266                                         if(reader.LocalName == "group")\r
267                                         {\r
268                                                 level = 3;\r
269                                                 XmlSchemaGroupRef group = XmlSchemaGroupRef.Read(reader,h);\r
270                                                 if(group != null)\r
271                                                         extension.particle = group;\r
272                                                 continue;\r
273                                         }\r
274                                         if(reader.LocalName == "all")\r
275                                         {\r
276                                                 level = 3;\r
277                                                 XmlSchemaAll all = XmlSchemaAll.Read(reader,h);\r
278                                                 if(all != null)\r
279                                                         extension.particle = all;\r
280                                                 continue;\r
281                                         }\r
282                                         if(reader.LocalName == "choice")\r
283                                         {\r
284                                                 level = 3;\r
285                                                 XmlSchemaChoice choice = XmlSchemaChoice.Read(reader,h);\r
286                                                 if(choice != null)\r
287                                                         extension.particle = choice;\r
288                                                 continue;\r
289                                         }\r
290                                         if(reader.LocalName == "sequence")\r
291                                         {\r
292                                                 level = 3;\r
293                                                 XmlSchemaSequence sequence = XmlSchemaSequence.Read(reader,h);\r
294                                                 if(sequence != null)\r
295                                                         extension.particle = sequence;\r
296                                                 continue;\r
297                                         }\r
298                                 }\r
299                                 if(level <= 3)\r
300                                 {\r
301                                         if(reader.LocalName == "attribute")\r
302                                         {\r
303                                                 level = 3;\r
304                                                 XmlSchemaAttribute attr = XmlSchemaAttribute.Read(reader,h);\r
305                                                 if(attr != null)\r
306                                                         extension.Attributes.Add(attr);\r
307                                                 continue;\r
308                                         }\r
309                                         if(reader.LocalName == "attributeGroup")\r
310                                         {\r
311                                                 level = 3;\r
312                                                 XmlSchemaAttributeGroupRef attr = XmlSchemaAttributeGroupRef.Read(reader,h);\r
313                                                 if(attr != null)\r
314                                                         extension.attributes.Add(attr);\r
315                                                 continue;\r
316                                         }\r
317                                 }\r
318                                 if(level <= 4 && reader.LocalName == "anyAttribute")\r
319                                 {\r
320                                         level = 5;\r
321                                         XmlSchemaAnyAttribute anyattr = XmlSchemaAnyAttribute.Read(reader,h);\r
322                                         if(anyattr != null)\r
323                                                 extension.AnyAttribute = anyattr;\r
324                                         continue;\r
325                                 }\r
326                                 reader.RaiseInvalidElementError();\r
327                         }\r
328                         return extension;\r
329                 }\r
330         }\r
331 }\r