Add unit test for AggregateException.GetBaseException that works on .net but is broke...
[mono.git] / mcs / class / Microsoft.Build.Utilities / Mono.XBuild.Utilities / ReservedNameUtils.cs
1 //
2 // ReservedNameUtils.cs
3 //
4 // Author:
5 //   Marek Sieradzki (marek.sieradzki@gmail.com)
6 //
7 // (C) 2006 Marek Sieradzki
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
28 using System;
29 using System.Collections;
30 using System.Collections.Specialized;
31 using System.IO;
32 using System.Text;
33
34 namespace Mono.XBuild.Utilities {
35
36         internal static class ReservedNameUtils {
37         
38                 static string[] reservedMetadataNames;
39                 static Hashtable reservedMetadataHash;
40         
41                 static ReservedNameUtils ()
42                 {
43                         reservedMetadataNames = new string [] {
44                                 "FullPath", "RootDir", "Filename", "Extension", "RelativeDir", "Directory",
45                                 "RecursiveDir", "Identity", "ModifiedTime", "CreatedTime", "AccessedTime"};
46                         reservedMetadataHash = CollectionsUtil.CreateCaseInsensitiveHashtable (ReservedMetadataNameCount);
47                         foreach (string s in reservedMetadataNames) {
48                                 reservedMetadataHash.Add (s, null);
49                         }
50                 }
51                 
52                 public static ICollection ReservedMetadataNames {
53                         get {
54                                 return (ICollection) reservedMetadataNames.Clone ();
55                         }
56                 }
57
58                 public static int ReservedMetadataNameCount {
59                         get {
60                                 return reservedMetadataNames.Length;
61                         }
62                 }
63
64                 public static bool IsReservedMetadataName (string metadataName)
65                 {
66                         return reservedMetadataHash.Contains (metadataName);
67                 }
68
69                 public static string GetReservedMetadata (string itemSpec,
70                                                    string metadataName, IDictionary metadata)
71                 {
72                         if (metadataName == null)
73                                 throw new ArgumentNullException ();
74
75                         if (String.IsNullOrEmpty (itemSpec))
76                                 return String.Empty;
77                 
78                         switch (metadataName.ToLowerInvariant ()) {
79                         case "fullpath":
80                                 return Path.GetFullPath (itemSpec);
81                         case "rootdir":
82                                 if (Path.IsPathRooted (itemSpec))
83                                         return Path.GetPathRoot (itemSpec);
84                                 else
85                                         return Path.GetPathRoot (Environment.CurrentDirectory);
86                         case "filename":
87                                 return Path.GetFileNameWithoutExtension (itemSpec);
88                         case "extension":
89                                 return Path.GetExtension (itemSpec);
90                         case "relativedir":
91                                 return WithTrailingSlash (AbsoluteToRelativePath (Environment.CurrentDirectory, Path.GetDirectoryName (itemSpec)));
92                         case "directory":
93                                 string fullpath = Path.GetFullPath (itemSpec);
94                                 return WithTrailingSlash (
95                                          Path.GetDirectoryName (fullpath).Substring (Path.GetPathRoot (fullpath).Length));
96                         case "recursivedir":
97                                 if (metadata != null && metadata.Contains ("RecursiveDir"))
98                                         return (string)metadata ["RecursiveDir"];
99                                 else
100                                         return String.Empty;
101                         case "identity":
102                                 return Path.Combine (Path.GetDirectoryName (itemSpec), Path.GetFileName (itemSpec));
103                         case "modifiedtime":
104                                 if (File.Exists (itemSpec))
105                                         return File.GetLastWriteTime (itemSpec).ToString ();
106                                 else if (Directory.Exists (itemSpec))
107                                         return Directory.GetLastWriteTime (itemSpec).ToString ();
108                                 else
109                                         return String.Empty;
110                         case "createdtime":
111                                 if (File.Exists (itemSpec))
112                                         return File.GetCreationTime (itemSpec).ToString ();
113                                 else if (Directory.Exists (itemSpec))
114                                         return Directory.GetCreationTime (itemSpec).ToString ();
115                                 else
116                                         return String.Empty;
117                         case "accessedtime":
118                                 if (File.Exists (itemSpec))
119                                         return File.GetLastAccessTime (itemSpec).ToString ();
120                                 else if (Directory.Exists (itemSpec))
121                                         return Directory.GetLastAccessTime (itemSpec).ToString ();
122                                 else
123                                         return String.Empty;
124                         default:
125                                 throw new ArgumentException ("Invalid reserved metadata name");
126                         }
127                 }
128
129                 static string WithTrailingSlash (string path)
130                 {
131                         if (String.IsNullOrEmpty (path))
132                                 return String.Empty;
133
134                         if (path.Length > 0)
135                                 return path + Path.DirectorySeparatorChar;
136                         else
137                                 return path;
138                 }
139
140                 readonly static char[] separators = { Path.DirectorySeparatorChar, Path.VolumeSeparatorChar, Path.AltDirectorySeparatorChar };
141                 static string AbsoluteToRelativePath (string baseDirectoryPath, string absPath)
142                 {
143                         if (!Path.IsPathRooted (absPath))
144                                 return absPath;
145
146                         absPath           = Path.GetFullPath (absPath);
147                         baseDirectoryPath = Path.GetFullPath (baseDirectoryPath.TrimEnd (Path.DirectorySeparatorChar));
148
149                         string[] bPath = baseDirectoryPath.Split (separators);
150                         string[] aPath = absPath.Split (separators);
151                         int indx = 0;
152
153                         for (; indx < System.Math.Min (bPath.Length, aPath.Length); indx++) {
154                                 if (!bPath[indx].Equals(aPath[indx]))
155                                         break;
156                         }
157
158                         if (indx == 0) 
159                                 return absPath;
160
161                         StringBuilder result = new StringBuilder ();
162
163                         for (int i = indx; i < bPath.Length; i++) {
164                                 result.Append ("..");
165                                 if (i + 1 < bPath.Length || aPath.Length - indx > 0)
166                                         result.Append (Path.DirectorySeparatorChar);
167                         }
168
169
170                         result.Append (String.Join(Path.DirectorySeparatorChar.ToString(), aPath, indx, aPath.Length - indx));
171                         if (result.Length == 0)
172                                 return ".";
173                         return result.ToString ();
174                 }
175
176                 static string RelativeToAbsolutePath (string baseDirectoryPath, string relPath)
177                 {
178                         return Path.GetFullPath (Path.Combine (baseDirectoryPath, relPath));
179                 }
180         }
181 }