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