2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / System.XML / System.Xml.Schema / XmlSchemaComplexContent.cs
1 //\r
2 // System.Xml.Schema.XmlSchemaComplexContent.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
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29 using System;\r
30 using System.Xml.Serialization;\r
31 using System.Xml;\r
32 \r
33 namespace System.Xml.Schema\r
34 {\r
35         /// <summary>\r
36         /// Summary description for XmlSchemaComplexContent.\r
37         /// </summary>\r
38         public class XmlSchemaComplexContent : XmlSchemaContentModel\r
39         {\r
40                 private XmlSchemaContent content;\r
41                 private bool isMixed;\r
42                 const string xmlname = "complexContent";\r
43 \r
44                 public XmlSchemaComplexContent()\r
45                 {}\r
46 \r
47                 [System.Xml.Serialization.XmlAttribute("mixed")]\r
48                 public bool IsMixed \r
49                 {\r
50                         get{ return  isMixed; } \r
51                         set{ isMixed = value; }\r
52                 }\r
53 \r
54                 [XmlElement("restriction",typeof(XmlSchemaComplexContentRestriction),Namespace=XmlSchema.Namespace)]\r
55                 [XmlElement("extension",typeof(XmlSchemaComplexContentExtension),Namespace=XmlSchema.Namespace)]\r
56                 public override XmlSchemaContent Content \r
57                 {\r
58                         get{ return  content; } \r
59                         set{ content = value; }\r
60                 }\r
61 \r
62                 /// <remarks>\r
63                 /// 1. Content 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 NET_2_0\r
72                                 if (Content != null)\r
73                                         Content.Parent = this;\r
74 #endif\r
75 \r
76                         if (isRedefinedComponent) {\r
77                                 if (Annotation != null)\r
78                                         Annotation.isRedefinedComponent = true;\r
79                                 if (Content != null)\r
80                                         Content.isRedefinedComponent = true;\r
81                         }\r
82 \r
83                         if(Content == null)\r
84                         {\r
85                                 error(h, "Content must be present in a complexContent");\r
86                         }\r
87                         else\r
88                         {\r
89                                 if(Content is XmlSchemaComplexContentRestriction)\r
90                                 {\r
91                                         XmlSchemaComplexContentRestriction xscr = (XmlSchemaComplexContentRestriction) Content;\r
92                                         errorCount += xscr.Compile(h, schema);\r
93                                 }\r
94                                 else if(Content is XmlSchemaComplexContentExtension)\r
95                                 {\r
96                                         XmlSchemaComplexContentExtension xsce = (XmlSchemaComplexContentExtension) Content;\r
97                                         errorCount += xsce.Compile(h, schema);\r
98                                 }\r
99                                 else\r
100                                         error(h,"complexContent can't have any value other than restriction or extention");\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 int Validate(ValidationEventHandler h, XmlSchema schema)\r
110                 {\r
111                         if (IsValidated (schema.ValidationId))\r
112                                 return errorCount;\r
113 \r
114                         errorCount += Content.Validate (h, schema);\r
115 \r
116                         ValidationId = schema.ValidationId;\r
117                         return errorCount;\r
118                 }\r
119                 //<complexContent\r
120                 //  id = ID\r
121                 //  mixed = boolean\r
122                 //  {any attributes with non-schema namespace . . .}>\r
123                 //  Content: (annotation?, (restriction | extension))\r
124                 //</complexContent>\r
125                 internal static XmlSchemaComplexContent Read(XmlSchemaReader reader, ValidationEventHandler h)\r
126                 {\r
127                         XmlSchemaComplexContent complex = new XmlSchemaComplexContent();\r
128                         reader.MoveToElement();\r
129 \r
130                         if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)\r
131                         {\r
132                                 error(h,"Should not happen :1: XmlSchemaComplexContent.Read, name="+reader.Name,null);\r
133                                 reader.Skip();\r
134                                 return null;\r
135                         }\r
136 \r
137                         complex.LineNumber = reader.LineNumber;\r
138                         complex.LinePosition = reader.LinePosition;\r
139                         complex.SourceUri = reader.BaseURI;\r
140 \r
141                         while(reader.MoveToNextAttribute())\r
142                         {\r
143                                 if(reader.Name == "id")\r
144                                 {\r
145                                         complex.Id = reader.Value;\r
146                                 }\r
147                                 else if(reader.Name == "mixed")\r
148                                 {\r
149                                         Exception innerex;\r
150                                         complex.isMixed = XmlSchemaUtil.ReadBoolAttribute(reader,out innerex);\r
151                                         if(innerex != null)\r
152                                                 error(h,reader.Value + " is an invalid value for mixed",innerex);\r
153                                 }\r
154                                 else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)\r
155                                 {\r
156                                         error(h,reader.Name + " is not a valid attribute for complexContent",null);\r
157                                 }\r
158                                 else\r
159                                 {\r
160                                         XmlSchemaUtil.ReadUnhandledAttribute(reader,complex);\r
161                                 }\r
162                         }\r
163                         \r
164                         reader.MoveToElement();\r
165                         if(reader.IsEmptyElement)\r
166                                 return complex;\r
167                         //Content: (annotation?, (restriction | extension))\r
168                         int level = 1;\r
169                         while(reader.ReadNextElement())\r
170                         {\r
171                                 if(reader.NodeType == XmlNodeType.EndElement)\r
172                                 {\r
173                                         if(reader.LocalName != xmlname)\r
174                                                 error(h,"Should not happen :2: XmlSchemaComplexContent.Read, name="+reader.Name,null);\r
175                                         break;\r
176                                 }\r
177                                 if(level <= 1 && reader.LocalName == "annotation")\r
178                                 {\r
179                                         level = 2; //Only one annotation\r
180                                         XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);\r
181                                         if(annotation != null)\r
182                                                 complex.Annotation = annotation;\r
183                                         continue;\r
184                                 }\r
185                                 if(level <=2)\r
186                                 {\r
187                                         if(reader.LocalName == "restriction")\r
188                                         {\r
189                                                 level = 3;\r
190                                                 XmlSchemaComplexContentRestriction restriction = XmlSchemaComplexContentRestriction.Read(reader,h);\r
191                                                 if(restriction != null)\r
192                                                         complex.content = restriction;\r
193                                                 continue;\r
194                                         }\r
195                                         if(reader.LocalName == "extension")\r
196                                         {\r
197                                                 level = 3;\r
198                                                 XmlSchemaComplexContentExtension extension = XmlSchemaComplexContentExtension.Read(reader,h);\r
199                                                 if(extension != null)\r
200                                                         complex.content = extension;\r
201                                                 continue;\r
202                                         }\r
203                                 }\r
204                                 reader.RaiseInvalidElementError();\r
205                         }\r
206                         return complex;\r
207                 }\r
208         }\r
209 }\r