2003-10-23 Atsushi Enomoto <ginga@kit.hi-ho.ne.jp>
[mono.git] / mcs / class / System.XML / System.Xml.Schema / XmlSchemaAttributeGroupRef.cs
1 //\r
2 // System.Xml.Schema.XmlSchemaAttributeGroupRef.cs\r
3 //\r
4 // Author:\r
5 //      Dwivedi, Ajay kumar  Adwiv@Yahoo.com\r
6 //      Atsushi Enomoto  ginga@kit.hi-ho.ne.jp\r
7 //\r
8 using System;\r
9 using System.Xml;\r
10 using System.Xml.Serialization;\r
11 \r
12 namespace System.Xml.Schema\r
13 {\r
14         /// <summary>\r
15         /// Summary description for XmlSchemaAttributeGroupRef.\r
16         /// </summary>\r
17         public class XmlSchemaAttributeGroupRef : XmlSchemaAnnotated\r
18         {\r
19                 private XmlQualifiedName refName;\r
20                 private static string xmlname = "attributeGroup";\r
21 \r
22                 public XmlSchemaAttributeGroupRef()\r
23                 {\r
24                         refName = XmlQualifiedName.Empty;\r
25                 }\r
26                 \r
27                 [System.Xml.Serialization.XmlAttribute("ref")]\r
28                 public XmlQualifiedName RefName \r
29                 {\r
30                         get{ return  refName; }\r
31                         set{ refName = value; }\r
32                 }\r
33 \r
34                 /// <remarks>\r
35                 /// 1. ref must be present\r
36                 /// 2. The element must be empty. ?? FIXME: Is this correct or annotation is permitted?\r
37                 /// </remarks>\r
38                 internal override int Compile(ValidationEventHandler h, XmlSchema schema)\r
39                 {\r
40                         // If this is already compiled this time, simply skip.\r
41                         if (this.IsComplied (schema.CompilationId))\r
42                                 return 0;\r
43 \r
44                         errorCount = 0;\r
45                         if(RefName == null || RefName.IsEmpty)\r
46                                 error(h, "ref must be present");\r
47                         else if(!XmlSchemaUtil.CheckQName(RefName))\r
48                                 error(h, "ref must be a valid qname");\r
49 \r
50                         XmlSchemaUtil.CompileID(Id,this, schema.IDCollection,h);\r
51 \r
52 //                      if(this.Annotation != null)\r
53 //                              error(h, "attributegroup with a ref can't have any content");\r
54                         \r
55                         this.CompilationId = schema.CompilationId;\r
56                         return errorCount;\r
57                 }\r
58                 \r
59                 internal override int Validate(ValidationEventHandler h, XmlSchema schema)\r
60                 {\r
61                         return errorCount;\r
62                 }\r
63 \r
64                 //<attributeGroup\r
65                 //  id = ID\r
66                 //  ref = QName\r
67                 //  {any attributes with non-schema namespace . . .}>\r
68                 //  Content: (annotation?)\r
69                 //</attributeGroup>\r
70                 internal static XmlSchemaAttributeGroupRef Read(XmlSchemaReader reader, ValidationEventHandler h)\r
71                 {\r
72                         XmlSchemaAttributeGroupRef attrgrp = new XmlSchemaAttributeGroupRef();\r
73                         reader.MoveToElement();\r
74 \r
75                         if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)\r
76                         {\r
77                                 error(h,"Should not happen :1: XmlSchemaAttributeGroupRef.Read, name="+reader.Name,null);\r
78                                 reader.SkipToEnd();\r
79                                 return null;\r
80                         }\r
81 \r
82                         attrgrp.LineNumber = reader.LineNumber;\r
83                         attrgrp.LinePosition = reader.LinePosition;\r
84                         attrgrp.SourceUri = reader.BaseURI;\r
85 \r
86                         while(reader.MoveToNextAttribute())\r
87                         {\r
88                                 if(reader.Name == "id")\r
89                                 {\r
90                                         attrgrp.Id = reader.Value;\r
91                                 }\r
92                                 else if(reader.Name == "ref")\r
93                                 {\r
94                                         Exception innerex;\r
95                                         attrgrp.refName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);\r
96                                         if(innerex != null)\r
97                                                 error(h, reader.Value + " is not a valid value for ref attribute",innerex);\r
98                                 }\r
99                                 else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)\r
100                                 {\r
101                                         error(h,reader.Name + " is not a valid attribute for attributeGroup in this context",null);\r
102                                 }\r
103                                 else\r
104                                 {\r
105                                         XmlSchemaUtil.ReadUnhandledAttribute(reader,attrgrp);\r
106                                 }\r
107                         }\r
108                         \r
109                         reader.MoveToElement();\r
110                         if(reader.IsEmptyElement)\r
111                                 return attrgrp;\r
112                         int level = 1;\r
113 \r
114                         while(reader.ReadNextElement())\r
115                         {\r
116                                 if(reader.NodeType == XmlNodeType.EndElement)\r
117                                 {\r
118                                         if(reader.LocalName != xmlname)\r
119                                                 error(h,"Should not happen :2: XmlSchemaAttributeGroupRef.Read, name="+reader.Name,null);\r
120                                         break;\r
121                                 }\r
122                                 if(level <= 1 && reader.LocalName == "annotation")\r
123                                 {\r
124                                         level = 2; //Only one annotation\r
125                                         XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);\r
126                                         if(annotation != null)\r
127                                                 attrgrp.Annotation = annotation;\r
128                                         continue;\r
129                                 }\r
130                                 reader.RaiseInvalidElementError();\r
131                         }\r
132                         return attrgrp;\r
133                 }\r
134         }\r
135 }\r