2010-04-06 Jb Evain <jbevain@novell.com>
[mcs.git] / class / System.XML / System.Xml.Schema / XmlSchemaAttributeGroup.cs
blob5922b8c1e907ce2bc4b5e6d54b6234c8064eab37
1 //
2 // System.Xml.Schema.XmlSchemaAttributeGroup.cs
3 //
4 // Authors:
5 // Dwivedi, Ajay kumar Adwiv@Yahoo.com
6 // Enomoto, Atsushi 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.Collections;
31 using System.Xml.Serialization;
32 using System.Xml;
34 namespace System.Xml.Schema
36 /// <summary>
37 /// Summary description for XmlSchemaAttributeGroup.
38 /// </summary>
39 public class XmlSchemaAttributeGroup : XmlSchemaAnnotated
41 private XmlSchemaAnyAttribute anyAttribute;
42 private XmlSchemaObjectCollection attributes;
43 private string name;
44 private XmlSchemaAttributeGroup redefined;
45 private XmlQualifiedName qualifiedName;
46 const string xmlname = "attributeGroup";
47 private XmlSchemaObjectTable attributeUses;
48 private XmlSchemaAnyAttribute anyAttributeUse;
50 internal bool AttributeGroupRecursionCheck;
52 public XmlSchemaAttributeGroup()
54 attributes = new XmlSchemaObjectCollection();
55 qualifiedName = XmlQualifiedName.Empty;
58 [System.Xml.Serialization.XmlAttribute("name")]
59 public string Name
61 get{ return name;}
62 set{ name = value;}
65 [XmlElement("attribute",typeof(XmlSchemaAttribute))]
66 [XmlElement("attributeGroup",typeof(XmlSchemaAttributeGroupRef))]
67 public XmlSchemaObjectCollection Attributes
69 get{ return attributes;}
72 internal XmlSchemaObjectTable AttributeUses
74 get { return attributeUses; }
76 internal XmlSchemaAnyAttribute AnyAttributeUse
78 get { return anyAttributeUse; }
81 [XmlElement("anyAttribute")]
82 public XmlSchemaAnyAttribute AnyAttribute
84 get{ return anyAttribute;}
85 set{ anyAttribute = value;}
88 //Undocumented property
89 [XmlIgnore]
90 public XmlSchemaAttributeGroup RedefinedAttributeGroup
92 get{ return redefined;}
95 [XmlIgnore]
96 #if NET_2_0
97 public XmlQualifiedName QualifiedName
98 #else
99 internal XmlQualifiedName QualifiedName
100 #endif
102 get{ return qualifiedName;}
105 internal override void SetParent (XmlSchemaObject parent)
107 base.SetParent (parent);
108 if (this.AnyAttribute != null)
109 this.AnyAttribute.SetParent (this);
110 foreach (XmlSchemaObject obj in Attributes)
111 obj.SetParent (this);
114 /// <remarks>
115 /// An Attribute group can only be defined as a child of XmlSchema or in XmlSchemaRedefine.
116 /// The other attributeGroup has type XmlSchemaAttributeGroupRef.
117 /// 1. Name must be present
118 /// </remarks>
119 internal override int Compile(ValidationEventHandler h, XmlSchema schema)
121 // If this is already compiled this time, simply skip.
122 if (CompilationId == schema.CompilationId)
123 return errorCount;
125 errorCount = 0;
127 if (redefinedObject != null) {
128 errorCount += redefined.Compile (h, schema);
129 if (errorCount == 0)
130 redefined = (XmlSchemaAttributeGroup) redefinedObject;
133 XmlSchemaUtil.CompileID(Id,this, schema.IDCollection,h);
135 if(this.Name == null || this.Name == String.Empty) //1
136 error(h,"Name is required in top level simpletype");
137 else if(!XmlSchemaUtil.CheckNCName(this.Name)) // b.1.2
138 error(h,"name attribute of a simpleType must be NCName");
139 else
140 this.qualifiedName = new XmlQualifiedName(this.Name, AncestorSchema.TargetNamespace);
142 if(this.AnyAttribute != null)
144 errorCount += this.AnyAttribute.Compile(h, schema);
147 foreach(XmlSchemaObject obj in Attributes)
149 if(obj is XmlSchemaAttribute)
151 XmlSchemaAttribute attr = (XmlSchemaAttribute) obj;
152 errorCount += attr.Compile(h, schema);
154 else if(obj is XmlSchemaAttributeGroupRef)
156 XmlSchemaAttributeGroupRef gref = (XmlSchemaAttributeGroupRef) obj;
157 errorCount += gref.Compile(h, schema);
159 else
161 error(h,"invalid type of object in Attributes property");
164 this.CompilationId = schema.CompilationId;
165 return errorCount;
168 internal override int Validate(ValidationEventHandler h, XmlSchema schema)
170 if (IsValidated (schema.CompilationId))
171 return errorCount;
173 if (redefined == null && redefinedObject != null) {
174 redefinedObject.Compile (h, schema);
175 redefined = (XmlSchemaAttributeGroup) redefinedObject;
176 redefined.Validate (h, schema);
179 XmlSchemaObjectCollection actualAttributes = null;
181 if (this.redefined != null) {
182 actualAttributes = new XmlSchemaObjectCollection ();
183 foreach (XmlSchemaObject obj in Attributes) {
184 XmlSchemaAttributeGroupRef grp = obj as XmlSchemaAttributeGroupRef;
185 if (grp != null && grp.QualifiedName == this.QualifiedName)
186 actualAttributes.Add (redefined);
187 else
188 actualAttributes.Add (obj);
191 else
193 actualAttributes = Attributes;
195 attributeUses = new XmlSchemaObjectTable ();
196 errorCount += XmlSchemaUtil.ValidateAttributesResolved (attributeUses,
197 h, schema, actualAttributes, AnyAttribute,
198 ref anyAttributeUse, redefined, false);
199 ValidationId = schema.ValidationId;
200 return errorCount;
203 //<attributeGroup
204 // id = ID
205 // name = NCName
206 // ref = QName // Not present in this class.
207 // {any attributes with non-schema namespace . . .}>
208 // Content: (annotation?, ((attribute | attributeGroup)*, anyAttribute?))
209 //</attributeGroup>
210 internal static XmlSchemaAttributeGroup Read(XmlSchemaReader reader, ValidationEventHandler h)
212 XmlSchemaAttributeGroup attrgrp = new XmlSchemaAttributeGroup();
213 reader.MoveToElement();
215 if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
217 error(h,"Should not happen :1: XmlSchemaAttributeGroup.Read, name="+reader.Name,null);
218 reader.SkipToEnd();
219 return null;
222 attrgrp.LineNumber = reader.LineNumber;
223 attrgrp.LinePosition = reader.LinePosition;
224 attrgrp.SourceUri = reader.BaseURI;
226 while(reader.MoveToNextAttribute())
228 if(reader.Name == "id")
230 attrgrp.Id = reader.Value;
232 else if(reader.Name == "name")
234 attrgrp.name = reader.Value;
236 else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
238 error(h,reader.Name + " is not a valid attribute for attributeGroup in this context",null);
240 else
242 XmlSchemaUtil.ReadUnhandledAttribute(reader,attrgrp);
246 reader.MoveToElement();
247 if(reader.IsEmptyElement)
248 return attrgrp;
250 //Content: 1.annotation?, 2.(attribute | attributeGroup)*, 3.anyAttribute?
251 int level = 1;
252 while(reader.ReadNextElement())
254 if(reader.NodeType == XmlNodeType.EndElement)
256 if(reader.LocalName != xmlname)
257 error(h,"Should not happen :2: XmlSchemaAttributeGroup.Read, name="+reader.Name,null);
258 break;
260 if(level <= 1 && reader.LocalName == "annotation")
262 level = 2; //Only one annotation
263 XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
264 if(annotation != null)
265 attrgrp.Annotation = annotation;
266 continue;
268 if(level <= 2)
270 if(reader.LocalName == "attribute")
272 level = 2;
273 XmlSchemaAttribute attr = XmlSchemaAttribute.Read(reader,h);
274 if(attr != null)
275 attrgrp.Attributes.Add(attr);
276 continue;
278 if(reader.LocalName == "attributeGroup")
280 level = 2;
281 XmlSchemaAttributeGroupRef attr = XmlSchemaAttributeGroupRef.Read(reader,h);
282 if(attr != null)
283 attrgrp.attributes.Add(attr);
284 continue;
287 if(level <= 3 && reader.LocalName == "anyAttribute")
289 level = 4;
290 XmlSchemaAnyAttribute anyattr = XmlSchemaAnyAttribute.Read(reader,h);
291 if(anyattr != null)
292 attrgrp.AnyAttribute = anyattr;
293 continue;
295 reader.RaiseInvalidElementError();
297 return attrgrp;