[runtime] Fix test_op_il_seq_point in amd64.
[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
29 using System;
30 using System.Collections;
31 using System.Collections.Specialized;
32 using System.IO;
33 using Microsoft.Build.Framework;
34 using Mono.XBuild.Utilities;
35
36 namespace Microsoft.Build.Utilities
37 {
38 #if !MICROSOFT_BUILD_DLL
39         public
40 #endif
41         sealed class TaskItem : MarshalByRefObject, ITaskItem
42                 , ITaskItem2
43         {
44                 IDictionary             escapedMetadata;
45                 string                  escapedItemSpec;
46
47                 public TaskItem ()
48                 {
49                         this.escapedItemSpec = String.Empty;
50                         this.escapedMetadata = CollectionsUtil.CreateCaseInsensitiveHashtable ();
51                 }
52
53                 public TaskItem (ITaskItem sourceItem)
54                 {
55                         if (sourceItem == null)
56                                 throw new ArgumentNullException ("sourceItem");
57
58                         var ti2 = sourceItem as ITaskItem2;
59                         if (ti2 != null) {
60                                 escapedItemSpec = ti2.EvaluatedIncludeEscaped;
61                                 escapedMetadata = ti2.CloneCustomMetadataEscaped ();
62                         } else
63                         {
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]);
68                         }
69                 }
70
71                 public TaskItem (string itemSpec)
72                 {
73                         if (itemSpec == null)
74                                 throw new ArgumentNullException ("itemSpec");
75                         
76                         escapedItemSpec = itemSpec;
77                         escapedMetadata = CollectionsUtil.CreateCaseInsensitiveHashtable ();
78                 }
79
80                 public TaskItem (string itemSpec, IDictionary itemMetadata)
81                 {
82                         if (itemSpec == null)
83                                 throw new ArgumentNullException ("itemSpec");
84                         
85                         if (itemMetadata == null)
86                                 throw new ArgumentNullException ("itemMetadata");
87                         
88                         escapedItemSpec = itemSpec;
89                         escapedMetadata = CollectionsUtil.CreateCaseInsensitiveHashtable (itemMetadata);
90                 }
91
92                 public IDictionary CloneCustomMetadata ()
93                 {
94                         IDictionary clonedMetadata = CollectionsUtil.CreateCaseInsensitiveHashtable ();
95                         foreach (DictionaryEntry de in escapedMetadata)
96                                 clonedMetadata.Add (de.Key, MSBuildUtils.Unescape ((string) de.Value));
97                         return clonedMetadata;
98                 }
99
100                 IDictionary CloneCustomMetadataEscaped ()
101                 {
102                         return CollectionsUtil.CreateCaseInsensitiveHashtable (escapedMetadata);
103                 }
104
105                 IDictionary ITaskItem2.CloneCustomMetadataEscaped ()
106                 {
107                         return CloneCustomMetadataEscaped ();
108                 }
109
110                 public void CopyMetadataTo (ITaskItem destinationItem)
111                 {
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));
115                                 }
116                         }
117                 }
118
119                 public static explicit operator string (TaskItem taskItemToCast)
120                 {
121                         return taskItemToCast.ItemSpec;
122                 }
123
124                 public string GetMetadata (string metadataName)
125                 {
126                         return MSBuildUtils.Unescape (GetMetadataValue (metadataName));
127                 }
128
129                 string GetMetadataValue (string metadataName)
130                 {
131                         if (ReservedNameUtils.IsReservedMetadataName (metadataName))
132                                 return ReservedNameUtils.GetReservedMetadata (ItemSpec, metadataName, escapedMetadata);
133                         return ((string) escapedMetadata [metadataName]) ?? String.Empty;
134                 }
135
136                 string ITaskItem2.GetMetadataValueEscaped (string metadataName)
137                 {
138                         return GetMetadataValue (metadataName);
139                 }
140
141                 public override object InitializeLifetimeService ()
142                 {
143                         return null;
144                 }
145
146                 public void RemoveMetadata (string metadataName)
147                 {
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);
153                 }
154
155                 public void SetMetadata (string metadataName, string metadataValue)
156                 {
157                         if (metadataName == null)
158                                 throw new ArgumentNullException ("metadataName");
159                         if (metadataValue == null)
160                                 throw new ArgumentNullException ("metadataValue");
161
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");
166                                 
167                         escapedMetadata [metadataName] = metadataValue;
168                 }
169
170                 void ITaskItem2.SetMetadataValueLiteral (string metadataName, string metadataValue)
171                 {
172                         SetMetadata (metadataName, MSBuildUtils.Escape (metadataValue));
173                 }
174                 public override string ToString ()
175                 {
176                         return ItemSpec;
177                 }
178                 
179                 public string ItemSpec {
180                         get { return MSBuildUtils.Unescape (escapedItemSpec); }
181                         set { escapedItemSpec = value; }
182                 }
183
184                 string ITaskItem2.EvaluatedIncludeEscaped {
185                         get { return escapedItemSpec; }
186                         set { escapedItemSpec = value; }
187                 }
188
189                 public int MetadataCount {
190                         get { return escapedMetadata.Count + 11; }
191                 }
192
193                 public ICollection MetadataNames {
194                         get {
195                                 ArrayList list = new ArrayList ();
196                                 
197                                 foreach (string s in ReservedNameUtils.ReservedMetadataNames)
198                                         list.Add (s);
199                                 foreach (string s in escapedMetadata.Keys)
200                                         list.Add (s);
201
202                                 return list;
203                         }
204                 }
205
206         }
207 }
208