2009-09-22 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / corlib / System.Security.Permissions / PermissionSetAttribute.cs
index a88f924340c905ef39f2a098ebdb1c168bdfb67a..74ceebd9477f8d8c220abd0fc950651315584248 100644 (file)
@@ -3,13 +3,10 @@
 //
 // Authors
 //     Duncan Mak <duncan@ximian.com>
-//     Sebastien Pouliot  <spouliot@videotron.ca>
+//     Sebastien Pouliot  <sebastien@ximian.com>
 //
 // (C) 2002 Ximian, Inc. http://www.ximian.com
-//
-
-//
-// 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
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
-using System;
 using System.IO;
+using System.Runtime.InteropServices;
 using System.Security.Policy;
 using System.Text;
 
+using Mono.Security.Cryptography;
 using Mono.Xml;
 
 namespace System.Security.Permissions {
 
+#if NET_2_0
+       [ComVisible (true)]
+#endif
        [AttributeUsage (AttributeTargets.Assembly | AttributeTargets.Class |
                         AttributeTargets.Struct | AttributeTargets.Constructor |
                         AttributeTargets.Method, AllowMultiple=true, Inherited=false)]
@@ -51,6 +52,9 @@ namespace System.Security.Permissions {
                private string name;
                private bool isUnicodeEncoded;
                private string xml;
+#if NET_2_0
+               private string hex;
+#endif
                
                // Constructor
                public PermissionSetAttribute (SecurityAction action)
@@ -59,26 +63,27 @@ namespace System.Security.Permissions {
                }
                
                // Properties
-               public string File
-               {
+               public string File {
                        get { return file; }
                        set { file = value; }
                }
-
-               public string Name
-               {
+#if NET_2_0
+               public string Hex {
+                       get { return hex; }
+                       set { hex = value; }
+               }
+#endif
+               public string Name {
                        get { return name; }
                        set { name = value; }
                }
 
-               public bool UnicodeEncoded
-               {
+               public bool UnicodeEncoded {
                        get { return isUnicodeEncoded; }
                        set { isUnicodeEncoded = value; }
                }
 
-               public string XML
-               {
+               public string XML {
                        get { return xml; }
                        set { xml = value; }
                }
@@ -91,8 +96,14 @@ namespace System.Security.Permissions {
 
                private PermissionSet CreateFromXml (string xml) 
                {
+#if !NET_2_1
                        SecurityParser sp = new SecurityParser ();
-                       sp.LoadXml (xml);
+                       try {
+                               sp.LoadXml (xml);
+                       }
+                       catch (Mono.Xml.SmallXmlParserException xe) {
+                               throw new XmlSyntaxException (xe.Line, xe.ToString ());
+                       }
                        SecurityElement se = sp.ToXml ();
 
                        string className = se.Attribute ("class");
@@ -100,23 +111,27 @@ namespace System.Security.Permissions {
                                return null;
 
                        PermissionState state = PermissionState.None;
-                       if (se.Attribute ("Unrestricted") == "true")
+                       if (CodeAccessPermission.IsUnrestricted (se))
                                state = PermissionState.Unrestricted;
 
                        if (className.EndsWith ("NamedPermissionSet")) {
                                NamedPermissionSet nps = new NamedPermissionSet (se.Attribute ("Name"), state);
+                               nps.FromXml (se);
                                return (PermissionSet) nps;
                        }
                        else if (className.EndsWith ("PermissionSet")) {
                                PermissionSet ps = new PermissionSet (state);
+                               ps.FromXml (se);
                                return ps;
                        }
+#endif
                        return null;
                }
 
                public PermissionSet CreatePermissionSet ()
                {
                        PermissionSet pset = null;
+#if !NET_2_1
                        if (this.Unrestricted)
                                pset = new PermissionSet (PermissionState.Unrestricted);
                        else {
@@ -133,7 +148,17 @@ namespace System.Security.Permissions {
                                else if (xml != null) {
                                        pset = CreateFromXml (xml);
                                }
+#if NET_2_0
+                               else if (hex != null) {
+                                       // Unicode isn't supported
+                                       //Encoding e = ((isUnicodeEncoded) ? System.Text.Encoding.Unicode : System.Text.Encoding.ASCII);
+                                       Encoding e = System.Text.Encoding.ASCII;
+                                       byte[] bin = CryptoConvert.FromHex (hex);
+                                       pset = CreateFromXml (e.GetString (bin, 0, bin.Length));
+                               }
+#endif
                        }
+#endif
                        return pset;
                }
        }