2 // System.Xml.Schema.XmlSchemaSimpleContentExtension.cs
5 // Dwivedi, Ajay kumar Adwiv@Yahoo.com
6 // Atsushi Enomoto ginga@kit.hi-ho.ne.jp
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
31 using System
.Xml
.Serialization
;
33 namespace System
.Xml
.Schema
36 /// Summary description for XmlSchemaSimpleContentExtension.
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
73 // internal properties
74 internal override bool IsExtension
{
79 /// 1. Base must be present and a QName
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
))
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
);
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
;
130 internal override XmlQualifiedName
GetBaseTypeName ()
135 internal override XmlSchemaParticle
GetParticle ()
140 internal override int Validate(ValidationEventHandler h
, XmlSchema schema
)
142 if (IsValidated (schema
.ValidationId
))
145 XmlSchemaType st
= schema
.SchemaTypes
[baseTypeName
] as XmlSchemaType
;
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
;
170 //{any attributes with non-schema namespace . . .}>
171 //Content: (annotation?, ((attribute | attributeGroup)*, anyAttribute?))
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);
185 extension
.LineNumber
= reader
.LineNumber
;
186 extension
.LinePosition
= reader
.LinePosition
;
187 extension
.SourceUri
= reader
.BaseURI
;
189 while(reader
.MoveToNextAttribute())
191 if(reader
.Name
== "base")
194 extension
.baseTypeName
= XmlSchemaUtil
.ReadQNameAttribute(reader
,out innerex
);
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);
208 XmlSchemaUtil
.ReadUnhandledAttribute(reader
,extension
);
212 reader
.MoveToElement();
213 if(reader
.IsEmptyElement
)
216 //Content: 1.annotation?, 2.(attribute | attributeGroup)*, 3.anyAttribute?
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);
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
;
236 if(reader
.LocalName
== "attribute")
239 XmlSchemaAttribute attr
= XmlSchemaAttribute
.Read(reader
,h
);
241 extension
.Attributes
.Add(attr
);
244 if(reader
.LocalName
== "attributeGroup")
247 XmlSchemaAttributeGroupRef attr
= XmlSchemaAttributeGroupRef
.Read(reader
,h
);
249 extension
.attributes
.Add(attr
);
253 if(level
<= 3 && reader
.LocalName
== "anyAttribute")
256 XmlSchemaAnyAttribute anyattr
= XmlSchemaAnyAttribute
.Read(reader
,h
);
258 extension
.AnyAttribute
= anyattr
;
261 reader
.RaiseInvalidElementError();