implemented more ProjectInstance family. Enabled some tests back.
[mono.git] / mcs / class / Microsoft.Build / Microsoft.Build.Execution / ProjectItemInstance.cs
1 //
2 // ProjectItemInstance.cs
3 //
4 // Author:
5 //   Rolf Bjarne Kvinge (rolf@xamarin.com)
6 //   Atsushi Enomoto (atsushi@xamarin.com)
7 //
8 // Copyright (C) 2011,2013 Xamarin Inc.
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 //
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System;
31 using System.Collections.Generic;
32 using System.Linq;
33 using Microsoft.Build.Framework;
34 using Microsoft.Build.Evaluation;
35 using System.Collections;
36 using Microsoft.Build.Construction;
37
38 namespace Microsoft.Build.Execution
39 {
40         public class ProjectItemInstance
41                 : ITaskItem2
42         {
43                 internal ProjectItemInstance (ProjectInstance project, ProjectItemElement xml, string evaluatedInclude)
44                 {
45                         this.project = project;
46                         this.evaluated_include = evaluatedInclude;
47                         item_type = xml.ItemType;
48                         metadata = xml.Metadata.Select (m => new ProjectMetadataInstance (m.Name, m.Value)).ToList ();
49                 }
50                 
51                 readonly ProjectInstance project;
52                 readonly string item_type;
53                 string evaluated_include;
54                 readonly List<ProjectMetadataInstance> metadata;
55                 
56                 public ProjectMetadataInstance GetMetadata (string name)
57                 {
58                         return Metadata.FirstOrDefault (m => m.Name.Equals (name, StringComparison.OrdinalIgnoreCase));
59                 }
60
61                 public string GetMetadataValue (string name)
62                 {
63                         var m = GetMetadata (name);
64                         return m != null ? m.EvaluatedValue : null;
65                 }
66
67                 public bool HasMetadata (string name)
68                 {
69                         return GetMetadata (name) != null;
70                 }
71
72                 public void RemoveMetadata (string metadataName)
73                 {
74                         var m = GetMetadata (metadataName);
75                         if (m != null)
76                                 metadata.Remove (m);
77                 }
78
79                 public void SetMetadata (IEnumerable<KeyValuePair<string, string>> metadataDictionary)
80                 {
81                         foreach (var p in metadataDictionary)
82                                 SetMetadata (p.Key, p.Value);
83                 }
84
85                 public ProjectMetadataInstance SetMetadata (string name, string evaluatedValue)
86                 {
87                         var m = metadata.FirstOrDefault (_ => _.Name.Equals (name, StringComparison.OrdinalIgnoreCase));
88                         if (m != null)
89                                 metadata.Remove (m);
90                         m = new ProjectMetadataInstance (name, evaluatedValue);
91                         metadata.Add (m);
92                         return m;
93                 }
94
95                 public int DirectMetadataCount {
96                         get { throw new NotImplementedException (); }
97                 }
98
99                 public string EvaluatedInclude {
100                         get { return evaluated_include; }
101                         set {
102                                 if (value == null)
103                                         throw new ArgumentNullException ("value");
104                                 evaluated_include = value;
105                         }
106                 }
107
108                 public string ItemType {
109                         get { return item_type; }
110                 }
111
112                 public IEnumerable<ProjectMetadataInstance> Metadata {
113                         get { return metadata; }
114                 }
115
116                 public int MetadataCount {
117                         get { return metadata.Count; }
118                 }
119
120                 public ICollection<string> MetadataNames {
121                         get { return metadata.Select (m => m.Name).ToArray (); }
122                 }
123
124                 public ProjectInstance Project {
125                         get { return project; }
126                 }
127                 
128                 internal string RecursiveDir { get; set; }
129
130                 #region ITaskItem2 implementation
131
132                 string ITaskItem2.GetMetadataValueEscaped (string metadataName)
133                 {
134                         return ProjectCollection.Escape (GetMetadataValue (metadataName));
135                 }
136
137                 void ITaskItem2.SetMetadataValueLiteral (string metadataName, string metadataValue)
138                 {
139                         SetMetadata (metadataName, metadataValue);
140                 }
141
142                 System.Collections.IDictionary ITaskItem2.CloneCustomMetadataEscaped ()
143                 {
144                         var dic = ((ITaskItem) this).CloneCustomMetadata ();
145                         foreach (DictionaryEntry p in dic)
146                                 dic [p.Key] = ProjectCollection.Escape ((string) p.Value);
147                         return dic;
148                 }
149
150                 string ITaskItem2.EvaluatedIncludeEscaped {
151                         get { return ProjectCollection.Escape (EvaluatedInclude); }
152                         set { EvaluatedInclude = ProjectCollection.Unescape (value); }
153                 }
154
155                 #endregion
156
157                 #region ITaskItem implementation
158
159                 IDictionary ITaskItem.CloneCustomMetadata ()
160                 {
161                         var dic = new Hashtable ();
162                         foreach (var md in Metadata)
163                                 dic [md.Name] = md.EvaluatedValue;
164                         return dic;
165                 }
166
167                 void ITaskItem.CopyMetadataTo (ITaskItem destinationItem)
168                 {
169                         if (destinationItem == null)
170                                 throw new ArgumentNullException ("destinationItem");
171                         foreach (var md in Metadata)
172                                 destinationItem.SetMetadata (md.Name, md.EvaluatedValue);
173                 }
174
175                 string ITaskItem.GetMetadata (string metadataName)
176                 {
177                         return GetMetadataValue (metadataName);
178                 }
179
180                 void ITaskItem.RemoveMetadata (string metadataName)
181                 {
182                         RemoveMetadata (metadataName);
183                 }
184
185                 void ITaskItem.SetMetadata (string metadataName, string metadataValue)
186                 {
187                         SetMetadata (metadataName, ProjectCollection.Unescape (metadataValue));
188                 }
189
190                 string ITaskItem.ItemSpec {
191                         get { return EvaluatedInclude; }
192                         set { EvaluatedInclude = value; }
193                 }
194
195                 int ITaskItem.MetadataCount {
196                         get { return MetadataCount; }
197                 }
198
199                 ICollection ITaskItem.MetadataNames {
200                         get { return MetadataNames.ToArray (); }
201                 }
202
203                 #endregion
204         }
205 }
206