2 // System.Xml.Schema.XmlSchemaAttribute.cs
\r
5 // Dwivedi, Ajay kumar Adwiv@Yahoo.com
\r
6 // Enomoto, Atsushi ginga@kit.hi-ho.ne.jp
\r
10 using System.ComponentModel;
\r
11 using System.Xml.Serialization;
\r
13 namespace System.Xml.Schema
\r
16 /// Summary description for XmlSchemaAttribute.
\r
18 public class XmlSchemaAttribute : XmlSchemaAnnotated
\r
20 private object attributeType;
\r
21 private string defaultValue;
\r
22 private string fixedValue;
\r
23 private string validatedDefaultValue;
\r
24 private string validatedFixedValue;
\r
25 private XmlSchemaForm form;
\r
26 private string name;
\r
27 private string targetNamespace;
\r
28 private XmlQualifiedName qualifiedName;
\r
29 private XmlQualifiedName refName;
\r
30 private XmlSchemaSimpleType schemaType;
\r
31 private XmlQualifiedName schemaTypeName;
\r
32 private XmlSchemaUse use;
\r
33 //Compilation fields
\r
34 internal bool ParentIsSchema = false;
\r
35 private XmlSchemaAttribute referencedAttribute;
\r
36 private static string xmlname = "attribute";
\r
38 public XmlSchemaAttribute()
\r
40 //FIXME: Docs says the default is optional.
\r
41 //Whereas the MS implementation has default None.
\r
42 form = XmlSchemaForm.None;
\r
43 use = XmlSchemaUse.None;
\r
44 schemaTypeName = XmlQualifiedName.Empty;
\r
45 qualifiedName = XmlQualifiedName.Empty;
\r
46 refName = XmlQualifiedName.Empty;
\r
52 [DefaultValue(null)]
\r
53 [System.Xml.Serialization.XmlAttribute("default")]
\r
54 public string DefaultValue
\r
56 get{ return defaultValue;}
\r
58 { // Default Value and fixed Value are mutually exclusive
\r
60 defaultValue = value;
\r
64 [DefaultValue(null)]
\r
65 [System.Xml.Serialization.XmlAttribute("fixed")]
\r
66 public string FixedValue
\r
68 get{ return fixedValue;}
\r
70 { // Default Value and fixed Value are mutually exclusive
\r
71 defaultValue = null;
\r
76 [DefaultValue(XmlSchemaForm.None)]
\r
77 [System.Xml.Serialization.XmlAttribute("form")]
\r
78 public XmlSchemaForm Form
\r
84 [System.Xml.Serialization.XmlAttribute("name")]
\r
94 [System.Xml.Serialization.XmlAttribute("ref")]
\r
95 public XmlQualifiedName RefName
\r
97 get{ return refName;}
\r
104 [System.Xml.Serialization.XmlAttribute("type")]
\r
105 public XmlQualifiedName SchemaTypeName
\r
107 get{ return schemaTypeName;}
\r
108 set{ schemaTypeName = value;}
\r
111 [XmlElement("simpleType",Namespace=XmlSchema.Namespace)]
\r
112 public XmlSchemaSimpleType SchemaType
\r
114 get{ return schemaType;}
\r
115 set{ schemaType = value;}
\r
118 [DefaultValue(XmlSchemaUse.None)]
\r
119 [System.Xml.Serialization.XmlAttribute("use")]
\r
120 public XmlSchemaUse Use
\r
127 public XmlQualifiedName QualifiedName
\r
129 get{ return qualifiedName;}
\r
133 public object AttributeType
\r
136 if (referencedAttribute != null)
\r
137 return referencedAttribute.AttributeType;
\r
139 return attributeType;
\r
143 // Post compilation default value (normalized)
\r
144 internal string ValidatedDefaultValue
\r
146 // DefaultValue can be overriden in case of ref.
\r
147 get { return validatedDefaultValue; }
\r
150 // Post compilation fixed value (normalized)
\r
151 internal string ValidatedFixedValue
\r
153 // FixedValue can be overriden in case of ref.
\r
154 get { return validatedFixedValue; }
\r
160 /// For an attribute:
\r
161 /// a) If the parent is schema
\r
162 /// 1-5 are from <xs:complexType name="topLevelAttribute"> in the Schema for Schema
\r
163 /// 6-8 are from "Constraints on XML Representations of Attribute Declarations"
\r
164 /// 9-10 are from "Attribute Declaration Schema Component"
\r
165 /// 11-16 are from "Constraints on Attribute Declaration Schema Components"
\r
166 /// 1. ref must be absent
\r
167 /// 2. form must be absent
\r
168 /// 3. use must be absent
\r
169 /// 4. name must be present and of type NCName
\r
170 /// 5. *NO CHECK REQUIRED* Only simple types and annotation are allowed as content
\r
171 /// 6. default and fixed must not both be present.
\r
172 /// 7. *NO CHECK REQUIRED* If default and use are both present... (Not possible since use is absent)
\r
173 /// 8. type and <simpleType> must not both be present.
\r
174 /// 9. Target Namespace should be schema's targetnamespace or absent
\r
175 /// 10. Type Definiton coressponds to <simpletype> element, or type value, or absent
\r
176 /// 11. *TO UNDERSTAND* Missing Sub-components
\r
177 /// 12. value constraint must be of the same datatype as of type
\r
178 /// 13. if the type definition is ID then there should be no value constraint.
\r
179 /// 14. name must not be xmlns
\r
180 /// 15. Targetnamespace must not be xsi. This implies the target namespace of schema can't be xsi if toplevel attributes are used.
\r
181 /// 16. *Exception to rule 15* inbuilt attributes: xsi:nil, xsi:type, xsi:schemaLocation, xsi: noNamespaceSchemaLocation
\r
182 /// b) If the parent is complextype and ref is not set
\r
183 /// 1. name must be present and of type NCName.
\r
184 /// 2. type and <simpleType> must not both be present.
\r
185 /// 3. default and fixed must not both be present.
\r
186 /// 4. If default and use are both present, use must have the ·actual value· optional.
\r
187 /// 5. name must not be xmlns
\r
188 /// 6. Targetnamespace must not be xsi.
\r
189 /// 7. *Exception to rule 15* inbuilt attributes: xsi:nil, xsi:type, xsi:schemaLocation, xsi: noNamespaceSchemaLocation
\r
190 /// 8. If form has actual value qualified or the schema's formdefault is qualified, targetnamespace
\r
191 /// is same as schema's target namespace, otherwise absent.
\r
192 /// c) if the parent is not schema and ref is set
\r
193 /// 1. name must not be present
\r
194 /// 2. all of <simpleType>, form and type must be absent.
\r
195 /// 3. default and fixed must not both be present.
\r
196 /// 4. If default and use are both present, use must have the ·actual value· optional.
\r
199 internal override int Compile(ValidationEventHandler h, XmlSchema schema)
\r
201 // If this is already compiled this time, simply skip.
\r
202 if (this.IsComplied (schema.CompilationId))
\r
207 if(ParentIsSchema || isRedefineChild)//a
\r
209 if(RefName!= null && !RefName.IsEmpty) // a.1
\r
210 error(h,"ref must be absent in the top level <attribute>");
\r
212 if(Form != XmlSchemaForm.None) // a.2
\r
213 error(h,"form must be absent in the top level <attribute>");
\r
215 if(Use != XmlSchemaUse.None) // a.3
\r
216 error(h,"use must be absent in the top level <attribute>");
\r
218 targetNamespace = schema.TargetNamespace;
\r
220 // TODO: a.10, a.11, a.12, a.13
\r
221 CompileCommon(h, schema, true);
\r
225 //FIXME: How to Use of AttributeFormDefault????
\r
226 if(RefName == null || RefName.IsEmpty)
\r
228 if(form == XmlSchemaForm.Qualified || (form == XmlSchemaForm.None && schema.AttributeFormDefault == XmlSchemaForm.Qualified))
\r
229 this.targetNamespace = schema.TargetNamespace;
\r
231 this.targetNamespace = "";
\r
234 CompileCommon(h, schema, true);
\r
238 if(this.name != null)
\r
239 error(h,"name must be absent if ref is present");
\r
240 if(this.form != XmlSchemaForm.None)
\r
241 error(h,"form must be absent if ref is present");
\r
242 if(this.schemaType != null)
\r
243 error(h,"simpletype must be absent if ref is present");
\r
244 if(this.schemaTypeName != null && !this.schemaTypeName.IsEmpty)
\r
245 error(h,"type must be absent if ref is present");
\r
247 CompileCommon(h, schema, false);
\r
251 this.CompilationId = schema.CompilationId;
\r
255 private void CompileCommon(ValidationEventHandler h, XmlSchema schema, bool refIsNotPresent)
\r
257 if(refIsNotPresent)
\r
259 if(Name == null) //a.4, b.1,
\r
260 error(h,"Required attribute name must be present");
\r
261 else if(!XmlSchemaUtil.CheckNCName(Name)) // a.4.2, b1.2
\r
262 error(h,"attribute name must be NCName");
\r
263 else if(Name == "xmlns") // a.14 , b5
\r
264 error(h,"attribute name must not be xmlns");
\r
266 qualifiedName = new XmlQualifiedName(Name, targetNamespace);
\r
268 if(SchemaType != null)
\r
270 if(SchemaTypeName != null && !SchemaTypeName.IsEmpty) // a.8
\r
271 error(h,"attribute can't have both a type and <simpleType> content");
\r
273 errorCount += SchemaType.Compile(h, schema);
\r
276 if(SchemaTypeName != null && !XmlSchemaUtil.CheckQName(SchemaTypeName))
\r
277 error(h,SchemaTypeName+" is not a valid QName");
\r
281 if(RefName == null || RefName.IsEmpty)
\r
282 throw new NotImplementedException ("Error: Should Never Happen. refname must be present");
\r
284 qualifiedName = RefName;
\r
287 if(schema.TargetNamespace == XmlSchema.InstanceNamespace && Name != "nil" && Name != "type"
\r
288 && Name != "schemaLocation" && Name != "noNamespaceSchemaLocation") // a.15, a.16
\r
289 error(h,"targetNamespace can't be " + XmlSchema.InstanceNamespace);
\r
291 if(DefaultValue != null && FixedValue != null) // a.6, b.3, c.3
\r
292 error(h,"default and fixed must not both be present in an Attribute");
\r
294 if(DefaultValue != null && Use != XmlSchemaUse.None && Use != XmlSchemaUse.Optional)
\r
295 error(h,"if default is present, use must be optional");
\r
297 XmlSchemaUtil.CompileID(Id, this, schema.IDCollection, h);
\r
301 /// Schema Component:
\r
302 /// QName, SimpleType, Scope, Default|Fixed, annotation
\r
305 internal override int Validate(ValidationEventHandler h, XmlSchema schema)
\r
307 if(IsValidated (schema.ValidationId))
\r
310 // -- Attribute Declaration Schema Component --
\r
311 // {name}, {target namespace} -> QualifiedName. Already Compile()d.
\r
312 // {type definition} -> attributeType. From SchemaType or SchemaTypeName.
\r
313 // {scope} -> ParentIsSchema | isRedefineChild.
\r
314 // {value constraint} -> ValidatedFixedValue, ValidatedDefaultValue.
\r
316 // -- Attribute Use Schema Component --
\r
318 // {attribute declaration}
\r
319 // {value constraint}
\r
321 // First, fill type information for type reference
\r
322 if (SchemaTypeName != null && SchemaTypeName != XmlQualifiedName.Empty)
\r
324 // If type is null, then it is missing sub components .
\r
325 XmlSchemaType type = schema.SchemaTypes [SchemaTypeName] as XmlSchemaType;
\r
326 if (type is XmlSchemaComplexType)
\r
327 error(h,"An attribute can't have complexType Content");
\r
328 else if (type != null) { // simple type
\r
329 errorCount += type.Validate (h, schema);
\r
330 attributeType = type;
\r
332 else if (SchemaTypeName == XmlSchemaComplexType.AnyTypeName)
\r
333 attributeType = XmlSchemaComplexType.AnyType;
334 else if (SchemaTypeName.Namespace == XmlSchema.Namespace) {
\r
335 attributeType = XmlSchemaDatatype.FromName (SchemaTypeName);
\r
336 if (attributeType == null)
\r
337 error (h, "Invalid xml schema namespace datatype was specified.");
\r
339 // otherwise, it might be missing sub components.
\r
340 else if (!schema.IsNamespaceAbsent (SchemaTypeName.Namespace))
\r
341 error (h, "Referenced schema type " + SchemaTypeName + " was not found in the corresponding schema.");
\r
344 // Then, fill type information for the type references for the referencing attributes
\r
345 if (RefName != null && RefName != XmlQualifiedName.Empty)
\r
347 referencedAttribute = schema.Attributes [RefName] as XmlSchemaAttribute;
\r
348 // If el is null, then it is missing sub components .
\r
349 if (referencedAttribute != null)
\r
350 errorCount += referencedAttribute.Validate (h, schema);
\r
351 // otherwise, it might be missing sub components.
\r
352 else if (!schema.IsNamespaceAbsent (RefName.Namespace))
\r
353 error (h, "Referenced attribute " + RefName + " was not found in the corresponding schema.");
\r
356 if (attributeType == null)
\r
357 attributeType = XmlSchemaSimpleType.AnySimpleType;
\r
359 // Validate {value constraints}
\r
360 if (defaultValue != null || fixedValue != null) {
\r
361 XmlSchemaDatatype datatype = attributeType as XmlSchemaDatatype;
\r
362 if (datatype == null)
\r
363 datatype = ((XmlSchemaSimpleType) attributeType).Datatype;
\r
364 if (datatype.TokenizedType == XmlTokenizedType.QName)
\r
365 error (h, "By the defection of the W3C XML Schema specification, it is impossible to supply QName default or fixed values.");
\r
368 if (defaultValue != null) {
\r
369 validatedDefaultValue = datatype.Normalize (defaultValue);
\r
370 datatype.ParseValue (validatedDefaultValue, null, null);
\r
372 } catch (Exception ex) {
\r
373 // FIXME: This is not a good way to handle exception.
\r
374 error (h, "The Attribute's default value is invalid with its type definition.", ex);
\r
377 if (fixedValue != null) {
\r
378 validatedFixedValue = datatype.Normalize (fixedValue);
\r
379 datatype.ParseValue (validatedFixedValue, null, null);
\r
381 } catch (Exception ex) {
\r
382 // FIXME: This is not a good way to handle exception.
\r
383 error (h, "The Attribute's fixed value is invalid with its type definition.", ex);
\r
388 ValidationId = schema.ValidationId;
\r
393 // default = string
\r
395 // form = (qualified | unqualified)
\r
400 // use = (optional | prohibited | required) : optional
\r
401 // {any attributes with non-schema namespace . . .}>
\r
402 // Content: (annotation?, (simpleType?))
\r
404 internal static XmlSchemaAttribute Read(XmlSchemaReader reader, ValidationEventHandler h)
\r
406 XmlSchemaAttribute attribute = new XmlSchemaAttribute();
\r
407 reader.MoveToElement();
\r
409 if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
\r
411 error(h,"Should not happen :1: XmlSchemaAttribute.Read, name="+reader.Name,null);
\r
412 reader.SkipToEnd();
\r
416 attribute.LineNumber = reader.LineNumber;
\r
417 attribute.LinePosition = reader.LinePosition;
\r
418 attribute.SourceUri = reader.BaseURI;
\r
420 while(reader.MoveToNextAttribute())
\r
422 if(reader.Name == "default")
\r
424 attribute.defaultValue = reader.Value;
\r
426 else if(reader.Name == "fixed")
\r
428 attribute.fixedValue = reader.Value;
\r
430 else if(reader.Name == "form")
\r
433 attribute.form = XmlSchemaUtil.ReadFormAttribute(reader,out innerex);
\r
434 if(innerex != null)
\r
435 error(h, reader.Value + " is not a valid value for form attribute", innerex);
\r
437 else if(reader.Name == "id")
\r
439 attribute.Id = reader.Value;
\r
441 else if(reader.Name == "name")
\r
443 attribute.name = reader.Value;
\r
445 else if(reader.Name == "ref")
\r
448 attribute.refName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);
\r
449 if(innerex != null)
\r
450 error(h, reader.Value + " is not a valid value for ref attribute",innerex);
\r
452 else if(reader.Name == "type")
\r
455 attribute.schemaTypeName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);
\r
456 if(innerex != null)
\r
457 error(h, reader.Value + " is not a valid value for type attribute",innerex);
\r
459 else if(reader.Name == "use")
\r
462 attribute.use = XmlSchemaUtil.ReadUseAttribute(reader,out innerex);
\r
463 if(innerex != null)
\r
464 error(h, reader.Value + " is not a valid value for use attribute", innerex);
\r
466 else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
\r
468 error(h,reader.Name + " is not a valid attribute for attribute",null);
\r
472 XmlSchemaUtil.ReadUnhandledAttribute(reader,attribute);
\r
476 reader.MoveToElement();
\r
477 if(reader.IsEmptyElement)
\r
480 // Content: (annotation?, (simpleType?))
\r
482 while(reader.ReadNextElement())
\r
484 if(reader.NodeType == XmlNodeType.EndElement)
\r
486 if(reader.LocalName != xmlname)
\r
487 error(h,"Should not happen :2: XmlSchemaAttribute.Read, name="+reader.Name,null);
\r
490 if(level <= 1 && reader.LocalName == "annotation")
\r
492 level = 2; //Only one annotation
\r
493 XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
\r
494 if(annotation != null)
\r
495 attribute.Annotation = annotation;
\r
498 if(level <=2 && reader.LocalName == "simpleType")
\r
501 XmlSchemaSimpleType stype = XmlSchemaSimpleType.Read(reader,h);
\r
503 attribute.schemaType = stype;
\r
506 reader.RaiseInvalidElementError();
\r