Merge pull request #586 from awinters-fvs/awinters/sgen-los-sizeof-fix
[mono.git] / mcs / class / corlib / System.Security / SecurityElement.cs
index c1be6f1eb117b1221a53f3cae198c957404cba1b..ca0513ba86a37a1b191f2ac6ad4f8c9c771834e0 100644 (file)
@@ -38,9 +38,7 @@ using Mono.Xml;
 
 namespace System.Security {
 
-#if NET_2_0
        [ComVisible (true)]
-#endif
        [Serializable]
        public sealed class SecurityElement 
        {
@@ -58,7 +56,7 @@ namespace System.Security {
                                        throw new ArgumentException (Locale.GetText ("Invalid XML attribute value") + ": " + value);
 
                                _name = name;
-                               _value = value;
+                               _value = SecurityElement.Unescape (value);
                        }
 
                        public string Name {
@@ -95,8 +93,13 @@ namespace System.Security {
                
                public SecurityElement (string tag, string text)
                {
-                       this.Tag = tag;
-                       this.Text = text;
+                       if (tag == null)
+                               throw new ArgumentNullException ("tag");
+                       if (!IsValidTag (tag))
+                               throw new ArgumentException (Locale.GetText ("Invalid XML string") + ": " + tag);
+                       this.tag = tag;
+
+                       Text = text;
                }
 
                // not a deep copy (childs are references)
@@ -129,7 +132,7 @@ namespace System.Security {
                                return result;
                        }
 
-                       set {                           
+                       set {
                                if (value == null || value.Count == 0) {
                                        attributes.Clear ();
                                        return;
@@ -170,11 +173,10 @@ namespace System.Security {
                        }
                        set {
                                if (value == null)
-                                       throw new ArgumentNullException ();
+                                       throw new ArgumentNullException ("Tag");
                                if (!IsValidTag (value))
                                        throw new ArgumentException (Locale.GetText ("Invalid XML string") + ": " + value);
-                               int colon = value.IndexOf (':');
-                               tag = colon < 0 ? value : value.Substring (colon + 1);
+                               tag = value;
                        }
                }
 
@@ -184,9 +186,13 @@ namespace System.Security {
                        }
 
                        set {
-                               if (!IsValidText (value))
-                                       throw new ArgumentException (Locale.GetText ("Invalid XML string") + ": " + text);                              
-                               text = value;
+                               if (value != null) {
+                                       if (!IsValidText (value))
+                                               throw new ArgumentException (
+                                                       Locale.GetText ("Invalid XML string")
+                                                       + ": " + value);
+                               }
+                               text = Unescape (value);
                        }
                }
 
@@ -224,13 +230,11 @@ namespace System.Security {
                        return ((sa == null) ? null : sa.Value);
                }
 
-#if NET_2_0
                [ComVisible (false)]
                public SecurityElement Copy ()
                {
                        return new SecurityElement (this);
                }
-#endif
 
                public bool Equal (SecurityElement other)
                {
@@ -282,7 +286,10 @@ namespace System.Security {
                public static string Escape (string str)
                {
                        StringBuilder sb;
-                       
+
+                       if (str == null)
+                               return null;
+
                        if (str.IndexOfAny (invalid_chars) == -1)
                                return str;
 
@@ -305,12 +312,23 @@ namespace System.Security {
                        return sb.ToString ();
                }
 
-#if NET_2_0
-               public 
-#else
-               internal
-#endif
-               static SecurityElement FromString (string xml)
+               private static string Unescape (string str)
+               {
+                       StringBuilder sb;
+
+                       if (str == null)
+                               return null;
+
+                       sb = new StringBuilder (str);
+                       sb.Replace ("&lt;", "<");
+                       sb.Replace ("&gt;", ">");
+                       sb.Replace ("&amp;", "&");
+                       sb.Replace ("&quot;", "\"");
+                       sb.Replace ("&apos;", "'");
+                       return sb.ToString ();
+               }
+
+               public static SecurityElement FromString (string xml)
                {
                        if (xml == null)
                                throw new ArgumentNullException ("xml");
@@ -321,8 +339,7 @@ namespace System.Security {
                                SecurityParser sp = new SecurityParser ();
                                sp.LoadXml (xml);
                                return sp.ToXml ();
-                       }
-                       catch (Exception e) {
+                       } catch (Exception e) {
                                string msg = Locale.GetText ("Invalid XML.");
                                throw new XmlSyntaxException (msg, e);
                        }
@@ -338,16 +355,14 @@ namespace System.Security {
                        return value != null && value.IndexOfAny (invalid_attr_value_chars) == -1;
                }
 
-               public static bool IsValidTag (string value)
+               public static bool IsValidTag (string tag)
                {
-                       return value != null && value.IndexOfAny (invalid_tag_chars) == -1;
+                       return tag != null && tag.IndexOfAny (invalid_tag_chars) == -1;
                }
 
-               public static bool IsValidText (string value)
+               public static bool IsValidText (string text)
                {
-                       if (value == null)
-                               return true;
-                       return value.IndexOfAny (invalid_text_chars) == -1;
+                       return text != null && text.IndexOfAny (invalid_text_chars) == -1;
                }
 
                public SecurityElement SearchForChildByTag (string tag) 
@@ -364,7 +379,7 @@ namespace System.Security {
                                        return elem;
                        }
                        return null;
-               }                       
+               }
 
                public string SearchForTextOfTag (string tag) 
                {
@@ -383,7 +398,7 @@ namespace System.Security {
                                        return result;
                        }
 
-                       return null;                    
+                       return null;
                }
                
                public override string ToString ()
@@ -395,27 +410,16 @@ namespace System.Security {
                
                private void ToXml (ref StringBuilder s, int level)
                {
-#if ! NET_2_0
-                       s.Append (' ', level * 3);
-#endif
                        s.Append ("<");
                        s.Append (tag);
                        
                        if (attributes != null) {
-#if NET_2_0
                                s.Append (" ");
-#endif
                                for (int i=0; i < attributes.Count; i++) {
                                        SecurityAttribute sa = (SecurityAttribute) attributes [i];
-#if ! NET_2_0
-                                       s.Append (" ");
-                                       // all other attributes must align with the first one
-                                       if (i != 0)
-                                               s.Append (' ', (level * 3) + tag.Length + 1);
-#endif
                                        s.Append (sa.Name)
                                         .Append ("=\"")
-                                        .Append (sa.Value)
+                                        .Append (Escape (sa.Value))
                                         .Append ("\"");
                                        if (i != attributes.Count - 1)
                                                s.Append (Environment.NewLine);
@@ -426,15 +430,12 @@ namespace System.Security {
                            (children == null || children.Count == 0))
                                s.Append ("/>").Append (Environment.NewLine);
                        else {
-                               s.Append (">").Append (text);
+                               s.Append (">").Append (Escape (text));
                                if (children != null) {
                                        s.Append (Environment.NewLine);
                                        foreach (SecurityElement child in children) {
                                                child.ToXml (ref s, level + 1);
                                        }
-#if ! NET_2_0
-                                       s.Append (' ', level * 3);
-#endif
                                }
                                s.Append ("</")
                                 .Append (tag)