Merged into single file, added assertions
[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         [ComVisible (true)]
38         [Serializable]
39         public sealed class NamedPermissionSet : PermissionSet {
40
41                 private string name;
42                 private string description;
43
44                 // for PolicyLevel (to avoid validation duplication)
45                 internal NamedPermissionSet ()
46                         : base ()
47                 {
48                 }
49
50                 public NamedPermissionSet (string name, PermissionSet permSet) 
51                         : base (permSet) 
52                 {
53                         Name = name;
54                 }
55
56                 public NamedPermissionSet (string name, PermissionState state) 
57                         : base (state) 
58                 {
59                         Name = name;
60                 }
61
62                 public NamedPermissionSet (NamedPermissionSet permSet) 
63                         : base (permSet)
64                 {
65                         name = permSet.name; // name can be null here
66                         description = permSet.description;
67                 }
68
69                 public NamedPermissionSet (string name) 
70                         : this (name, PermissionState.Unrestricted)
71                 {
72                 }
73
74                 // properties
75
76                 public string Description {
77                         get { return description; }
78                         set { description = value; }
79                 }
80
81                 public string Name {
82                         get { return name; }
83                         set { 
84                                 if ((value == null) || (value == String.Empty)) {
85                                         throw new ArgumentException (Locale.GetText ("invalid name"));
86                                 }
87                                 name = value; 
88                         }
89                 }
90
91                 // methods
92
93                 public override PermissionSet Copy () 
94                 {
95                         return new NamedPermissionSet (this);
96                 }
97
98                 public NamedPermissionSet Copy (string name) 
99                 {
100                         NamedPermissionSet nps = new NamedPermissionSet (this);
101                         nps.Name = name;                // get the new name
102                         return nps;
103                 }
104
105                 public override void FromXml (SecurityElement et) 
106                 {
107                         base.FromXml (et);
108                         // strangely it can import a null Name (bypassing property setter)
109                         name = et.Attribute ("Name");
110                         description = et.Attribute ("Description");
111                         if (description == null)
112                                 description = String.Empty;
113                 }
114
115                 public override SecurityElement ToXml () 
116                 {
117                         SecurityElement se = base.ToXml ();
118                         if (name != null)
119                                 se.AddAttribute ("Name", name);
120                         if (description != null)
121                                 se.AddAttribute ("Description", description);
122                         return se;
123                 }
124
125                 [ComVisible (false)]
126                 public override bool Equals (object obj)
127                 {
128                         if (obj == null)
129                                 return false;
130                         NamedPermissionSet nps = (obj as NamedPermissionSet);
131                         if (nps == null)
132                                 return false;
133                         // description isn't part of the comparaison
134                         return ((name == nps.Name) && base.Equals (obj));
135                 }
136
137                 [ComVisible (false)]
138                 public override int GetHashCode ()
139                 {
140                         int hc = base.GetHashCode ();
141                         // name is part of the hash code (except when null)
142                         if (name != null)
143                                 hc ^= name.GetHashCode ();
144                         // description is never part of the hash code
145                         return hc;
146                 }
147         }
148 }