| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440 |
- // Author: Dwivedi, Ajay kumar
- // [email protected]
- using System;
- using System.Xml;
- using System.ComponentModel;
- using System.Xml.Serialization;
- namespace System.Xml.Schema
- {
- /// <summary>
- /// Summary description for XmlSchemaAttribute.
- /// </summary>
- public class XmlSchemaAttribute : XmlSchemaAnnotated
- {
- private object attributeType;
- private string defaultValue;
- private string fixedValue;
- private XmlSchemaForm form;
- private string name;
- private XmlQualifiedName qualifiedName;
- private XmlQualifiedName refName;
- private XmlSchemaSimpleType schemaType;
- private XmlQualifiedName schemaTypeName;
- private XmlSchemaUse use;
- //Compilation fields
- internal bool parentIsSchema = false;
- private static string xmlname = "attribute";
- public XmlSchemaAttribute()
- {
- //FIXME: Docs says the default is optional.
- //Whereas the MS implementation has default None.
- form = XmlSchemaForm.None;
- use = XmlSchemaUse.None;
- schemaTypeName = XmlQualifiedName.Empty;
- qualifiedName = XmlQualifiedName.Empty;
- refName = XmlQualifiedName.Empty;
- }
- // Properties
- #region Properties
- [DefaultValue(null)]
- [System.Xml.Serialization.XmlAttribute("default")]
- public string DefaultValue
- {
- get{ return defaultValue;}
- set
- { // Default Value and fixed Value are mutually exclusive
- fixedValue = null;
- defaultValue = value;
- }
- }
- [DefaultValue(null)]
- [System.Xml.Serialization.XmlAttribute("fixed")]
- public string FixedValue
- {
- get{ return fixedValue;}
- set
- { // Default Value and fixed Value are mutually exclusive
- defaultValue = null;
- fixedValue = value;
- }
- }
- [DefaultValue(XmlSchemaForm.None)]
- [System.Xml.Serialization.XmlAttribute("form")]
- public XmlSchemaForm Form
- {
- get{ return form;}
- set{ form = value;}
- }
- [System.Xml.Serialization.XmlAttribute("name")]
- public string Name
- {
- get{ return name;}
- set
- {
- name = value;
- }
- }
- [System.Xml.Serialization.XmlAttribute("ref")]
- public XmlQualifiedName RefName
- {
- get{ return refName;}
- set
- {
- refName = value;
- }
- }
-
- [System.Xml.Serialization.XmlAttribute("type")]
- public XmlQualifiedName SchemaTypeName
- {
- get{ return schemaTypeName;}
- set{ schemaTypeName = value;}
- }
- [XmlElement("simpleType",Namespace=XmlSchema.Namespace)]
- public XmlSchemaSimpleType SchemaType
- {
- get{ return schemaType;}
- set{ schemaType = value;}
- }
- [DefaultValue(XmlSchemaUse.None)]
- [System.Xml.Serialization.XmlAttribute("use")]
- public XmlSchemaUse Use
- {
- get{ return use;}
- set{ use = value;}
- }
- [XmlIgnore]
- public XmlQualifiedName QualifiedName
- {
- get{ return qualifiedName;}
- }
- [XmlIgnore]
- public object AttributeType
- { //FIXME: This is not correct. Is it?
- get{ return attributeType; }
- }
- #endregion
- /// <remarks>
- /// For an attribute:
- /// a) If the parent is schema
- /// 1-5 are from <xs:complexType name="topLevelAttribute"> in the Schema for Schema
- /// 6-8 are from "Constraints on XML Representations of Attribute Declarations"
- /// 9-10 are from "Attribute Declaration Schema Component"
- /// 11-16 are from "Constraints on Attribute Declaration Schema Components"
- /// 1. ref must be absent
- /// 2. form must be absent
- /// 3. use must be absent
- /// 4. name must be present and of type NCName
- /// 5. *NO CHECK REQUIRED* Only simple types and annotation are allowed as content
- /// 6. default and fixed must not both be present.
- /// 7. *NO CHECK REQUIRED* If default and use are both present... (Not possible since use is absent)
- /// 8. type and <simpleType> must not both be present.
- /// 9. Target Namespace should be schema's targetnamespace or absent
- /// 10. Type Definiton coressponds to <simpletype> element, or type value, or absent
- /// 11. *TO UNDERSTAND* Missing Sub-components
- /// 12. value constraint must be of the same datatype as of type
- /// 13. if the type definition is ID then there should be no value constraint.
- /// 14. name must not be xmlns
- /// 15. Targetnamespace must not be xsi. This implies the target namespace of schema can't be xsi if toplevel attributes are used.
- /// 16. *Exception to rule 15* inbuilt attributes: xsi:nil, xsi:type, xsi:schemaLocation, xsi: noNamespaceSchemaLocation
- /// b) If the parent is complextype and ref is not set
- /// 1. name must be present and of type NCName.
- /// 2. type and <simpleType> must not both be present.
- /// 3. default and fixed must not both be present.
- /// 4. If default and use are both present, use must have the ·actual value· optional.
- /// 5. name must not be xmlns
- /// 6. Targetnamespace must not be xsi.
- /// 7. *Exception to rule 15* inbuilt attributes: xsi:nil, xsi:type, xsi:schemaLocation, xsi: noNamespaceSchemaLocation
- /// 8. If form has actual value qualified or the schema's formdefault is qualified, targetnamespace
- /// is same as schema's target namespace, otherwise absent.
- /// c) if the parent is not schema and ref is set
- /// 1. name must not be present
- /// 2. all of <simpleType>, form and type must be absent.
- /// 3. default and fixed must not both be present.
- /// 4. If default and use are both present, use must have the ·actual value· optional.
- /// </remarks>
- [MonoTODO]
- internal int Compile(ValidationEventHandler h, XmlSchema schema)
- {
- // If this is already compiled this time, simply skip.
- if (this.IsComplied (schema.CompilationId))
- return 0;
- errorCount = 0;
-
- if(parentIsSchema)//a
- {
- if(RefName!= null && !RefName.IsEmpty) // a.1
- error(h,"ref must be absent in the top level <attribute>");
-
- if(Form != XmlSchemaForm.None) // a.2
- error(h,"form must be absent in the top level <attribute>");
-
- if(Use != XmlSchemaUse.None) // a.3
- error(h,"use must be absent in the top level <attribute>");
- // TODO: a.10, a.11, a.12, a.13
- CompileCommon(h, schema, true);
- }
- else // local
- {
- //FIXME: How to Use of AttributeFormDefault????
- if(RefName == null || RefName.IsEmpty)
- {
- //TODO: b.8
- CompileCommon(h, schema, true);
- }
- else
- {
- if(this.name != null)
- error(h,"name must be absent if ref is present");
- if(this.form != XmlSchemaForm.None)
- error(h,"form must be absent if ref is present");
- if(this.schemaType != null)
- error(h,"simpletype must be absent if ref is present");
- if(this.schemaTypeName != null && !this.schemaTypeName.IsEmpty)
- error(h,"type must be absent if ref is present");
- CompileCommon(h, schema, false);
- }
- }
- this.CompilationId = schema.CompilationId;
- return errorCount;
- }
-
- private void CompileCommon(ValidationEventHandler h, XmlSchema schema, bool refIsNotPresent)
- {
- if(refIsNotPresent)
- {
- if(Name == null) //a.4, b.1,
- error(h,"Required attribute name must be present");
- else if(!XmlSchemaUtil.CheckNCName(Name)) // a.4.2, b1.2
- error(h,"attribute name must be NCName");
- else if(Name == "xmlns") // a.14 , b5
- error(h,"attribute name must not be xmlns");
- else
- qualifiedName = new XmlQualifiedName(Name, schema.TargetNamespace);
- if(SchemaType != null)
- {
- if(SchemaTypeName != null && !SchemaTypeName.IsEmpty) // a.8
- error(h,"attribute can't have both a type and <simpleType> content");
- errorCount += SchemaType.Compile(h, schema);
- }
- if(SchemaTypeName != null && !XmlSchemaUtil.CheckQName(SchemaTypeName))
- error(h,SchemaTypeName+" is not a valid QName");
- }
- else
- {
- if(RefName == null || RefName.IsEmpty)
- error(h,"Error: Should Never Happen. refname must be present");
- else
- qualifiedName = RefName;
- }
- if(schema.TargetNamespace == XmlSchema.InstanceNamespace && Name != "nil" && Name != "type"
- && Name != "schemaLocation" && Name != "noNamespaceSchemaLocation") // a.15, a.16
- error(h,"targetNamespace can't be " + XmlSchema.InstanceNamespace);
- if(DefaultValue != null && FixedValue != null) // a.6, b.3, c.3
- error(h,"default and fixed must not both be present in an Attribute");
- if(DefaultValue != null && Use != XmlSchemaUse.None && Use != XmlSchemaUse.Optional)
- error(h,"if default is present, use must be optional");
- XmlSchemaUtil.CompileID(Id, this, schema.IDCollection, h);
- }
- /// <summary>
- /// Schema Component:
- /// QName, SimpleType, Scope, Default|Fixed, annotation
- /// </summary>
- [MonoTODO]
- internal int Validate(ValidationEventHandler h, XmlSchema schema)
- {
- if(isCompiled)
- return errorCount;
- //If Parent is schema:
- if(parentIsSchema)
- {
- if(SchemaType != null)
- {
- errorCount += SchemaType.Validate(h, schema);
- attributeType = SchemaType;
- }
- else if(SchemaTypeName != null && !SchemaTypeName.IsEmpty)
- {
- //First Try to get a Inbuilt DataType
- XmlSchemaDatatype dtype = XmlSchemaDatatype.FromName(SchemaTypeName);
- if(dtype != null)
- {
- attributeType = dtype;
- }
- else
- {
- XmlSchemaObject obj = schema.SchemaTypes[SchemaTypeName];
- if(obj is XmlSchemaSimpleType)
- {
- XmlSchemaSimpleType stype = (XmlSchemaSimpleType) obj;
- errorCount += stype.Validate(h, schema);
- attributeType = stype;
- }
- else if(attributeType == null)
- error(h,"The type '"+ SchemaTypeName +"' is not defined in the schema");
- else if(attributeType is XmlSchemaComplexType)
- error(h,"An attribute can't have complexType Content");
- else
- error(h, "Should Never Happen. Illegal content in SchemaTypes");
- }
- }
- else
- {
- error(h,"Should Never Happen. Attribute SimpleType Not set. Should have been caught in the Compile() phase");
- }
- }
- else
- {
- //TODO: Local Attribute Validation
- }
- isCompiled = true;
- return errorCount;
- }
- //<attribute
- // default = string
- // fixed = string
- // form = (qualified | unqualified)
- // id = ID
- // name = NCName
- // ref = QName
- // type = QName
- // use = (optional | prohibited | required) : optional
- // {any attributes with non-schema namespace . . .}>
- // Content: (annotation?, (simpleType?))
- //</attribute>
- internal static XmlSchemaAttribute Read(XmlSchemaReader reader, ValidationEventHandler h)
- {
- XmlSchemaAttribute attribute = new XmlSchemaAttribute();
- reader.MoveToElement();
- if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
- {
- error(h,"Should not happen :1: XmlSchemaAttribute.Read, name="+reader.Name,null);
- reader.SkipToEnd();
- return null;
- }
- attribute.LineNumber = reader.LineNumber;
- attribute.LinePosition = reader.LinePosition;
- attribute.SourceUri = reader.BaseURI;
- while(reader.MoveToNextAttribute())
- {
- if(reader.Name == "default")
- {
- attribute.defaultValue = reader.Value;
- }
- else if(reader.Name == "fixed")
- {
- attribute.fixedValue = reader.Value;
- }
- else if(reader.Name == "form")
- {
- Exception innerex;
- attribute.form = XmlSchemaUtil.ReadFormAttribute(reader,out innerex);
- if(innerex != null)
- error(h, reader.Value + " is not a valid value for form attribute", innerex);
- }
- else if(reader.Name == "id")
- {
- attribute.Id = reader.Value;
- }
- else if(reader.Name == "name")
- {
- attribute.name = reader.Value;
- }
- else if(reader.Name == "ref")
- {
- Exception innerex;
- attribute.refName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);
- if(innerex != null)
- error(h, reader.Value + " is not a valid value for ref attribute",innerex);
- }
- else if(reader.Name == "type")
- {
- Exception innerex;
- attribute.schemaTypeName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);
- if(innerex != null)
- error(h, reader.Value + " is not a valid value for type attribute",innerex);
- }
- else if(reader.Name == "use")
- {
- Exception innerex;
- attribute.use = XmlSchemaUtil.ReadUseAttribute(reader,out innerex);
- if(innerex != null)
- error(h, reader.Value + " is not a valid value for use attribute", innerex);
- }
- else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
- {
- error(h,reader.Name + " is not a valid attribute for attribute",null);
- }
- else
- {
- XmlSchemaUtil.ReadUnhandledAttribute(reader,attribute);
- }
- }
-
- reader.MoveToElement();
- if(reader.IsEmptyElement)
- return attribute;
- // Content: (annotation?, (simpleType?))
- int level = 1;
- while(reader.ReadNextElement())
- {
- if(reader.NodeType == XmlNodeType.EndElement)
- {
- if(reader.LocalName != xmlname)
- error(h,"Should not happen :2: XmlSchemaAttribute.Read, name="+reader.Name,null);
- break;
- }
- if(level <= 1 && reader.LocalName == "annotation")
- {
- level = 2; //Only one annotation
- XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
- if(annotation != null)
- attribute.Annotation = annotation;
- continue;
- }
- if(level <=2 && reader.LocalName == "simpleType")
- {
- level = 3;
- XmlSchemaSimpleType stype = XmlSchemaSimpleType.Read(reader,h);
- if(stype != null)
- attribute.schemaType = stype;
- continue;
- }
- reader.RaiseInvalidElementError();
- }
- return attribute;
- }
-
- }
- }
|