1 //------------------------------------------------------------------------------
2 // <copyright file="XmlSchemaElement.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
5 // <owner current="true" primary="true">Microsoft</owner>
6 //------------------------------------------------------------------------------
8 using System.ComponentModel;
9 using System.Xml.Serialization;
10 using System.Diagnostics;
12 namespace System.Xml.Schema {
15 /// <include file='doc\XmlSchemaElement.uex' path='docs/doc[@for="XmlSchemaElement"]/*' />
17 /// <para>[To be supplied.]</para>
19 public class XmlSchemaElement : XmlSchemaParticle {
21 bool hasAbstractAttribute;
23 bool hasNillableAttribute;
24 bool isLocalTypeDerivationChecked;
26 XmlSchemaDerivationMethod block = XmlSchemaDerivationMethod.None;
27 XmlSchemaDerivationMethod final = XmlSchemaDerivationMethod.None;
28 XmlSchemaForm form = XmlSchemaForm.None;
33 XmlQualifiedName refName = XmlQualifiedName.Empty;
34 XmlQualifiedName substitutionGroup = XmlQualifiedName.Empty;
35 XmlQualifiedName typeName = XmlQualifiedName.Empty;
36 XmlSchemaType type = null;
38 XmlQualifiedName qualifiedName = XmlQualifiedName.Empty;
39 XmlSchemaType elementType;
40 XmlSchemaDerivationMethod blockResolved;
41 XmlSchemaDerivationMethod finalResolved;
42 XmlSchemaObjectCollection constraints;
43 SchemaElementDecl elementDecl;
46 /// <include file='doc\XmlSchemaElement.uex' path='docs/doc[@for="XmlSchemaElement.IsAbstract"]/*' />
48 /// <para>[To be supplied.]</para>
50 [XmlAttribute("abstract"), DefaultValue(false)]
51 public bool IsAbstract {
52 get { return isAbstract; }
55 hasAbstractAttribute = true;
59 /// <include file='doc\XmlSchemaElement.uex' path='docs/doc[@for="XmlSchemaElement.Block"]/*' />
61 /// <para>[To be supplied.]</para>
63 [XmlAttribute("block"), DefaultValue(XmlSchemaDerivationMethod.None)]
64 public XmlSchemaDerivationMethod Block {
66 set { block = value; }
69 /// <include file='doc\XmlSchemaElement.uex' path='docs/doc[@for="XmlSchemaElement.DefaultValue"]/*' />
71 /// <para>[To be supplied.]</para>
73 [XmlAttribute("default")]
75 public string DefaultValue {
76 get { return defaultValue; }
77 set { defaultValue = value; }
80 /// <include file='doc\XmlSchemaElement.uex' path='docs/doc[@for="XmlSchemaElement.Final"]/*' />
82 /// <para>[To be supplied.]</para>
84 [XmlAttribute("final"), DefaultValue(XmlSchemaDerivationMethod.None)]
85 public XmlSchemaDerivationMethod Final {
87 set { final = value; }
90 /// <include file='doc\XmlSchemaElement.uex' path='docs/doc[@for="XmlSchemaElement.FixedValue"]/*' />
92 /// <para>[To be supplied.]</para>
94 [XmlAttribute("fixed")]
96 public string FixedValue {
97 get { return fixedValue; }
98 set { fixedValue = value; }
101 /// <include file='doc\XmlSchemaElement.uex' path='docs/doc[@for="XmlSchemaElement.Form"]/*' />
103 /// <para>[To be supplied.]</para>
105 [XmlAttribute("form"), DefaultValue(XmlSchemaForm.None)]
106 public XmlSchemaForm Form {
108 set { form = value; }
111 /// <include file='doc\XmlSchemaElement.uex' path='docs/doc[@for="XmlSchemaElement.Name"]/*' />
113 /// <para>[To be supplied.]</para>
115 [XmlAttribute("name"), DefaultValue("")]
118 set { name = value; }
121 /// <include file='doc\XmlSchemaElement.uex' path='docs/doc[@for="XmlSchemaElement.IsNillable"]/*' />
123 /// <para>[To be supplied.]</para>
125 [XmlAttribute("nillable"), DefaultValue(false)]
126 public bool IsNillable {
127 get { return isNillable; }
128 set { isNillable = value; hasNillableAttribute = true; }
132 internal bool HasNillableAttribute {
133 get { return hasNillableAttribute; }
137 internal bool HasAbstractAttribute {
138 get { return hasAbstractAttribute; }
140 /// <include file='doc\XmlSchemaElement.uex' path='docs/doc[@for="XmlSchemaElement.RefName"]/*' />
142 /// <para>[To be supplied.]</para>
144 [XmlAttribute("ref")]
145 public XmlQualifiedName RefName {
146 get { return refName; }
147 set { refName = (value == null ? XmlQualifiedName.Empty : value); }
150 /// <include file='doc\XmlSchemaElement.uex' path='docs/doc[@for="XmlSchemaElement.SubstitutionGroup"]/*' />
152 /// <para>[To be supplied.]</para>
154 [XmlAttribute("substitutionGroup")]
155 public XmlQualifiedName SubstitutionGroup {
156 get { return substitutionGroup; }
157 set { substitutionGroup = (value == null ? XmlQualifiedName.Empty : value); }
160 /// <include file='doc\XmlSchemaElement.uex' path='docs/doc[@for="XmlSchemaElement.SchemaTypeName"]/*' />
162 /// <para>[To be supplied.]</para>
164 [XmlAttribute("type")]
165 public XmlQualifiedName SchemaTypeName {
166 get { return typeName; }
167 set { typeName = (value == null ? XmlQualifiedName.Empty : value); }
170 /// <include file='doc\XmlSchemaElement.uex' path='docs/doc[@for="XmlSchemaElement.SchemaType"]/*' />
172 /// <para>[To be supplied.]</para>
174 [XmlElement("complexType", typeof(XmlSchemaComplexType)),
175 XmlElement("simpleType", typeof(XmlSchemaSimpleType))]
176 public XmlSchemaType SchemaType {
178 set { type = value; }
181 /// <include file='doc\XmlSchemaElement.uex' path='docs/doc[@for="XmlSchemaElement.Constraints"]/*' />
183 /// <para>[To be supplied.]</para>
185 [XmlElement("key", typeof(XmlSchemaKey)),
186 XmlElement("keyref", typeof(XmlSchemaKeyref)),
187 XmlElement("unique", typeof(XmlSchemaUnique))]
188 public XmlSchemaObjectCollection Constraints {
190 if (constraints == null) {
191 constraints = new XmlSchemaObjectCollection();
197 /// <include file='doc\XmlSchemaElement.uex' path='docs/doc[@for="XmlSchemaElement.QualifiedName"]/*' />
199 /// <para>[To be supplied.]</para>
202 public XmlQualifiedName QualifiedName {
203 get { return qualifiedName; }
206 /// <include file='doc\XmlSchemaElement.uex' path='docs/doc[@for="XmlSchemaElement.ElementType"]/*' />
208 /// <para>[To be supplied.]</para>
211 [Obsolete("This property has been deprecated. Please use ElementSchemaType property that returns a strongly typed element type. http://go.microsoft.com/fwlink/?linkid=14202")]
212 public object ElementType {
214 if (elementType == null)
217 if (elementType.QualifiedName.Namespace == XmlReservedNs.NsXs) {
218 return elementType.Datatype; //returns XmlSchemaDatatype;
224 /// <include file='doc\XmlSchemaElement.uex' path='docs/doc[@for="XmlSchemaElement.ElementType"]/*' />
226 /// <para>[To be supplied.]</para>
229 public XmlSchemaType ElementSchemaType {
230 get { return elementType; }
233 /// <include file='doc\XmlSchemaElement.uex' path='docs/doc[@for="XmlSchemaElement.BlockResolved"]/*' />
235 /// <para>[To be supplied.]</para>
238 public XmlSchemaDerivationMethod BlockResolved {
239 get { return blockResolved; }
242 /// <include file='doc\XmlSchemaElement.uex' path='docs/doc[@for="XmlSchemaElement.FinalResolved"]/*' />
244 /// <para>[To be supplied.]</para>
247 public XmlSchemaDerivationMethod FinalResolved {
248 get { return finalResolved; }
251 internal XmlReader Validate(XmlReader reader, XmlResolver resolver, XmlSchemaSet schemaSet , ValidationEventHandler valEventHandler) {
252 if (schemaSet != null) {
253 XmlReaderSettings readerSettings = new XmlReaderSettings();
254 readerSettings.ValidationType = ValidationType.Schema;
255 readerSettings.Schemas = schemaSet;
256 readerSettings.ValidationEventHandler += valEventHandler;
257 return new XsdValidatingReader(reader, resolver, readerSettings, this);
262 internal void SetQualifiedName(XmlQualifiedName value) {
263 qualifiedName = value;
266 internal void SetElementType(XmlSchemaType value) {
270 internal void SetBlockResolved(XmlSchemaDerivationMethod value) {
271 blockResolved = value;
274 internal void SetFinalResolved(XmlSchemaDerivationMethod value) {
275 finalResolved = value;
279 internal bool HasDefault {
280 get { return defaultValue != null && defaultValue.Length > 0; }
283 internal bool HasConstraints {
284 get { return constraints != null && constraints.Count > 0; }
287 internal bool IsLocalTypeDerivationChecked {
289 return isLocalTypeDerivationChecked;
292 isLocalTypeDerivationChecked = value;
296 internal SchemaElementDecl ElementDecl {
297 get { return elementDecl; }
298 set { elementDecl = value; }
302 internal override string NameAttribute {
304 set { Name = value; }
308 internal override string NameString {
310 return qualifiedName.ToString();
314 internal override XmlSchemaObject Clone() {
315 System.Diagnostics.Debug.Assert(false, "Should never call Clone() on XmlSchemaElement. Call Clone(XmlSchema) instead.");
319 internal XmlSchemaObject Clone(XmlSchema parentSchema) {
320 XmlSchemaElement newElem = (XmlSchemaElement)MemberwiseClone();
322 //Deep clone the QNames as these will be updated on chameleon includes
323 newElem.refName = this.refName.Clone();
324 newElem.substitutionGroup = this.substitutionGroup.Clone();
325 newElem.typeName = this.typeName.Clone();
326 newElem.qualifiedName = this.qualifiedName.Clone();
327 // If this element has a complex type which is anonymous (declared in place with the element)
328 // it needs to be cloned as well, since it may contain named elements and such. And these names
329 // will need to be cloned since they may change their namespace on chameleon includes
330 XmlSchemaComplexType complexType = this.type as XmlSchemaComplexType;
331 if (complexType != null && complexType.QualifiedName.IsEmpty) {
332 newElem.type = (XmlSchemaType)complexType.Clone(parentSchema);
335 //Clear compiled tables
336 newElem.constraints = null;