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