/// <summary>
/// Processes the specified attribute which is an instance of <c>YAXAttribute</c>.
/// </summary>
/// <param name="attr">The attribute to process.</param>
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
{
throw new Exception("Added new attribute type to the library but not yet processed!");
}
}