**** Merged from MCS ****
[mono-project.git] / mcs / class / System.XML / System.Xml.Schema / XmlSchemaComplexContentRestriction.cs
blob08a56feb6e4dc5c47ec0dc2f87936555238444fa
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),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 false; }
88 /// <remarks>
89 /// 1. base must be present
90 /// </remarks>
91 internal override int Compile(ValidationEventHandler h, XmlSchema schema)
93 // If this is already compiled this time, simply skip.
94 if (this.IsComplied (schema.CompilationId))
95 return 0;
97 if (this.isRedefinedComponent) {
98 if (Annotation != null)
99 Annotation.isRedefinedComponent = true;
100 if (AnyAttribute != null)
101 AnyAttribute.isRedefinedComponent = true;
102 foreach (XmlSchemaObject obj in Attributes)
103 obj.isRedefinedComponent = true;
104 if (Particle != null)
105 Particle.isRedefinedComponent = true;
108 if(BaseTypeName == null || BaseTypeName.IsEmpty)
110 error(h, "base must be present, as a QName");
112 else if(!XmlSchemaUtil.CheckQName(BaseTypeName))
113 error(h,"BaseTypeName is not a valid XmlQualifiedName");
115 if(this.AnyAttribute != null)
117 errorCount += AnyAttribute.Compile(h, schema);
120 foreach(XmlSchemaObject obj in Attributes)
122 if(obj is XmlSchemaAttribute)
124 XmlSchemaAttribute attr = (XmlSchemaAttribute) obj;
125 errorCount += attr.Compile(h, schema);
127 else if(obj is XmlSchemaAttributeGroupRef)
129 XmlSchemaAttributeGroupRef atgrp = (XmlSchemaAttributeGroupRef) obj;
130 errorCount += atgrp.Compile(h, schema);
132 else
133 error(h,obj.GetType() +" is not valid in this place::ComplexContentRestriction");
136 if(Particle != null)
138 if(Particle is XmlSchemaGroupRef)
140 errorCount += ((XmlSchemaGroupRef)Particle).Compile(h, schema);
142 else if(Particle is XmlSchemaAll)
144 errorCount += ((XmlSchemaAll)Particle).Compile(h, schema);
146 else if(Particle is XmlSchemaChoice)
148 errorCount += ((XmlSchemaChoice)Particle).Compile(h, schema);
150 else if(Particle is XmlSchemaSequence)
152 errorCount += ((XmlSchemaSequence)Particle).Compile(h, schema);
154 else
155 error (h, "Particle of a restriction is limited only to group, sequence, choice and all.");
158 XmlSchemaUtil.CompileID(Id,this, schema.IDCollection,h);
160 this.CompilationId = schema.CompilationId;
161 return errorCount;
164 internal override XmlQualifiedName GetBaseTypeName ()
166 return baseTypeName;
169 internal override XmlSchemaParticle GetParticle ()
171 return particle;
174 internal override int Validate (ValidationEventHandler h, XmlSchema schema)
176 return errorCount;
179 //<restriction
180 // base = QName
181 // id = ID
182 // {any attributes with non-schema namespace . . .}>
183 // Content: (annotation?, ((group | all | choice | sequence)?, ((attribute | attributeGroup)*, anyAttribute?)))
184 //</restriction>
185 internal static XmlSchemaComplexContentRestriction Read(XmlSchemaReader reader, ValidationEventHandler h)
187 XmlSchemaComplexContentRestriction restriction = new XmlSchemaComplexContentRestriction();
188 reader.MoveToElement();
190 if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
192 error(h,"Should not happen :1: XmlSchemaComplexContentRestriction.Read, name="+reader.Name,null);
193 reader.Skip();
194 return null;
197 restriction.LineNumber = reader.LineNumber;
198 restriction.LinePosition = reader.LinePosition;
199 restriction.SourceUri = reader.BaseURI;
201 while(reader.MoveToNextAttribute())
203 if(reader.Name == "base")
205 Exception innerex;
206 restriction.baseTypeName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);
207 if(innerex != null)
208 error(h, reader.Value + " is not a valid value for base attribute",innerex);
210 else if(reader.Name == "id")
212 restriction.Id = reader.Value;
214 else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
216 error(h,reader.Name + " is not a valid attribute for restriction",null);
218 else
220 XmlSchemaUtil.ReadUnhandledAttribute(reader,restriction);
224 reader.MoveToElement();
225 if(reader.IsEmptyElement)
226 return restriction;
227 //Content: 1. annotation?,
228 // (2.(group | all | choice | sequence)?, (3.(attribute | attributeGroup)*, 4.anyAttribute?)))
229 int level = 1;
230 while(reader.ReadNextElement())
232 if(reader.NodeType == XmlNodeType.EndElement)
234 if(reader.LocalName != xmlname)
235 error(h,"Should not happen :2: XmlSchemaComplexContentRestriction.Read, name="+reader.Name,null);
236 break;
238 if(level <= 1 && reader.LocalName == "annotation")
240 level = 2; //Only one annotation
241 XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
242 if(annotation != null)
243 restriction.Annotation = annotation;
244 continue;
246 if(level <= 2)
248 if(reader.LocalName == "group")
250 level = 3;
251 XmlSchemaGroupRef group = XmlSchemaGroupRef.Read(reader,h);
252 if(group != null)
253 restriction.particle = group;
254 continue;
256 if(reader.LocalName == "all")
258 level = 3;
259 XmlSchemaAll all = XmlSchemaAll.Read(reader,h);
260 if(all != null)
261 restriction.particle = all;
262 continue;
264 if(reader.LocalName == "choice")
266 level = 3;
267 XmlSchemaChoice choice = XmlSchemaChoice.Read(reader,h);
268 if(choice != null)
269 restriction.particle = choice;
270 continue;
272 if(reader.LocalName == "sequence")
274 level = 3;
275 XmlSchemaSequence sequence = XmlSchemaSequence.Read(reader,h);
276 if(sequence != null)
277 restriction.particle = sequence;
278 continue;
281 if(level <= 3)
283 if(reader.LocalName == "attribute")
285 level = 3;
286 XmlSchemaAttribute attr = XmlSchemaAttribute.Read(reader,h);
287 if(attr != null)
288 restriction.Attributes.Add(attr);
289 continue;
291 if(reader.LocalName == "attributeGroup")
293 level = 3;
294 XmlSchemaAttributeGroupRef attr = XmlSchemaAttributeGroupRef.Read(reader,h);
295 if(attr != null)
296 restriction.attributes.Add(attr);
297 continue;
300 if(level <= 4 && reader.LocalName == "anyAttribute")
302 level = 5;
303 XmlSchemaAnyAttribute anyattr = XmlSchemaAnyAttribute.Read(reader,h);
304 if(anyattr != null)
305 restriction.AnyAttribute = anyattr;
306 continue;
308 reader.RaiseInvalidElementError();
310 return restriction;