Move from xbuild into mcs
[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 using System;
29 using System.Collections;
30 using System.IO;
31 using Microsoft.Build.Framework;
32
33 namespace Microsoft.Build.Utilities
34 {
35         public sealed class TaskItem : MarshalByRefObject, ITaskItem
36         {
37                 IDictionary     metadata;
38                 string          itemSpec;
39                 string          recursiveDir;
40                 
41                 public TaskItem ()
42                 {
43                         this.itemSpec = String.Empty;
44                         this.recursiveDir = String.Empty;
45                         this.metadata = new Hashtable (new CaseInsensitiveHashCodeProvider (), new CaseInsensitiveComparer ());
46                 }
47
48                 public TaskItem (ITaskItem sourceItem)
49                 {
50                         this.itemSpec = sourceItem.ItemSpec;
51                         this.recursiveDir = String.Empty;
52                         this.metadata = sourceItem.CloneCustomMetadata ();
53                 }
54
55                 public TaskItem (string itemSpec)
56                 {
57                         this.ItemSpec = itemSpec;
58                         this.recursiveDir = String.Empty;
59                         metadata = new Hashtable (new CaseInsensitiveHashCodeProvider (), new CaseInsensitiveComparer ());
60                 }
61
62                 public TaskItem (string itemSpec, IDictionary itemMetadata)
63                 {
64                         this.itemSpec = itemSpec;
65                         this.recursiveDir = String.Empty;
66                         this.metadata = itemMetadata;
67                 }
68
69                 public IDictionary CloneCustomMetadata ()
70                 {
71                         IDictionary clonedMetadata = new Hashtable (new CaseInsensitiveHashCodeProvider (), new CaseInsensitiveComparer ());
72                         foreach (DictionaryEntry de in metadata)
73                                 clonedMetadata.Add (de.Key, de.Value);
74                         return clonedMetadata;
75                 }
76
77                 public void CopyMetadataTo (ITaskItem destinationItem)
78                 {
79                         foreach (DictionaryEntry e in metadata) {
80                                 destinationItem.SetMetadata ((string)e.Key, (string)e.Value);
81                         }
82                 }
83
84                 public static explicit operator string (TaskItem taskItemToCast)
85                 {
86                         return taskItemToCast.ToString ();
87                 }
88
89                 public string GetMetadata (string metadataName)
90                 {
91                         switch (metadataName.ToLower ()) {
92                         case "fullpath":
93                                 return Path.GetFullPath (itemSpec);
94                         case "rootdir":
95                                 return "/";
96                         case "filename":
97                                 return Path.GetFileNameWithoutExtension (itemSpec);
98                         case "extension":
99                                 return Path.GetExtension (itemSpec);
100                         case "relativedir":
101                                 return Path.GetDirectoryName (itemSpec);
102                         case "directory":
103                                 return Path.GetDirectoryName (Path.GetFullPath (itemSpec));
104                         case "recursivedir":
105                                 return recursiveDir;
106                         case "identity":
107                                 return Path.Combine (Path.GetDirectoryName (itemSpec), Path.GetFileName (itemSpec));
108                         case "modifiedtime":
109                                 return File.GetLastWriteTime (itemSpec).ToString ();
110                         case "createdtime":
111                                 return File.GetCreationTime (itemSpec).ToString ();
112                         case "accessedtime":
113                                 return File.GetLastAccessTime (itemSpec).ToString ();
114                         default:
115                                 return (string) metadata [metadataName];
116                         }
117                 }
118
119                 public override object InitializeLifetimeService ()
120                 {
121                         return null;
122                 }
123
124                 public void RemoveMetadata (string metadataName)
125                 {
126                         if (metadata.Contains (metadataName))
127                                 metadata.Remove (metadataName);
128                 }
129
130                 public void SetMetadata (string metadataName, string metadataValue)
131                 {
132                         if (metadata.Contains (metadataName))
133                                 metadata.Remove (metadataName);
134                         metadata.Add (metadataName, metadataValue);
135                 }
136
137                 public override string ToString ()
138                 {
139                         return itemSpec;
140                 }
141
142                 public string ItemSpec {
143                         get { return itemSpec; }
144                         set { itemSpec = value; }
145                 }
146
147                 public int MetadataCount {
148                 // predefined metadata
149                         get { return metadata.Count + 11; }
150                 }
151
152                 public ICollection MetadataNames {
153                         get { return metadata.Keys; }
154                 }
155         }
156 }