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