| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439 |
- //
- // XmlAttributes.cs:
- //
- // Author:
- // John Donagher ([email protected])
- //
- // (C) 2002 John Donagher
- //
- using System.Reflection;
- using System;
- using System.ComponentModel;
- using System.Collections;
- namespace System.Xml.Serialization
- {
- /// <summary>
- /// Summary description for XmlAttributes.
- /// </summary>
- public class XmlAttributes
- {
- private XmlAnyAttributeAttribute xmlAnyAttribute;
- private XmlAnyElementAttributes xmlAnyElements;
- private XmlArrayAttribute xmlArray;
- private XmlArrayItemAttributes xmlArrayItems;
- private XmlAttributeAttribute xmlAttribute;
- private XmlChoiceIdentifierAttribute xmlChoiceIdentifier;
- private object xmlDefaultValue;
- private XmlElementAttributes xmlElements;
- private XmlEnumAttribute xmlEnum;
- private bool xmlIgnore;
- private bool xmlns;
- private XmlRootAttribute xmlRoot;
- private XmlTextAttribute xmlText;
- private XmlTypeAttribute xmlType;
- private MemberInfo minfo;
- private FieldInfo finfo;
- private PropertyInfo pinfo;
- internal ArrayList XmlIncludes;
- //internal string ElementName;
- //The element Order in serialization.
- internal int order;
- internal bool isAttribute;
- internal static XmlAttributes.XmlAttributesComparer attrComparer;
- //Sorting Order of Elements: XmlNs, XmlAttributes, XmlElement
- internal class XmlAttributesComparer : IComparer
- {
- public int Compare(object x,object y)
- {
- if(x is XmlAttributes && y is XmlAttributes)
- {
- XmlAttributes attx = (XmlAttributes)x;
- XmlAttributes atty = (XmlAttributes)y;
- if(attx.xmlns)
- return -1;
- if(atty.xmlns)
- return 1;
- if(attx.isAttribute)
- return -1;
- if(atty.isAttribute)
- return 1;
- int diff = attx.order - atty.order;
- if(diff == 0)
- return 0;
- if(diff > 0)
- return 1;
- if(diff < 0)
- return -1;
- }
- if(x == null)
- return -1;
- if(y == null)
- return 1;
- throw new Exception("Should never occur. XmlAttributesComparer.Compare");
- }
- }
- public XmlAttributes ()
- {
- xmlAnyElements = new XmlAnyElementAttributes ();
- xmlArrayItems = new XmlArrayItemAttributes ();
- xmlElements = new XmlElementAttributes ();
- XmlIncludes = new ArrayList();
- }
- static XmlAttributes ()
- {
- attrComparer = new XmlAttributes.XmlAttributesComparer();
- }
- public XmlAttributes (ICustomAttributeProvider provider)
- {
- object[] attributes = provider.GetCustomAttributes(false);
- foreach(object obj in attributes)
- {
- if(obj is XmlAnyAttributeAttribute)
- xmlAnyAttribute = (XmlAnyAttributeAttribute) obj;
- else if(obj is XmlAnyElementAttribute)
- xmlAnyElements.Add((XmlAnyElementAttribute) obj);
- else if(obj is XmlArrayAttribute)
- xmlArray = (XmlArrayAttribute) obj;
- else if(obj is XmlArrayItemAttribute)
- xmlArrayItems.Add((XmlArrayItemAttribute) obj);
- else if(obj is XmlAttributeAttribute)
- xmlAttribute = (XmlAttributeAttribute) obj;
- else if(obj is XmlChoiceIdentifierAttribute)
- xmlChoiceIdentifier = (XmlChoiceIdentifierAttribute) obj;
- else if(obj is DefaultValueAttribute)
- xmlDefaultValue = obj;
- else if(obj is XmlElementAttribute )
- xmlElements.Add((XmlElementAttribute ) obj);
- else if(obj is XmlEnumAttribute)
- xmlEnum = (XmlEnumAttribute) obj;
- else if(obj is XmlIgnoreAttribute)
- xmlIgnore = true;
- else if(obj is XmlNamespaceDeclarationsAttribute)
- xmlns = true;
- else if(obj is XmlRootAttribute)
- xmlRoot = (XmlRootAttribute) obj;
- else if(obj is XmlTextAttribute)
- xmlText = (XmlTextAttribute) obj;
- else if(obj is XmlTypeAttribute)
- xmlType = (XmlTypeAttribute) obj;
- }
- }
- #region public properties
- public XmlAnyAttributeAttribute XmlAnyAttribute
- {
- get
- {
- return xmlAnyAttribute;
- }
- set
- {
- xmlAnyAttribute = value;
- }
- }
- public XmlAnyElementAttributes XmlAnyElements
- {
- get
- {
- return xmlAnyElements;
- }
- }
- public XmlArrayAttribute XmlArray
- {
- get
- {
- return xmlArray;
- }
- set
- {
- xmlArray = value;
- }
- }
- public XmlArrayItemAttributes XmlArrayItems
- {
- get
- {
- return xmlArrayItems;
- }
- }
- public XmlAttributeAttribute XmlAttribute
- {
- get
- {
- return xmlAttribute;
- }
- set
- {
- xmlAttribute = value;
- }
- }
- public XmlChoiceIdentifierAttribute XmlChoiceIdentifier
- {
- get
- {
- return xmlChoiceIdentifier;
- }
- }
- public object XmlDefaultValue
- {
- get
- {
- return xmlDefaultValue;
- }
- set
- {
- xmlDefaultValue = value;
- }
- }
- public XmlElementAttributes XmlElements
- {
- get
- {
- return xmlElements;
- }
- }
- public XmlEnumAttribute XmlEnum
- {
- get
- {
- return xmlEnum;
- }
- set
- {
- xmlEnum = value;
- }
- }
- public bool XmlIgnore
- {
- get
- {
- return xmlIgnore;
- }
- set
- {
- xmlIgnore = value;
- }
- }
- public bool Xmlns
- {
- get
- {
- return xmlns;
- }
- set
- {
- xmlns = value;
- }
- }
- public XmlRootAttribute XmlRoot
- {
- get
- {
- return xmlRoot;}
- set
- {
- xmlRoot = value;
- }
- }
- public XmlTextAttribute XmlText
- {
- get
- {
- return xmlText;
- }
- set
- {
- xmlText = value;
- }
- }
- public XmlTypeAttribute XmlType
- {
- get
- {
- return xmlType;
- }
- set
- {
- xmlType = value;
- }
- }
- #endregion
- #region internal properties
- internal MemberInfo MemberInfo
- {
- get { return minfo; }
- set { minfo = value; }
- }
- internal FieldInfo FieldInfo
- {
- get { return finfo; }
- set { finfo = value; }
- }
- internal PropertyInfo PropertyInfo
- {
- get { return pinfo; }
- set { pinfo = value; }
- }
- #endregion
- //Only permissible attributes for a class type are: XmlRoot and XmlInclude
- internal static XmlAttributes FromClass(Type classType)
- {
- XmlAttributes XmlAttr = new XmlAttributes();
- object[] attributes = classType.GetCustomAttributes(false);
- foreach(object obj in attributes)
- {
- if(obj is XmlRootAttribute)
- XmlAttr.xmlRoot = (XmlRootAttribute) obj;
- else if(obj is XmlIncludeAttribute)
- XmlAttr.XmlIncludes.Add(obj);
- }
- return XmlAttr;
- }
- internal static XmlAttributes FromField(MemberInfo member, FieldInfo finfo)
- {
- XmlAttributes XmlAttr = new XmlAttributes();
- object[] attributes = member.GetCustomAttributes(false);
- XmlAttr.AddMemberAttributes(attributes);
- XmlAttr.minfo = member;
- XmlAttr.finfo = finfo;
- return XmlAttr;
- }
-
- internal static XmlAttributes FromProperty(MemberInfo member, PropertyInfo pinfo)
- {
- XmlAttributes XmlAttr = new XmlAttributes();
- object[] attributes = member.GetCustomAttributes(false);
- XmlAttr.AddMemberAttributes(attributes);
- XmlAttr.minfo = member;
- XmlAttr.pinfo = pinfo;
- return XmlAttr;
- }
- internal void AddMemberAttributes(object[] attributes)
- {
- foreach(object obj in attributes)
- {
- if(obj is XmlAnyAttributeAttribute)
- {
- xmlAnyAttribute = (XmlAnyAttributeAttribute) obj;
- isAttribute = true;
- }
- else if(obj is XmlAttributeAttribute)
- {
- xmlAttribute = (XmlAttributeAttribute) obj;
- isAttribute = true;
- }
- else if(obj is XmlNamespaceDeclarationsAttribute)
- {
- xmlns = true;
- isAttribute = true;
- }
- else if(obj is XmlAnyElementAttribute)
- {
- xmlAnyElements.Add((XmlAnyElementAttribute) obj);
- order = ((XmlAnyElementAttribute) obj).Order;
- }
- else if(obj is XmlArrayAttribute)
- {
- xmlArray = (XmlArrayAttribute) obj;
- order = ((XmlArrayAttribute) obj).Order;
- }
- else if(obj is XmlArrayItemAttribute)
- {
- xmlArrayItems.Add((XmlArrayItemAttribute) obj);
- order = ((XmlArrayItemAttribute) obj).Order;
- }
- else if(obj is XmlChoiceIdentifierAttribute)
- {
- xmlChoiceIdentifier = (XmlChoiceIdentifierAttribute) obj;
- order = ((XmlChoiceIdentifierAttribute) obj).Order;
- }
- else if(obj is XmlTextAttribute)
- {
- xmlText = (XmlTextAttribute) obj;
- order = ((XmlTextAttribute) obj).Order;
- }
- else if(obj is XmlElementAttribute )
- {
- xmlElements.Add((XmlElementAttribute ) obj);
- order = ((XmlElementAttribute ) obj).Order;
- }
- else if(obj is DefaultValueAttribute)
- {
- xmlDefaultValue = ((DefaultValueAttribute ) obj).Value;
- }
- else if(obj is XmlEnumAttribute)
- {
- xmlEnum = (XmlEnumAttribute) obj;
- }
- else if(obj is XmlIgnoreAttribute)
- {
- xmlIgnore = true;
- }
- else if(obj is XmlRootAttribute)
- {
- throw new Exception("should never happen. XmlRoot on a member");
- }
- else if(obj is XmlTypeAttribute)
- {
- xmlType = (XmlTypeAttribute) obj;
- }
- }
- }
- internal string GetAttributeName(Type type, string defaultName)
- {
- if(XmlAttribute != null && XmlAttribute.AttributeName != null && XmlAttribute.AttributeName != "")
- return XmlAttribute.AttributeName;
- return defaultName;
- }
- internal string GetElementName(Type type, string defaultName)
- {
- foreach(XmlElementAttribute elem in XmlElements)
- {
- if(elem.Type == type && elem.ElementName != null && elem.ElementName != "")
- return elem.ElementName;
- else if(elem.Type == null && elem.ElementName != null && elem.ElementName != "")
- return elem.ElementName;
- }
- return defaultName;
- }
- internal string GetAttributeNamespace(Type type)
- {
- if(XmlAttribute != null)
- return XmlAttribute.Namespace;
- return null;
- }
- internal string GetElementNamespace(Type type)
- {
- foreach(XmlElementAttribute elem in XmlElements)
- {
- if(elem.Type == type )
- return elem.Namespace;
- else if(elem.Type == null)
- return elem.Namespace;
- }
- return null;
- }
- }
- }
|