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
46 this.itemSpec = String.Empty;
47 this.recursiveDir = String.Empty;
48 this.metadata = CollectionsUtil.CreateCaseInsensitiveHashtable ();
51 public TaskItem (ITaskItem sourceItem)
53 this.itemSpec = sourceItem.ItemSpec;
54 this.recursiveDir = String.Empty;
55 this.metadata = sourceItem.CloneCustomMetadata ();
58 public TaskItem (string itemSpec)
60 this.ItemSpec = itemSpec;
61 this.recursiveDir = String.Empty;
62 this.metadata = CollectionsUtil.CreateCaseInsensitiveHashtable ();
65 public TaskItem (string itemSpec, IDictionary itemMetadata)
67 this.itemSpec = itemSpec;
68 this.recursiveDir = String.Empty;
69 this.metadata = itemMetadata;
72 public IDictionary CloneCustomMetadata ()
74 IDictionary clonedMetadata = CollectionsUtil.CreateCaseInsensitiveHashtable ();
75 foreach (DictionaryEntry de in metadata)
76 clonedMetadata.Add (de.Key, de.Value);
77 return clonedMetadata;
80 public void CopyMetadataTo (ITaskItem destinationItem)
82 foreach (DictionaryEntry e in metadata) {
83 destinationItem.SetMetadata ((string)e.Key, (string)e.Value);
87 public static explicit operator string (TaskItem taskItemToCast)
89 return taskItemToCast.ToString ();
92 public string GetMetadata (string metadataName)
94 switch (metadataName.ToLower ()) {
96 return Path.GetFullPath (itemSpec);
100 return Path.GetFileNameWithoutExtension (itemSpec);
102 return Path.GetExtension (itemSpec);
104 return Path.GetDirectoryName (itemSpec);
106 return Path.GetDirectoryName (Path.GetFullPath (itemSpec));
110 return Path.Combine (Path.GetDirectoryName (itemSpec), Path.GetFileName (itemSpec));
112 return File.GetLastWriteTime (itemSpec).ToString ();
114 return File.GetCreationTime (itemSpec).ToString ();
116 return File.GetLastAccessTime (itemSpec).ToString ();
118 return (string) metadata [metadataName];
122 public override object InitializeLifetimeService ()
127 public void RemoveMetadata (string metadataName)
129 if (metadata.Contains (metadataName))
130 metadata.Remove (metadataName);
133 public void SetMetadata (string metadataName, string metadataValue)
135 if (metadata.Contains (metadataName))
136 metadata.Remove (metadataName);
137 metadata.Add (metadataName, metadataValue);
140 public override string ToString ()
145 public string ItemSpec {
146 get { return itemSpec; }
147 set { itemSpec = value; }
150 public int MetadataCount {
151 // predefined metadata
152 get { return metadata.Count + 11; }
155 public ICollection MetadataNames {
156 get { return metadata.Keys; }