2010-04-06 Jb Evain <jbevain@novell.com>
[mcs.git] / class / System.XML / System.Xml.Schema / XmlSchemaComplexContent.cs
blob1cd9ebbaa3d0430f934cb45b12c03e4dc9c00138
1 //
2 // System.Xml.Schema.XmlSchemaComplexContent.cs
3 //
4 // Author:
5 // Dwivedi, Ajay kumar Adwiv@Yahoo.com
6 // Atsushi Enomoto ginga@kit.hi-ho.ne.jp
7 //
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.
29 using System;
30 using System.Xml.Serialization;
31 using System.Xml;
33 namespace System.Xml.Schema
35 /// <summary>
36 /// Summary description for XmlSchemaComplexContent.
37 /// </summary>
38 public class XmlSchemaComplexContent : XmlSchemaContentModel
40 private XmlSchemaContent content;
41 private bool isMixed;
42 const string xmlname = "complexContent";
44 public XmlSchemaComplexContent()
47 [System.Xml.Serialization.XmlAttribute("mixed")]
48 public bool IsMixed
50 get{ return isMixed; }
51 set{ isMixed = value; }
54 [XmlElement("restriction",typeof(XmlSchemaComplexContentRestriction))]
55 [XmlElement("extension",typeof(XmlSchemaComplexContentExtension))]
56 public override XmlSchemaContent Content
58 get{ return content; }
59 set{ content = value; }
62 internal override void SetParent (XmlSchemaObject parent)
64 base.SetParent (parent);
65 if (Content != null)
66 Content.SetParent (this);
69 /// <remarks>
70 /// 1. Content must be present
71 /// </remarks>
72 internal override int Compile(ValidationEventHandler h, XmlSchema schema)
74 // If this is already compiled this time, simply skip.
75 if (CompilationId == schema.CompilationId)
76 return 0;
78 if (isRedefinedComponent) {
79 if (Annotation != null)
80 Annotation.isRedefinedComponent = true;
81 if (Content != null)
82 Content.isRedefinedComponent = true;
85 if(Content == null)
87 error(h, "Content must be present in a complexContent");
89 else
91 if(Content is XmlSchemaComplexContentRestriction)
93 XmlSchemaComplexContentRestriction xscr = (XmlSchemaComplexContentRestriction) Content;
94 errorCount += xscr.Compile(h, schema);
96 else if(Content is XmlSchemaComplexContentExtension)
98 XmlSchemaComplexContentExtension xsce = (XmlSchemaComplexContentExtension) Content;
99 errorCount += xsce.Compile(h, schema);
101 else
102 error(h,"complexContent can't have any value other than restriction or extention");
105 XmlSchemaUtil.CompileID(Id,this, schema.IDCollection,h);
107 this.CompilationId = schema.CompilationId;
108 return errorCount;
111 internal override int Validate(ValidationEventHandler h, XmlSchema schema)
113 if (IsValidated (schema.ValidationId))
114 return errorCount;
116 errorCount += Content.Validate (h, schema);
118 ValidationId = schema.ValidationId;
119 return errorCount;
121 //<complexContent
122 // id = ID
123 // mixed = boolean
124 // {any attributes with non-schema namespace . . .}>
125 // Content: (annotation?, (restriction | extension))
126 //</complexContent>
127 internal static XmlSchemaComplexContent Read(XmlSchemaReader reader, ValidationEventHandler h)
129 XmlSchemaComplexContent complex = new XmlSchemaComplexContent();
130 reader.MoveToElement();
132 if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
134 error(h,"Should not happen :1: XmlSchemaComplexContent.Read, name="+reader.Name,null);
135 reader.Skip();
136 return null;
139 complex.LineNumber = reader.LineNumber;
140 complex.LinePosition = reader.LinePosition;
141 complex.SourceUri = reader.BaseURI;
143 while(reader.MoveToNextAttribute())
145 if(reader.Name == "id")
147 complex.Id = reader.Value;
149 else if(reader.Name == "mixed")
151 Exception innerex;
152 complex.isMixed = XmlSchemaUtil.ReadBoolAttribute(reader,out innerex);
153 if(innerex != null)
154 error(h,reader.Value + " is an invalid value for mixed",innerex);
156 else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
158 error(h,reader.Name + " is not a valid attribute for complexContent",null);
160 else
162 XmlSchemaUtil.ReadUnhandledAttribute(reader,complex);
166 reader.MoveToElement();
167 if(reader.IsEmptyElement)
168 return complex;
169 //Content: (annotation?, (restriction | extension))
170 int level = 1;
171 while(reader.ReadNextElement())
173 if(reader.NodeType == XmlNodeType.EndElement)
175 if(reader.LocalName != xmlname)
176 error(h,"Should not happen :2: XmlSchemaComplexContent.Read, name="+reader.Name,null);
177 break;
179 if(level <= 1 && reader.LocalName == "annotation")
181 level = 2; //Only one annotation
182 XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
183 if(annotation != null)
184 complex.Annotation = annotation;
185 continue;
187 if(level <=2)
189 if(reader.LocalName == "restriction")
191 level = 3;
192 XmlSchemaComplexContentRestriction restriction = XmlSchemaComplexContentRestriction.Read(reader,h);
193 if(restriction != null)
194 complex.content = restriction;
195 continue;
197 if(reader.LocalName == "extension")
199 level = 3;
200 XmlSchemaComplexContentExtension extension = XmlSchemaComplexContentExtension.Read(reader,h);
201 if(extension != null)
202 complex.content = extension;
203 continue;
206 reader.RaiseInvalidElementError();
208 return complex;