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 XmlSchemaForm form;
\r
50 private string name;
\r
51 private string targetNamespace;
\r
52 private XmlQualifiedName qualifiedName;
\r
53 private XmlQualifiedName refName;
\r
54 private XmlSchemaSimpleType schemaType;
\r
55 private XmlQualifiedName schemaTypeName;
\r
56 private XmlSchemaUse use;
\r
57 private XmlSchemaUse validatedUse;
\r
58 //Compilation fields
\r
59 internal bool ParentIsSchema = false;
\r
60 private XmlSchemaAttribute referencedAttribute;
\r
61 const string xmlname = "attribute";
\r
63 public XmlSchemaAttribute()
\r
65 //LAMESPEC: Docs says the default is optional.
\r
66 //Whereas the MS implementation has default None.
\r
67 form = XmlSchemaForm.None;
\r
68 use = XmlSchemaUse.None;
\r
69 schemaTypeName = XmlQualifiedName.Empty;
\r
70 qualifiedName = XmlQualifiedName.Empty;
\r
71 refName = XmlQualifiedName.Empty;
\r
77 [DefaultValue(null)]
\r
78 [System.Xml.Serialization.XmlAttribute("default")]
\r
79 public string DefaultValue
\r
81 get{ return defaultValue;}
\r
83 { // Default Value and fixed Value are mutually exclusive
\r
85 defaultValue = value;
\r
89 [DefaultValue(null)]
\r
90 [System.Xml.Serialization.XmlAttribute("fixed")]
\r
91 public string FixedValue
\r
93 get{ return fixedValue;}
\r
95 { // Default Value and fixed Value are mutually exclusive
\r
96 defaultValue = null;
\r
101 [DefaultValue(XmlSchemaForm.None)]
\r
102 [System.Xml.Serialization.XmlAttribute("form")]
\r
103 public XmlSchemaForm Form
\r
106 set{ form = value;}
\r
109 [System.Xml.Serialization.XmlAttribute("name")]
\r
110 public string Name
\r
119 [System.Xml.Serialization.XmlAttribute("ref")]
\r
120 public XmlQualifiedName RefName
\r
122 get{ return refName;}
\r
129 [System.Xml.Serialization.XmlAttribute("type")]
\r
130 public XmlQualifiedName SchemaTypeName
\r
132 get{ return schemaTypeName;}
\r
133 set{ schemaTypeName = value;}
\r
136 [XmlElement("simpleType")]
\r
137 public XmlSchemaSimpleType SchemaType
\r
139 get{ return schemaType;}
\r
140 set{ schemaType = value;}
\r
143 [DefaultValue(XmlSchemaUse.None)]
\r
144 [System.Xml.Serialization.XmlAttribute("use")]
\r
145 public XmlSchemaUse Use
\r
152 public XmlQualifiedName QualifiedName
\r
154 get{ return qualifiedName;}
\r
161 public object AttributeType
\r
164 if (referencedAttribute != null)
\r
165 return referencedAttribute.AttributeType;
\r
167 return attributeType;
\r
173 public XmlSchemaSimpleType AttributeSchemaType
\r
176 if (referencedAttribute != null)
\r
177 return referencedAttribute.AttributeSchemaType;
\r
179 return attributeSchemaType;
\r
184 // Post compilation default value (normalized)
\r
185 internal string ValidatedDefaultValue
\r
187 // DefaultValue can be overriden in case of ref.
\r
188 get { return validatedDefaultValue; }
\r
191 // Post compilation fixed value (normalized)
\r
192 internal string ValidatedFixedValue
\r
194 // FixedValue can be overriden in case of ref.
\r
195 get { return validatedFixedValue; }
\r
197 internal XmlSchemaUse ValidatedUse
\r
199 get { return validatedUse; }
\r
205 /// For an attribute:
\r
206 /// a) If the parent is schema
\r
207 /// 1-5 are from <xs:complexType name="topLevelAttribute"> in the Schema for Schema
\r
208 /// 6-8 are from "Constraints on XML Representations of Attribute Declarations"
\r
209 /// 9-10 are from "Attribute Declaration Schema Component"
\r
210 /// 11-16 are from "Constraints on Attribute Declaration Schema Components"
\r
211 /// 1. ref must be absent
\r
212 /// 2. form must be absent
\r
213 /// 3. use must be absent
\r
214 /// 4. name must be present and of type NCName
\r
215 /// 5. *NO CHECK REQUIRED* Only simple types and annotation are allowed as content
\r
216 /// 6. default and fixed must not both be present.
\r
217 /// 7. *NO CHECK REQUIRED* If default and use are both present... (Not possible since use is absent)
\r
218 /// 8. type and <simpleType> must not both be present.
\r
219 /// 9. Target Namespace should be schema's targetnamespace or absent
\r
220 /// 10. Type Definiton coressponds to <simpletype> element, or type value, or absent
\r
221 /// 11. *TO UNDERSTAND* Missing Sub-components
\r
222 /// 12. value constraint must be of the same datatype as of type
\r
223 /// 13. if the type definition is ID then there should be no value constraint.
\r
224 /// 14. name must not be xmlns
\r
225 /// 15. Targetnamespace must not be xsi. This implies the target namespace of schema can't be xsi if toplevel attributes are used.
\r
226 /// 16. *Exception to rule 15* inbuilt attributes: xsi:nil, xsi:type, xsi:schemaLocation, xsi: noNamespaceSchemaLocation
\r
227 /// b) If the parent is complextype and ref is not set
\r
228 /// 1. name must be present and of type NCName.
\r
229 /// 2. type and <simpleType> must not both be present.
\r
230 /// 3. default and fixed must not both be present.
\r
231 /// 4. If default and use are both present, use must have the ·actual value· optional.
\r
232 /// 5. name must not be xmlns
\r
233 /// 6. Targetnamespace must not be xsi.
\r
234 /// 7. *Exception to rule 15* inbuilt attributes: xsi:nil, xsi:type, xsi:schemaLocation, xsi: noNamespaceSchemaLocation
\r
235 /// 8. If form has actual value qualified or the schema's formdefault is qualified, targetnamespace
\r
236 /// is same as schema's target namespace, otherwise absent.
\r
237 /// c) if the parent is not schema and ref is set
\r
238 /// 1. name must not be present
\r
239 /// 2. all of <simpleType>, form and type must be absent.
\r
240 /// 3. default and fixed must not both be present.
\r
241 /// 4. If default and use are both present, use must have the ·actual value· optional.
\r
243 internal override int Compile(ValidationEventHandler h, XmlSchema schema)
\r
245 // If this is already compiled this time, simply skip.
\r
246 if (this.IsComplied (schema.CompilationId))
\r
250 if (schemaType != null)
\r
251 schemaType.Parent = this;
\r
256 if(ParentIsSchema || isRedefineChild)//a
\r
258 if(RefName!= null && !RefName.IsEmpty) // a.1
\r
259 error(h,"ref must be absent in the top level <attribute>");
\r
261 if(Form != XmlSchemaForm.None) // a.2
\r
262 error(h,"form must be absent in the top level <attribute>");
\r
264 if(Use != XmlSchemaUse.None) // a.3
\r
265 error(h,"use must be absent in the top level <attribute>");
\r
267 targetNamespace = schema.TargetNamespace;
\r
269 CompileCommon (h, schema, true);
\r
273 // Q:How to Use of AttributeFormDefault????
\r
274 // A:Global attribute cannot be defined locally
\r
275 if(RefName == null || RefName.IsEmpty)
\r
277 if(form == XmlSchemaForm.Qualified || (form == XmlSchemaForm.None && schema.AttributeFormDefault == XmlSchemaForm.Qualified))
\r
278 this.targetNamespace = schema.TargetNamespace;
\r
280 this.targetNamespace = "";
\r
282 CompileCommon(h, schema, true);
\r
286 if(this.name != null)
\r
287 error(h,"name must be absent if ref is present");
\r
288 if(this.form != XmlSchemaForm.None)
\r
289 error(h,"form must be absent if ref is present");
\r
290 if(this.schemaType != null)
\r
291 error(h,"simpletype must be absent if ref is present");
\r
292 if(this.schemaTypeName != null && !this.schemaTypeName.IsEmpty)
\r
293 error(h,"type must be absent if ref is present");
\r
295 CompileCommon(h, schema, false);
\r
299 this.CompilationId = schema.CompilationId;
\r
303 private void CompileCommon(ValidationEventHandler h, XmlSchema schema, bool refIsNotPresent)
\r
305 if(refIsNotPresent)
\r
307 if(Name == null) //a.4, b.1,
\r
308 error(h,"Required attribute name must be present");
\r
309 else if(!XmlSchemaUtil.CheckNCName(Name)) // a.4.2, b1.2
\r
310 error(h,"attribute name must be NCName");
\r
311 else if(Name == "xmlns") // a.14 , b5
\r
312 error(h,"attribute name must not be xmlns");
\r
314 qualifiedName = new XmlQualifiedName(Name, targetNamespace);
\r
316 if(SchemaType != null)
\r
318 if(SchemaTypeName != null && !SchemaTypeName.IsEmpty) // a.8
\r
319 error(h,"attribute can't have both a type and <simpleType> content");
\r
321 errorCount += SchemaType.Compile(h, schema);
\r
324 if(SchemaTypeName != null && !XmlSchemaUtil.CheckQName(SchemaTypeName))
\r
325 error(h,SchemaTypeName+" is not a valid QName");
\r
329 if(RefName == null || RefName.IsEmpty)
\r
330 throw new InvalidOperationException ("Error: Should Never Happen. refname must be present");
\r
332 qualifiedName = RefName;
\r
335 if(schema.TargetNamespace == XmlSchema.InstanceNamespace && Name != "nil" && Name != "type"
\r
336 && Name != "schemaLocation" && Name != "noNamespaceSchemaLocation") // a.15, a.16
\r
337 error(h,"targetNamespace can't be " + XmlSchema.InstanceNamespace);
\r
339 if(DefaultValue != null && FixedValue != null) // a.6, b.3, c.3
\r
340 error(h,"default and fixed must not both be present in an Attribute");
\r
342 if(DefaultValue != null && Use != XmlSchemaUse.None && Use != XmlSchemaUse.Optional)
\r
343 error(h,"if default is present, use must be optional");
\r
345 XmlSchemaUtil.CompileID(Id, this, schema.IDCollection, h);
\r
349 /// Schema Component:
\r
350 /// QName, SimpleType, Scope, Default|Fixed, annotation
\r
352 internal override int Validate(ValidationEventHandler h, XmlSchema schema)
\r
354 if(IsValidated (schema.ValidationId))
\r
357 // -- Attribute Declaration Schema Component --
\r
358 // {name}, {target namespace} -> QualifiedName. Already Compile()d.
\r
359 // {type definition} -> attributeType. From SchemaType or SchemaTypeName.
\r
360 // {scope} -> ParentIsSchema | isRedefineChild.
\r
361 // {value constraint} -> ValidatedFixedValue, ValidatedDefaultValue.
\r
363 // -- Attribute Use Schema Component --
\r
365 // {attribute declaration}
\r
366 // {value constraint}
\r
368 // First, fill type information for type reference
\r
369 if (SchemaType != null) {
\r
370 SchemaType.Validate (h, schema);
\r
371 attributeType = SchemaType;
\r
373 else if (SchemaTypeName != null && SchemaTypeName != XmlQualifiedName.Empty)
\r
375 // If type is null, then it is missing sub components .
\r
376 XmlSchemaType type = schema.SchemaTypes [SchemaTypeName] as XmlSchemaType;
\r
377 if (type is XmlSchemaComplexType)
\r
378 error(h,"An attribute can't have complexType Content");
\r
379 else if (type != null) { // simple type
\r
380 errorCount += type.Validate (h, schema);
\r
381 attributeType = type;
\r
383 else if (SchemaTypeName == XmlSchemaComplexType.AnyTypeName)
\r
384 attributeType = XmlSchemaComplexType.AnyType;
385 else if (XmlSchemaUtil.IsBuiltInDatatypeName (SchemaTypeName)) {
\r
386 attributeType = XmlSchemaDatatype.FromName (SchemaTypeName);
\r
387 if (attributeType == null)
\r
388 error (h, "Invalid xml schema namespace datatype was specified.");
\r
390 // otherwise, it might be missing sub components.
\r
391 else if (!schema.IsNamespaceAbsent (SchemaTypeName.Namespace))
\r
392 error (h, "Referenced schema type " + SchemaTypeName + " was not found in the corresponding schema.");
\r
395 // Then, fill type information for the type references for the referencing attributes
\r
396 if (RefName != null && RefName != XmlQualifiedName.Empty)
\r
398 referencedAttribute = schema.Attributes [RefName] as XmlSchemaAttribute;
\r
399 // If el is null, then it is missing sub components .
\r
400 if (referencedAttribute != null)
\r
401 errorCount += referencedAttribute.Validate (h, schema);
\r
402 // otherwise, it might be missing sub components.
\r
403 else if (!schema.IsNamespaceAbsent (RefName.Namespace))
\r
404 error (h, "Referenced attribute " + RefName + " was not found in the corresponding schema.");
\r
407 if (attributeType == null)
\r
408 attributeType = XmlSchemaSimpleType.AnySimpleType;
\r
410 // Validate {value constraints}
\r
411 if (defaultValue != null || fixedValue != null) {
\r
412 XmlSchemaDatatype datatype = attributeType as XmlSchemaDatatype;
\r
413 if (datatype == null)
\r
414 datatype = ((XmlSchemaSimpleType) attributeType).Datatype;
\r
415 if (datatype.TokenizedType == XmlTokenizedType.QName)
\r
416 error (h, "By the defection of the W3C XML Schema specification, it is impossible to supply QName default or fixed values.");
\r
419 if (defaultValue != null) {
\r
420 validatedDefaultValue = datatype.Normalize (defaultValue);
\r
421 datatype.ParseValue (validatedDefaultValue, null, null);
\r
423 } catch (Exception ex) {
\r
424 // FIXME: This is not a good way to handle exception.
\r
425 error (h, "The Attribute's default value is invalid with its type definition.", ex);
\r
428 if (fixedValue != null) {
\r
429 validatedFixedValue = datatype.Normalize (fixedValue);
\r
430 datatype.ParseValue (validatedFixedValue, null, null);
\r
432 } catch (Exception ex) {
\r
433 // FIXME: This is not a good way to handle exception.
\r
434 error (h, "The Attribute's fixed value is invalid with its type definition.", ex);
\r
438 if (Use == XmlSchemaUse.None)
\r
439 validatedUse = XmlSchemaUse.Optional;
\r
441 validatedUse = Use;
\r
444 attributeSchemaType = attributeType as XmlSchemaSimpleType;
\r
445 if (attributeSchemaType == null)
\r
446 attributeSchemaType = XmlSchemaType.GetBuiltInSimpleType (((XmlSchemaDatatype) attributeType).TypeCode);
\r
449 ValidationId = schema.ValidationId;
\r
454 // default = string
\r
456 // form = (qualified | unqualified)
\r
461 // use = (optional | prohibited | required) : optional
\r
462 // {any attributes with non-schema namespace . . .}>
\r
463 // Content: (annotation?, (simpleType?))
\r
465 internal static XmlSchemaAttribute Read(XmlSchemaReader reader, ValidationEventHandler h)
\r
467 XmlSchemaAttribute attribute = new XmlSchemaAttribute();
\r
468 reader.MoveToElement();
\r
470 if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
\r
472 error(h,"Should not happen :1: XmlSchemaAttribute.Read, name="+reader.Name,null);
\r
473 reader.SkipToEnd();
\r
477 attribute.LineNumber = reader.LineNumber;
\r
478 attribute.LinePosition = reader.LinePosition;
\r
479 attribute.SourceUri = reader.BaseURI;
\r
481 while(reader.MoveToNextAttribute())
\r
483 if(reader.Name == "default")
\r
485 attribute.defaultValue = reader.Value;
\r
487 else if(reader.Name == "fixed")
\r
489 attribute.fixedValue = reader.Value;
\r
491 else if(reader.Name == "form")
\r
494 attribute.form = XmlSchemaUtil.ReadFormAttribute(reader,out innerex);
\r
495 if(innerex != null)
\r
496 error(h, reader.Value + " is not a valid value for form attribute", innerex);
\r
498 else if(reader.Name == "id")
\r
500 attribute.Id = reader.Value;
\r
502 else if(reader.Name == "name")
\r
504 attribute.name = reader.Value;
\r
506 else if(reader.Name == "ref")
\r
509 attribute.refName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);
\r
510 if(innerex != null)
\r
511 error(h, reader.Value + " is not a valid value for ref attribute",innerex);
\r
513 else if(reader.Name == "type")
\r
516 attribute.schemaTypeName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);
\r
517 if(innerex != null)
\r
518 error(h, reader.Value + " is not a valid value for type attribute",innerex);
\r
520 else if(reader.Name == "use")
\r
523 attribute.use = XmlSchemaUtil.ReadUseAttribute(reader,out innerex);
\r
524 if(innerex != null)
\r
525 error(h, reader.Value + " is not a valid value for use attribute", innerex);
\r
527 else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
\r
529 error(h,reader.Name + " is not a valid attribute for attribute",null);
\r
533 XmlSchemaUtil.ReadUnhandledAttribute(reader,attribute);
\r
537 reader.MoveToElement();
\r
538 if(reader.IsEmptyElement)
\r
541 // Content: (annotation?, (simpleType?))
\r
543 while(reader.ReadNextElement())
\r
545 if(reader.NodeType == XmlNodeType.EndElement)
\r
547 if(reader.LocalName != xmlname)
\r
548 error(h,"Should not happen :2: XmlSchemaAttribute.Read, name="+reader.Name,null);
\r
551 if(level <= 1 && reader.LocalName == "annotation")
\r
553 level = 2; //Only one annotation
\r
554 XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
\r
555 if(annotation != null)
\r
556 attribute.Annotation = annotation;
\r
559 if(level <=2 && reader.LocalName == "simpleType")
\r
562 XmlSchemaSimpleType stype = XmlSchemaSimpleType.Read(reader,h);
\r
564 attribute.schemaType = stype;
\r
567 reader.RaiseInvalidElementError();
\r