System.Drawing: added email to icon and test file headers
[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  <sebastien@ximian.com>
7 //
8 // (C) 2003 Ximian, Inc (http://www.ximian.com)
9 // (C) 2004 Motus Technologies Inc. (http://www.motus.com)
10 // Copyright (C) 2004-2005 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.Globalization;
33 using System.Runtime.InteropServices;
34 using System.Security.Permissions;
35
36 using Mono.Security;
37
38 namespace System.Security.Policy {
39
40         [Serializable]
41         [ComVisible (true)]
42         public sealed class Url: IIdentityPermissionFactory, IBuiltInEvidence {
43
44                 private string origin_url;
45                 
46                 public Url (string name)
47                         : this (name, false)
48                 {
49                 }
50
51                 internal Url (string name, bool validated) 
52                 {
53                         origin_url = validated ? name : Prepare (name);
54                 }
55
56                 // methods
57
58                 public object Copy ()
59                 {
60                         // dont re-validate the Url
61                         return new Url (origin_url, true);
62                 }
63
64                 public IPermission CreateIdentityPermission (Evidence evidence)
65                 {
66                         return new UrlIdentityPermission (origin_url);
67                 }
68
69                 public override bool Equals (object o)
70                 {
71                         Url u = (o as System.Security.Policy.Url);
72                         if (u == null)
73                                 return false;
74
75                         string url1 = u.Value;
76                         string url2 = origin_url;
77                         if (url1.IndexOf (Uri.SchemeDelimiter) < 0)
78                                 url1 = "file://" + url1;
79                         if (url2.IndexOf (Uri.SchemeDelimiter) < 0)
80                                 url2 = "file://" + url2;
81                         return (String.Compare (url1, url2, true, CultureInfo.InvariantCulture) == 0);
82                 }
83
84                 public override int GetHashCode ()
85                 {
86                         string s = origin_url;
87                         if (s.IndexOf (Uri.SchemeDelimiter) < 0)
88                                 s = "file://" + s;
89                         return s.GetHashCode ();
90                 }
91
92                 public override string ToString ()
93                 {
94                         SecurityElement element = new SecurityElement ("System.Security.Policy.Url");
95                         element.AddAttribute ("version", "1");
96                         element.AddChild (new SecurityElement ("Url", origin_url));
97                         return element.ToString ();
98                 }
99
100                 public string Value {
101                         get { return origin_url; }
102                 }
103
104                 // interface IBuiltInEvidence
105
106                 int IBuiltInEvidence.GetRequiredSize (bool verbose) 
107                 {
108                         return (verbose ? 3 : 1) + origin_url.Length;
109                 }
110
111                 [MonoTODO ("IBuiltInEvidence")]
112                 int IBuiltInEvidence.InitFromBuffer (char [] buffer, int position) 
113                 {
114                         return 0;
115                 }
116
117                 [MonoTODO ("IBuiltInEvidence")]
118                 int IBuiltInEvidence.OutputToBuffer (char [] buffer, int position, bool verbose) 
119                 {
120                         return 0;
121                 }
122
123                 // internal
124                 private string Prepare (string url) 
125                 {
126                         if (url == null)
127                                 throw new ArgumentNullException ("Url");
128                         if (url == String.Empty)
129                                 throw new FormatException (Locale.GetText ("Invalid (empty) Url"));
130
131                         int protocolPos = url.IndexOf (Uri.SchemeDelimiter);    // '://'
132                         if (protocolPos > 0) {
133                                 if (url.StartsWith ("file://")) {
134                                         // convert file url into uppercase
135                                         url = "file://" + url.Substring (7);
136                                 }
137                                 // don't escape and don't reduce (e.g. '.' and '..')
138                                 Uri uri = new Uri (url, false, false);
139                                 url = uri.ToString ();
140                         }
141
142                         int lastpos = url.Length - 1;
143                         if (url [lastpos] == '/')
144                                 url = url.Substring (0, lastpos);
145
146                         return url;
147                 }
148        }
149 }