2004-01-26 Sebastien Pouliot <spouliot@videotron.ca>
[mono.git] / mcs / class / corlib / System.Security.Policy / Url.cs
1 //
2 // System.Security.Policy.Url.cs
3 //
4 // Author
5 //      Duncan Mak (duncan@ximian.com)
6 //      Sebastien Pouliot (spouliot@motus.com)
7 //
8 // (C) 2003 Ximian, Inc (http://www.ximian.com)
9 // (C) 2004 Motus Technologies Inc. (http://www.motus.com)
10 //
11
12 using System;
13 using System.Globalization;
14 using System.Security.Permissions;
15 using System.Security.Policy;
16
17 namespace System.Security.Policy {
18
19         [Serializable]
20         public sealed class Url: IIdentityPermissionFactory, IBuiltInEvidence {
21
22                 string origin_url;
23                 
24                 public Url (string name)
25                 {
26                         origin_url = Prepare (name);
27                 }
28
29                 public object Copy ()
30                 {
31                         return new Url (origin_url);
32                 }
33
34                 public IPermission CreateIdentityPermission (Evidence evidence)
35                 {
36                         return new UrlIdentityPermission (origin_url);
37                 }
38
39                 public override bool Equals (object o)
40                 {
41                         if (o is System.Security.Policy.Url)
42                                 return (String.Compare (((Url) o).Value, Value, true, CultureInfo.InvariantCulture) == 0);
43                         return false;
44                 }
45
46                 public override int GetHashCode ()
47                 {
48                         return origin_url.GetHashCode ();
49                 }
50
51                 public override string ToString ()
52                 {
53                         SecurityElement element = new SecurityElement (typeof (System.Security.Policy.Url).FullName);
54                         element.AddAttribute ("version", "1");
55                         element.AddChild (new SecurityElement ("Url", origin_url));
56                         return element.ToString ();
57                 }
58
59                 public string Value {
60                         get { return origin_url; }
61                 }
62
63                 // interface IBuiltInEvidence
64
65                 [MonoTODO]
66                 int IBuiltInEvidence.GetRequiredSize (bool verbose) 
67                 {
68                         return 0;
69                 }
70
71                 [MonoTODO]
72                 int IBuiltInEvidence.InitFromBuffer (char [] buffer, int position) 
73                 {
74                         return 0;
75                 }
76
77                 [MonoTODO]
78                 int IBuiltInEvidence.OutputToBuffer (char [] buffer, int position, bool verbose) 
79                 {
80                         return 0;
81                 }
82
83                 // internal
84
85                 [MonoTODO ("missing site validation")]
86                 internal static string Prepare (string url) 
87                 {
88                         if (url == null)
89                                 throw new ArgumentNullException ("Url");
90                         if (url == String.Empty)
91                                 throw new FormatException (Locale.GetText ("Invalid (empty) Url"));
92
93                         // is a protocol specified
94                         int protocolPos = url.IndexOf ("://");
95                         if (protocolPos == -1)
96                                 return "file://" + url.ToUpper ();
97                         
98                         if (url.StartsWith ("file://"))
99                                 return "file://" + url.Substring (7).ToUpper ();
100
101                         // add a trailing slash if none (lonely one) is present
102                         if (url.LastIndexOf ("/") == protocolPos + 2)
103                                 return url + "/";
104                         else
105                                 return url;
106                 }
107
108                 internal static bool Compare (string mask, string url) 
109                 {
110                         int wildcard = mask.LastIndexOf ("*");
111                         if (wildcard > 0) {
112                                 // partial match with a wildcard at the end
113                                 return (String.Compare (mask, 0, url, 0, wildcard, true, CultureInfo.InvariantCulture) == 0);
114                         }
115                         else {
116                                 // exact match
117                                 return (String.Compare (mask, url, true, CultureInfo.InvariantCulture) == 0);
118                         }
119                 }
120         }
121 }