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.
30 using System.Collections;
31 using System.Collections.Specialized;
33 using Microsoft.Build.Framework;
34 using Mono.XBuild.Utilities;
36 namespace Microsoft.Build.Utilities
38 #if !MICROSOFT_BUILD_DLL
41 sealed class TaskItem : MarshalByRefObject, ITaskItem
44 IDictionary escapedMetadata;
45 string escapedItemSpec;
49 this.escapedItemSpec = String.Empty;
50 this.escapedMetadata = CollectionsUtil.CreateCaseInsensitiveHashtable ();
53 public TaskItem (ITaskItem sourceItem)
55 if (sourceItem == null)
56 throw new ArgumentNullException ("sourceItem");
58 var ti2 = sourceItem as ITaskItem2;
60 escapedItemSpec = ti2.EvaluatedIncludeEscaped;
61 escapedMetadata = ti2.CloneCustomMetadataEscaped ();
64 escapedItemSpec = MSBuildUtils.Escape (sourceItem.ItemSpec);
65 escapedMetadata = sourceItem.CloneCustomMetadata ();
66 foreach (string key in new ArrayList (escapedMetadata.Keys))
67 escapedMetadata [key] = MSBuildUtils.Escape ((string)escapedMetadata [key]);
71 public TaskItem (string itemSpec)
74 throw new ArgumentNullException ("itemSpec");
76 escapedItemSpec = itemSpec;
77 escapedMetadata = CollectionsUtil.CreateCaseInsensitiveHashtable ();
80 public TaskItem (string itemSpec, IDictionary itemMetadata)
83 throw new ArgumentNullException ("itemSpec");
85 if (itemMetadata == null)
86 throw new ArgumentNullException ("itemMetadata");
88 escapedItemSpec = itemSpec;
89 escapedMetadata = CollectionsUtil.CreateCaseInsensitiveHashtable (itemMetadata);
92 public IDictionary CloneCustomMetadata ()
94 IDictionary clonedMetadata = CollectionsUtil.CreateCaseInsensitiveHashtable ();
95 foreach (DictionaryEntry de in escapedMetadata)
96 clonedMetadata.Add (de.Key, MSBuildUtils.Unescape ((string) de.Value));
97 return clonedMetadata;
100 IDictionary CloneCustomMetadataEscaped ()
102 return CollectionsUtil.CreateCaseInsensitiveHashtable (escapedMetadata);
105 IDictionary ITaskItem2.CloneCustomMetadataEscaped ()
107 return CloneCustomMetadataEscaped ();
110 public void CopyMetadataTo (ITaskItem destinationItem)
112 foreach (DictionaryEntry e in escapedMetadata) {
113 if (destinationItem.GetMetadata ((string)e.Key) == String.Empty) {
114 destinationItem.SetMetadata ((string)e.Key, MSBuildUtils.Unescape ((string)e.Value));
119 public static explicit operator string (TaskItem taskItemToCast)
121 return taskItemToCast.ItemSpec;
124 public string GetMetadata (string metadataName)
126 return MSBuildUtils.Unescape (GetMetadataValue (metadataName));
129 string GetMetadataValue (string metadataName)
131 if (ReservedNameUtils.IsReservedMetadataName (metadataName))
132 return ReservedNameUtils.GetReservedMetadata (ItemSpec, metadataName, escapedMetadata);
133 return ((string) escapedMetadata [metadataName]) ?? String.Empty;
136 string ITaskItem2.GetMetadataValueEscaped (string metadataName)
138 return GetMetadataValue (metadataName);
141 public override object InitializeLifetimeService ()
146 public void RemoveMetadata (string metadataName)
148 if (metadataName == null)
149 throw new ArgumentNullException ("metadataName");
150 if (ReservedNameUtils.IsReservedMetadataName (metadataName))
151 throw new ArgumentException ("Can't remove reserved metadata");
152 escapedMetadata.Remove (metadataName);
155 public void SetMetadata (string metadataName, string metadataValue)
157 if (metadataName == null)
158 throw new ArgumentNullException ("metadataName");
159 if (metadataValue == null)
160 throw new ArgumentNullException ("metadataValue");
162 // allow RecursiveDir to be set, it gets set by DirectoryScanner
163 if (String.Compare (metadataName, "RecursiveDir", StringComparison.InvariantCultureIgnoreCase) != 0 &&
164 ReservedNameUtils.IsReservedMetadataName (metadataName))
165 throw new ArgumentException ("Can't modify reserved metadata");
167 escapedMetadata [metadataName] = metadataValue;
170 void ITaskItem2.SetMetadataValueLiteral (string metadataName, string metadataValue)
172 SetMetadata (metadataName, MSBuildUtils.Escape (metadataValue));
174 public override string ToString ()
179 public string ItemSpec {
180 get { return MSBuildUtils.Unescape (escapedItemSpec); }
181 set { escapedItemSpec = value; }
184 string ITaskItem2.EvaluatedIncludeEscaped {
185 get { return escapedItemSpec; }
186 set { escapedItemSpec = value; }
189 public int MetadataCount {
190 get { return escapedMetadata.Count + 11; }
193 public ICollection MetadataNames {
195 ArrayList list = new ArrayList ();
197 foreach (string s in ReservedNameUtils.ReservedMetadataNames)
199 foreach (string s in escapedMetadata.Keys)