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