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