2002-06-14 Dwivedi, Ajay kumar <adwiv@yahoo.com>
[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("anyAttribute",Namespace="http://www.w3.org/2001/XMLSchema")]\r
34                 public XmlSchemaAnyAttribute AnyAttribute \r
35                 {\r
36                         get{ return  any; }\r
37                         set{ any = value; }\r
38                 }\r
39 \r
40                 [XmlElement("attribute",typeof(XmlSchemaAttribute),Namespace="http://www.w3.org/2001/XMLSchema")]\r
41                 [XmlElement("attributeGroup",typeof(XmlSchemaAttributeGroupRef),Namespace="http://www.w3.org/2001/XMLSchema")]\r
42                 public XmlSchemaObjectCollection Attributes \r
43                 {\r
44                         get{ return attributes; }\r
45                 }\r
46 \r
47                 [XmlElement("group",typeof(XmlSchemaGroupRef),Namespace="http://www.w3.org/2001/XMLSchema")]\r
48                 [XmlElement("all",typeof(XmlSchemaAll),Namespace="http://www.w3.org/2001/XMLSchema")]\r
49                 [XmlElement("choice",typeof(XmlSchemaChoice),Namespace="http://www.w3.org/2001/XMLSchema")]\r
50                 [XmlElement("sequence",typeof(XmlSchemaSequence),Namespace="http://www.w3.org/2001/XMLSchema")]\r
51                 public XmlSchemaParticle Particle \r
52                 {\r
53                         get{ return  particle; }\r
54                         set{ particle = 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, XmlSchemaInfo info)\r
62                 {\r
63                         if(BaseTypeName == null || BaseTypeName.IsEmpty)\r
64                         {\r
65                                 error(h, "base must be present and a QName");\r
66                         }\r
67                         else if(!XmlSchemaUtil.CheckQName(BaseTypeName))\r
68                                 error(h,"BaseTypeName is not a valid XmlQualifiedName");\r
69 \r
70                         if(this.AnyAttribute != null)\r
71                         {\r
72                                 errorCount += AnyAttribute.Compile(h,info);\r
73                         }\r
74 \r
75                         foreach(XmlSchemaObject obj in Attributes)\r
76                         {\r
77                                 if(obj is XmlSchemaAttribute)\r
78                                 {\r
79                                         XmlSchemaAttribute attr = (XmlSchemaAttribute) obj;\r
80                                         errorCount += attr.Compile(h,info);\r
81                                 }\r
82                                 else if(obj is XmlSchemaAttributeGroupRef)\r
83                                 {\r
84                                         XmlSchemaAttributeGroupRef atgrp = (XmlSchemaAttributeGroupRef) obj;\r
85                                         errorCount += atgrp.Compile(h,info);\r
86                                 }\r
87                                 else\r
88                                         error(h,obj.GetType() +" is not valid in this place::ComplexContentRestriction");\r
89                         }\r
90                         \r
91                         if(Particle != null)\r
92                         {\r
93                                 if(Particle is XmlSchemaGroupRef)\r
94                                 {\r
95                                         errorCount += ((XmlSchemaGroupRef)Particle).Compile(h,info);\r
96                                 }\r
97                                 else if(Particle is XmlSchemaAll)\r
98                                 {\r
99                                         errorCount += ((XmlSchemaAll)Particle).Compile(h,info);\r
100                                 }\r
101                                 else if(Particle is XmlSchemaChoice)\r
102                                 {\r
103                                         errorCount += ((XmlSchemaChoice)Particle).Compile(h,info);\r
104                                 }\r
105                                 else if(Particle is XmlSchemaSequence)\r
106                                 {\r
107                                         errorCount += ((XmlSchemaSequence)Particle).Compile(h,info);\r
108                                 }\r
109                         }\r
110                         \r
111                         XmlSchemaUtil.CompileID(Id,this,info.IDCollection,h);\r
112 \r
113                         return errorCount;\r
114                 }\r
115                 \r
116                 [MonoTODO]\r
117                 internal int Validate(ValidationEventHandler h)\r
118                 {\r
119                         return errorCount;\r
120                 }\r
121 \r
122                 //<restriction\r
123                 //  base = QName\r
124                 //  id = ID\r
125                 //  {any attributes with non-schema namespace . . .}>\r
126                 //  Content: (annotation?, ((group | all | choice | sequence)?, ((attribute | attributeGroup)*, anyAttribute?)))\r
127                 //</restriction>\r
128                 internal static XmlSchemaComplexContentRestriction Read(XmlSchemaReader reader, ValidationEventHandler h)\r
129                 {\r
130                         XmlSchemaComplexContentRestriction restriction = new XmlSchemaComplexContentRestriction();\r
131                         reader.MoveToElement();\r
132 \r
133                         if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)\r
134                         {\r
135                                 error(h,"Should not happen :1: XmlSchemaComplexContentRestriction.Read, name="+reader.Name,null);\r
136                                 reader.Skip();\r
137                                 return null;\r
138                         }\r
139 \r
140                         restriction.LineNumber = reader.LineNumber;\r
141                         restriction.LinePosition = reader.LinePosition;\r
142                         restriction.SourceUri = reader.BaseURI;\r
143 \r
144                         while(reader.MoveToNextAttribute())\r
145                         {\r
146                                 if(reader.Name == "base")\r
147                                 {\r
148                                         Exception innerex;\r
149                                         restriction.baseTypeName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);\r
150                                         if(innerex != null)\r
151                                                 error(h, reader.Value + " is not a valid value for base attribute",innerex);\r
152                                 }\r
153                                 else if(reader.Name == "id")\r
154                                 {\r
155                                         restriction.Id = reader.Value;\r
156                                 }\r
157                                 else if(reader.NamespaceURI == "" || reader.NamespaceURI == XmlSchema.Namespace)\r
158                                 {\r
159                                         error(h,reader.Name + " is not a valid attribute for restriction",null);\r
160                                 }\r
161                                 else\r
162                                 {\r
163                                         if(reader.Prefix == "xmlns")\r
164                                                 restriction.Namespaces.Add(reader.LocalName, reader.Value);\r
165                                         else if(reader.Name == "xmlns")\r
166                                                 restriction.Namespaces.Add("",reader.Value);\r
167                                         //TODO: Add to Unhandled attributes\r
168                                 }\r
169                         }\r
170                         \r
171                         reader.MoveToElement();\r
172                         if(reader.IsEmptyElement)\r
173                                 return restriction;\r
174                         //Content: 1. annotation?, \r
175                         //                      (2.(group | all | choice | sequence)?, (3.(attribute | attributeGroup)*, 4.anyAttribute?)))\r
176                         int level = 1;\r
177                         while(reader.ReadNextElement())\r
178                         {\r
179                                 if(reader.NodeType == XmlNodeType.EndElement)\r
180                                 {\r
181                                         if(reader.LocalName != xmlname)\r
182                                                 error(h,"Should not happen :2: XmlSchemaComplexContentRestriction.Read, name="+reader.Name,null);\r
183                                         break;\r
184                                 }\r
185                                 if(level <= 1 && reader.LocalName == "annotation")\r
186                                 {\r
187                                         level = 2; //Only one annotation\r
188                                         XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);\r
189                                         if(annotation != null)\r
190                                                 restriction.Annotation = annotation;\r
191                                         continue;\r
192                                 }\r
193                                 if(level <= 2)\r
194                                 {\r
195                                         if(reader.LocalName == "group")\r
196                                         {\r
197                                                 level = 3;\r
198                                                 XmlSchemaGroupRef group = XmlSchemaGroupRef.Read(reader,h);\r
199                                                 if(group != null)\r
200                                                         restriction.particle = group;\r
201                                                 continue;\r
202                                         }\r
203                                         if(reader.LocalName == "all")\r
204                                         {\r
205                                                 level = 3;\r
206                                                 XmlSchemaAll all = XmlSchemaAll.Read(reader,h);\r
207                                                 if(all != null)\r
208                                                         restriction.particle = all;\r
209                                                 continue;\r
210                                         }\r
211                                         if(reader.LocalName == "choice")\r
212                                         {\r
213                                                 level = 3;\r
214                                                 XmlSchemaChoice choice = XmlSchemaChoice.Read(reader,h);\r
215                                                 if(choice != null)\r
216                                                         restriction.particle = choice;\r
217                                                 continue;\r
218                                         }\r
219                                         if(reader.LocalName == "sequence")\r
220                                         {\r
221                                                 level = 3;\r
222                                                 XmlSchemaSequence sequence = XmlSchemaSequence.Read(reader,h);\r
223                                                 if(sequence != null)\r
224                                                         restriction.particle = sequence;\r
225                                                 continue;\r
226                                         }\r
227                                 }\r
228                                 if(level <= 3)\r
229                                 {\r
230                                         if(reader.LocalName == "attribute")\r
231                                         {\r
232                                                 level = 3;\r
233                                                 XmlSchemaAttribute attr = XmlSchemaAttribute.Read(reader,h);\r
234                                                 if(attr != null)\r
235                                                         restriction.Attributes.Add(attr);\r
236                                                 continue;\r
237                                         }\r
238                                         if(reader.LocalName == "attributeGroup")\r
239                                         {\r
240                                                 level = 3;\r
241                                                 XmlSchemaAttributeGroupRef attr = XmlSchemaAttributeGroupRef.Read(reader,h);\r
242                                                 if(attr != null)\r
243                                                         restriction.attributes.Add(attr);\r
244                                                 continue;\r
245                                         }\r
246                                 }\r
247                                 if(level <= 4 && reader.LocalName == "anyAttribute")\r
248                                 {\r
249                                         level = 5;\r
250                                         XmlSchemaAnyAttribute anyattr = XmlSchemaAnyAttribute.Read(reader,h);\r
251                                         if(anyattr != null)\r
252                                                 restriction.AnyAttribute = anyattr;\r
253                                         continue;\r
254                                 }\r
255                                 reader.RaiseInvalidElementError();\r
256                         }\r
257                         return restriction;\r
258                 }\r
259         }\r
260 }\r