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;
35 using Mono.XBuild.Utilities;
37 namespace Microsoft.Build.Utilities
39 #if !MICROSOFT_BUILD_DLL
42 sealed class TaskItem : MarshalByRefObject, ITaskItem
47 IDictionary escapedMetadata;
48 string escapedItemSpec;
52 this.escapedItemSpec = String.Empty;
53 this.escapedMetadata = CollectionsUtil.CreateCaseInsensitiveHashtable ();
56 public TaskItem (ITaskItem sourceItem)
58 if (sourceItem == null)
59 throw new ArgumentNullException ("sourceItem");
62 var ti2 = sourceItem as ITaskItem2;
64 escapedItemSpec = ti2.EvaluatedIncludeEscaped;
65 escapedMetadata = ti2.CloneCustomMetadataEscaped ();
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]);
76 public TaskItem (string itemSpec)
79 throw new ArgumentNullException ("itemSpec");
81 escapedItemSpec = itemSpec;
82 escapedMetadata = CollectionsUtil.CreateCaseInsensitiveHashtable ();
85 public TaskItem (string itemSpec, IDictionary itemMetadata)
88 throw new ArgumentNullException ("itemSpec");
90 if (itemMetadata == null)
91 throw new ArgumentNullException ("itemMetadata");
93 escapedItemSpec = itemSpec;
94 escapedMetadata = CollectionsUtil.CreateCaseInsensitiveHashtable (itemMetadata);
97 public IDictionary CloneCustomMetadata ()
99 IDictionary clonedMetadata = CollectionsUtil.CreateCaseInsensitiveHashtable ();
100 foreach (DictionaryEntry de in escapedMetadata)
101 clonedMetadata.Add (de.Key, MSBuildUtils.Unescape ((string) de.Value));
102 return clonedMetadata;
105 IDictionary CloneCustomMetadataEscaped ()
107 return CollectionsUtil.CreateCaseInsensitiveHashtable (escapedMetadata);
111 IDictionary ITaskItem2.CloneCustomMetadataEscaped ()
113 return CloneCustomMetadataEscaped ();
117 public void CopyMetadataTo (ITaskItem destinationItem)
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));
126 public static explicit operator string (TaskItem taskItemToCast)
128 return taskItemToCast.ItemSpec;
131 public string GetMetadata (string metadataName)
133 return MSBuildUtils.Unescape (GetMetadataValue (metadataName));
136 string GetMetadataValue (string metadataName)
138 if (ReservedNameUtils.IsReservedMetadataName (metadataName))
139 return ReservedNameUtils.GetReservedMetadata (ItemSpec, metadataName, escapedMetadata);
140 return ((string) escapedMetadata [metadataName]) ?? String.Empty;
144 string ITaskItem2.GetMetadataValueEscaped (string metadataName)
146 return GetMetadataValue (metadataName);
150 public override object InitializeLifetimeService ()
155 public void RemoveMetadata (string metadataName)
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);
164 public void SetMetadata (string metadataName, string metadataValue)
166 if (metadataName == null)
167 throw new ArgumentNullException ("metadataName");
168 if (metadataValue == null)
169 throw new ArgumentNullException ("metadataValue");
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");
176 escapedMetadata [metadataName] = metadataValue;
180 void ITaskItem2.SetMetadataValueLiteral (string metadataName, string metadataValue)
182 SetMetadata (metadataName, MSBuildUtils.Escape (metadataValue));
185 public override string ToString ()
187 return escapedItemSpec;
190 public string ItemSpec {
191 get { return MSBuildUtils.Unescape (escapedItemSpec); }
192 set { escapedItemSpec = value; }
196 string ITaskItem2.EvaluatedIncludeEscaped {
197 get { return escapedItemSpec; }
198 set { escapedItemSpec = value; }
202 public int MetadataCount {
203 get { return escapedMetadata.Count + 11; }
206 public ICollection MetadataNames {
208 ArrayList list = new ArrayList ();
210 foreach (string s in ReservedNameUtils.ReservedMetadataNames)
212 foreach (string s in escapedMetadata.Keys)