2010-04-06 Jb Evain <jbevain@novell.com>
[mcs.git] / class / System.XML / System.Xml.Schema / XmlSchemaComplexContentRestriction.cs
blob8f7d112e08f09db5413200f1bae48aa596f7d1e8
1 //
2 // System.Xml.Schema.XmlSchemaComplexContentRestriction.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 XmlSchemaComplexContentRestriction.
37 /// </summary>
38 public class XmlSchemaComplexContentRestriction : XmlSchemaContent
40 private XmlSchemaAnyAttribute any;
41 private XmlSchemaObjectCollection attributes;
42 private XmlQualifiedName baseTypeName;
43 private XmlSchemaParticle particle;
44 const string xmlname = "restriction";
46 public XmlSchemaComplexContentRestriction()
48 baseTypeName = XmlQualifiedName.Empty;
49 attributes = new XmlSchemaObjectCollection();
52 [System.Xml.Serialization.XmlAttribute("base")]
53 public XmlQualifiedName BaseTypeName
55 get{ return baseTypeName; }
56 set{ baseTypeName = value; }
59 [XmlElement("group",typeof(XmlSchemaGroupRef))]
60 [XmlElement("all",typeof(XmlSchemaAll))]
61 [XmlElement("choice",typeof(XmlSchemaChoice))]
62 [XmlElement("sequence",typeof(XmlSchemaSequence))]
63 public XmlSchemaParticle Particle
65 get{ return particle; }
66 set{ particle = value; }
69 [XmlElement("attribute",typeof(XmlSchemaAttribute))]
70 [XmlElement("attributeGroup",typeof(XmlSchemaAttributeGroupRef))]
71 public XmlSchemaObjectCollection Attributes
73 get{ return attributes; }
76 [XmlElement("anyAttribute")]
77 public XmlSchemaAnyAttribute AnyAttribute
79 get{ return any; }
80 set{ any = value; }
83 // internal properties
84 internal override bool IsExtension {
85 get { return false; }
88 internal override void SetParent (XmlSchemaObject parent)
90 base.SetParent (parent);
91 if (Particle != null)
92 Particle.SetParent (this);
93 if (AnyAttribute != null)
94 AnyAttribute.SetParent (this);
95 foreach (XmlSchemaObject obj in Attributes)
96 obj.SetParent (this);
99 /// <remarks>
100 /// 1. base must be present
101 /// </remarks>
102 internal override int Compile(ValidationEventHandler h, XmlSchema schema)
104 // If this is already compiled this time, simply skip.
105 if (CompilationId == schema.CompilationId)
106 return 0;
108 if (this.isRedefinedComponent) {
109 if (Annotation != null)
110 Annotation.isRedefinedComponent = true;
111 if (AnyAttribute != null)
112 AnyAttribute.isRedefinedComponent = true;
113 foreach (XmlSchemaObject obj in Attributes)
114 obj.isRedefinedComponent = true;
115 if (Particle != null)
116 Particle.isRedefinedComponent = true;
119 if(BaseTypeName == null || BaseTypeName.IsEmpty)
121 error(h, "base must be present, as a QName");
123 else if(!XmlSchemaUtil.CheckQName(BaseTypeName))
124 error(h,"BaseTypeName is not a valid XmlQualifiedName");
126 if(this.AnyAttribute != null)
128 errorCount += AnyAttribute.Compile(h, schema);
131 foreach(XmlSchemaObject obj in Attributes)
133 if(obj is XmlSchemaAttribute)
135 XmlSchemaAttribute attr = (XmlSchemaAttribute) obj;
136 errorCount += attr.Compile(h, schema);
138 else if(obj is XmlSchemaAttributeGroupRef)
140 XmlSchemaAttributeGroupRef atgrp = (XmlSchemaAttributeGroupRef) obj;
141 errorCount += atgrp.Compile(h, schema);
143 else
144 error(h,obj.GetType() +" is not valid in this place::ComplexContentRestriction");
147 if(Particle != null)
149 if(Particle is XmlSchemaGroupRef)
151 errorCount += ((XmlSchemaGroupRef)Particle).Compile(h, schema);
153 else if(Particle is XmlSchemaAll)
155 errorCount += ((XmlSchemaAll)Particle).Compile(h, schema);
157 else if(Particle is XmlSchemaChoice)
159 errorCount += ((XmlSchemaChoice)Particle).Compile(h, schema);
161 else if(Particle is XmlSchemaSequence)
163 errorCount += ((XmlSchemaSequence)Particle).Compile(h, schema);
165 else
166 error (h, "Particle of a restriction is limited only to group, sequence, choice and all.");
169 XmlSchemaUtil.CompileID(Id,this, schema.IDCollection,h);
171 this.CompilationId = schema.CompilationId;
172 return errorCount;
175 internal override XmlQualifiedName GetBaseTypeName ()
177 return baseTypeName;
180 internal override XmlSchemaParticle GetParticle ()
182 return particle;
185 internal override int Validate (ValidationEventHandler h, XmlSchema schema)
187 if (Particle != null)
188 Particle.Validate (h, schema);
189 return errorCount;
192 //<restriction
193 // base = QName
194 // id = ID
195 // {any attributes with non-schema namespace . . .}>
196 // Content: (annotation?, ((group | all | choice | sequence)?, ((attribute | attributeGroup)*, anyAttribute?)))
197 //</restriction>
198 internal static XmlSchemaComplexContentRestriction Read(XmlSchemaReader reader, ValidationEventHandler h)
200 XmlSchemaComplexContentRestriction restriction = new XmlSchemaComplexContentRestriction();
201 reader.MoveToElement();
203 if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
205 error(h,"Should not happen :1: XmlSchemaComplexContentRestriction.Read, name="+reader.Name,null);
206 reader.Skip();
207 return null;
210 restriction.LineNumber = reader.LineNumber;
211 restriction.LinePosition = reader.LinePosition;
212 restriction.SourceUri = reader.BaseURI;
214 while(reader.MoveToNextAttribute())
216 if(reader.Name == "base")
218 Exception innerex;
219 restriction.baseTypeName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);
220 if(innerex != null)
221 error(h, reader.Value + " is not a valid value for base attribute",innerex);
223 else if(reader.Name == "id")
225 restriction.Id = reader.Value;
227 else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
229 error(h,reader.Name + " is not a valid attribute for restriction",null);
231 else
233 XmlSchemaUtil.ReadUnhandledAttribute(reader,restriction);
237 reader.MoveToElement();
238 if(reader.IsEmptyElement)
239 return restriction;
240 //Content: 1. annotation?,
241 // (2.(group | all | choice | sequence)?, (3.(attribute | attributeGroup)*, 4.anyAttribute?)))
242 int level = 1;
243 while(reader.ReadNextElement())
245 if(reader.NodeType == XmlNodeType.EndElement)
247 if(reader.LocalName != xmlname)
248 error(h,"Should not happen :2: XmlSchemaComplexContentRestriction.Read, name="+reader.Name,null);
249 break;
251 if(level <= 1 && reader.LocalName == "annotation")
253 level = 2; //Only one annotation
254 XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
255 if(annotation != null)
256 restriction.Annotation = annotation;
257 continue;
259 if(level <= 2)
261 if(reader.LocalName == "group")
263 level = 3;
264 XmlSchemaGroupRef group = XmlSchemaGroupRef.Read(reader,h);
265 if(group != null)
266 restriction.particle = group;
267 continue;
269 if(reader.LocalName == "all")
271 level = 3;
272 XmlSchemaAll all = XmlSchemaAll.Read(reader,h);
273 if(all != null)
274 restriction.particle = all;
275 continue;
277 if(reader.LocalName == "choice")
279 level = 3;
280 XmlSchemaChoice choice = XmlSchemaChoice.Read(reader,h);
281 if(choice != null)
282 restriction.particle = choice;
283 continue;
285 if(reader.LocalName == "sequence")
287 level = 3;
288 XmlSchemaSequence sequence = XmlSchemaSequence.Read(reader,h);
289 if(sequence != null)
290 restriction.particle = sequence;
291 continue;
294 if(level <= 3)
296 if(reader.LocalName == "attribute")
298 level = 3;
299 XmlSchemaAttribute attr = XmlSchemaAttribute.Read(reader,h);
300 if(attr != null)
301 restriction.Attributes.Add(attr);
302 continue;
304 if(reader.LocalName == "attributeGroup")
306 level = 3;
307 XmlSchemaAttributeGroupRef attr = XmlSchemaAttributeGroupRef.Read(reader,h);
308 if(attr != null)
309 restriction.attributes.Add(attr);
310 continue;
313 if(level <= 4 && reader.LocalName == "anyAttribute")
315 level = 5;
316 XmlSchemaAnyAttribute anyattr = XmlSchemaAnyAttribute.Read(reader,h);
317 if(anyattr != null)
318 restriction.AnyAttribute = anyattr;
319 continue;
321 reader.RaiseInvalidElementError();
323 return restriction;