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