2004-01-13 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.XML / System.Xml.Schema / XmlSchemaComplexContentRestriction.cs
1 //\r
2 // System.Xml.Schema.XmlSchemaComplexContentRestriction.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 XmlSchemaComplexContentRestriction.\r
16         /// </summary>\r
17         public class XmlSchemaComplexContentRestriction : XmlSchemaContent\r
18         {\r
19                 private XmlSchemaAnyAttribute any;\r
20                 private XmlSchemaObjectCollection attributes;\r
21                 private XmlQualifiedName baseTypeName;\r
22                 private XmlSchemaParticle particle;\r
23                 private static string xmlname = "restriction";\r
24 \r
25                 public XmlSchemaComplexContentRestriction()\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("group",typeof(XmlSchemaGroupRef),Namespace=XmlSchema.Namespace)]\r
39                 [XmlElement("all",typeof(XmlSchemaAll),Namespace=XmlSchema.Namespace)]\r
40                 [XmlElement("choice",typeof(XmlSchemaChoice),Namespace=XmlSchema.Namespace)]\r
41                 [XmlElement("sequence",typeof(XmlSchemaSequence),Namespace=XmlSchema.Namespace)]\r
42                 public XmlSchemaParticle Particle \r
43                 {\r
44                         get{ return  particle; }\r
45                         set{ particle = value; }\r
46                 }\r
47 \r
48                 [XmlElement("attribute",typeof(XmlSchemaAttribute),Namespace=XmlSchema.Namespace)]\r
49                 [XmlElement("attributeGroup",typeof(XmlSchemaAttributeGroupRef),Namespace=XmlSchema.Namespace)]\r
50                 public XmlSchemaObjectCollection Attributes \r
51                 {\r
52                         get{ return attributes; }\r
53                 }\r
54 \r
55                 [XmlElement("anyAttribute",Namespace=XmlSchema.Namespace)]\r
56                 public XmlSchemaAnyAttribute AnyAttribute \r
57                 {\r
58                         get{ return  any; }\r
59                         set{ any = value; }\r
60                 }\r
61 \r
62                 /// <remarks>\r
63                 /// 1. base must be present\r
64                 /// </remarks>\r
65                 internal override int Compile(ValidationEventHandler h, XmlSchema schema)\r
66                 {\r
67                         // If this is already compiled this time, simply skip.\r
68                         if (this.IsComplied (schema.CompilationId))\r
69                                 return 0;\r
70 \r
71                         if (this.isRedefinedComponent) {\r
72                                 if (Annotation != null)\r
73                                         Annotation.isRedefinedComponent = true;\r
74                                 if (AnyAttribute != null)\r
75                                         AnyAttribute.isRedefinedComponent = true;\r
76                                 foreach (XmlSchemaObject obj in Attributes)\r
77                                         obj.isRedefinedComponent = true;\r
78                                 if (Particle != null)\r
79                                         Particle.isRedefinedComponent = true;\r
80                         }\r
81 \r
82                         if(BaseTypeName == null || BaseTypeName.IsEmpty)\r
83                         {\r
84                                 error(h, "base must be present, as a QName");\r
85                         }\r
86                         else if(!XmlSchemaUtil.CheckQName(BaseTypeName))\r
87                                 error(h,"BaseTypeName is not a valid XmlQualifiedName");\r
88 \r
89                         if(this.AnyAttribute != null)\r
90                         {\r
91                                 errorCount += AnyAttribute.Compile(h, schema);\r
92                         }\r
93 \r
94                         foreach(XmlSchemaObject obj in Attributes)\r
95                         {\r
96                                 if(obj is XmlSchemaAttribute)\r
97                                 {\r
98                                         XmlSchemaAttribute attr = (XmlSchemaAttribute) obj;\r
99                                         errorCount += attr.Compile(h, schema);\r
100                                 }\r
101                                 else if(obj is XmlSchemaAttributeGroupRef)\r
102                                 {\r
103                                         XmlSchemaAttributeGroupRef atgrp = (XmlSchemaAttributeGroupRef) obj;\r
104                                         errorCount += atgrp.Compile(h, schema);\r
105                                 }\r
106                                 else\r
107                                         error(h,obj.GetType() +" is not valid in this place::ComplexContentRestriction");\r
108                         }\r
109                         \r
110                         if(Particle != null)\r
111                         {\r
112                                 if(Particle is XmlSchemaGroupRef)\r
113                                 {\r
114                                         errorCount += ((XmlSchemaGroupRef)Particle).Compile(h, schema);\r
115                                 }\r
116                                 else if(Particle is XmlSchemaAll)\r
117                                 {\r
118                                         errorCount += ((XmlSchemaAll)Particle).Compile(h, schema);\r
119                                 }\r
120                                 else if(Particle is XmlSchemaChoice)\r
121                                 {\r
122                                         errorCount += ((XmlSchemaChoice)Particle).Compile(h, schema);\r
123                                 }\r
124                                 else if(Particle is XmlSchemaSequence)\r
125                                 {\r
126                                         errorCount += ((XmlSchemaSequence)Particle).Compile(h, schema);\r
127                                 }\r
128                                 else\r
129                                         error (h, "Particle of a restriction is limited only to group, sequence, choice and all.");\r
130                         }\r
131                         \r
132                         XmlSchemaUtil.CompileID(Id,this, schema.IDCollection,h);\r
133 \r
134                         this.CompilationId = schema.CompilationId;\r
135                         return errorCount;\r
136                 }\r
137                 \r
138                 internal override XmlQualifiedName GetBaseTypeName ()\r
139                 {\r
140                         return baseTypeName;\r
141                 }\r
142 \r
143                 internal override XmlSchemaParticle GetParticle ()\r
144                 {\r
145                         return particle;\r
146                 }\r
147 \r
148                 internal override int Validate(ValidationEventHandler h, XmlSchema schema)\r
149                 {\r
150                         return errorCount;\r
151                 }\r
152 \r
153                 //<restriction\r
154                 //  base = QName\r
155                 //  id = ID\r
156                 //  {any attributes with non-schema namespace . . .}>\r
157                 //  Content: (annotation?, ((group | all | choice | sequence)?, ((attribute | attributeGroup)*, anyAttribute?)))\r
158                 //</restriction>\r
159                 internal static XmlSchemaComplexContentRestriction Read(XmlSchemaReader reader, ValidationEventHandler h)\r
160                 {\r
161                         XmlSchemaComplexContentRestriction restriction = new XmlSchemaComplexContentRestriction();\r
162                         reader.MoveToElement();\r
163 \r
164                         if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)\r
165                         {\r
166                                 error(h,"Should not happen :1: XmlSchemaComplexContentRestriction.Read, name="+reader.Name,null);\r
167                                 reader.Skip();\r
168                                 return null;\r
169                         }\r
170 \r
171                         restriction.LineNumber = reader.LineNumber;\r
172                         restriction.LinePosition = reader.LinePosition;\r
173                         restriction.SourceUri = reader.BaseURI;\r
174 \r
175                         while(reader.MoveToNextAttribute())\r
176                         {\r
177                                 if(reader.Name == "base")\r
178                                 {\r
179                                         Exception innerex;\r
180                                         restriction.baseTypeName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);\r
181                                         if(innerex != null)\r
182                                                 error(h, reader.Value + " is not a valid value for base attribute",innerex);\r
183                                 }\r
184                                 else if(reader.Name == "id")\r
185                                 {\r
186                                         restriction.Id = reader.Value;\r
187                                 }\r
188                                 else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)\r
189                                 {\r
190                                         error(h,reader.Name + " is not a valid attribute for restriction",null);\r
191                                 }\r
192                                 else\r
193                                 {\r
194                                         XmlSchemaUtil.ReadUnhandledAttribute(reader,restriction);\r
195                                 }\r
196                         }\r
197                         \r
198                         reader.MoveToElement();\r
199                         if(reader.IsEmptyElement)\r
200                                 return restriction;\r
201                         //Content: 1. annotation?, \r
202                         //                      (2.(group | all | choice | sequence)?, (3.(attribute | attributeGroup)*, 4.anyAttribute?)))\r
203                         int level = 1;\r
204                         while(reader.ReadNextElement())\r
205                         {\r
206                                 if(reader.NodeType == XmlNodeType.EndElement)\r
207                                 {\r
208                                         if(reader.LocalName != xmlname)\r
209                                                 error(h,"Should not happen :2: XmlSchemaComplexContentRestriction.Read, name="+reader.Name,null);\r
210                                         break;\r
211                                 }\r
212                                 if(level <= 1 && reader.LocalName == "annotation")\r
213                                 {\r
214                                         level = 2; //Only one annotation\r
215                                         XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);\r
216                                         if(annotation != null)\r
217                                                 restriction.Annotation = annotation;\r
218                                         continue;\r
219                                 }\r
220                                 if(level <= 2)\r
221                                 {\r
222                                         if(reader.LocalName == "group")\r
223                                         {\r
224                                                 level = 3;\r
225                                                 XmlSchemaGroupRef group = XmlSchemaGroupRef.Read(reader,h);\r
226                                                 if(group != null)\r
227                                                         restriction.particle = group;\r
228                                                 continue;\r
229                                         }\r
230                                         if(reader.LocalName == "all")\r
231                                         {\r
232                                                 level = 3;\r
233                                                 XmlSchemaAll all = XmlSchemaAll.Read(reader,h);\r
234                                                 if(all != null)\r
235                                                         restriction.particle = all;\r
236                                                 continue;\r
237                                         }\r
238                                         if(reader.LocalName == "choice")\r
239                                         {\r
240                                                 level = 3;\r
241                                                 XmlSchemaChoice choice = XmlSchemaChoice.Read(reader,h);\r
242                                                 if(choice != null)\r
243                                                         restriction.particle = choice;\r
244                                                 continue;\r
245                                         }\r
246                                         if(reader.LocalName == "sequence")\r
247                                         {\r
248                                                 level = 3;\r
249                                                 XmlSchemaSequence sequence = XmlSchemaSequence.Read(reader,h);\r
250                                                 if(sequence != null)\r
251                                                         restriction.particle = sequence;\r
252                                                 continue;\r
253                                         }\r
254                                 }\r
255                                 if(level <= 3)\r
256                                 {\r
257                                         if(reader.LocalName == "attribute")\r
258                                         {\r
259                                                 level = 3;\r
260                                                 XmlSchemaAttribute attr = XmlSchemaAttribute.Read(reader,h);\r
261                                                 if(attr != null)\r
262                                                         restriction.Attributes.Add(attr);\r
263                                                 continue;\r
264                                         }\r
265                                         if(reader.LocalName == "attributeGroup")\r
266                                         {\r
267                                                 level = 3;\r
268                                                 XmlSchemaAttributeGroupRef attr = XmlSchemaAttributeGroupRef.Read(reader,h);\r
269                                                 if(attr != null)\r
270                                                         restriction.attributes.Add(attr);\r
271                                                 continue;\r
272                                         }\r
273                                 }\r
274                                 if(level <= 4 && reader.LocalName == "anyAttribute")\r
275                                 {\r
276                                         level = 5;\r
277                                         XmlSchemaAnyAttribute anyattr = XmlSchemaAnyAttribute.Read(reader,h);\r
278                                         if(anyattr != null)\r
279                                                 restriction.AnyAttribute = anyattr;\r
280                                         continue;\r
281                                 }\r
282                                 reader.RaiseInvalidElementError();\r
283                         }\r
284                         return restriction;\r
285                 }\r
286         }\r
287 }\r