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 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.ComponentModel;
\r
32 using System.Xml.Serialization;
\r
34 namespace System.Xml.Schema
\r
37 /// Summary description for XmlSchemaAttribute.
\r
39 public class XmlSchemaAttribute : XmlSchemaAnnotated
\r
41 private object attributeType;
\r
43 private XmlSchemaSimpleType attributeSchemaType;
\r
45 private string defaultValue;
\r
46 private string fixedValue;
\r
47 private string validatedDefaultValue;
\r
48 private string validatedFixedValue;
\r
49 private object validatedFixedTypedValue;
\r
50 private XmlSchemaForm form;
\r
51 private string name;
\r
52 private string targetNamespace;
\r
53 private XmlQualifiedName qualifiedName;
\r
54 private XmlQualifiedName refName;
\r
55 private XmlSchemaSimpleType schemaType;
\r
56 private XmlQualifiedName schemaTypeName;
\r
57 private XmlSchemaUse use;
\r
58 private XmlSchemaUse validatedUse;
\r
59 //Compilation fields
\r
60 internal bool ParentIsSchema = false;
\r
61 private XmlSchemaAttribute referencedAttribute;
\r
62 const string xmlname = "attribute";
\r
64 public XmlSchemaAttribute()
\r
66 //LAMESPEC: Docs says the default is optional.
\r
67 //Whereas the MS implementation has default None.
\r
68 form = XmlSchemaForm.None;
\r
69 use = XmlSchemaUse.None;
\r
70 schemaTypeName = XmlQualifiedName.Empty;
\r
71 qualifiedName = XmlQualifiedName.Empty;
\r
72 refName = XmlQualifiedName.Empty;
\r
78 [DefaultValue(null)]
\r
79 [System.Xml.Serialization.XmlAttribute("default")]
\r
80 public string DefaultValue
\r
82 get{ return defaultValue;}
\r
84 { // Default Value and fixed Value are mutually exclusive
\r
86 defaultValue = value;
\r
90 [DefaultValue(null)]
\r
91 [System.Xml.Serialization.XmlAttribute("fixed")]
\r
92 public string FixedValue
\r
94 get{ return fixedValue;}
\r
96 { // Default Value and fixed Value are mutually exclusive
\r
97 defaultValue = null;
\r
102 [DefaultValue(XmlSchemaForm.None)]
\r
103 [System.Xml.Serialization.XmlAttribute("form")]
\r
104 public XmlSchemaForm Form
\r
107 set{ form = value;}
\r
110 [System.Xml.Serialization.XmlAttribute("name")]
\r
111 public string Name
\r
120 [System.Xml.Serialization.XmlAttribute("ref")]
\r
121 public XmlQualifiedName RefName
\r
123 get{ return refName;}
\r
130 [System.Xml.Serialization.XmlAttribute("type")]
\r
131 public XmlQualifiedName SchemaTypeName
\r
133 get{ return schemaTypeName;}
\r
134 set{ schemaTypeName = value;}
\r
137 [XmlElement("simpleType")]
\r
138 public XmlSchemaSimpleType SchemaType
\r
140 get{ return schemaType;}
\r
141 set{ schemaType = value;}
\r
144 [DefaultValue(XmlSchemaUse.None)]
\r
145 [System.Xml.Serialization.XmlAttribute("use")]
\r
146 public XmlSchemaUse Use
\r
153 public XmlQualifiedName QualifiedName
\r
155 get{ return qualifiedName;}
\r
162 public object AttributeType
\r
165 if (referencedAttribute != null)
\r
166 return referencedAttribute.AttributeType;
\r
168 return attributeType;
\r
174 public XmlSchemaSimpleType AttributeSchemaType
\r
177 if (referencedAttribute != null)
\r
178 return referencedAttribute.AttributeSchemaType;
\r
180 return attributeSchemaType;
\r
185 // Post compilation default value (normalized)
\r
186 internal string ValidatedDefaultValue
\r
188 // DefaultValue can be overriden in case of ref.
\r
189 get { return validatedDefaultValue; }
\r
192 // Post compilation fixed value (normalized)
\r
193 internal string ValidatedFixedValue
\r
195 // FixedValue can be overriden in case of ref.
\r
196 get { return validatedFixedValue; }
\r
199 internal object ValidatedFixedTypedValue
\r
201 get { return validatedFixedTypedValue; }
\r
204 internal XmlSchemaUse ValidatedUse
\r
206 get { return validatedUse; }
\r
212 /// For an attribute:
\r
213 /// a) If the parent is schema
\r
214 /// 1-5 are from <xs:complexType name="topLevelAttribute"> in the Schema for Schema
\r
215 /// 6-8 are from "Constraints on XML Representations of Attribute Declarations"
\r
216 /// 9-10 are from "Attribute Declaration Schema Component"
\r
217 /// 11-16 are from "Constraints on Attribute Declaration Schema Components"
\r
218 /// 1. ref must be absent
\r
219 /// 2. form must be absent
\r
220 /// 3. use must be absent
\r
221 /// 4. name must be present and of type NCName
\r
222 /// 5. *NO CHECK REQUIRED* Only simple types and annotation are allowed as content
\r
223 /// 6. default and fixed must not both be present.
\r
224 /// 7. *NO CHECK REQUIRED* If default and use are both present... (Not possible since use is absent)
\r
225 /// 8. type and <simpleType> must not both be present.
\r
226 /// 9. Target Namespace should be schema's targetnamespace or absent
\r
227 /// 10. Type Definiton coressponds to <simpletype> element, or type value, or absent
\r
228 /// 11. *TO UNDERSTAND* Missing Sub-components
\r
229 /// 12. value constraint must be of the same datatype as of type
\r
230 /// 13. if the type definition is ID then there should be no value constraint.
\r
231 /// 14. name must not be xmlns
\r
232 /// 15. Targetnamespace must not be xsi. This implies the target namespace of schema can't be xsi if toplevel attributes are used.
\r
233 /// 16. *Exception to rule 15* inbuilt attributes: xsi:nil, xsi:type, xsi:schemaLocation, xsi: noNamespaceSchemaLocation
\r
234 /// b) If the parent is complextype and ref is not set
\r
235 /// 1. name must be present and of type NCName.
\r
236 /// 2. type and <simpleType> must not both be present.
\r
237 /// 3. default and fixed must not both be present.
\r
238 /// 4. If default and use are both present, use must have the ·actual value· optional.
\r
239 /// 5. name must not be xmlns
\r
240 /// 6. Targetnamespace must not be xsi.
\r
241 /// 7. *Exception to rule 15* inbuilt attributes: xsi:nil, xsi:type, xsi:schemaLocation, xsi: noNamespaceSchemaLocation
\r
242 /// 8. If form has actual value qualified or the schema's formdefault is qualified, targetnamespace
\r
243 /// is same as schema's target namespace, otherwise absent.
\r
244 /// c) if the parent is not schema and ref is set
\r
245 /// 1. name must not be present
\r
246 /// 2. all of <simpleType>, form and type must be absent.
\r
247 /// 3. default and fixed must not both be present.
\r
248 /// 4. If default and use are both present, use must have the ·actual value· optional.
\r
250 internal override int Compile(ValidationEventHandler h, XmlSchema schema)
\r
252 // If this is already compiled this time, simply skip.
\r
253 if (this.IsComplied (schema.CompilationId))
\r
257 if (schemaType != null)
\r
258 schemaType.Parent = this;
\r
263 if(ParentIsSchema || isRedefineChild)//a
\r
265 if(RefName!= null && !RefName.IsEmpty) // a.1
\r
266 error(h,"ref must be absent in the top level <attribute>");
\r
268 if(Form != XmlSchemaForm.None) // a.2
\r
269 error(h,"form must be absent in the top level <attribute>");
\r
271 if(Use != XmlSchemaUse.None) // a.3
\r
272 error(h,"use must be absent in the top level <attribute>");
\r
274 targetNamespace = schema.TargetNamespace;
\r
276 CompileCommon (h, schema, true);
\r
280 // Q:How to Use of AttributeFormDefault????
\r
281 // A:Global attribute cannot be defined locally
\r
282 if(RefName == null || RefName.IsEmpty)
\r
284 if(form == XmlSchemaForm.Qualified || (form == XmlSchemaForm.None && schema.AttributeFormDefault == XmlSchemaForm.Qualified))
\r
285 this.targetNamespace = schema.TargetNamespace;
\r
287 this.targetNamespace = "";
\r
289 CompileCommon(h, schema, true);
\r
293 if(this.name != null)
\r
294 error(h,"name must be absent if ref is present");
\r
295 if(this.form != XmlSchemaForm.None)
\r
296 error(h,"form must be absent if ref is present");
\r
297 if(this.schemaType != null)
\r
298 error(h,"simpletype must be absent if ref is present");
\r
299 if(this.schemaTypeName != null && !this.schemaTypeName.IsEmpty)
\r
300 error(h,"type must be absent if ref is present");
\r
302 CompileCommon(h, schema, false);
\r
306 this.CompilationId = schema.CompilationId;
\r
310 private void CompileCommon(ValidationEventHandler h, XmlSchema schema, bool refIsNotPresent)
\r
312 if(refIsNotPresent)
\r
314 if(Name == null) //a.4, b.1,
\r
315 error(h,"Required attribute name must be present");
\r
316 else if(!XmlSchemaUtil.CheckNCName(Name)) // a.4.2, b1.2
\r
317 error(h,"attribute name must be NCName");
\r
318 else if(Name == "xmlns") // a.14 , b5
\r
319 error(h,"attribute name must not be xmlns");
\r
321 qualifiedName = new XmlQualifiedName(Name, targetNamespace);
\r
323 if(SchemaType != null)
\r
325 if(SchemaTypeName != null && !SchemaTypeName.IsEmpty) // a.8
\r
326 error(h,"attribute can't have both a type and <simpleType> content");
\r
328 errorCount += SchemaType.Compile(h, schema);
\r
331 if(SchemaTypeName != null && !XmlSchemaUtil.CheckQName(SchemaTypeName))
\r
332 error(h,SchemaTypeName+" is not a valid QName");
\r
336 if(RefName == null || RefName.IsEmpty)
\r
337 throw new InvalidOperationException ("Error: Should Never Happen. refname must be present");
\r
339 qualifiedName = RefName;
\r
342 if(schema.TargetNamespace == XmlSchema.InstanceNamespace && Name != "nil" && Name != "type"
\r
343 && Name != "schemaLocation" && Name != "noNamespaceSchemaLocation") // a.15, a.16
\r
344 error(h,"targetNamespace can't be " + XmlSchema.InstanceNamespace);
\r
346 if(DefaultValue != null && FixedValue != null) // a.6, b.3, c.3
\r
347 error(h,"default and fixed must not both be present in an Attribute");
\r
349 if(DefaultValue != null && Use != XmlSchemaUse.None && Use != XmlSchemaUse.Optional)
\r
350 error(h,"if default is present, use must be optional");
\r
352 XmlSchemaUtil.CompileID(Id, this, schema.IDCollection, h);
\r
356 /// Schema Component:
\r
357 /// QName, SimpleType, Scope, Default|Fixed, annotation
\r
359 internal override int Validate(ValidationEventHandler h, XmlSchema schema)
\r
361 if(IsValidated (schema.ValidationId))
\r
364 // -- Attribute Declaration Schema Component --
\r
365 // {name}, {target namespace} -> QualifiedName. Already Compile()d.
\r
366 // {type definition} -> attributeType. From SchemaType or SchemaTypeName.
\r
367 // {scope} -> ParentIsSchema | isRedefineChild.
\r
368 // {value constraint} -> ValidatedFixedValue, ValidatedDefaultValue.
\r
370 // -- Attribute Use Schema Component --
\r
372 // {attribute declaration}
\r
373 // {value constraint}
\r
375 // First, fill type information for type reference
\r
376 if (SchemaType != null) {
\r
377 SchemaType.Validate (h, schema);
\r
378 attributeType = SchemaType;
\r
380 else if (SchemaTypeName != null && SchemaTypeName != XmlQualifiedName.Empty)
\r
382 // If type is null, then it is missing sub components .
\r
383 XmlSchemaType type = schema.SchemaTypes [SchemaTypeName] as XmlSchemaType;
\r
384 if (type is XmlSchemaComplexType)
\r
385 error(h,"An attribute can't have complexType Content");
\r
386 else if (type != null) { // simple type
\r
387 errorCount += type.Validate (h, schema);
\r
388 attributeType = type;
\r
390 else if (SchemaTypeName == XmlSchemaComplexType.AnyTypeName)
\r
391 attributeType = XmlSchemaComplexType.AnyType;
392 else if (XmlSchemaUtil.IsBuiltInDatatypeName (SchemaTypeName)) {
\r
393 attributeType = XmlSchemaDatatype.FromName (SchemaTypeName);
\r
394 if (attributeType == null)
\r
395 error (h, "Invalid xml schema namespace datatype was specified.");
\r
397 // otherwise, it might be missing sub components.
\r
398 else if (!schema.IsNamespaceAbsent (SchemaTypeName.Namespace))
\r
399 error (h, "Referenced schema type " + SchemaTypeName + " was not found in the corresponding schema.");
\r
402 // Then, fill type information for the type references for the referencing attributes
\r
403 if (RefName != null && RefName != XmlQualifiedName.Empty)
\r
405 referencedAttribute = schema.Attributes [RefName] as XmlSchemaAttribute;
\r
406 // If el is null, then it is missing sub components .
\r
407 if (referencedAttribute != null)
\r
408 errorCount += referencedAttribute.Validate (h, schema);
\r
409 // otherwise, it might be missing sub components.
\r
410 else if (!schema.IsNamespaceAbsent (RefName.Namespace))
\r
411 error (h, "Referenced attribute " + RefName + " was not found in the corresponding schema.");
\r
414 if (attributeType == null)
\r
415 attributeType = XmlSchemaSimpleType.AnySimpleType;
\r
417 // Validate {value constraints}
\r
418 if (defaultValue != null || fixedValue != null) {
\r
419 XmlSchemaDatatype datatype = attributeType as XmlSchemaDatatype;
\r
420 if (datatype == null)
\r
421 datatype = ((XmlSchemaSimpleType) attributeType).Datatype;
\r
422 if (datatype.TokenizedType == XmlTokenizedType.QName)
\r
423 error (h, "By the defection of the W3C XML Schema specification, it is impossible to supply QName default or fixed values.");
\r
426 if (defaultValue != null) {
\r
427 validatedDefaultValue = datatype.Normalize (defaultValue);
\r
428 datatype.ParseValue (validatedDefaultValue, null, null);
\r
430 } catch (Exception ex) {
\r
431 // FIXME: This is not a good way to handle exception.
\r
432 error (h, "The Attribute's default value is invalid with its type definition.", ex);
\r
435 if (fixedValue != null) {
\r
436 validatedFixedValue = datatype.Normalize (fixedValue);
\r
437 validatedFixedTypedValue = datatype.ParseValue (validatedFixedValue, null, null);
\r
439 } catch (Exception ex) {
\r
440 // FIXME: This is not a good way to handle exception.
\r
441 error (h, "The Attribute's fixed value is invalid with its type definition.", ex);
\r
445 if (Use == XmlSchemaUse.None)
\r
446 validatedUse = XmlSchemaUse.Optional;
\r
448 validatedUse = Use;
\r
451 attributeSchemaType = attributeType as XmlSchemaSimpleType;
\r
452 if (attributeSchemaType == null)
\r
453 attributeSchemaType = XmlSchemaType.GetBuiltInSimpleType (((XmlSchemaDatatype) attributeType).TypeCode);
\r
456 ValidationId = schema.ValidationId;
\r
461 // default = string
\r
463 // form = (qualified | unqualified)
\r
468 // use = (optional | prohibited | required) : optional
\r
469 // {any attributes with non-schema namespace . . .}>
\r
470 // Content: (annotation?, (simpleType?))
\r
472 internal static XmlSchemaAttribute Read(XmlSchemaReader reader, ValidationEventHandler h)
\r
474 XmlSchemaAttribute attribute = new XmlSchemaAttribute();
\r
475 reader.MoveToElement();
\r
477 if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
\r
479 error(h,"Should not happen :1: XmlSchemaAttribute.Read, name="+reader.Name,null);
\r
480 reader.SkipToEnd();
\r
484 attribute.LineNumber = reader.LineNumber;
\r
485 attribute.LinePosition = reader.LinePosition;
\r
486 attribute.SourceUri = reader.BaseURI;
\r
488 while(reader.MoveToNextAttribute())
\r
490 if(reader.Name == "default")
\r
492 attribute.defaultValue = reader.Value;
\r
494 else if(reader.Name == "fixed")
\r
496 attribute.fixedValue = reader.Value;
\r
498 else if(reader.Name == "form")
\r
501 attribute.form = XmlSchemaUtil.ReadFormAttribute(reader,out innerex);
\r
502 if(innerex != null)
\r
503 error(h, reader.Value + " is not a valid value for form attribute", innerex);
\r
505 else if(reader.Name == "id")
\r
507 attribute.Id = reader.Value;
\r
509 else if(reader.Name == "name")
\r
511 attribute.name = reader.Value;
\r
513 else if(reader.Name == "ref")
\r
516 attribute.refName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);
\r
517 if(innerex != null)
\r
518 error(h, reader.Value + " is not a valid value for ref attribute",innerex);
\r
520 else if(reader.Name == "type")
\r
523 attribute.schemaTypeName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);
\r
524 if(innerex != null)
\r
525 error(h, reader.Value + " is not a valid value for type attribute",innerex);
\r
527 else if(reader.Name == "use")
\r
530 attribute.use = XmlSchemaUtil.ReadUseAttribute(reader,out innerex);
\r
531 if(innerex != null)
\r
532 error(h, reader.Value + " is not a valid value for use attribute", innerex);
\r
534 else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
\r
536 error(h,reader.Name + " is not a valid attribute for attribute",null);
\r
540 XmlSchemaUtil.ReadUnhandledAttribute(reader,attribute);
\r
544 reader.MoveToElement();
\r
545 if(reader.IsEmptyElement)
\r
548 // Content: (annotation?, (simpleType?))
\r
550 while(reader.ReadNextElement())
\r
552 if(reader.NodeType == XmlNodeType.EndElement)
\r
554 if(reader.LocalName != xmlname)
\r
555 error(h,"Should not happen :2: XmlSchemaAttribute.Read, name="+reader.Name,null);
\r
558 if(level <= 1 && reader.LocalName == "annotation")
\r
560 level = 2; //Only one annotation
\r
561 XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
\r
562 if(annotation != null)
\r
563 attribute.Annotation = annotation;
\r
566 if(level <=2 && reader.LocalName == "simpleType")
\r
569 XmlSchemaSimpleType stype = XmlSchemaSimpleType.Read(reader,h);
\r
571 attribute.schemaType = stype;
\r
574 reader.RaiseInvalidElementError();
\r