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