Merge pull request #5714 from alexischr/update_bockbuild
[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 :
43                 EvidenceBase,
44                 IBuiltInEvidence {
45                 
46                 private string directory;
47
48                 //
49                 // Public Constructors
50                 //
51                 
52                 public ApplicationDirectory (string name)
53                 {
54                         if (null == name)
55                                 throw new ArgumentNullException ("name");
56                         if (name.Length < 1)
57                                 throw new FormatException (Locale.GetText ("Empty"));
58                         directory = name;
59                 }
60
61                 //
62                 // Public Properties
63                 //
64                 
65                 public string Directory {
66                         get { return directory; }
67                 }
68                 
69                 //
70                 // Public Methods
71                 //
72                 
73                 public object Copy ()
74                 {       
75                         return new ApplicationDirectory (this.Directory);
76                 }
77                 
78                 public override bool Equals (object o)
79                 {
80                         ApplicationDirectory compare = (o as ApplicationDirectory);
81                         if (compare != null) {
82                                 // MS "by design" behaviour (see FDBK14362)
83                                 ThrowOnInvalid (compare.directory);
84                                 // no C14N or other mojo here (it's done elsewhere)
85                                 return (directory == compare.directory);
86                         }
87                         return false;
88                 }
89                 
90                 public override int GetHashCode ()
91                 {
92                         return Directory.GetHashCode ();
93                 }
94                 
95                 public override string ToString ()
96                 {
97                         // MS "by design" behaviour (see FDBK14362)
98                         ThrowOnInvalid (Directory);
99                         SecurityElement element = new SecurityElement ("System.Security.Policy.ApplicationDirectory");
100                         element.AddAttribute ("version", "1");
101                         element.AddChild (new SecurityElement ("Directory", directory));
102                         return element.ToString ();
103                 }
104
105                 // interface IBuiltInEvidence
106
107                 int IBuiltInEvidence.GetRequiredSize (bool verbose) 
108                 {
109                         return ((verbose) ? 3 : 1) + directory.Length;
110                 }
111
112                 [MonoTODO ("IBuiltInEvidence")]
113                 int IBuiltInEvidence.InitFromBuffer (char [] buffer, int position) 
114                 {
115                         return 0;
116                 }
117
118                 [MonoTODO ("IBuiltInEvidence")]
119                 int IBuiltInEvidence.OutputToBuffer (char [] buffer, int position, bool verbose) 
120                 {
121                         return 0;
122                 }
123
124                 // internal stuff
125
126                 private void ThrowOnInvalid (string appdir) 
127                 {
128                         if (appdir.IndexOfAny (Path.InvalidPathChars) != -1) {
129                                 string msg = Locale.GetText ("Invalid character(s) in directory {0}");
130                                 throw new ArgumentException (String.Format (msg, appdir), "other");
131                         }
132                 }
133         }
134 }