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