align with latest changes in *.sources file
[mono.git] / mcs / class / System.XML / System.Xml.Schema / XmlSchemaAttributeGroupRef.cs
1 //
2 // System.Xml.Schema.XmlSchemaAttributeGroupRef.cs
3 //
4 // Author:
5 //      Dwivedi, Ajay kumar  Adwiv@Yahoo.com
6 //      Atsushi Enomoto  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.Xml;
31 using System.Xml.Serialization;
32
33 namespace System.Xml.Schema
34 {
35         /// <summary>
36         /// Summary description for XmlSchemaAttributeGroupRef.
37         /// </summary>
38         public class XmlSchemaAttributeGroupRef : XmlSchemaAnnotated
39         {
40                 private XmlQualifiedName refName;
41                 const string xmlname = "attributeGroup";
42
43                 public XmlSchemaAttributeGroupRef()
44                 {
45                         refName = XmlQualifiedName.Empty;
46                 }
47                 
48                 [System.Xml.Serialization.XmlAttribute("ref")]
49                 public XmlQualifiedName RefName 
50                 {
51                         get{ return  refName; }
52                         set{ refName = value; }
53                 }
54
55                 /// <remarks>
56                 /// 1. ref must be present
57                 /// </remarks>
58                 internal override int Compile(ValidationEventHandler h, XmlSchema schema)
59                 {
60                         // If this is already compiled this time, simply skip.
61                         if (CompilationId == schema.CompilationId)
62                                 return 0;
63
64                         errorCount = 0;
65                         if(RefName == null || RefName.IsEmpty)
66                                 error(h, "ref must be present");
67                         else if(!XmlSchemaUtil.CheckQName(RefName))
68                                 error(h, "ref must be a valid qname");
69
70                         XmlSchemaUtil.CompileID(Id,this, schema.IDCollection,h);
71
72                         this.CompilationId = schema.CompilationId;
73                         return errorCount;
74                 }
75                 
76                 internal override int Validate(ValidationEventHandler h, XmlSchema schema)
77                 {
78                         return errorCount;
79                 }
80
81                 //<attributeGroup
82                 //  id = ID
83                 //  ref = QName
84                 //  {any attributes with non-schema namespace . . .}>
85                 //  Content: (annotation?)
86                 //</attributeGroup>
87                 internal static XmlSchemaAttributeGroupRef Read(XmlSchemaReader reader, ValidationEventHandler h)
88                 {
89                         XmlSchemaAttributeGroupRef attrgrp = new XmlSchemaAttributeGroupRef();
90                         reader.MoveToElement();
91
92                         if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
93                         {
94                                 error(h,"Should not happen :1: XmlSchemaAttributeGroupRef.Read, name="+reader.Name,null);
95                                 reader.SkipToEnd();
96                                 return null;
97                         }
98
99                         attrgrp.LineNumber = reader.LineNumber;
100                         attrgrp.LinePosition = reader.LinePosition;
101                         attrgrp.SourceUri = reader.BaseURI;
102
103                         while(reader.MoveToNextAttribute())
104                         {
105                                 if(reader.Name == "id")
106                                 {
107                                         attrgrp.Id = reader.Value;
108                                 }
109                                 else if(reader.Name == "ref")
110                                 {
111                                         Exception innerex;
112                                         attrgrp.refName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);
113                                         if(innerex != null)
114                                                 error(h, reader.Value + " is not a valid value for ref attribute",innerex);
115                                 }
116                                 else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
117                                 {
118                                         error(h,reader.Name + " is not a valid attribute for attributeGroup in this context",null);
119                                 }
120                                 else
121                                 {
122                                         XmlSchemaUtil.ReadUnhandledAttribute(reader,attrgrp);
123                                 }
124                         }
125                         
126                         reader.MoveToElement();
127                         if(reader.IsEmptyElement)
128                                 return attrgrp;
129                         int level = 1;
130
131                         while(reader.ReadNextElement())
132                         {
133                                 if(reader.NodeType == XmlNodeType.EndElement)
134                                 {
135                                         if(reader.LocalName != xmlname)
136                                                 error(h,"Should not happen :2: XmlSchemaAttributeGroupRef.Read, name="+reader.Name,null);
137                                         break;
138                                 }
139                                 if(level <= 1 && reader.LocalName == "annotation")
140                                 {
141                                         level = 2; //Only one annotation
142                                         XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
143                                         if(annotation != null)
144                                                 attrgrp.Annotation = annotation;
145                                         continue;
146                                 }
147                                 reader.RaiseInvalidElementError();
148                         }
149                         return attrgrp;
150                 }
151         }
152 }