Merge pull request #586 from awinters-fvs/awinters/sgen-los-sizeof-fix
[mono.git] / mcs / class / corlib / System.Security / SecurityElement.cs
old mode 100755 (executable)
new mode 100644 (file)
index 08d7828..ca0513b
@@ -2,37 +2,12 @@
 // System.Security.SecurityElement.cs
 //
 // Authors:
-//   Miguel de Icaza (miguel@ximian.com)
-//   Lawrence Pit (loz@cable.a2000.nl)
-//   Sebastien Pouliot  <spouliot@videotron.ca>
+//     Miguel de Icaza (miguel@ximian.com)
+//     Lawrence Pit (loz@cable.a2000.nl)
+//     Sebastien Pouliot  <sebastien@ximian.com>
 //
 // (C) Ximian, Inc. http://www.ximian.com
-
-//
-// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
-//
-// 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.
-//
-
-//
-// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
+// Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
 
 using System.Globalization;
 using System.Collections;
+using System.Runtime.InteropServices;
 using System.Text;
 
-namespace System.Security 
-{
+using Mono.Xml;
+
+namespace System.Security {
+
+       [ComVisible (true)]
        [Serializable]
        public sealed class SecurityElement 
        {
@@ -77,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 {
@@ -114,8 +93,31 @@ 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)
+               internal SecurityElement (SecurityElement se)
+               {
+                       this.Tag = se.Tag;
+                       this.Text = se.Text;
+
+                       if (se.attributes != null) {
+                               foreach (SecurityAttribute sa in se.attributes) {
+                                       this.AddAttribute (sa.Name, sa.Value);
+                               }
+                       }
+                       if (se.children != null) {
+                               foreach (SecurityElement child in se.children) {
+                                       this.AddChild (child);
+                               }
+                       }
                }
                
                public Hashtable Attributes {
@@ -130,7 +132,7 @@ namespace System.Security
                                return result;
                        }
 
-                       set {                           
+                       set {
                                if (value == null || value.Count == 0) {
                                        attributes.Clear ();
                                        return;
@@ -171,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;
                        }
                }
 
@@ -185,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);
                        }
                }
 
@@ -225,6 +230,12 @@ namespace System.Security
                        return ((sa == null) ? null : sa.Value);
                }
 
+               [ComVisible (false)]
+               public SecurityElement Copy ()
+               {
+                       return new SecurityElement (this);
+               }
+
                public bool Equal (SecurityElement other)
                {
                        if (other == null)
@@ -275,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;
 
@@ -298,6 +312,39 @@ namespace System.Security
                        return sb.ToString ();
                }
 
+               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");
+                       if (xml.Length == 0)
+                               throw new XmlSyntaxException (Locale.GetText ("Empty string."));
+
+                       try {
+                               SecurityParser sp = new SecurityParser ();
+                               sp.LoadXml (xml);
+                               return sp.ToXml ();
+                       } catch (Exception e) {
+                               string msg = Locale.GetText ("Invalid XML.");
+                               throw new XmlSyntaxException (msg, e);
+                       }
+               }
+
                public static bool IsValidAttributeName (string name)
                {
                        return name != null && name.IndexOfAny (invalid_attr_name_chars) == -1;
@@ -308,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) 
@@ -334,7 +379,7 @@ namespace System.Security
                                        return elem;
                        }
                        return null;
-               }                       
+               }
 
                public string SearchForTextOfTag (string tag) 
                {
@@ -353,7 +398,7 @@ namespace System.Security
                                        return result;
                        }
 
-                       return null;                    
+                       return null;
                }
                
                public override string ToString ()
@@ -365,20 +410,16 @@ namespace System.Security
                
                private void ToXml (ref StringBuilder s, int level)
                {
-                       s.Append (' ', level * 3);
                        s.Append ("<");
                        s.Append (tag);
                        
                        if (attributes != null) {
+                               s.Append (" ");
                                for (int i=0; i < attributes.Count; i++) {
                                        SecurityAttribute sa = (SecurityAttribute) attributes [i];
-                                       s.Append (" ");
-                                       // all other attributes must align with the first one
-                                       if (i != 0)
-                                               s.Append (' ', (level * 3) + tag.Length + 1);
                                        s.Append (sa.Name)
                                         .Append ("=\"")
-                                        .Append (sa.Value)
+                                        .Append (Escape (sa.Value))
                                         .Append ("\"");
                                        if (i != attributes.Count - 1)
                                                s.Append (Environment.NewLine);
@@ -389,13 +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);
                                        }
-                                       s.Append (' ', level * 3);
                                }
                                s.Append ("</")
                                 .Append (tag)