* roottypes.cs: Rename from tree.cs.
[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         public sealed class TaskItem : MarshalByRefObject, ITaskItem
40         {
41                 IDictionary             metadata;
42                 string                  itemSpec;
43
44                 public TaskItem ()
45                 {
46                         this.itemSpec = String.Empty;
47                         this.metadata = CollectionsUtil.CreateCaseInsensitiveHashtable ();
48                 }
49
50                 public TaskItem (ITaskItem sourceItem)
51                 {
52                         if (sourceItem == null)
53                                 throw new ArgumentNullException ("sourceItem");
54                         
55                         this.itemSpec = sourceItem.ItemSpec;
56                         this.metadata = sourceItem.CloneCustomMetadata ();
57                 }
58
59                 public TaskItem (string itemSpec)
60                 {
61                         if (itemSpec == null)
62                                 throw new ArgumentNullException ("itemSpec");
63                         
64                         this.ItemSpec = itemSpec;
65                         this.metadata = CollectionsUtil.CreateCaseInsensitiveHashtable ();
66                 }
67
68                 public TaskItem (string itemSpec, IDictionary itemMetadata)
69                 {
70                         if (itemSpec == null)
71                                 throw new ArgumentNullException ("itemSpec");
72                         
73                         if (itemMetadata == null)
74                                 throw new ArgumentNullException ("itemMetadata");
75                         
76                         this.itemSpec = itemSpec;
77                         this.metadata = CollectionsUtil.CreateCaseInsensitiveHashtable (itemMetadata);
78                 }
79
80                 public IDictionary CloneCustomMetadata ()
81                 {
82                         IDictionary clonedMetadata = CollectionsUtil.CreateCaseInsensitiveHashtable ();
83                         foreach (DictionaryEntry de in metadata)
84                                 clonedMetadata.Add (de.Key, de.Value);
85                         return clonedMetadata;
86                 }
87
88                 public void CopyMetadataTo (ITaskItem destinationItem)
89                 {
90                         foreach (DictionaryEntry e in metadata) {
91                                 if (destinationItem.GetMetadata ((string)e.Key) == String.Empty) {
92                                         destinationItem.SetMetadata ((string)e.Key, (string)e.Value);
93                                 }
94                         }
95                 }
96
97                 public static explicit operator string (TaskItem taskItemToCast)
98                 {
99                         return taskItemToCast.ItemSpec;
100                 }
101
102                 public string GetMetadata (string metadataName)
103                 {
104                         if (ReservedNameUtils.IsReservedMetadataName (metadataName))
105                                 return ReservedNameUtils.GetReservedMetadata (ItemSpec, metadataName);
106                         else if (metadata.Contains (metadataName))
107                                 return (string) metadata [metadataName];
108                         else
109                                 return String.Empty;
110                 }
111
112                 public override object InitializeLifetimeService ()
113                 {
114                         return null;
115                 }
116
117                 public void RemoveMetadata (string metadataName)
118                 {
119                         if (metadataName == null)
120                                 throw new ArgumentNullException ("metadataName");
121                         if (ReservedNameUtils.IsReservedMetadataName (metadataName))
122                                 throw new ArgumentException ("Can't remove reserved metadata");
123                         if (metadata.Contains (metadataName))
124                                 metadata.Remove (metadataName);
125                 }
126
127                 public void SetMetadata (string metadataName, string metadataValue)
128                 {
129                         if (metadataName == null)
130                                 throw new ArgumentNullException ("metadataName");
131                         if (metadataValue == null)
132                                 throw new ArgumentNullException ("metadataValue");
133                         if (ReservedNameUtils.IsReservedMetadataName (metadataName))
134                                 throw new ArgumentException ("Can't modify reserved metadata");
135                                 
136                         if (metadata.Contains (metadataName))
137                                 metadata.Remove (metadataName);
138                         metadata.Add (metadataName, metadataValue);
139                 }
140
141                 public override string ToString ()
142                 {
143                         return itemSpec;
144                 }
145                 
146                 public string ItemSpec {
147                         get { return itemSpec; }
148                         set { itemSpec = value; }
149                 }
150
151                 public int MetadataCount {
152                         get { return metadata.Count + 11; }
153                 }
154
155                 public ICollection MetadataNames {
156                         get {
157                                 ArrayList list = new ArrayList ();
158                                 
159                                 foreach (string s in ReservedNameUtils.ReservedMetadataNames)
160                                         list.Add (s);
161                                 foreach (string s in metadata.Keys)
162                                         list.Add (s);
163
164                                 return list;
165                         }
166                 }
167         }
168 }
169
170 #endif