Merge pull request #819 from brendanzagaeski/patch-1
[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 using Mono.XBuild.Utilities;
36
37 namespace Microsoft.Build.Utilities
38 {
39 #if !MICROSOFT_BUILD_DLL
40         public
41 #endif
42         sealed class TaskItem : MarshalByRefObject, ITaskItem
43 #if NET_4_0
44                 , ITaskItem2
45 #endif
46         {
47                 IDictionary             escapedMetadata;
48                 string                  escapedItemSpec;
49
50                 public TaskItem ()
51                 {
52                         this.escapedItemSpec = String.Empty;
53                         this.escapedMetadata = CollectionsUtil.CreateCaseInsensitiveHashtable ();
54                 }
55
56                 public TaskItem (ITaskItem sourceItem)
57                 {
58                         if (sourceItem == null)
59                                 throw new ArgumentNullException ("sourceItem");
60
61 #if NET_4_0
62                         var ti2 = sourceItem as ITaskItem2;
63                         if (ti2 != null) {
64                                 escapedItemSpec = ti2.EvaluatedIncludeEscaped;
65                                 escapedMetadata = ti2.CloneCustomMetadataEscaped ();
66                         } else
67 #endif
68                         {
69                                 escapedItemSpec = MSBuildUtils.Escape (sourceItem.ItemSpec);
70                                 escapedMetadata = sourceItem.CloneCustomMetadata ();
71                                 foreach (string key in new ArrayList (escapedMetadata.Keys))
72                                         escapedMetadata [key] = MSBuildUtils.Escape ((string)escapedMetadata [key]);
73                         }
74                 }
75
76                 public TaskItem (string itemSpec)
77                 {
78                         if (itemSpec == null)
79                                 throw new ArgumentNullException ("itemSpec");
80                         
81                         escapedItemSpec = itemSpec;
82                         escapedMetadata = CollectionsUtil.CreateCaseInsensitiveHashtable ();
83                 }
84
85                 public TaskItem (string itemSpec, IDictionary itemMetadata)
86                 {
87                         if (itemSpec == null)
88                                 throw new ArgumentNullException ("itemSpec");
89                         
90                         if (itemMetadata == null)
91                                 throw new ArgumentNullException ("itemMetadata");
92                         
93                         escapedItemSpec = itemSpec;
94                         escapedMetadata = CollectionsUtil.CreateCaseInsensitiveHashtable (itemMetadata);
95                 }
96
97                 public IDictionary CloneCustomMetadata ()
98                 {
99                         IDictionary clonedMetadata = CollectionsUtil.CreateCaseInsensitiveHashtable ();
100                         foreach (DictionaryEntry de in escapedMetadata)
101                                 clonedMetadata.Add (de.Key, MSBuildUtils.Unescape ((string) de.Value));
102                         return clonedMetadata;
103                 }
104
105                 IDictionary CloneCustomMetadataEscaped ()
106                 {
107                         return CollectionsUtil.CreateCaseInsensitiveHashtable (escapedMetadata);
108                 }
109
110 #if NET_4_0
111                 IDictionary ITaskItem2.CloneCustomMetadataEscaped ()
112                 {
113                         return CloneCustomMetadataEscaped ();
114                 }
115 #endif
116
117                 public void CopyMetadataTo (ITaskItem destinationItem)
118                 {
119                         foreach (DictionaryEntry e in escapedMetadata) {
120                                 if (destinationItem.GetMetadata ((string)e.Key) == String.Empty) {
121                                         destinationItem.SetMetadata ((string)e.Key, MSBuildUtils.Unescape ((string)e.Value));
122                                 }
123                         }
124                 }
125
126                 public static explicit operator string (TaskItem taskItemToCast)
127                 {
128                         return taskItemToCast.ItemSpec;
129                 }
130
131                 public string GetMetadata (string metadataName)
132                 {
133                         return MSBuildUtils.Unescape (GetMetadataValue (metadataName));
134                 }
135
136                 string GetMetadataValue (string metadataName)
137                 {
138                         if (ReservedNameUtils.IsReservedMetadataName (metadataName))
139                                 return ReservedNameUtils.GetReservedMetadata (ItemSpec, metadataName, escapedMetadata);
140                         return ((string) escapedMetadata [metadataName]) ?? String.Empty;
141                 }
142
143 #if NET_4_0
144                 string ITaskItem2.GetMetadataValueEscaped (string metadataName)
145                 {
146                         return GetMetadataValue (metadataName);
147                 }
148 #endif
149
150                 public override object InitializeLifetimeService ()
151                 {
152                         return null;
153                 }
154
155                 public void RemoveMetadata (string metadataName)
156                 {
157                         if (metadataName == null)
158                                 throw new ArgumentNullException ("metadataName");
159                         if (ReservedNameUtils.IsReservedMetadataName (metadataName))
160                                 throw new ArgumentException ("Can't remove reserved metadata");
161                         escapedMetadata.Remove (metadataName);
162                 }
163
164                 public void SetMetadata (string metadataName, string metadataValue)
165                 {
166                         if (metadataName == null)
167                                 throw new ArgumentNullException ("metadataName");
168                         if (metadataValue == null)
169                                 throw new ArgumentNullException ("metadataValue");
170
171                         // allow RecursiveDir to be set, it gets set by DirectoryScanner
172                         if (String.Compare (metadataName, "RecursiveDir", StringComparison.InvariantCultureIgnoreCase) != 0 &&
173                                 ReservedNameUtils.IsReservedMetadataName (metadataName))
174                                 throw new ArgumentException ("Can't modify reserved metadata");
175                                 
176                         escapedMetadata [metadataName] = metadataValue;
177                 }
178
179 #if NET_4_0
180                 void ITaskItem2.SetMetadataValueLiteral (string metadataName, string metadataValue)
181                 {
182                         SetMetadata (metadataName, MSBuildUtils.Escape (metadataValue));
183                 }
184 #endif
185                 public override string ToString ()
186                 {
187                         return escapedItemSpec;
188                 }
189                 
190                 public string ItemSpec {
191                         get { return MSBuildUtils.Unescape (escapedItemSpec); }
192                         set { escapedItemSpec = value; }
193                 }
194
195 #if NET_4_0
196                 string ITaskItem2.EvaluatedIncludeEscaped {
197                         get { return escapedItemSpec; }
198                         set { escapedItemSpec = value; }
199                 }
200 #endif
201
202                 public int MetadataCount {
203                         get { return escapedMetadata.Count + 11; }
204                 }
205
206                 public ICollection MetadataNames {
207                         get {
208                                 ArrayList list = new ArrayList ();
209                                 
210                                 foreach (string s in ReservedNameUtils.ReservedMetadataNames)
211                                         list.Add (s);
212                                 foreach (string s in escapedMetadata.Keys)
213                                         list.Add (s);
214
215                                 return list;
216                         }
217                 }
218
219         }
220 }
221
222 #endif