Updates referencesource to .NET 4.7
[mono.git] / mcs / class / referencesource / System.Xml / System / Xml / Schema / XmlSchemaElement.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="XmlSchemaElement.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>  
5 // <owner current="true" primary="true">Microsoft</owner>                                                              
6 //------------------------------------------------------------------------------
7
8 using System.ComponentModel;
9 using System.Xml.Serialization;
10 using System.Diagnostics;
11
12 namespace System.Xml.Schema {
13
14
15     /// <include file='doc\XmlSchemaElement.uex' path='docs/doc[@for="XmlSchemaElement"]/*' />
16     /// <devdoc>
17     ///    <para>[To be supplied.]</para>
18     /// </devdoc>
19     public class XmlSchemaElement : XmlSchemaParticle {
20         bool isAbstract;
21         bool hasAbstractAttribute;
22         bool isNillable;
23         bool hasNillableAttribute;
24         bool isLocalTypeDerivationChecked;
25
26         XmlSchemaDerivationMethod block = XmlSchemaDerivationMethod.None;
27         XmlSchemaDerivationMethod final = XmlSchemaDerivationMethod.None;
28         XmlSchemaForm form = XmlSchemaForm.None;
29         string defaultValue;
30         string fixedValue;
31         string name;        
32         
33         XmlQualifiedName refName = XmlQualifiedName.Empty;
34         XmlQualifiedName substitutionGroup = XmlQualifiedName.Empty;
35         XmlQualifiedName typeName = XmlQualifiedName.Empty;
36         XmlSchemaType type = null;
37
38         XmlQualifiedName qualifiedName = XmlQualifiedName.Empty;
39         XmlSchemaType elementType;
40         XmlSchemaDerivationMethod blockResolved;
41         XmlSchemaDerivationMethod finalResolved;
42         XmlSchemaObjectCollection constraints;
43         SchemaElementDecl elementDecl;
44
45
46         /// <include file='doc\XmlSchemaElement.uex' path='docs/doc[@for="XmlSchemaElement.IsAbstract"]/*' />
47         /// <devdoc>
48         ///    <para>[To be supplied.]</para>
49         /// </devdoc>
50         [XmlAttribute("abstract"), DefaultValue(false)]
51         public bool IsAbstract {
52             get { return isAbstract; }
53             set { 
54                 isAbstract = value; 
55                 hasAbstractAttribute = true;
56             }
57         }
58
59         /// <include file='doc\XmlSchemaElement.uex' path='docs/doc[@for="XmlSchemaElement.Block"]/*' />
60         /// <devdoc>
61         ///    <para>[To be supplied.]</para>
62         /// </devdoc>
63         [XmlAttribute("block"), DefaultValue(XmlSchemaDerivationMethod.None)]
64         public XmlSchemaDerivationMethod Block {
65              get { return block; }
66              set { block = value; }
67         }
68
69         /// <include file='doc\XmlSchemaElement.uex' path='docs/doc[@for="XmlSchemaElement.DefaultValue"]/*' />
70         /// <devdoc>
71         ///    <para>[To be supplied.]</para>
72         /// </devdoc>
73         [XmlAttribute("default")]
74         [DefaultValue(null)]
75         public string DefaultValue { 
76             get { return defaultValue; }
77             set { defaultValue = value; }
78         }
79
80         /// <include file='doc\XmlSchemaElement.uex' path='docs/doc[@for="XmlSchemaElement.Final"]/*' />
81         /// <devdoc>
82         ///    <para>[To be supplied.]</para>
83         /// </devdoc>
84         [XmlAttribute("final"), DefaultValue(XmlSchemaDerivationMethod.None)]
85         public XmlSchemaDerivationMethod Final {
86              get { return final; }
87              set { final = value; }
88         }
89
90         /// <include file='doc\XmlSchemaElement.uex' path='docs/doc[@for="XmlSchemaElement.FixedValue"]/*' />
91         /// <devdoc>
92         ///    <para>[To be supplied.]</para>
93         /// </devdoc>
94         [XmlAttribute("fixed")]
95         [DefaultValue(null)]
96         public string FixedValue { 
97             get { return fixedValue; }
98             set { fixedValue = value; }
99         }
100
101         /// <include file='doc\XmlSchemaElement.uex' path='docs/doc[@for="XmlSchemaElement.Form"]/*' />
102         /// <devdoc>
103         ///    <para>[To be supplied.]</para>
104         /// </devdoc>
105         [XmlAttribute("form"), DefaultValue(XmlSchemaForm.None)]
106         public XmlSchemaForm Form {
107              get { return form; }
108              set { form = value; }
109         }
110
111         /// <include file='doc\XmlSchemaElement.uex' path='docs/doc[@for="XmlSchemaElement.Name"]/*' />
112         /// <devdoc>
113         ///    <para>[To be supplied.]</para>
114         /// </devdoc>
115         [XmlAttribute("name"), DefaultValue("")]
116         public string Name { 
117             get { return name; }
118             set { name = value; }
119         }
120         
121         /// <include file='doc\XmlSchemaElement.uex' path='docs/doc[@for="XmlSchemaElement.IsNillable"]/*' />
122         /// <devdoc>
123         ///    <para>[To be supplied.]</para>
124         /// </devdoc>
125         [XmlAttribute("nillable"), DefaultValue(false)]
126         public bool IsNillable {
127             get { return isNillable; }
128             set { isNillable = value; hasNillableAttribute = true; }
129         }
130
131         [XmlIgnore]
132         internal bool HasNillableAttribute {
133             get { return hasNillableAttribute; } 
134         } 
135         
136         [XmlIgnore]
137         internal bool HasAbstractAttribute {
138             get { return hasAbstractAttribute; } 
139         } 
140         /// <include file='doc\XmlSchemaElement.uex' path='docs/doc[@for="XmlSchemaElement.RefName"]/*' />
141         /// <devdoc>
142         ///    <para>[To be supplied.]</para>
143         /// </devdoc>
144         [XmlAttribute("ref")]
145         public XmlQualifiedName RefName { 
146             get { return refName; }
147             set { refName = (value == null ? XmlQualifiedName.Empty : value); }
148         }
149         
150         /// <include file='doc\XmlSchemaElement.uex' path='docs/doc[@for="XmlSchemaElement.SubstitutionGroup"]/*' />
151         /// <devdoc>
152         ///    <para>[To be supplied.]</para>
153         /// </devdoc>
154         [XmlAttribute("substitutionGroup")]
155         public XmlQualifiedName SubstitutionGroup {
156             get { return substitutionGroup; }
157             set { substitutionGroup = (value == null ? XmlQualifiedName.Empty : value); }
158         }
159     
160         /// <include file='doc\XmlSchemaElement.uex' path='docs/doc[@for="XmlSchemaElement.SchemaTypeName"]/*' />
161         /// <devdoc>
162         ///    <para>[To be supplied.]</para>
163         /// </devdoc>
164         [XmlAttribute("type")]
165         public XmlQualifiedName SchemaTypeName { 
166             get { return typeName; }
167             set { typeName = (value == null ? XmlQualifiedName.Empty : value); }
168         }
169         
170         /// <include file='doc\XmlSchemaElement.uex' path='docs/doc[@for="XmlSchemaElement.SchemaType"]/*' />
171         /// <devdoc>
172         ///    <para>[To be supplied.]</para>
173         /// </devdoc>
174         [XmlElement("complexType", typeof(XmlSchemaComplexType)),
175          XmlElement("simpleType", typeof(XmlSchemaSimpleType))]
176         public XmlSchemaType SchemaType {
177             get { return type; }
178             set { type = value; }
179         }
180         
181         /// <include file='doc\XmlSchemaElement.uex' path='docs/doc[@for="XmlSchemaElement.Constraints"]/*' />
182         /// <devdoc>
183         ///    <para>[To be supplied.]</para>
184         /// </devdoc>
185         [XmlElement("key", typeof(XmlSchemaKey)),
186          XmlElement("keyref", typeof(XmlSchemaKeyref)),
187          XmlElement("unique", typeof(XmlSchemaUnique))]
188         public XmlSchemaObjectCollection Constraints {
189             get {
190                 if (constraints == null) {
191                     constraints = new XmlSchemaObjectCollection();
192                 }
193                 return constraints;
194             }
195         }
196
197         /// <include file='doc\XmlSchemaElement.uex' path='docs/doc[@for="XmlSchemaElement.QualifiedName"]/*' />
198         /// <devdoc>
199         ///    <para>[To be supplied.]</para>
200         /// </devdoc>
201         [XmlIgnore]
202         public XmlQualifiedName QualifiedName { 
203             get { return qualifiedName; }
204         }
205
206         /// <include file='doc\XmlSchemaElement.uex' path='docs/doc[@for="XmlSchemaElement.ElementType"]/*' />
207         /// <devdoc>
208         ///    <para>[To be supplied.]</para>
209         /// </devdoc>
210         [XmlIgnore]
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 {
213             get {
214                 if (elementType == null)
215                     return null;
216
217                 if (elementType.QualifiedName.Namespace == XmlReservedNs.NsXs) {
218                     return elementType.Datatype; //returns XmlSchemaDatatype;
219                 }
220                 return elementType; 
221             }
222         }
223         
224         /// <include file='doc\XmlSchemaElement.uex' path='docs/doc[@for="XmlSchemaElement.ElementType"]/*' />
225         /// <devdoc>
226         ///    <para>[To be supplied.]</para>
227         /// </devdoc>
228         [XmlIgnore]
229         public XmlSchemaType ElementSchemaType {
230             get { return elementType; }
231         }
232
233         /// <include file='doc\XmlSchemaElement.uex' path='docs/doc[@for="XmlSchemaElement.BlockResolved"]/*' />
234         /// <devdoc>
235         ///    <para>[To be supplied.]</para>
236         /// </devdoc>
237         [XmlIgnore]
238         public XmlSchemaDerivationMethod BlockResolved {
239              get { return blockResolved; }
240         }
241
242         /// <include file='doc\XmlSchemaElement.uex' path='docs/doc[@for="XmlSchemaElement.FinalResolved"]/*' />
243         /// <devdoc>
244         ///    <para>[To be supplied.]</para>
245         /// </devdoc>
246         [XmlIgnore]
247         public XmlSchemaDerivationMethod FinalResolved {
248              get { return finalResolved; }
249         }
250
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);
258             }
259             return null;
260         }
261
262         internal void SetQualifiedName(XmlQualifiedName value) { 
263             qualifiedName = value;
264         }
265
266         internal void SetElementType(XmlSchemaType value) { 
267             elementType = value;
268         }
269
270         internal void SetBlockResolved(XmlSchemaDerivationMethod value) {
271              blockResolved = value; 
272         }
273
274         internal void SetFinalResolved(XmlSchemaDerivationMethod value) {
275              finalResolved = value; 
276         }
277
278         [XmlIgnore]
279         internal bool HasDefault {
280             get { return defaultValue != null && defaultValue.Length > 0; }
281         }
282
283         internal bool HasConstraints {
284             get { return constraints != null && constraints.Count > 0; }
285         }
286
287         internal bool IsLocalTypeDerivationChecked {
288             get {
289                 return isLocalTypeDerivationChecked;
290             }
291             set {
292                 isLocalTypeDerivationChecked = value;
293             }
294         }
295
296         internal SchemaElementDecl ElementDecl {
297             get { return elementDecl; }
298             set { elementDecl = value; }
299         }
300
301         [XmlIgnore]
302         internal override string NameAttribute {
303             get { return Name; }
304             set { Name = value; }
305         }
306
307         [XmlIgnore]
308         internal override string NameString {
309             get {
310                 return qualifiedName.ToString();
311             }
312         }
313
314         internal override XmlSchemaObject Clone() {
315             System.Diagnostics.Debug.Assert(false, "Should never call Clone() on XmlSchemaElement. Call Clone(XmlSchema) instead.");
316             return Clone(null);
317         } 
318
319         internal XmlSchemaObject Clone(XmlSchema parentSchema) {
320             XmlSchemaElement newElem = (XmlSchemaElement)MemberwiseClone();
321
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);
333             }
334
335             //Clear compiled tables
336             newElem.constraints = null;
337             return newElem;
338         }
339     }
340 }
341