2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / corlib / System.Security.Policy / UrlMembershipCondition.cs
1 //
2 // System.Security.Policy.UrlMembershipCondition.cs
3 //
4 // Authors:
5 //      Duncan Mak (duncan@ximian.com)
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // (C) 2003, Ximian Inc.
9 // (C) 2004 Motus Technologies Inc. (http://www.motus.com)
10 // Copyright (C) 2004 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.Collections;
33 using System.Globalization;
34
35 namespace System.Security.Policy {
36
37         [Serializable]
38         public sealed class UrlMembershipCondition : IMembershipCondition, IConstantMembershipCondition {
39
40                 private readonly int version = 1;
41
42                 private Url url;
43                 
44                 public UrlMembershipCondition (string url)
45                 {
46                         this.url = new Url (url);
47                 }
48
49                 internal UrlMembershipCondition (Url url)
50                 {
51                         // as the Url object has already been validated there's no
52                         // need to restart the whole process by converting to string
53                         this.url = (Url) url.Copy ();
54                 }
55
56                 // properties
57
58                 public string Url {
59                         get { return url.Value; }
60                         set { url = new Url (value); }
61                 }
62
63                 // methods
64
65                 public bool Check (Evidence evidence)
66                 {
67                         if (evidence == null)
68                                 return false;
69
70                         string u = url.Value;
71                         int wildcard = u.LastIndexOf ("*");     // partial match with a wildcard at the end
72                         if (wildcard == -1)
73                                 wildcard = u.Length;            // exact match
74
75                         IEnumerator e = evidence.GetHostEnumerator ();
76                         while (e.MoveNext ()) {
77                                 if (e.Current is Url) {
78                                         // note: there shouldn't be more than one Url evidence
79                                         if (String.Compare (u, 0, (e.Current as Url).Value, 0, wildcard,
80                                                 true, CultureInfo.InvariantCulture) == 0) {
81                                                 return true;
82                                         }
83                                         // but we must check for all of them!
84                                 }
85                         }
86                         return false;
87                 }
88
89                 public IMembershipCondition Copy ()
90                 {
91                         return new UrlMembershipCondition (url);
92                 }
93
94                 public override bool Equals (object o)
95                 {
96                         if (o is UrlMembershipCondition) {
97                                 string u = url.Value;
98                                 int wildcard = u.LastIndexOf ("*");     // partial match with a wildcard at the end
99                                 if (wildcard == -1)
100                                         wildcard = u.Length;            // exact match
101
102                                 return (String.Compare (u, 0, (o as UrlMembershipCondition).Url,
103                                         0, wildcard, true, CultureInfo.InvariantCulture) == 0);
104                         }
105                         return false;
106                 }
107
108                 public void FromXml (SecurityElement element)
109                 {
110                         FromXml (element, null);
111                 }
112
113                 public void FromXml (SecurityElement element, PolicyLevel level)
114                 {
115                         MembershipConditionHelper.CheckSecurityElement (element, "element", version, version);
116                         
117                         string u = element.Attribute ("Url");
118                         url = (u == null) ? null : new Url (u);
119                 }
120
121                 public override int GetHashCode ()
122                 {
123                         return url.GetHashCode ();
124                 }
125
126                 public override string ToString ()
127                 {
128                         return "Url - " + url.Value;
129                 }
130
131                 public SecurityElement ToXml ()
132                 {
133                         return ToXml (null);
134                 }
135
136                 public SecurityElement ToXml (PolicyLevel level)
137                 {
138                         // PolicyLevel isn't used as there's no need to resolve NamedPermissionSet references
139                         SecurityElement se = MembershipConditionHelper.Element (typeof (UrlMembershipCondition), version);
140                         se.AddAttribute ("Url", url.Value);
141                         return se;
142                 }
143         }
144 }