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 public sealed class TaskItem : MarshalByRefObject, ITaskItem
46 this.itemSpec = String.Empty;
47 this.metadata = CollectionsUtil.CreateCaseInsensitiveHashtable ();
50 public TaskItem (ITaskItem sourceItem)
52 if (sourceItem == null)
53 throw new ArgumentNullException ("sourceItem");
55 this.itemSpec = sourceItem.ItemSpec;
56 this.metadata = sourceItem.CloneCustomMetadata ();
59 public TaskItem (string itemSpec)
62 throw new ArgumentNullException ("itemSpec");
64 this.itemSpec = itemSpec;
65 this.metadata = CollectionsUtil.CreateCaseInsensitiveHashtable ();
68 this.itemSpec = itemSpec.Replace ('\\', Path.DirectorySeparatorChar);
71 public TaskItem (string itemSpec, IDictionary itemMetadata)
74 throw new ArgumentNullException ("itemSpec");
76 if (itemMetadata == null)
77 throw new ArgumentNullException ("itemMetadata");
79 this.itemSpec = itemSpec;
80 this.metadata = CollectionsUtil.CreateCaseInsensitiveHashtable (itemMetadata);
83 public IDictionary CloneCustomMetadata ()
85 IDictionary clonedMetadata = CollectionsUtil.CreateCaseInsensitiveHashtable ();
86 foreach (DictionaryEntry de in metadata)
87 clonedMetadata.Add (de.Key, de.Value);
88 return clonedMetadata;
91 public void CopyMetadataTo (ITaskItem destinationItem)
93 foreach (DictionaryEntry e in metadata) {
94 if (destinationItem.GetMetadata ((string)e.Key) == String.Empty) {
95 destinationItem.SetMetadata ((string)e.Key, (string)e.Value);
100 public static explicit operator string (TaskItem taskItemToCast)
102 return taskItemToCast.ItemSpec;
105 public string GetMetadata (string metadataName)
107 if (ReservedNameUtils.IsReservedMetadataName (metadataName))
108 return ReservedNameUtils.GetReservedMetadata (ItemSpec, metadataName, metadata);
109 else if (metadata.Contains (metadataName))
110 return (string) metadata [metadataName];
115 public override object InitializeLifetimeService ()
120 public void RemoveMetadata (string metadataName)
122 if (metadataName == null)
123 throw new ArgumentNullException ("metadataName");
124 if (ReservedNameUtils.IsReservedMetadataName (metadataName))
125 throw new ArgumentException ("Can't remove reserved metadata");
126 if (metadata.Contains (metadataName))
127 metadata.Remove (metadataName);
130 public void SetMetadata (string metadataName, string metadataValue)
132 if (metadataName == null)
133 throw new ArgumentNullException ("metadataName");
134 if (metadataValue == null)
135 throw new ArgumentNullException ("metadataValue");
137 // allow RecursiveDir to be set, it gets set by DirectoryScanner
138 if (String.Compare (metadataName, "RecursiveDir", StringComparison.InvariantCultureIgnoreCase) != 0 &&
139 ReservedNameUtils.IsReservedMetadataName (metadataName))
140 throw new ArgumentException ("Can't modify reserved metadata");
142 if (metadata.Contains (metadataName))
143 metadata.Remove (metadataName);
144 metadata.Add (metadataName, metadataValue);
147 public override string ToString ()
152 public string ItemSpec {
153 get { return itemSpec; }
154 set { itemSpec = value; }
157 public int MetadataCount {
158 get { return metadata.Count + 11; }
161 public ICollection MetadataNames {
163 ArrayList list = new ArrayList ();
165 foreach (string s in ReservedNameUtils.ReservedMetadataNames)
167 foreach (string s in metadata.Keys)