System.Drawing: added email to icon and test file headers
[mono.git] / mcs / class / corlib / System.Security.Policy / ApplicationDirectory.cs
1 //
2 // System.Security.Policy.ApplicationDirectory.cs
3 //
4 // Authors:
5 //      Jackson Harper (Jackson@LatitudeGeo.com)
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // (C) 2002 Jackson Harper, All rights reserved.
9 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System.Globalization;
32 using System.IO;
33 using System.Runtime.InteropServices;
34 using System.Text;
35
36 using Mono.Security;
37
38 namespace System.Security.Policy {
39
40         [Serializable]
41         [ComVisible (true)]
42         public sealed class ApplicationDirectory : IBuiltInEvidence {
43                 
44                 private string directory;
45
46                 //
47                 // Public Constructors
48                 //
49                 
50                 public ApplicationDirectory (string name)
51                 {
52                         if (null == name)
53                                 throw new ArgumentNullException ("name");
54                         if (name.Length < 1)
55                                 throw new FormatException (Locale.GetText ("Empty"));
56                         directory = name;
57                 }
58
59                 //
60                 // Public Properties
61                 //
62                 
63                 public string Directory {
64                         get { return directory; }
65                 }
66                 
67                 //
68                 // Public Methods
69                 //
70                 
71                 public object Copy ()
72                 {       
73                         return new ApplicationDirectory (this.Directory);
74                 }
75                 
76                 public override bool Equals (object o)
77                 {
78                         ApplicationDirectory compare = (o as ApplicationDirectory);
79                         if (compare != null) {
80                                 // MS "by design" behaviour (see FDBK14362)
81                                 ThrowOnInvalid (compare.directory);
82                                 // no C14N or other mojo here (it's done elsewhere)
83                                 return (directory == compare.directory);
84                         }
85                         return false;
86                 }
87                 
88                 public override int GetHashCode ()
89                 {
90                         return Directory.GetHashCode ();
91                 }
92                 
93                 public override string ToString ()
94                 {
95                         // MS "by design" behaviour (see FDBK14362)
96                         ThrowOnInvalid (Directory);
97                         SecurityElement element = new SecurityElement ("System.Security.Policy.ApplicationDirectory");
98                         element.AddAttribute ("version", "1");
99                         element.AddChild (new SecurityElement ("Directory", directory));
100                         return element.ToString ();
101                 }
102
103                 // interface IBuiltInEvidence
104
105                 int IBuiltInEvidence.GetRequiredSize (bool verbose) 
106                 {
107                         return ((verbose) ? 3 : 1) + directory.Length;
108                 }
109
110                 [MonoTODO ("IBuiltInEvidence")]
111                 int IBuiltInEvidence.InitFromBuffer (char [] buffer, int position) 
112                 {
113                         return 0;
114                 }
115
116                 [MonoTODO ("IBuiltInEvidence")]
117                 int IBuiltInEvidence.OutputToBuffer (char [] buffer, int position, bool verbose) 
118                 {
119                         return 0;
120                 }
121
122                 // internal stuff
123
124                 private void ThrowOnInvalid (string appdir) 
125                 {
126                         if (appdir.IndexOfAny (Path.InvalidPathChars) != -1) {
127                                 string msg = Locale.GetText ("Invalid character(s) in directory {0}");
128                                 throw new ArgumentException (String.Format (msg, appdir), "other");
129                         }
130                 }
131         }
132 }