1 // Author: Dwivedi, Ajay kumar
\r
5 using System.ComponentModel;
\r
6 using System.Xml.Serialization;
\r
8 namespace System.Xml.Schema
\r
11 /// Summary description for XmlSchemaAttribute.
\r
13 public class XmlSchemaAttribute : XmlSchemaAnnotated
\r
15 private object attributeType;
\r
16 private string defaultValue;
\r
17 private string fixedValue;
\r
18 private XmlSchemaForm form;
\r
19 private string name;
\r
20 private XmlQualifiedName qualifiedName;
\r
21 private XmlQualifiedName refName;
\r
22 private XmlSchemaSimpleType schemaType;
\r
23 private XmlQualifiedName schemaTypeName;
\r
24 private XmlSchemaUse use;
\r
25 //Compilation fields
\r
26 private string targetNamespace;
\r
27 internal bool parentIsSchema = false;
\r
28 internal XmlSchema schema = null;
\r
29 internal bool errorOccured = false;
\r
31 public XmlSchemaAttribute()
\r
33 //FIXME: Docs says the default is optional.
\r
34 //Whereas the MS implementation has default None.
\r
35 form = XmlSchemaForm.None;
\r
36 use = XmlSchemaUse.None;
\r
37 schemaTypeName = XmlQualifiedName.Empty;
\r
38 qualifiedName = XmlQualifiedName.Empty;
\r
39 refName = XmlQualifiedName.Empty;
\r
44 public object AttributeType
\r
45 { //FIXME: This is not correct. Is it?
\r
46 get{ return attributeType; }
\r
49 [DefaultValue(null)]
\r
50 [System.Xml.Serialization.XmlAttribute("default")]
\r
51 public string DefaultValue
\r
53 get{ return defaultValue;}
\r
55 { // Default Value and fixed Value are mutually exclusive
\r
57 defaultValue = value;
\r
61 [DefaultValue(null)]
\r
62 [System.Xml.Serialization.XmlAttribute("fixed")]
\r
63 public string FixedValue
\r
65 get{ return fixedValue;}
\r
67 { // Default Value and fixed Value are mutually exclusive
\r
68 defaultValue = null;
\r
73 [DefaultValue(XmlSchemaForm.None)]
\r
74 [System.Xml.Serialization.XmlAttribute("form")]
\r
75 public XmlSchemaForm Form
\r
81 [System.Xml.Serialization.XmlAttribute("name")]
\r
91 public XmlQualifiedName QualifiedName
\r
93 get{ return qualifiedName;}
\r
96 [System.Xml.Serialization.XmlAttribute("ref")]
\r
97 public XmlQualifiedName RefName
\r
99 get{ return refName;}
\r
106 [XmlElement("simpleType",Namespace="http://www.w3.org/2001/XMLSchema")]
\r
107 public XmlSchemaSimpleType SchemaType
\r
109 get{ return schemaType;}
\r
110 set{ schemaType = value;}
\r
113 [System.Xml.Serialization.XmlAttribute("type")]
\r
114 public XmlQualifiedName SchemaTypeName
\r
116 get{ return schemaTypeName;}
\r
117 set{ schemaTypeName = value;}
\r
120 [DefaultValue(XmlSchemaUse.None)]
\r
121 [System.Xml.Serialization.XmlAttribute("use")]
\r
122 public XmlSchemaUse Use
\r
128 /// For an attribute:
\r
129 /// a) If the parent is schema
\r
130 /// 1-5 are from <xs:complexType name="topLevelAttribute"> in the Schema for Schema
\r
131 /// 6-8 are from "Constraints on XML Representations of Attribute Declarations"
\r
132 /// 9-10 are from "Attribute Declaration Schema Component"
\r
133 /// 11-16 are from "Constraints on Attribute Declaration Schema Components"
\r
134 /// 1. ref must be absent
\r
135 /// 2. form must be absent
\r
136 /// 3. use must be absent
\r
137 /// 4. name must be present and of type NCName
\r
138 /// 5. *NO CHECK REQUIRED* Only simple types and annotation are allowed as content
\r
139 /// 6. default and fixed must not both be present.
\r
140 /// 7. *NO CHECK REQUIRED* If default and use are both present... (Not possible since use is absent)
\r
141 /// 8. type and <simpleType> must not both be present.
\r
142 /// 9. Target Namespace should be schema's targetnamespace or absent
\r
143 /// 10. Type Definiton coressponds to <simpletype> element, or type value, or absent
\r
144 /// 11. *TO UNDERSTAND* Missing Sub-components
\r
145 /// 12. value constraint must be of the same datatype as of type
\r
146 /// 13. if the type definition is ID then there should be no value constraint.
\r
147 /// 14. name must not be xmlns
\r
148 /// 15. Targetnamespace must not be xsi. This implies the target namespace of schema can't be xsi if toplevel attributes are used.
\r
149 /// 16. *Exception to rule 15* inbuilt attributes: xsi:nil, xsi:type, xsi:schemaLocation, xsi: noNamespaceSchemaLocation
\r
150 /// b) *TODO*: If the parent is complextype and ref is not set
\r
151 /// c) *TODO*: if the parent is not schema and ref is set
\r
154 //FIXME: Should we set a property to null if an error occurs? Or should we stop the validation?
\r
155 internal bool Compile(ValidationEventHandler h, XmlSchemaInfo info)
\r
157 if(parentIsSchema)//a
\r
159 if(this.refName!= null && !this.refName.IsEmpty) // a.1
\r
160 error(h,"ref must be absent in the top level <attribute>");
\r
161 if(this.form!= XmlSchemaForm.None) // a.2
\r
162 error(h,"form must be absent in the top level <attribute>");
\r
163 if(this.use!= XmlSchemaUse.None) // a.3
\r
164 error(h,"use must be absent in the top level <attribute>");
\r
165 if(this.name == null) //a.4
\r
166 error(h,"name must be present if attribute has schema as its parent");
\r
167 // FIXME: A better way to check NCName? Something like IsNCName()?
\r
168 else if(this.name.IndexOf(":") != -1) // a.4.2
\r
169 error(h,"attribute name must be NCName");
\r
170 else if(this.name == "xmlns") // a.14
\r
171 error(h,"attribute name can't be xmlns");
\r
173 this.qualifiedName = new XmlQualifiedName(this.name, info.targetNS);
\r
175 // TODO: a.10, a.11, a.12, a.13
\r
176 if(this.defaultValue != null && this.fixedValue != null) // a.6
\r
177 error(h,"default and fixed must not both be present in an Attribute");
\r
178 this.targetNamespace = this.schema.TargetNamespace; // a.9
\r
179 if(this.schemaType != null)
\r
181 if(this.schemaTypeName != null && !this.SchemaTypeName.IsEmpty) // a.8
\r
182 error(h,"attribute can't have both a type and <simpleType> content");
\r
185 this.SchemaType.islocal = true;
\r
186 this.schemaType.Compile(h,info);
\r
189 if(this.targetNamespace == XmlSchema.InstanceNamespace && this.name != "nil" && this.name != "type"
\r
190 && this.name != "schemaLocation" && this.name != "noNamespaceSchemaLocation") // a.15, a.16
\r
191 error(h,"targetNamespace can't be " + XmlSchema.InstanceNamespace);
\r
195 return errorOccured;
\r
199 internal bool Validate(ValidationEventHandler h)
\r
204 internal void error(ValidationEventHandler handle,string message)
\r
206 this.errorOccured = true;
\r
207 ValidationHandler.RaiseValidationError(handle,this,message);
\r