**** Merged from MCS ****
[mono-project.git] / mcs / class / System.XML / System.Xml.Schema / XmlSchemaComplexContentExtension.cs
blob3f1196739f8b5e0bb262dbfa1c4942002f45c3b8
1 //
2 // System.Xml.Schema.XmlSchemaComplexContentExtension.cs
3 //
4 // Author:
5 // Dwivedi, Ajay kumar Adwiv@Yahoo.com
6 // Atsushi Enomoto ginga@kit.hi-ho.ne.jp
7 //
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.
29 using System;
30 using System.Xml;
31 using System.Xml.Serialization;
33 namespace System.Xml.Schema
35 /// <summary>
36 /// Summary description for XmlSchemaComplexContentExtension.
37 /// </summary>
38 public class XmlSchemaComplexContentExtension : XmlSchemaContent
40 private XmlSchemaAnyAttribute any;
41 private XmlSchemaObjectCollection attributes;
42 private XmlQualifiedName baseTypeName;
43 private XmlSchemaParticle particle;
44 const string xmlname = "extension";
46 public XmlSchemaComplexContentExtension()
48 attributes = new XmlSchemaObjectCollection();
49 baseTypeName = XmlQualifiedName.Empty;
52 [System.Xml.Serialization.XmlAttribute("base")]
53 public XmlQualifiedName BaseTypeName
55 get{ return baseTypeName; }
56 set{ baseTypeName = value; }
59 [XmlElement("group",typeof(XmlSchemaGroupRef),Namespace=XmlSchema.Namespace)]
60 [XmlElement("all",typeof(XmlSchemaAll),Namespace=XmlSchema.Namespace)]
61 [XmlElement("choice",typeof(XmlSchemaChoice),Namespace=XmlSchema.Namespace)]
62 [XmlElement("sequence",typeof(XmlSchemaSequence),Namespace=XmlSchema.Namespace)]
63 public XmlSchemaParticle Particle
65 get{ return particle; }
66 set{ particle = value; }
69 [XmlElement("attribute",typeof(XmlSchemaAttribute),Namespace=XmlSchema.Namespace)]
70 [XmlElement("attributeGroup",typeof(XmlSchemaAttributeGroupRef),Namespace=XmlSchema.Namespace)]
71 public XmlSchemaObjectCollection Attributes
73 get{ return attributes; }
76 [XmlElement("anyAttribute",Namespace=XmlSchema.Namespace)]
77 public XmlSchemaAnyAttribute AnyAttribute
79 get{ return any; }
80 set{ any = value;}
83 // internal properties
84 internal override bool IsExtension {
85 get { return true; }
88 /// <remarks>
89 /// </remarks>
90 internal override int Compile(ValidationEventHandler h, XmlSchema schema)
92 // If this is already compiled this time, simply skip.
93 if (this.IsComplied (schema.CompilationId))
94 return 0;
96 if (this.isRedefinedComponent) {
97 if (Annotation != null)
98 Annotation.isRedefinedComponent = true;
99 if (AnyAttribute != null)
100 AnyAttribute.isRedefinedComponent = true;
101 foreach (XmlSchemaObject obj in Attributes)
102 obj.isRedefinedComponent = true;
103 if (Particle != null)
104 Particle.isRedefinedComponent = true;
107 if(BaseTypeName == null || BaseTypeName.IsEmpty)
109 error(h, "base must be present, as a QName");
111 else if(!XmlSchemaUtil.CheckQName(BaseTypeName))
112 error(h,"BaseTypeName is not a valid XmlQualifiedName");
114 if(this.AnyAttribute != null)
116 errorCount += AnyAttribute.Compile(h, schema);
119 foreach(XmlSchemaObject obj in Attributes)
121 if(obj is XmlSchemaAttribute)
123 XmlSchemaAttribute attr = (XmlSchemaAttribute) obj;
124 errorCount += attr.Compile(h, schema);
126 else if(obj is XmlSchemaAttributeGroupRef)
128 XmlSchemaAttributeGroupRef atgrp = (XmlSchemaAttributeGroupRef) obj;
129 errorCount += atgrp.Compile(h, schema);
131 else
132 error(h,obj.GetType() +" is not valid in this place::ComplexConetnetExtension");
135 if(Particle != null)
137 if(Particle is XmlSchemaGroupRef)
139 errorCount += ((XmlSchemaGroupRef)Particle).Compile(h, schema);
141 else if(Particle is XmlSchemaAll)
143 errorCount += ((XmlSchemaAll)Particle).Compile(h, schema);
145 else if(Particle is XmlSchemaChoice)
147 errorCount += ((XmlSchemaChoice)Particle).Compile(h, schema);
149 else if(Particle is XmlSchemaSequence)
151 errorCount += ((XmlSchemaSequence)Particle).Compile(h, schema);
153 else
154 error (h, "Particle of a restriction is limited only to group, sequence, choice and all.");
157 XmlSchemaUtil.CompileID(Id,this, schema.IDCollection,h);
159 this.CompilationId = schema.CompilationId;
160 return errorCount;
163 internal override XmlQualifiedName GetBaseTypeName ()
165 return baseTypeName;
168 internal override XmlSchemaParticle GetParticle ()
170 return particle;
173 internal override int Validate(ValidationEventHandler h, XmlSchema schema)
175 if (IsValidated (schema.ValidationId))
176 return errorCount;
178 if (AnyAttribute != null)
179 errorCount += AnyAttribute.Validate (h, schema);
180 foreach (XmlSchemaObject attrObj in Attributes)
181 errorCount += attrObj.Validate (h, schema);
182 if (Particle != null)
183 errorCount += Particle.Validate (h, schema);
185 ValidationId = schema.ValidationId;
186 return errorCount;
188 //<extension
189 // base = QName
190 // id = ID
191 // {any attributes with non-schema namespace . . .}>
192 // Content: (annotation?, ((group | all | choice | sequence)?, ((attribute | attributeGroup)*, anyAttribute?)))
193 //</extension>
194 internal static XmlSchemaComplexContentExtension Read(XmlSchemaReader reader, ValidationEventHandler h)
196 XmlSchemaComplexContentExtension extension = new XmlSchemaComplexContentExtension();
197 reader.MoveToElement();
199 if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
201 error(h,"Should not happen :1: XmlSchemaComplexContentExtension.Read, name="+reader.Name,null);
202 reader.Skip();
203 return null;
206 extension.LineNumber = reader.LineNumber;
207 extension.LinePosition = reader.LinePosition;
208 extension.SourceUri = reader.BaseURI;
210 while(reader.MoveToNextAttribute())
212 if(reader.Name == "base")
214 Exception innerex;
215 extension.baseTypeName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);
216 if(innerex != null)
217 error(h, reader.Value + " is not a valid value for base attribute",innerex);
219 else if(reader.Name == "id")
221 extension.Id = reader.Value;
223 else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
225 error(h,reader.Name + " is not a valid attribute for extension",null);
227 else
229 XmlSchemaUtil.ReadUnhandledAttribute(reader,extension);
233 reader.MoveToElement();
234 if(reader.IsEmptyElement)
235 return extension;
236 //Content: 1. annotation?,
237 // (2.(group | all | choice | sequence)?, (3.(attribute | attributeGroup)*, 4.anyAttribute?)))
238 int level = 1;
239 while(reader.ReadNextElement())
241 if(reader.NodeType == XmlNodeType.EndElement)
243 if(reader.LocalName != xmlname)
244 error(h,"Should not happen :2: XmlSchemaComplexContentExtension.Read, name="+reader.Name,null);
245 break;
247 if(level <= 1 && reader.LocalName == "annotation")
249 level = 2; //Only one annotation
250 XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
251 if(annotation != null)
252 extension.Annotation = annotation;
253 continue;
255 if(level <= 2)
257 if(reader.LocalName == "group")
259 level = 3;
260 XmlSchemaGroupRef group = XmlSchemaGroupRef.Read(reader,h);
261 if(group != null)
262 extension.particle = group;
263 continue;
265 if(reader.LocalName == "all")
267 level = 3;
268 XmlSchemaAll all = XmlSchemaAll.Read(reader,h);
269 if(all != null)
270 extension.particle = all;
271 continue;
273 if(reader.LocalName == "choice")
275 level = 3;
276 XmlSchemaChoice choice = XmlSchemaChoice.Read(reader,h);
277 if(choice != null)
278 extension.particle = choice;
279 continue;
281 if(reader.LocalName == "sequence")
283 level = 3;
284 XmlSchemaSequence sequence = XmlSchemaSequence.Read(reader,h);
285 if(sequence != null)
286 extension.particle = sequence;
287 continue;
290 if(level <= 3)
292 if(reader.LocalName == "attribute")
294 level = 3;
295 XmlSchemaAttribute attr = XmlSchemaAttribute.Read(reader,h);
296 if(attr != null)
297 extension.Attributes.Add(attr);
298 continue;
300 if(reader.LocalName == "attributeGroup")
302 level = 3;
303 XmlSchemaAttributeGroupRef attr = XmlSchemaAttributeGroupRef.Read(reader,h);
304 if(attr != null)
305 extension.attributes.Add(attr);
306 continue;
309 if(level <= 4 && reader.LocalName == "anyAttribute")
311 level = 5;
312 XmlSchemaAnyAttribute anyattr = XmlSchemaAnyAttribute.Read(reader,h);
313 if(anyattr != null)
314 extension.AnyAttribute = anyattr;
315 continue;
317 reader.RaiseInvalidElementError();
319 return extension;