private void ProcessYaxAttribute(object attr)
{
if (attr is YAXCommentAttribute)
{
string comment = (attr as YAXCommentAttribute).Comment;
if (!String.IsNullOrEmpty(comment))
{
string[] comments = comment.Split(new [] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < comments.Length; i++)
{
comments[i] = String.Format(" {0} ", comments[i].Trim());
}
this.Comment = comments;
}
}
else if (attr is YAXSerializableFieldAttribute)
{
IsAttributedAsSerializable = true;
}
else if (attr is YAXAttributeForClassAttribute)
{
// it is required that YAXCustomSerializerAttribute is processed earlier
if (ReflectionUtils.IsBasicType(MemberType) || CustomSerializerType != null ||
(m_collectionAttributeInstance != null && m_collectionAttributeInstance.SerializationType == YAXCollectionSerializationTypes.Serially))
{
IsSerializedAsAttribute = true;
SerializationLocation = ".";
}
}
else if (attr is YAXValueForClassAttribute)
{
// it is required that YAXCustomSerializerAttribute is processed earlier
if (ReflectionUtils.IsBasicType(MemberType) || CustomSerializerType != null ||
(m_collectionAttributeInstance != null && m_collectionAttributeInstance.SerializationType == YAXCollectionSerializationTypes.Serially))
{
IsSerializedAsValue = true;
SerializationLocation = ".";
}
}
else if (attr is YAXAttributeForAttribute)
{
// it is required that YAXCustomSerializerAttribute is processed earlier
if (ReflectionUtils.IsBasicType(MemberType) || CustomSerializerType != null ||
(m_collectionAttributeInstance != null && m_collectionAttributeInstance.SerializationType == YAXCollectionSerializationTypes.Serially))
{
IsSerializedAsAttribute = true;
string path, alias;
StringUtils.ExttractPathAndAliasFromLocationString((attr as YAXAttributeForAttribute).Parent, out path, out alias);
SerializationLocation = path;
if (!String.IsNullOrEmpty(alias))
Alias = StringUtils.RefineSingleElement(alias);
}
}
else if (attr is YAXElementForAttribute)
{
IsSerializedAsElement = true;
string path, alias;
StringUtils.ExttractPathAndAliasFromLocationString((attr as YAXElementForAttribute).Parent, out path, out alias);
SerializationLocation = path;
if (!String.IsNullOrEmpty(alias))
Alias = StringUtils.RefineSingleElement(alias);
}
else if (attr is YAXValueForAttribute)
{
// it is required that YAXCustomSerializerAttribute is processed earlier
if (ReflectionUtils.IsBasicType(this.MemberType) || CustomSerializerType != null ||
(m_collectionAttributeInstance != null && m_collectionAttributeInstance.SerializationType == YAXCollectionSerializationTypes.Serially))
{
IsSerializedAsValue = true;
string path, alias;
StringUtils.ExttractPathAndAliasFromLocationString((attr as YAXValueForAttribute).Parent, out path, out alias);
SerializationLocation = path;
if (!String.IsNullOrEmpty(alias))
Alias = StringUtils.RefineSingleElement(alias);
}
}
else if (attr is YAXDontSerializeAttribute)
{
IsAttributedAsDontSerialize = true;
}
else if (attr is YAXSerializeAsAttribute)
{
Alias = StringUtils.RefineSingleElement((attr as YAXSerializeAsAttribute).SerializeAs);
}
else if (attr is YAXCollectionAttribute)
{
m_collectionAttributeInstance = attr as YAXCollectionAttribute;
}
else if (attr is YAXDictionaryAttribute)
{
m_dictionaryAttributeInstance = attr as YAXDictionaryAttribute;
}
else if (attr is YAXErrorIfMissedAttribute)
{
var temp = attr as YAXErrorIfMissedAttribute;
DefaultValue = temp.DefaultValue;
TreatErrorsAs = temp.TreatAs;
}
else if (attr is YAXFormatAttribute)
{
Format = (attr as YAXFormatAttribute).Format;
}
else if (attr is YAXNotCollectionAttribute)
{
// arrays are always treated as collections
if (!ReflectionUtils.IsArray(MemberType))
IsAttributedAsNotCollection = true;
}
else if (attr is YAXCustomSerializerAttribute)
{
Type serType = (attr as YAXCustomSerializerAttribute).CustomSerializerType;
Type genTypeArg;
bool isDesiredInterface = ReflectionUtils.IsDerivedFromGenericInterfaceType(serType, typeof(ICustomSerializer<>), out genTypeArg);
if (!isDesiredInterface)
{
throw new YAXException("The provided custom serialization type is not derived from the proper interface");
}
else if (genTypeArg != this.MemberType)
{
throw new YAXException("The generic argument of the class and the member type do not match");
}
else
{
CustomSerializerType = serType;
}
}
else if(attr is YAXPreserveWhitespaceAttribute)
{
PreservesWhitespace = true;
}
else if (attr is YAXSerializableTypeAttribute)
{
// this should not happen
throw new Exception("This attribute is not applicable to fields and properties!");
}
else if (attr is YAXNamespaceAttribute)
{
var nsAttrib = (attr as YAXNamespaceAttribute);
Namespace = nsAttrib.Namespace;
NamespacePrefix = nsAttrib.Prefix;
}
else if (attr is YAXTypeAttribute)
{
var yaxTypeAttr = attr as YAXTypeAttribute;
string alias = yaxTypeAttr.Alias;
if (alias != null)
{
alias = alias.Trim();
if (alias.Length == 0)
alias = null;
}
if(m_possibleRealTypes.Any(x => x.Type == yaxTypeAttr.Type))
throw new YAXPolymorphicException(String.Format("The type \"{0}\" for field/property \"{1}\" has already been defined through another attribute.", yaxTypeAttr.Type.Name, m_memberInfo));
if (alias != null && m_possibleRealTypes.Any(x => alias.Equals(x.Alias, StringComparison.Ordinal)))
throw new YAXPolymorphicException(String.Format("The alias \"{0}\" given to type \"{1}\" for field/property \"{2}\" has already been given to another type through another attribute.",
alias, yaxTypeAttr.Type.Name, m_memberInfo));
m_possibleRealTypes.Add(yaxTypeAttr);
}
else if (attr is YAXCollectionItemTypeAttribute)
{
var yaxColletionItemTypeAttr = attr as YAXCollectionItemTypeAttribute;
string alias = yaxColletionItemTypeAttr.Alias;
if (alias != null)
{
alias = alias.Trim();
if (alias.Length == 0)
alias = null;
}
if (m_possibleCollectionItemRealTypes.Any(x => x.Type == yaxColletionItemTypeAttr.Type))
throw new YAXPolymorphicException(String.Format("The collection-item type \"{0}\" for collection \"{1}\" has already been defined through another attribute.", yaxColletionItemTypeAttr.Type.Name, m_memberInfo));
if (alias != null && m_possibleCollectionItemRealTypes.Any(x => alias.Equals(x.Alias, StringComparison.Ordinal)))
throw new YAXPolymorphicException(String.Format("The alias \"{0}\" given to collection-item type \"{1}\" for field/property \"{2}\" has already been given to another type through another attribute.",
alias, yaxColletionItemTypeAttr.Type.Name, m_memberInfo));
m_possibleCollectionItemRealTypes.Add(yaxColletionItemTypeAttr);
}
else if (attr is YAXDontSerializeIfNullAttribute)
{
IsAttributedAsDontSerializeIfNull = true;
}
else if (attr is YAXElementOrder)
{
Order = (attr as YAXElementOrder).Order;
}
else
{
throw new Exception("Added new attribute type to the library but not yet processed!");
}
}