fix freeze
[mono.git] / mcs / class / corlib / System.Security.Policy / SiteMembershipCondition.cs
1 //
2 // SiteMembershipCondition.cs: Site MembershipCondition
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // (C) 2004 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System.Collections;
31 using System.Globalization;
32 using System.Runtime.InteropServices;
33
34 namespace System.Security.Policy {
35
36         [Serializable]
37 #if NET_2_0
38         [ComVisible (true)]
39 #endif
40         public sealed class SiteMembershipCondition : IMembershipCondition, IConstantMembershipCondition {
41
42                 private readonly int version = 1;
43
44                 private string _site;
45
46                 // constructors
47
48                 // so System.Activator.CreateInstance can create an instance...
49                 internal SiteMembershipCondition ()
50                 {
51                 }
52
53                 public SiteMembershipCondition (string site)
54                 {
55                         Site = site;
56                 }
57
58                 // properties
59
60                 public string Site { 
61                         get { return _site; }
62                         set {
63                                 if (value == null)
64                                         throw new ArgumentNullException ("site");
65                                 if (!System.Security.Policy.Site.IsValid (value))
66                                         throw new ArgumentException ("invalid site");
67                                 _site = value;
68                         }
69                 }
70
71                 // methods
72
73                 public bool Check (Evidence evidence) 
74                 {
75                         if (evidence == null)
76                                 return false;
77
78                         IEnumerator e = evidence.GetHostEnumerator ();
79                         while (e.MoveNext ()) {
80                                 if (e.Current is Site) {
81                                         string[] s1 = _site.Split ('.');
82                                         string[] s2 = (e.Current as Site).origin_site.Split ('.');
83                                         for (int i = s1.Length - 1, j = s2.Length - 1; i>=0; i--, j--) {
84                                                 if (i == 0) {
85                                                         // special * case
86                                                         return (String.Compare (s1 [0], "*", true, CultureInfo.InvariantCulture) == 0);
87                                                 }
88                                                 if (String.Compare (s1 [i], s2 [j], true, CultureInfo.InvariantCulture) != 0)
89                                                         return false;
90                                         }
91                                         return true;
92                                 }
93                         }
94                         return false;
95                 }
96                 
97                 public IMembershipCondition Copy () 
98                 {
99                         return new SiteMembershipCondition (_site);
100                 }
101
102                 public override bool Equals (object o) 
103                 {
104                         if (o == null)
105                                 return false;
106                         if (o is SiteMembershipCondition) {
107                                 Site s = new Site ((o as SiteMembershipCondition)._site);
108                                 return s.Equals (new Site (_site));
109                         }
110                         return false;
111                 }
112
113                 public void FromXml (SecurityElement e) 
114                 {
115                         FromXml (e, null);
116                 }
117
118                 public void FromXml (SecurityElement e, PolicyLevel level) 
119                 {
120                         MembershipConditionHelper.CheckSecurityElement (e, "e", version, version);
121                         _site = e.Attribute ("Site");
122                         // PolicyLevel isn't used as there's no need to resolve NamedPermissionSet references
123                 }
124
125                 public override int GetHashCode () 
126                 {
127                         return _site.GetHashCode ();
128                 }
129
130                 public override string ToString () 
131                 {
132                         return "Site - " + _site;
133                 }
134
135                 public SecurityElement ToXml () 
136                 {
137                         return ToXml (null);
138                 }
139
140                 public SecurityElement ToXml (PolicyLevel level) 
141                 {
142                         // PolicyLevel isn't used as there's no need to resolve NamedPermissionSet references
143                         SecurityElement se = MembershipConditionHelper.Element (typeof (SiteMembershipCondition), version);
144                         se.AddAttribute ("Site", _site);
145                         return se;
146                 }
147         }
148 }