2002-04-26 Duncan Mak <duncan@ximian.com>
[mono.git] / mcs / class / System.XML / System.Xml.Schema / XmlSchemaAttribute.cs
1 // Author: Dwivedi, Ajay kumar\r
2 //            Adwiv@Yahoo.com\r
3 using System;\r
4 using System.Xml;\r
5 using System.ComponentModel;\r
6 using System.Xml.Serialization;\r
7 \r
8 namespace System.Xml.Schema\r
9 {\r
10         /// <summary>\r
11         /// Summary description for XmlSchemaAttribute.\r
12         /// </summary>\r
13         public class XmlSchemaAttribute : XmlSchemaAnnotated\r
14         {\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
30                 \r
31                 public XmlSchemaAttribute()\r
32                 {\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
40                 }\r
41 \r
42                 // Properties\r
43                 [XmlIgnore]\r
44                 public object AttributeType \r
45                 { //FIXME: This is not correct. Is it?\r
46                         get{ return attributeType; }\r
47                 }\r
48 \r
49                 [DefaultValue(null)]\r
50                 [System.Xml.Serialization.XmlAttribute("default")]\r
51                 public string DefaultValue \r
52                 {\r
53                         get{ return defaultValue;}\r
54                         set\r
55                         { // Default Value and fixed Value are mutually exclusive\r
56                                 fixedValue = null;\r
57                                 defaultValue = value;\r
58                         }\r
59                 }\r
60 \r
61                 [DefaultValue(null)]\r
62                 [System.Xml.Serialization.XmlAttribute("fixed")]\r
63                 public string FixedValue \r
64                 {\r
65                         get{ return fixedValue;}\r
66                         set\r
67                         { // Default Value and fixed Value are mutually exclusive\r
68                                 defaultValue = null;\r
69                                 fixedValue = value;\r
70                         }\r
71                 }\r
72 \r
73                 [DefaultValue(XmlSchemaForm.None)]\r
74                 [System.Xml.Serialization.XmlAttribute("form")]\r
75                 public XmlSchemaForm Form \r
76                 {\r
77                         get{ return form;}\r
78                         set{ form = value;}\r
79                 }\r
80 \r
81                 [System.Xml.Serialization.XmlAttribute("name")]\r
82                 public string Name \r
83                 {\r
84                         get{ return name;}\r
85                         set\r
86                         {\r
87                                 name  = value;\r
88                         }\r
89                 }\r
90                 [XmlIgnore]\r
91                 public XmlQualifiedName QualifiedName \r
92                 {\r
93                         get{ return qualifiedName;}\r
94                 }\r
95 \r
96                 [System.Xml.Serialization.XmlAttribute("ref")]\r
97                 public XmlQualifiedName RefName \r
98                 {\r
99                         get{ return refName;}\r
100                         set\r
101                         {\r
102                                 refName = value; \r
103                         }\r
104                 }\r
105 \r
106                 [XmlElement("simpleType",Namespace="http://www.w3.org/2001/XMLSchema")]\r
107                 public XmlSchemaSimpleType SchemaType \r
108                 {\r
109                         get{ return schemaType;}\r
110                         set{ schemaType = value;}\r
111                 }\r
112                 \r
113                 [System.Xml.Serialization.XmlAttribute("type")]\r
114                 public XmlQualifiedName SchemaTypeName \r
115                 {\r
116                         get{ return schemaTypeName;}\r
117                         set{ schemaTypeName = value;}\r
118                 }\r
119 \r
120                 [DefaultValue(XmlSchemaUse.None)]\r
121                 [System.Xml.Serialization.XmlAttribute("use")]\r
122                 public XmlSchemaUse Use \r
123                 {\r
124                         get{ return use;}\r
125                         set{ use = value;}\r
126                 }\r
127                 /// <remarks>\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
152                 /// </remarks>\r
153                 [MonoTODO]\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
156                 {\r
157                         if(parentIsSchema)//a\r
158                         {\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
172                                 else\r
173                                         this.qualifiedName = new XmlQualifiedName(this.name, info.targetNS);    \r
174         \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
180                                 {\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
183                                         else \r
184                                         {\r
185                                                 this.SchemaType.islocal = true;\r
186                                                 this.schemaType.Compile(h,info); \r
187                                         }\r
188                                 }\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
192 \r
193                         }\r
194                         // TODO: else\r
195                         return errorOccured;\r
196                 }\r
197                 \r
198                 [MonoTODO]\r
199                 internal bool Validate(ValidationEventHandler h)\r
200                 {\r
201                         return false;\r
202                 }\r
203                 \r
204                 internal void error(ValidationEventHandler handle,string message)\r
205                 {\r
206                         this.errorOccured = true;\r
207                         ValidationHandler.RaiseValidationError(handle,this,message);\r
208                 }\r
209         }\r
210 }\r