**** Merged from MCS ****
[mono-project.git] / mcs / class / System.XML / System.Xml.Schema / XmlSchemaSimpleContentExtension.cs
blob158fd988adb24ce99d724c74af3ea2cd083ed3ba
1 //
2 // System.Xml.Schema.XmlSchemaSimpleContentExtension.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;
31 using System.Xml.Serialization;
33 namespace System.Xml.Schema
35 /// <summary>
36 /// Summary description for XmlSchemaSimpleContentExtension.
37 /// </summary>
38 public class XmlSchemaSimpleContentExtension : XmlSchemaContent
41 private XmlSchemaAnyAttribute any;
42 private XmlSchemaObjectCollection attributes;
43 private XmlQualifiedName baseTypeName;
44 const string xmlname = "extension";
46 public XmlSchemaSimpleContentExtension()
48 baseTypeName = XmlQualifiedName.Empty;
49 attributes = new XmlSchemaObjectCollection();
52 [System.Xml.Serialization.XmlAttribute("base")]
53 public XmlQualifiedName BaseTypeName
55 get{ return baseTypeName; }
56 set{ baseTypeName = value; }
59 [XmlElement("attribute",typeof(XmlSchemaAttribute),Namespace=XmlSchema.Namespace)]
60 [XmlElement("attributeGroup",typeof(XmlSchemaAttributeGroupRef),Namespace=XmlSchema.Namespace)]
61 public XmlSchemaObjectCollection Attributes
63 get{ return attributes; }
66 [XmlElement("anyAttribute",Namespace=XmlSchema.Namespace)]
67 public XmlSchemaAnyAttribute AnyAttribute
69 get{ return any; }
70 set{ any = value; }
73 // internal properties
74 internal override bool IsExtension {
75 get { return true; }
78 ///<remarks>
79 /// 1. Base must be present and a QName
80 ///</remarks>
81 internal override int Compile(ValidationEventHandler h, XmlSchema schema)
83 // If this is already compiled this time, simply skip.
84 if (this.IsComplied (schema.CompilationId))
85 return 0;
87 if (this.isRedefinedComponent) {
88 if (Annotation != null)
89 Annotation.isRedefinedComponent = true;
90 if (AnyAttribute != null)
91 AnyAttribute.isRedefinedComponent = true;
92 foreach (XmlSchemaObject obj in Attributes)
93 obj.isRedefinedComponent = true;
96 if(BaseTypeName == null || BaseTypeName.IsEmpty)
98 error(h, "base must be present, as a QName");
100 else if(!XmlSchemaUtil.CheckQName(BaseTypeName))
101 error(h,"BaseTypeName must be a QName");
103 if(this.AnyAttribute != null)
105 errorCount += AnyAttribute.Compile(h,schema);
108 foreach(XmlSchemaObject obj in Attributes)
110 if(obj is XmlSchemaAttribute)
112 XmlSchemaAttribute attr = (XmlSchemaAttribute) obj;
113 errorCount += attr.Compile(h,schema);
115 else if(obj is XmlSchemaAttributeGroupRef)
117 XmlSchemaAttributeGroupRef atgrp = (XmlSchemaAttributeGroupRef) obj;
118 errorCount += atgrp.Compile(h,schema);
120 else
121 error(h,obj.GetType() +" is not valid in this place::SimpleConentExtension");
124 XmlSchemaUtil.CompileID(Id,this,schema.IDCollection,h);
126 this.CompilationId = schema.CompilationId;
127 return errorCount;
130 internal override XmlQualifiedName GetBaseTypeName ()
132 return baseTypeName;
135 internal override XmlSchemaParticle GetParticle ()
137 return null;
140 internal override int Validate(ValidationEventHandler h, XmlSchema schema)
142 if (IsValidated (schema.ValidationId))
143 return errorCount;
145 XmlSchemaType st = schema.SchemaTypes [baseTypeName] as XmlSchemaType;
146 if (st != null) {
147 XmlSchemaComplexType ct = st as XmlSchemaComplexType;
148 if (ct != null && ct.ContentModel is XmlSchemaComplexContent)
149 error (h, "Specified type is complex type which contains complex content.");
150 st.Validate (h, schema);
151 actualBaseSchemaType = st;
152 } else if (baseTypeName == XmlSchemaComplexType.AnyTypeName) {
153 actualBaseSchemaType = XmlSchemaComplexType.AnyType;
154 } else if (XmlSchemaUtil.IsBuiltInDatatypeName (baseTypeName)) {
155 actualBaseSchemaType = XmlSchemaDatatype.FromName (baseTypeName);
156 if (actualBaseSchemaType == null)
157 error (h, "Invalid schema datatype name is specified.");
159 // otherwise, it might be missing sub components.
160 else if (!schema.IsNamespaceAbsent (baseTypeName.Namespace))
161 error (h, "Referenced base schema type " + baseTypeName + " was not found in the corresponding schema.");
163 ValidationId = schema.ValidationId;
164 return errorCount;
167 //<extension
168 //base = QName
169 //id = ID
170 //{any attributes with non-schema namespace . . .}>
171 //Content: (annotation?, ((attribute | attributeGroup)*, anyAttribute?))
172 //</extension>
173 internal static XmlSchemaSimpleContentExtension Read(XmlSchemaReader reader, ValidationEventHandler h)
175 XmlSchemaSimpleContentExtension extension = new XmlSchemaSimpleContentExtension();
176 reader.MoveToElement();
178 if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
180 error(h,"Should not happen :1: XmlSchemaAttributeGroup.Read, name="+reader.Name,null);
181 reader.Skip();
182 return null;
185 extension.LineNumber = reader.LineNumber;
186 extension.LinePosition = reader.LinePosition;
187 extension.SourceUri = reader.BaseURI;
189 while(reader.MoveToNextAttribute())
191 if(reader.Name == "base")
193 Exception innerex;
194 extension.baseTypeName= XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);
195 if(innerex != null)
196 error(h, reader.Value + " is not a valid value for base attribute",innerex);
198 else if(reader.Name == "id")
200 extension.Id = reader.Value;
202 else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
204 error(h,reader.Name + " is not a valid attribute for extension in this context",null);
206 else
208 XmlSchemaUtil.ReadUnhandledAttribute(reader,extension);
212 reader.MoveToElement();
213 if(reader.IsEmptyElement)
214 return extension;
216 //Content: 1.annotation?, 2.(attribute | attributeGroup)*, 3.anyAttribute?
217 int level = 1;
218 while(reader.ReadNextElement())
220 if(reader.NodeType == XmlNodeType.EndElement)
222 if(reader.LocalName != xmlname)
223 error(h,"Should not happen :2: XmlSchemaSimpleContentExtension.Read, name="+reader.Name,null);
224 break;
226 if(level <= 1 && reader.LocalName == "annotation")
228 level = 2; //Only one annotation
229 XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
230 if(annotation != null)
231 extension.Annotation = annotation;
232 continue;
234 if(level <= 2)
236 if(reader.LocalName == "attribute")
238 level = 2;
239 XmlSchemaAttribute attr = XmlSchemaAttribute.Read(reader,h);
240 if(attr != null)
241 extension.Attributes.Add(attr);
242 continue;
244 if(reader.LocalName == "attributeGroup")
246 level = 2;
247 XmlSchemaAttributeGroupRef attr = XmlSchemaAttributeGroupRef.Read(reader,h);
248 if(attr != null)
249 extension.attributes.Add(attr);
250 continue;
253 if(level <= 3 && reader.LocalName == "anyAttribute")
255 level = 4;
256 XmlSchemaAnyAttribute anyattr = XmlSchemaAnyAttribute.Read(reader,h);
257 if(anyattr != null)
258 extension.AnyAttribute = anyattr;
259 continue;
261 reader.RaiseInvalidElementError();
263 return extension;