Moved chain building and validation from Mono.Security to System
[mono.git] / mcs / class / System.XML / System.Xml.Serialization / XmlAttributes.cs
index 97a4ea740b853a5d18ef84a6967a6dea5778cb2d..eddc9b0c3bdb72e9c1551b7deb6102ed0bf93a1a 100644 (file)
@@ -7,6 +7,27 @@
 // (C) 2002 John Donagher
 //
 
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
 using System.Reflection;
 using System;
 using System.ComponentModel;
@@ -25,7 +46,7 @@ namespace System.Xml.Serialization
                private XmlArrayItemAttributes xmlArrayItems = new XmlArrayItemAttributes();
                private XmlAttributeAttribute xmlAttribute;
                private XmlChoiceIdentifierAttribute xmlChoiceIdentifier;
-               private object xmlDefaultValue;
+               private object xmlDefaultValue = System.DBNull.Value;
                private XmlElementAttributes xmlElements = new XmlElementAttributes();
                private XmlEnumAttribute xmlEnum;
                private bool xmlIgnore;
@@ -34,59 +55,10 @@ namespace System.Xml.Serialization
                private XmlTextAttribute xmlText;
                private XmlTypeAttribute xmlType;
 
-               private MemberInfo minfo;
-               private FieldInfo  finfo;
-               private PropertyInfo pinfo;
-               internal ArrayList XmlIncludes = new ArrayList();
-               //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 ()
                {
                }
 
-               static XmlAttributes ()
-               {
-                       attrComparer = new XmlAttributes.XmlAttributesComparer();
-               }
-
                public XmlAttributes (ICustomAttributeProvider provider)
                {
                        object[] attributes = provider.GetCustomAttributes(false);
@@ -105,7 +77,7 @@ namespace System.Xml.Serialization
                                else if(obj is XmlChoiceIdentifierAttribute)
                                        xmlChoiceIdentifier = (XmlChoiceIdentifierAttribute) obj;
                                else if(obj is DefaultValueAttribute)
-                                       xmlDefaultValue = obj;
+                                       xmlDefaultValue = ((DefaultValueAttribute)obj).Value;
                                else if(obj is XmlElementAttribute )
                                        xmlElements.Add((XmlElementAttribute ) obj);
                                else if(obj is XmlEnumAttribute)
@@ -262,197 +234,49 @@ namespace System.Xml.Serialization
                        }
                }
                #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;
-                       else if (XmlType != null && XmlType.TypeName != null && XmlType.TypeName != "")
-                               return XmlType.TypeName;
-                       return defaultName;
-               }
-
-               internal string GetElementName(Type type, string defaultName)
-               {
-                       string anonymousElemAttrName = null;
-                       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 != "")
-                                       anonymousElemAttrName = elem.ElementName;
-                       }
-                       if (anonymousElemAttrName != null)
-                               return anonymousElemAttrName;
-
-                       if (XmlType != null && XmlType.TypeName != null && XmlType.TypeName != "")
-                               return XmlType.TypeName;
-                       return defaultName;
-               }
-
-               internal string GetAttributeNamespace(Type type)
+               internal void AddKeyHash (System.Text.StringBuilder sb)
                {
-                       if(XmlAttribute != null)
-                               return XmlAttribute.Namespace;
-                       return null;
-               }
+                       sb.Append ("XA ");
+                       
+                       KeyHelper.AddField (sb, 1, xmlIgnore);
+                       KeyHelper.AddField (sb, 2, xmlns);
+                       KeyHelper.AddField (sb, 3, xmlAnyAttribute!=null);
 
-               internal string GetElementNamespace(Type type)
-               {
-                       string defaultNS = null;
-                       foreach(XmlElementAttribute elem in XmlElements)
-                       {
-                               if(elem.Type == type )
-                                       return elem.Namespace;
-                               else if(elem.Type == null)
-                                       defaultNS = elem.Namespace;
+                       xmlAnyElements.AddKeyHash (sb);
+                       xmlArrayItems.AddKeyHash (sb);
+                       xmlElements.AddKeyHash (sb);
+                       
+                       if (xmlArray != null)
+                               xmlArray.AddKeyHash (sb);
+                               
+                       if (xmlAttribute != null)
+                               xmlAttribute.AddKeyHash (sb);
+                               
+                       if (xmlDefaultValue == null) {
+                               sb.Append ("n");
                        }
-                       return defaultNS;
-               }
-
-               internal bool GetElementIsNullable (Type type)
-               {
-                       bool defaultIsNullable = false;
-                       foreach(XmlElementAttribute elem in XmlElements)
-                       {
-                               if(elem.Type == type)
-                                       return elem.IsNullable;
-                               else if(elem.Type == null)
-                                       defaultIsNullable = elem.IsNullable;
+                       else if (!(xmlDefaultValue is System.DBNull)) {
+                               string v = XmlCustomFormatter.ToXmlString (TypeTranslator.GetTypeData (xmlDefaultValue.GetType()), xmlDefaultValue);
+                               sb.Append ("v" + v);
                        }
-                       return defaultIsNullable;
+                       
+                       if (xmlEnum != null)
+                               xmlEnum.AddKeyHash (sb);
+                               
+                       if (xmlRoot != null)
+                               xmlRoot.AddKeyHash (sb);
+                               
+                       if (xmlText != null)
+                               xmlText.AddKeyHash (sb);
+                               
+                       if (xmlType != null)
+                               xmlType.AddKeyHash (sb);
+                               
+                       if (xmlChoiceIdentifier != null)
+                               xmlChoiceIdentifier.AddKeyHash (sb);
+                               
+                       sb.Append ("|");
                }
        }
 }