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