2006-03-29 Marek Sieradzki <marek.sieradzki@gmail.com>
[mono.git] / mcs / class / Microsoft.Build.Utilities / Microsoft.Build.Utilities / TaskItem.cs
1 //
2 // TaskItem.cs: Represents an item belonging to a task.
3 //
4 // Author:
5 //   Marek Sieradzki (marek.sieradzki@gmail.com)
6 //
7 // (C) 2005 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 #if NET_2_0
29
30 using System;
31 using System.Collections;
32 using System.Collections.Specialized;
33 using System.IO;
34 using Microsoft.Build.Framework;
35
36 namespace Microsoft.Build.Utilities
37 {
38         public sealed class TaskItem : MarshalByRefObject, ITaskItem
39         {
40                 IDictionary             metadata;
41                 string                  itemSpec;
42                 static string[]         reservedMetadataNames;
43                 static Hashtable        reservedMetadataHash;
44
45                 static TaskItem ()
46                 {
47                         reservedMetadataNames = new string [] {
48                                 "FullPath", "RootDir", "Filename", "Extension", "RelativeDir", "Directory",
49                                 "RecursiveDir", "Identity", "ModifiedTime", "CreatedTime", "AccessedTime"};
50                         reservedMetadataHash = CollectionsUtil.CreateCaseInsensitiveHashtable (ReservedMetadataNameCount);
51                         foreach (string s in reservedMetadataNames) {
52                                 reservedMetadataHash.Add (s, null);
53                         }
54                 }
55                 
56                 public TaskItem ()
57                 {
58                         this.itemSpec = String.Empty;
59                         this.metadata = CollectionsUtil.CreateCaseInsensitiveHashtable ();
60                 }
61
62                 public TaskItem (ITaskItem sourceItem)
63                 {
64                         if (sourceItem == null)
65                                 throw new ArgumentNullException ("sourceItem");
66                         
67                         this.itemSpec = sourceItem.ItemSpec;
68                         this.metadata = sourceItem.CloneCustomMetadata ();
69                 }
70
71                 public TaskItem (string itemSpec)
72                 {
73                         if (itemSpec == null)
74                                 throw new ArgumentNullException ("itemSpec");
75                         
76                         this.ItemSpec = itemSpec;
77                         this.metadata = CollectionsUtil.CreateCaseInsensitiveHashtable ();
78                 }
79
80                 public TaskItem (string itemSpec, IDictionary itemMetadata)
81                 {
82                         if (itemSpec == null)
83                                 throw new ArgumentNullException ("itemSpec");
84                         
85                         if (itemMetadata == null)
86                                 throw new ArgumentNullException ("itemMetadata");
87                         
88                         this.itemSpec = itemSpec;
89                         this.metadata = CollectionsUtil.CreateCaseInsensitiveHashtable (itemMetadata);
90                 }
91
92                 public static ICollection ReservedMetadataNames {
93                         get {
94                                 return (ICollection) reservedMetadataNames.Clone ();
95                         }
96                 }
97
98                 public static int ReservedMetadataNameCount {
99                         get {
100                                 return reservedMetadataNames.Length;
101                         }
102                 }
103
104                 public static bool IsReservedMetadataName (string metadataName)
105                 {
106                         return reservedMetadataHash.Contains (metadataName.ToLower ());
107                 }
108
109                 public static string GetReservedMetadata (string itemSpec, string metadataName)
110                 {
111                         switch (metadataName.ToLower ()) {
112                         case "fullpath":
113                                 return Path.GetFullPath (itemSpec);
114                         case "rootdir":
115                                 return Path.GetPathRoot (itemSpec);
116                         case "filename":
117                                 return Path.GetFileNameWithoutExtension (itemSpec);
118                         case "extension":
119                                 return Path.GetExtension (itemSpec);
120                         case "relativedir":
121                                 return Path.GetDirectoryName (itemSpec);
122                         case "directory":
123                                 return Path.GetDirectoryName (Path.GetFullPath (itemSpec));
124                         case "recursivedir":
125                                 // FIXME: how to handle this?
126                                 return "";
127                         case "identity":
128                                 return Path.Combine (Path.GetDirectoryName (itemSpec), Path.GetFileName (itemSpec));
129                         case "modifiedtime":
130                                 if (File.Exists (itemSpec))
131                                         return File.GetLastWriteTime (itemSpec).ToString ();
132                                 else if (Directory.Exists (itemSpec))
133                                         return Directory.GetLastWriteTime (itemSpec).ToString ();
134                                 else
135                                         return String.Empty;
136                         case "createdtime":
137                                 if (File.Exists (itemSpec))
138                                         return File.GetCreationTime (itemSpec).ToString ();
139                                 else if (Directory.Exists (itemSpec))
140                                         return Directory.GetCreationTime (itemSpec).ToString ();
141                                 else
142                                         return String.Empty;
143                         case "accessedtime":
144                                 if (File.Exists (itemSpec))
145                                         return File.GetLastAccessTime (itemSpec).ToString ();
146                                 else if (Directory.Exists (itemSpec))
147                                         return Directory.GetLastAccessTime (itemSpec).ToString ();
148                                 else
149                                         return String.Empty;
150                         default:
151                                 throw new ArgumentException ("Invalid reserved metadata name");
152                         }
153                 }
154
155                 public IDictionary CloneCustomMetadata ()
156                 {
157                         IDictionary clonedMetadata = CollectionsUtil.CreateCaseInsensitiveHashtable ();
158                         foreach (DictionaryEntry de in metadata)
159                                 clonedMetadata.Add (de.Key, de.Value);
160                         return clonedMetadata;
161                 }
162
163                 public void CopyMetadataTo (ITaskItem destinationItem)
164                 {
165                         foreach (DictionaryEntry e in metadata) {
166                                 if (destinationItem.GetMetadata ((string)e.Key) == String.Empty) {
167                                         destinationItem.SetMetadata ((string)e.Key, (string)e.Value);
168                                 }
169                         }
170                 }
171
172                 public static explicit operator string (TaskItem taskItemToCast)
173                 {
174                         return taskItemToCast.ToString ();
175                 }
176
177                 public string GetMetadata (string metadataName)
178                 {
179                         if (IsReservedMetadataName (metadataName))
180                                 return GetReservedMetadata (ItemSpec, metadataName);
181                         else if (metadata.Contains (metadataName))
182                                 return (string) metadata [metadataName];
183                         else
184                                 return String.Empty;
185                 }
186
187                 public override object InitializeLifetimeService ()
188                 {
189                         return null;
190                 }
191
192                 public void RemoveMetadata (string metadataName)
193                 {
194                         if (metadataName == null)
195                                 throw new ArgumentNullException ("metadataName");
196                         if (IsReservedMetadataName (metadataName))
197                                 throw new ArgumentException ("Can't remove reserved metadata");
198                         if (metadata.Contains (metadataName))
199                                 metadata.Remove (metadataName);
200                 }
201
202                 public void SetMetadata (string metadataName, string metadataValue)
203                 {
204                         if (metadataName == null)
205                                 throw new ArgumentNullException ("metadataName");
206                         if (metadataValue == null)
207                                 throw new ArgumentNullException ("metadataValue");
208                         if (IsReservedMetadataName (metadataName))
209                                 throw new ArgumentException ("Can't modify reserved metadata");
210                                 
211                         if (metadata.Contains (metadataName))
212                                 metadata.Remove (metadataName);
213                         metadata.Add (metadataName, metadataValue);
214                 }
215
216                 public override string ToString ()
217                 {
218                         return itemSpec;
219                 }
220                 
221                 public string ItemSpec {
222                         get { return itemSpec; }
223                         set { itemSpec = value; }
224                 }
225
226                 public int MetadataCount {
227                         get { return metadata.Count + 11; }
228                 }
229
230                 public ICollection MetadataNames {
231                         get {
232                                 ArrayList list = new ArrayList ();
233                                 
234                                 foreach (string s in ReservedMetadataNames)
235                                         list.Add (s);
236                                 foreach (string s in metadata.Keys)
237                                         list.Add (s);
238
239                                 return list;
240                         }
241                 }
242         }
243 }
244
245 #endif