2 // TaskItem.cs: Represents an item belonging to a task.
5 // Marek Sieradzki (marek.sieradzki@gmail.com)
7 // (C) 2005 Marek Sieradzki
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
31 using System.Collections;
32 using System.Collections.Specialized;
34 using Microsoft.Build.Framework;
36 namespace Microsoft.Build.Utilities
38 public sealed class TaskItem : MarshalByRefObject, ITaskItem
42 static string[] reservedMetadataNames;
43 static Hashtable reservedMetadataHash;
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);
58 this.itemSpec = String.Empty;
59 this.metadata = CollectionsUtil.CreateCaseInsensitiveHashtable ();
62 public TaskItem (ITaskItem sourceItem)
64 if (sourceItem == null)
65 throw new ArgumentNullException ("sourceItem");
67 this.itemSpec = sourceItem.ItemSpec;
68 this.metadata = sourceItem.CloneCustomMetadata ();
71 public TaskItem (string itemSpec)
74 throw new ArgumentNullException ("itemSpec");
76 this.ItemSpec = itemSpec;
77 this.metadata = CollectionsUtil.CreateCaseInsensitiveHashtable ();
80 public TaskItem (string itemSpec, IDictionary itemMetadata)
83 throw new ArgumentNullException ("itemSpec");
85 if (itemMetadata == null)
86 throw new ArgumentNullException ("itemMetadata");
88 this.itemSpec = itemSpec;
89 this.metadata = CollectionsUtil.CreateCaseInsensitiveHashtable (itemMetadata);
92 public static ICollection ReservedMetadataNames {
94 return (ICollection) reservedMetadataNames.Clone ();
98 public static int ReservedMetadataNameCount {
100 return reservedMetadataNames.Length;
104 public static bool IsReservedMetadataName (string metadataName)
106 return reservedMetadataHash.Contains (metadataName.ToLower ());
109 public static string GetReservedMetadata (string itemSpec, string metadataName)
111 switch (metadataName.ToLower ()) {
113 return Path.GetFullPath (itemSpec);
115 return Path.GetPathRoot (itemSpec);
117 return Path.GetFileNameWithoutExtension (itemSpec);
119 return Path.GetExtension (itemSpec);
121 return Path.GetDirectoryName (itemSpec);
123 return Path.GetDirectoryName (Path.GetFullPath (itemSpec));
125 // FIXME: how to handle this?
128 return Path.Combine (Path.GetDirectoryName (itemSpec), Path.GetFileName (itemSpec));
130 if (File.Exists (itemSpec))
131 return File.GetLastWriteTime (itemSpec).ToString ();
132 else if (Directory.Exists (itemSpec))
133 return Directory.GetLastWriteTime (itemSpec).ToString ();
137 if (File.Exists (itemSpec))
138 return File.GetCreationTime (itemSpec).ToString ();
139 else if (Directory.Exists (itemSpec))
140 return Directory.GetCreationTime (itemSpec).ToString ();
144 if (File.Exists (itemSpec))
145 return File.GetLastAccessTime (itemSpec).ToString ();
146 else if (Directory.Exists (itemSpec))
147 return Directory.GetLastAccessTime (itemSpec).ToString ();
151 throw new ArgumentException ("Invalid reserved metadata name");
155 public IDictionary CloneCustomMetadata ()
157 IDictionary clonedMetadata = CollectionsUtil.CreateCaseInsensitiveHashtable ();
158 foreach (DictionaryEntry de in metadata)
159 clonedMetadata.Add (de.Key, de.Value);
160 return clonedMetadata;
163 public void CopyMetadataTo (ITaskItem destinationItem)
165 foreach (DictionaryEntry e in metadata) {
166 if (destinationItem.GetMetadata ((string)e.Key) == String.Empty) {
167 destinationItem.SetMetadata ((string)e.Key, (string)e.Value);
172 public static explicit operator string (TaskItem taskItemToCast)
174 return taskItemToCast.ToString ();
177 public string GetMetadata (string metadataName)
179 if (IsReservedMetadataName (metadataName))
180 return GetReservedMetadata (ItemSpec, metadataName);
181 else if (metadata.Contains (metadataName))
182 return (string) metadata [metadataName];
187 public override object InitializeLifetimeService ()
192 public void RemoveMetadata (string metadataName)
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);
202 public void SetMetadata (string metadataName, string metadataValue)
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");
211 if (metadata.Contains (metadataName))
212 metadata.Remove (metadataName);
213 metadata.Add (metadataName, metadataValue);
216 public override string ToString ()
221 public string ItemSpec {
222 get { return itemSpec; }
223 set { itemSpec = value; }
226 public int MetadataCount {
227 get { return metadata.Count + 11; }
230 public ICollection MetadataNames {
232 ArrayList list = new ArrayList ();
234 foreach (string s in ReservedMetadataNames)
236 foreach (string s in metadata.Keys)