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