2004-01-13 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.XML / System.Xml.Schema / XmlSchemaSimpleContentExtension.cs
1 //\r
2 // System.Xml.Schema.XmlSchemaSimpleContentExtension.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 XmlSchemaSimpleContentExtension.\r
16         /// </summary>\r
17         public class XmlSchemaSimpleContentExtension : XmlSchemaContent\r
18         {\r
19 \r
20                 private XmlSchemaAnyAttribute any;\r
21                 private XmlSchemaObjectCollection attributes;\r
22                 private XmlQualifiedName baseTypeName;\r
23                 private static string xmlname = "extension";\r
24 \r
25                 public XmlSchemaSimpleContentExtension()\r
26                 {\r
27                         baseTypeName = XmlQualifiedName.Empty;\r
28                         attributes       = new XmlSchemaObjectCollection();\r
29                 }\r
30 \r
31                 [System.Xml.Serialization.XmlAttribute("base")]\r
32                 public XmlQualifiedName BaseTypeName \r
33                 {\r
34                         get{ return  baseTypeName; }\r
35                         set{ baseTypeName = value; }\r
36                 }\r
37 \r
38                 [XmlElement("attribute",typeof(XmlSchemaAttribute),Namespace=XmlSchema.Namespace)]\r
39                 [XmlElement("attributeGroup",typeof(XmlSchemaAttributeGroupRef),Namespace=XmlSchema.Namespace)]\r
40                 public XmlSchemaObjectCollection Attributes \r
41                 {\r
42                         get{ return attributes; }\r
43                 }\r
44 \r
45                 [XmlElement("anyAttribute",Namespace=XmlSchema.Namespace)]\r
46                 public XmlSchemaAnyAttribute AnyAttribute \r
47                 {\r
48                         get{ return  any; }\r
49                         set{ any = value; }\r
50                 }\r
51 \r
52                 ///<remarks>\r
53                 /// 1. Base must be present and a QName\r
54                 ///</remarks>\r
55                 internal override int Compile(ValidationEventHandler h, XmlSchema schema)\r
56                 {\r
57                         // If this is already compiled this time, simply skip.\r
58                         if (this.IsComplied (schema.CompilationId))\r
59                                 return 0;\r
60 \r
61                         if (this.isRedefinedComponent) {\r
62                                 if (Annotation != null)\r
63                                         Annotation.isRedefinedComponent = true;\r
64                                 if (AnyAttribute != null)\r
65                                         AnyAttribute.isRedefinedComponent = true;\r
66                                 foreach (XmlSchemaObject obj in Attributes)\r
67                                         obj.isRedefinedComponent = true;\r
68                         }\r
69 \r
70                         if(BaseTypeName == null || BaseTypeName.IsEmpty)\r
71                         {\r
72                                 error(h, "base must be present, as a QName");\r
73                         }\r
74                         else if(!XmlSchemaUtil.CheckQName(BaseTypeName))\r
75                                 error(h,"BaseTypeName must be a QName");\r
76 \r
77                         if(this.AnyAttribute != null)\r
78                         {\r
79                                 errorCount += AnyAttribute.Compile(h,schema);\r
80                         }\r
81 \r
82                         foreach(XmlSchemaObject obj in Attributes)\r
83                         {\r
84                                 if(obj is XmlSchemaAttribute)\r
85                                 {\r
86                                         XmlSchemaAttribute attr = (XmlSchemaAttribute) obj;\r
87                                         errorCount += attr.Compile(h,schema);\r
88                                 }\r
89                                 else if(obj is XmlSchemaAttributeGroupRef)\r
90                                 {\r
91                                         XmlSchemaAttributeGroupRef atgrp = (XmlSchemaAttributeGroupRef) obj;\r
92                                         errorCount += atgrp.Compile(h,schema);\r
93                                 }\r
94                                 else\r
95                                         error(h,obj.GetType() +" is not valid in this place::SimpleConentExtension");\r
96                         }\r
97                         \r
98                         XmlSchemaUtil.CompileID(Id,this,schema.IDCollection,h);\r
99 \r
100                         this.CompilationId = schema.CompilationId;\r
101                         return errorCount;\r
102                 }\r
103                 \r
104                 internal override XmlQualifiedName GetBaseTypeName ()\r
105                 {\r
106                         return baseTypeName;\r
107                 }\r
108 \r
109                 internal override int Validate(ValidationEventHandler h, XmlSchema schema)\r
110                 {\r
111                         if (IsValidated (schema.ValidationId))\r
112                                 return errorCount;\r
113 \r
114                         XmlSchemaType st = schema.SchemaTypes [baseTypeName] as XmlSchemaType;\r
115                         if (st != null) {\r
116                                 XmlSchemaComplexType ct = st as XmlSchemaComplexType;\r
117                                 if (ct != null && ct.ContentModel is XmlSchemaComplexContent)\r
118                                         error (h, "Specified type is complex type which contains complex content.");\r
119                                 st.Validate (h, schema);\r
120                                 actualBaseSchemaType = st;\r
121                         } else if (baseTypeName == XmlSchemaComplexType.AnyTypeName) {\r
122                                 actualBaseSchemaType = XmlSchemaComplexType.AnyType;
123                         } else if (baseTypeName.Namespace == XmlSchema.Namespace) {\r
124                                 actualBaseSchemaType = XmlSchemaDatatype.FromName (baseTypeName);\r
125                                 if (actualBaseSchemaType == null)\r
126                                         error (h, "Invalid schema datatype name is specified.");\r
127                         }\r
128                         // otherwise, it might be missing sub components.\r
129                         else if (!schema.IsNamespaceAbsent (baseTypeName.Namespace))\r
130                                 error (h, "Referenced base schema type " + baseTypeName + " was not found in the corresponding schema.");\r
131 \r
132                         ValidationId = schema.ValidationId;\r
133                         return errorCount;\r
134                 }\r
135 \r
136                 //<extension \r
137                 //base = QName \r
138                 //id = ID \r
139                 //{any attributes with non-schema namespace . . .}>\r
140                 //Content: (annotation?, ((attribute | attributeGroup)*, anyAttribute?))\r
141                 //</extension>\r
142                 internal static XmlSchemaSimpleContentExtension Read(XmlSchemaReader reader, ValidationEventHandler h)\r
143                 {\r
144                         XmlSchemaSimpleContentExtension extension = new XmlSchemaSimpleContentExtension();\r
145                         reader.MoveToElement();\r
146 \r
147                         if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)\r
148                         {\r
149                                 error(h,"Should not happen :1: XmlSchemaAttributeGroup.Read, name="+reader.Name,null);\r
150                                 reader.Skip();\r
151                                 return null;\r
152                         }\r
153 \r
154                         extension.LineNumber = reader.LineNumber;\r
155                         extension.LinePosition = reader.LinePosition;\r
156                         extension.SourceUri = reader.BaseURI;\r
157 \r
158                         while(reader.MoveToNextAttribute())\r
159                         {\r
160                                 if(reader.Name == "base")\r
161                                 {\r
162                                         Exception innerex;\r
163                                         extension.baseTypeName= XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);\r
164                                         if(innerex != null)\r
165                                                 error(h, reader.Value + " is not a valid value for base attribute",innerex);\r
166                                 }\r
167                                 else if(reader.Name == "id")\r
168                                 {\r
169                                         extension.Id = reader.Value;\r
170                                 }\r
171                                 else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)\r
172                                 {\r
173                                         error(h,reader.Name + " is not a valid attribute for extension in this context",null);\r
174                                 }\r
175                                 else\r
176                                 {\r
177                                         XmlSchemaUtil.ReadUnhandledAttribute(reader,extension);\r
178                                 }\r
179                         }\r
180                         \r
181                         reader.MoveToElement();\r
182                         if(reader.IsEmptyElement)\r
183                                 return extension;\r
184 \r
185                         //Content: 1.annotation?, 2.(attribute | attributeGroup)*, 3.anyAttribute?\r
186                         int level = 1;\r
187                         while(reader.ReadNextElement())\r
188                         {\r
189                                 if(reader.NodeType == XmlNodeType.EndElement)\r
190                                 {\r
191                                         if(reader.LocalName != xmlname)\r
192                                                 error(h,"Should not happen :2: XmlSchemaSimpleContentExtension.Read, name="+reader.Name,null);\r
193                                         break;\r
194                                 }\r
195                                 if(level <= 1 && reader.LocalName == "annotation")\r
196                                 {\r
197                                         level = 2; //Only one annotation\r
198                                         XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);\r
199                                         if(annotation != null)\r
200                                                 extension.Annotation = annotation;\r
201                                         continue;\r
202                                 }\r
203                                 if(level <= 2)\r
204                                 {\r
205                                         if(reader.LocalName == "attribute")\r
206                                         {\r
207                                                 level = 2;\r
208                                                 XmlSchemaAttribute attr = XmlSchemaAttribute.Read(reader,h);\r
209                                                 if(attr != null)\r
210                                                         extension.Attributes.Add(attr);\r
211                                                 continue;\r
212                                         }\r
213                                         if(reader.LocalName == "attributeGroup")\r
214                                         {\r
215                                                 level = 2;\r
216                                                 XmlSchemaAttributeGroupRef attr = XmlSchemaAttributeGroupRef.Read(reader,h);\r
217                                                 if(attr != null)\r
218                                                         extension.attributes.Add(attr);\r
219                                                 continue;\r
220                                         }\r
221                                 }\r
222                                 if(level <= 3 && reader.LocalName == "anyAttribute")\r
223                                 {\r
224                                         level = 4;\r
225                                         XmlSchemaAnyAttribute anyattr = XmlSchemaAnyAttribute.Read(reader,h);\r
226                                         if(anyattr != null)\r
227                                                 extension.AnyAttribute = anyattr;\r
228                                         continue;\r
229                                 }\r
230                                 reader.RaiseInvalidElementError();\r
231                         }\r
232                         return extension;\r
233                 }\r
234         }\r
235 }\r