private AttributeAccessor ImportAttribute(XmlSchemaAttribute attribute, string identifier, string ns, XmlSchemaAttribute defaultValueProvider)
{
if (attribute.Use == XmlSchemaUse.Prohibited) return null;
if (!attribute.RefName.IsEmpty)
{
if (attribute.RefName.Namespace == XmlReservedNs.NsXml)
return ImportSpecialAttribute(attribute.RefName, identifier);
else
return ImportAttribute(FindAttribute(attribute.RefName), identifier, attribute.RefName.Namespace, defaultValueProvider);
}
TypeMapping mapping;
if (attribute.Name.Length == 0) throw new InvalidOperationException(SR.XmlAttributeHasNoName);
if (identifier.Length == 0)
identifier = CodeIdentifier.MakeValid(attribute.Name);
else
identifier += CodeIdentifier.MakePascal(attribute.Name);
if (!attribute.SchemaTypeName.IsEmpty)
mapping = (TypeMapping)ImportType(attribute.SchemaTypeName, typeof(TypeMapping), null, TypeFlags.CanBeAttributeValue, false);
else if (attribute.SchemaType != null)
mapping = ImportDataType((XmlSchemaSimpleType)attribute.SchemaType, ns, identifier, null, TypeFlags.CanBeAttributeValue, false);
else
{
mapping = GetDefaultMapping(TypeFlags.CanBeAttributeValue);
}
// let the extensions to run
if (mapping != null && !mapping.TypeDesc.IsMappedType)
{
RunSchemaExtensions(mapping, attribute.SchemaTypeName, attribute.SchemaType, attribute, TypeFlags.CanBeElementValue | TypeFlags.CanBeAttributeValue | TypeFlags.CanBeTextValue);
}
AttributeAccessor accessor = new AttributeAccessor();
accessor.Name = attribute.Name;
accessor.Namespace = ns;
accessor.Form = AttributeForm(ns, attribute);
accessor.CheckSpecial();
accessor.Mapping = mapping;
accessor.IsList = mapping.IsList;
accessor.IsOptional = attribute.Use != XmlSchemaUse.Required;
if (defaultValueProvider.DefaultValue != null)
{
accessor.Default = defaultValueProvider.DefaultValue;
}
else if (defaultValueProvider.FixedValue != null)
{
accessor.Default = defaultValueProvider.FixedValue;
accessor.IsFixed = true;
}
else if (attribute != defaultValueProvider)
{
if (attribute.DefaultValue != null)
{
accessor.Default = attribute.DefaultValue;
}
else if (attribute.FixedValue != null)
{
accessor.Default = attribute.FixedValue;
accessor.IsFixed = true;
}
}
return accessor;
}