2004-02-16 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.XML / System.Xml.Schema / XmlSchemaAttributeGroup.cs
1 //\r
2 // System.Xml.Schema.XmlSchemaAttributeGroup.cs\r
3 //\r
4 // Authors:\r
5 //      Dwivedi, Ajay kumar  Adwiv@Yahoo.com\r
6 //      Enomoto, Atsushi     ginga@kit.hi-ho.ne.jp\r
7 //\r
8 using System;\r
9 using System.Collections;\r
10 using System.Xml.Serialization;\r
11 using System.Xml;\r
12 \r
13 namespace System.Xml.Schema\r
14 {\r
15         /// <summary>\r
16         /// Summary description for XmlSchemaAttributeGroup.\r
17         /// </summary>\r
18         public class XmlSchemaAttributeGroup : XmlSchemaAnnotated\r
19         {\r
20                 private XmlSchemaAnyAttribute anyAttribute;\r
21                 private XmlSchemaObjectCollection attributes;\r
22                 private string name;\r
23                 private XmlSchemaAttributeGroup redefined;\r
24                 private XmlQualifiedName qualifiedName;\r
25                 private static string xmlname = "attributeGroup";\r
26                 private XmlSchemaObjectTable attributeUses;\r
27                 private XmlSchemaAnyAttribute anyAttributeUse;\r
28 \r
29                 internal bool AttributeGroupRecursionCheck;\r
30 \r
31                 public XmlSchemaAttributeGroup()\r
32                 {\r
33                         attributes  = new XmlSchemaObjectCollection();\r
34                 }\r
35 \r
36                 [System.Xml.Serialization.XmlAttribute("name")]\r
37                 public string Name \r
38                 {\r
39                         get{ return name;}\r
40                         set{ name = value;}\r
41                 }\r
42 \r
43                 [XmlElement("attribute",typeof(XmlSchemaAttribute),Namespace=XmlSchema.Namespace)]\r
44                 [XmlElement("attributeGroup",typeof(XmlSchemaAttributeGroupRef),Namespace=XmlSchema.Namespace)]\r
45                 public XmlSchemaObjectCollection Attributes \r
46                 {\r
47                         get{ return attributes;}\r
48                 }\r
49 \r
50                 internal XmlSchemaObjectTable AttributeUses\r
51                 {\r
52                         get { return attributeUses; }\r
53                 }\r
54                 internal XmlSchemaAnyAttribute AnyAttributeUse\r
55                 {\r
56                         get { return anyAttributeUse; }\r
57                 }\r
58 \r
59                 [XmlElement("anyAttribute",Namespace=XmlSchema.Namespace)]\r
60                 public XmlSchemaAnyAttribute AnyAttribute \r
61                 {\r
62                         get{ return anyAttribute;}\r
63                         set{ anyAttribute = value;}\r
64                 }\r
65 \r
66                 //Undocumented property\r
67                 [XmlIgnore]\r
68                 public XmlSchemaAttributeGroup RedefinedAttributeGroup \r
69                 {\r
70                         get{ return redefined;}\r
71                 }\r
72 \r
73                 [XmlIgnore]\r
74                 internal XmlQualifiedName QualifiedName \r
75                 {\r
76                         get{ return qualifiedName;}\r
77                 }\r
78 \r
79                 /// <remarks>\r
80                 /// An Attribute group can only be defined as a child of XmlSchema or in XmlSchemaRedefine.\r
81                 /// The other attributeGroup has type XmlSchemaAttributeGroupRef.\r
82                 ///  1. Name must be present\r
83                 /// </remarks>\r
84                 internal override int Compile(ValidationEventHandler h, XmlSchema schema)\r
85                 {\r
86                         // If this is already compiled this time, simply skip.\r
87                         if (this.IsComplied (schema.CompilationId))\r
88                                 return errorCount;\r
89 \r
90                         errorCount = 0;\r
91 \r
92                         if (redefinedObject != null) {\r
93                                 errorCount += redefined.Compile (h, schema);\r
94                                 if (errorCount == 0)\r
95                                         redefined = (XmlSchemaAttributeGroup) redefinedObject;\r
96                         }\r
97 \r
98                         XmlSchemaUtil.CompileID(Id,this, schema.IDCollection,h);\r
99 \r
100                         if(this.Name == null || this.Name == String.Empty) //1\r
101                                 error(h,"Name is required in top level simpletype");\r
102                         else if(!XmlSchemaUtil.CheckNCName(this.Name)) // b.1.2\r
103                                 error(h,"name attribute of a simpleType must be NCName");\r
104                         else\r
105                                 this.qualifiedName = new XmlQualifiedName(this.Name, schema.TargetNamespace);\r
106                         \r
107                         if(this.AnyAttribute != null)\r
108                         {\r
109                                 errorCount += this.AnyAttribute.Compile(h, schema);\r
110                         }\r
111                         \r
112                         foreach(XmlSchemaObject obj in Attributes)\r
113                         {\r
114                                 if(obj is XmlSchemaAttribute)\r
115                                 {\r
116                                         XmlSchemaAttribute attr = (XmlSchemaAttribute) obj;\r
117                                         errorCount += attr.Compile(h, schema);\r
118                                 }\r
119                                 else if(obj is XmlSchemaAttributeGroupRef)\r
120                                 {\r
121                                         XmlSchemaAttributeGroupRef gref = (XmlSchemaAttributeGroupRef) obj;\r
122                                         errorCount += gref.Compile(h, schema);\r
123                                 }\r
124                                 else\r
125                                 {\r
126                                         error(h,"invalid type of object in Attributes property");\r
127                                 }\r
128                         }\r
129                         this.CompilationId = schema.CompilationId;\r
130                         return errorCount;\r
131                 }\r
132 \r
133                 internal override int Validate(ValidationEventHandler h, XmlSchema schema)\r
134                 {\r
135                         if (IsValidated (schema.CompilationId))\r
136                                 return errorCount;\r
137 \r
138                         if (redefined == null && redefinedObject != null) {\r
139                                 redefinedObject.Compile (h, schema);\r
140                                 redefined = (XmlSchemaAttributeGroup) redefinedObject;\r
141                                 redefined.Validate (h, schema);\r
142                         }\r
143 \r
144                         XmlSchemaObjectCollection actualAttributes = null;\r
145                         /*\r
146                         if (this.redefined != null) {\r
147                                 actualAttributes = new XmlSchemaObjectCollection ();\r
148                                 foreach (XmlSchemaObject obj in Attributes) {\r
149                                         XmlSchemaAttributeGroupRef grp = obj as XmlSchemaAttributeGroupRef;\r
150                                         if (grp != null && grp.QualifiedName == this.QualifiedName)\r
151                                                 actualAttributes.Add (redefined);\r
152                                         else\r
153                                                 actualAttributes.Add (obj);\r
154                                 }\r
155                         }\r
156                         else\r
157                         */\r
158                                 actualAttributes = Attributes;\r
159 \r
160                         attributeUses = new XmlSchemaObjectTable ();\r
161                         errorCount += XmlSchemaUtil.ValidateAttributesResolved (attributeUses,\r
162                                 h, schema, actualAttributes, AnyAttribute, \r
163                                 ref anyAttributeUse, redefined);\r
164                         ValidationId = schema.ValidationId;\r
165                         return errorCount;\r
166                 }\r
167 \r
168                 //<attributeGroup\r
169                 //  id = ID\r
170                 //  name = NCName\r
171                 //  ref = QName // Not present in this class.\r
172                 //  {any attributes with non-schema namespace . . .}>\r
173                 //  Content: (annotation?, ((attribute | attributeGroup)*, anyAttribute?))\r
174                 //</attributeGroup>\r
175                 internal static XmlSchemaAttributeGroup Read(XmlSchemaReader reader, ValidationEventHandler h)\r
176                 {\r
177                         XmlSchemaAttributeGroup attrgrp = new XmlSchemaAttributeGroup();\r
178                         reader.MoveToElement();\r
179 \r
180                         if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)\r
181                         {\r
182                                 error(h,"Should not happen :1: XmlSchemaAttributeGroup.Read, name="+reader.Name,null);\r
183                                 reader.SkipToEnd();\r
184                                 return null;\r
185                         }\r
186 \r
187                         attrgrp.LineNumber = reader.LineNumber;\r
188                         attrgrp.LinePosition = reader.LinePosition;\r
189                         attrgrp.SourceUri = reader.BaseURI;\r
190 \r
191                         while(reader.MoveToNextAttribute())\r
192                         {\r
193                                 if(reader.Name == "id")\r
194                                 {\r
195                                         attrgrp.Id = reader.Value;\r
196                                 }\r
197                                 else if(reader.Name == "name")\r
198                                 {\r
199                                         attrgrp.name = reader.Value;\r
200                                 }\r
201                                 else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)\r
202                                 {\r
203                                         error(h,reader.Name + " is not a valid attribute for attributeGroup in this context",null);\r
204                                 }\r
205                                 else\r
206                                 {\r
207                                         XmlSchemaUtil.ReadUnhandledAttribute(reader,attrgrp);\r
208                                 }\r
209                         }\r
210                         \r
211                         reader.MoveToElement();\r
212                         if(reader.IsEmptyElement)\r
213                                 return attrgrp;\r
214 \r
215                         //Content: 1.annotation?, 2.(attribute | attributeGroup)*, 3.anyAttribute?\r
216                         int level = 1;\r
217                         while(reader.ReadNextElement())\r
218                         {\r
219                                 if(reader.NodeType == XmlNodeType.EndElement)\r
220                                 {\r
221                                         if(reader.LocalName != xmlname)\r
222                                                 error(h,"Should not happen :2: XmlSchemaAttributeGroup.Read, name="+reader.Name,null);\r
223                                         break;\r
224                                 }\r
225                                 if(level <= 1 && reader.LocalName == "annotation")\r
226                                 {\r
227                                         level = 2; //Only one annotation\r
228                                         XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);\r
229                                         if(annotation != null)\r
230                                                 attrgrp.Annotation = annotation;\r
231                                         continue;\r
232                                 }\r
233                                 if(level <= 2)\r
234                                 {\r
235                                         if(reader.LocalName == "attribute")\r
236                                         {\r
237                                                 level = 2;\r
238                                                 XmlSchemaAttribute attr = XmlSchemaAttribute.Read(reader,h);\r
239                                                 if(attr != null)\r
240                                                         attrgrp.Attributes.Add(attr);\r
241                                                 continue;\r
242                                         }\r
243                                         if(reader.LocalName == "attributeGroup")\r
244                                         {\r
245                                                 level = 2;\r
246                                                 XmlSchemaAttributeGroupRef attr = XmlSchemaAttributeGroupRef.Read(reader,h);\r
247                                                 if(attr != null)\r
248                                                         attrgrp.attributes.Add(attr);\r
249                                                 continue;\r
250                                         }\r
251                                 }\r
252                                 if(level <= 3 && reader.LocalName == "anyAttribute")\r
253                                 {\r
254                                         level = 4;\r
255                                         XmlSchemaAnyAttribute anyattr = XmlSchemaAnyAttribute.Read(reader,h);\r
256                                         if(anyattr != null)\r
257                                                 attrgrp.AnyAttribute = anyattr;\r
258                                         continue;\r
259                                 }\r
260                                 reader.RaiseInvalidElementError();\r
261                         }\r
262                         return attrgrp;\r
263                 }\r
264                 \r
265         }\r
266 }\r