Switch to compiler-tester
[mono.git] / mcs / class / corlib / System.Security / NamedPermissionSet.cs
1 //
2 // System.Security.NamedPermissionSet
3 //
4 // Authors:
5 //      Dan Lewis (dihlewis@yahoo.co.uk)
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // (C) 2002
9 // Portions (C) 2003, 2004 Motus Technologies Inc. (http://www.motus.com)
10 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System.Runtime.InteropServices;
33 using System.Security.Permissions;
34
35 namespace System.Security {
36
37 #if NET_2_0
38         [ComVisible (true)]
39 #endif
40         [Serializable]
41         public sealed class NamedPermissionSet : PermissionSet {
42
43                 private string name;
44                 private string description;
45
46                 // for PolicyLevel (to avoid validation duplication)
47                 internal NamedPermissionSet ()
48                         : base ()
49                 {
50                 }
51
52                 public NamedPermissionSet (string name, PermissionSet set) 
53                         : base (set) 
54                 {
55                         Name = name;
56                 }
57
58                 public NamedPermissionSet (string name, PermissionState state) 
59                         : base (state) 
60                 {
61                         Name = name;
62                 }
63
64                 public NamedPermissionSet (NamedPermissionSet set) 
65                         : base (set)
66                 {
67                         name = set.name; // name can be null here
68                         description = set.description;
69                 }
70
71                 public NamedPermissionSet (string name) 
72                         : this (name, PermissionState.None)
73                 {
74                 }
75
76                 // properties
77
78                 public string Description {
79                         get { return description; }
80                         set { description = value; }
81                 }
82
83                 public string Name {
84                         get { return name; }
85                         set { 
86                                 if ((value == null) || (value == String.Empty)) {
87                                         throw new ArgumentException (Locale.GetText ("invalid name"));
88                                 }
89                                 name = value; 
90                         }
91                 }
92
93                 // methods
94
95                 public override PermissionSet Copy () 
96                 {
97                         return new NamedPermissionSet (this);
98                 }
99
100                 public NamedPermissionSet Copy (string name) 
101                 {
102                         NamedPermissionSet nps = new NamedPermissionSet (this);
103                         nps.Name = name;                // get the new name
104                         return nps;
105                 }
106
107                 public override void FromXml (SecurityElement e) 
108                 {
109                         base.FromXml (e);
110                         // strangely it can import a null Name (bypassing property setter)
111                         name = e.Attribute ("Name");
112                         description = e.Attribute ("Description");
113                         if (description == null)
114                                 description = String.Empty;
115                 }
116
117                 public override SecurityElement ToXml () 
118                 {
119                         SecurityElement se = base.ToXml ();
120                         if (name != null)
121                                 se.AddAttribute ("Name", name);
122                         if (description != null)
123                                 se.AddAttribute ("Description", description);
124                         return se;
125                 }
126
127 #if NET_2_0
128                 [ComVisible (false)]
129                 public override bool Equals (object obj)
130                 {
131                         if (obj == null)
132                                 return false;
133                         NamedPermissionSet nps = (obj as NamedPermissionSet);
134                         if (nps == null)
135                                 return false;
136                         // description isn't part of the comparaison
137                         return ((name == nps.Name) && base.Equals (obj));
138                 }
139
140                 [ComVisible (false)]
141                 public override int GetHashCode ()
142                 {
143                         int hc = base.GetHashCode ();
144                         // name is part of the hash code (except when null)
145                         if (name != null)
146                                 hc ^= name.GetHashCode ();
147                         // description is never part of the hash code
148                         return hc;
149                 }
150 #endif
151         }
152 }