2002-10-03 Dick Porter <dick@ximian.com>
[mono.git] / mcs / class / System / System.Net / DnsPermission.cs
1 //\r
2 // System.Net.DnsPermission.cs\r
3 //\r
4 // Author:\r
5 //   Lawrence Pit (loz@cable.a2000.nl)\r
6 //\r
7 \r
8 using System;\r
9 using System.Collections;\r
10 using System.Security;\r
11 using System.Security.Permissions;\r
12 \r
13 namespace System.Net\r
14 {\r
15         [Serializable]\r
16         public sealed class DnsPermission : CodeAccessPermission, IUnrestrictedPermission\r
17         {\r
18                 // Fields\r
19                 bool m_noRestriction;\r
20                 \r
21                 // Constructors\r
22                 public DnsPermission (PermissionState state) : base () \r
23                 {                                               \r
24                         m_noRestriction = (state == PermissionState.Unrestricted);\r
25                 }\r
26                 \r
27                 // Methods\r
28                                 \r
29                 public override IPermission Copy ()\r
30                 {\r
31                         // this is immutable.\r
32                         return this;            \r
33                 }\r
34                 \r
35                 public override IPermission Intersect (IPermission target)\r
36                 {\r
37                         // LAMESPEC: says to throw an exception when null\r
38                         // but at same time it says to return null. We'll\r
39                         // follow MS behaviour.\r
40                         if (target == null) \r
41                                 return null;\r
42                         \r
43                         DnsPermission perm = target as DnsPermission;\r
44                         \r
45                         if (perm == null)\r
46                                 throw new ArgumentException ("Argument not of type DnsPermission");\r
47                                 \r
48                         if (this.m_noRestriction && perm.m_noRestriction)\r
49                                 return this;\r
50                         \r
51                         return this.m_noRestriction ? perm : this;\r
52                 }\r
53                 \r
54                 public override bool IsSubsetOf (IPermission target) \r
55                 {\r
56                         if (target == null)\r
57                                 return !m_noRestriction;\r
58                         \r
59                         DnsPermission perm = target as DnsPermission;\r
60                         \r
61                         if (perm == null)\r
62                                 throw new ArgumentException ("Argument not of type DnsPermission");\r
63                         \r
64                         return !this.m_noRestriction || perm.m_noRestriction;\r
65                 }\r
66 \r
67                 public bool IsUnrestricted () \r
68                 {\r
69                         return this.m_noRestriction;\r
70                 }\r
71 \r
72                 /*\r
73                 \r
74                 DnsPermission dns1 = new DnsPermission (PermissionState.None);\r
75                 Console.WriteLine (dns1.ToXml ().ToString ());\r
76 \r
77                 DnsPermission dns2 = new DnsPermission (PermissionState.Unrestricted);\r
78                 Console.WriteLine (dns2.ToXml ().ToString ());\r
79                 \r
80                 This is the sample xml output:\r
81 \r
82                 <IPermission class="System.Net.DnsPermission, System, Version=1.0.3300.0, Cultur\r
83                 e=neutral, PublicKeyToken=b77a5c561934e089"\r
84                              version="1"/>\r
85 \r
86                 <IPermission class="System.Net.DnsPermission, System, Version=1.0.3300.0, Cultur\r
87                 e=neutral, PublicKeyToken=b77a5c561934e089"\r
88                              version="1"\r
89                              Unrestricted="true"/>\r
90                 */\r
91                 public override SecurityElement ToXml ()\r
92                 {\r
93              \r
94                         SecurityElement root = new SecurityElement ("IPermission");\r
95                         root.AddAttribute ("class", this.GetType ().AssemblyQualifiedName);\r
96                         root.AddAttribute ("version", "1");\r
97                         if (m_noRestriction)\r
98                                 root.AddAttribute ("Unrestricted", "true");                             \r
99 \r
100                         return root;\r
101                 }\r
102                 \r
103                 public override void FromXml (SecurityElement securityElement)\r
104                 {\r
105                         if (securityElement == null)\r
106                                 throw new ArgumentNullException ("securityElement");\r
107                                 \r
108                         // LAMESPEC: it says to throw an ArgumentNullException in this case                             \r
109                         if (securityElement.Tag != "IPermission")\r
110                                 throw new ArgumentException ("securityElement");\r
111                                 \r
112                         string classStr = securityElement.Attribute ("class");\r
113                         if (classStr == null || !classStr.StartsWith (this.GetType ().FullName + ","))\r
114                                 throw new ArgumentException ("securityElement");\r
115                                 \r
116                         string unrestricted = securityElement.Attribute ("Unrestricted");\r
117                         if (unrestricted != null) \r
118                                 this.m_noRestriction = (String.Compare (unrestricted, "true", true) == 0);\r
119                 }               \r
120                 \r
121                 public override IPermission Union (IPermission target) \r
122                 {\r
123                         // LAMESPEC: according to spec we should throw an \r
124                         // exception when target is null. We'll follow the\r
125                         // behaviour of MS.Net instead of the spec.\r
126                         if (target == null)\r
127                                 return this;\r
128                                 // throw new ArgumentNullException ("target");\r
129                                 \r
130                         DnsPermission perm = target as DnsPermission;\r
131                         \r
132                         if (perm == null)\r
133                                 throw new ArgumentException ("Argument not of type DnsPermission");\r
134                         \r
135                         return this.m_noRestriction ? this : perm;\r
136                 }\r
137 \r
138         }\r
139 }\r