In class/Microsoft.Build.Engine/Microsoft.Build.BuildEngine:
[mono.git] / mcs / class / Microsoft.Build.Engine / Microsoft.Build.BuildEngine / BuildItem.cs
1 //
2 // BuildItem.cs:
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.Generic;
33 using System.Collections.Specialized;
34 using System.IO;
35 using System.Text;
36 using System.Xml;
37 using Microsoft.Build.Framework;
38 using Microsoft.Build.Utilities;
39 using Mono.XBuild.Utilities;
40
41 namespace Microsoft.Build.BuildEngine {
42         public class BuildItem {
43
44                 BuildItemGroup  child_items;
45                 XmlElement      itemElement;
46                 string          finalItemSpec;
47                 bool            isImported;
48                 string          itemInclude;
49                 string          name;
50                 BuildItemGroup  parent_item_group;
51                 BuildItem       parent_item;
52                 //string                recursiveDir;
53                 IDictionary     evaluatedMetadata;
54                 IDictionary     unevaluatedMetadata;
55
56                 BuildItem ()
57                 {
58                 }
59                 
60                 public BuildItem (string itemName, ITaskItem taskItem)
61                 {
62                         if (taskItem == null)
63                                 throw new ArgumentNullException ("taskItem");
64
65                         this.name = itemName;
66                         this.finalItemSpec = taskItem.ItemSpec;
67                         this.itemInclude = Utilities.Escape (taskItem.ItemSpec);
68                         this.evaluatedMetadata = (Hashtable) taskItem.CloneCustomMetadata ();
69                         this.unevaluatedMetadata = (Hashtable) taskItem.CloneCustomMetadata ();
70                 }
71
72                 public BuildItem (string itemName, string itemInclude)
73                 {
74                         if (itemInclude == null)
75                                 throw new ArgumentNullException ("itemInclude");
76                         if (itemInclude == String.Empty)
77                                 throw new ArgumentException ("Parameter \"itemInclude\" cannot have zero length.");
78
79                         name = itemName;
80                         finalItemSpec = itemInclude;
81                         this.itemInclude = itemInclude;
82                         unevaluatedMetadata = CollectionsUtil.CreateCaseInsensitiveHashtable ();
83                         evaluatedMetadata = CollectionsUtil.CreateCaseInsensitiveHashtable ();
84                 }
85                 
86                 internal BuildItem (XmlElement itemElement, BuildItemGroup parentItemGroup)
87                 {
88                         child_items = new BuildItemGroup ();
89                         isImported = parentItemGroup.IsImported;
90                         unevaluatedMetadata = CollectionsUtil.CreateCaseInsensitiveHashtable ();
91                         evaluatedMetadata = CollectionsUtil.CreateCaseInsensitiveHashtable ();
92                         this.parent_item_group = parentItemGroup;
93                         
94                         this.itemElement = itemElement;
95                         
96                         if (Include == String.Empty)
97                                 throw new InvalidProjectFileException (String.Format ("The required attribute \"Include\" is missing from element <{0}>.", Name));
98                 }
99                 
100                 BuildItem (BuildItem parent)
101                 {
102                         isImported = parent.isImported;
103                         name = parent.Name;
104                         parent_item = parent;
105                         parent_item.child_items.AddItem (this);
106                         parent_item_group = parent.parent_item_group;
107                         unevaluatedMetadata = CollectionsUtil.CreateCaseInsensitiveHashtable (parent.unevaluatedMetadata);
108                         evaluatedMetadata = CollectionsUtil.CreateCaseInsensitiveHashtable (parent.evaluatedMetadata);
109                 }
110                 
111                 public void CopyCustomMetadataTo (BuildItem destinationItem)
112                 {
113                         if (destinationItem == null)
114                                 throw new ArgumentNullException ("destinationItem");
115
116                         foreach (DictionaryEntry de in unevaluatedMetadata)
117                                 destinationItem.AddMetadata ((string) de.Key, (string) de.Value);
118                 }
119                 
120                 [MonoTODO]
121                 public BuildItem Clone ()
122                 {
123                         return (BuildItem) this.MemberwiseClone ();
124                 }
125
126                 public string GetEvaluatedMetadata (string metadataName)
127                 {
128                         if (ReservedNameUtils.IsReservedMetadataName (metadataName)) {
129                                 string metadata = ReservedNameUtils.GetReservedMetadata (FinalItemSpec, metadataName);
130                                 return (metadataName.ToLower () == "fullpath") ? Utilities.Escape (metadata) : metadata;
131                         }
132
133                         if (evaluatedMetadata.Contains (metadataName))
134                                 return (string) evaluatedMetadata [metadataName];
135                         else
136                                 return String.Empty;
137                 }
138
139                 public string GetMetadata (string metadataName)
140                 {
141                         if (ReservedNameUtils.IsReservedMetadataName (metadataName)) {
142                                 string metadata = ReservedNameUtils.GetReservedMetadata (FinalItemSpec, metadataName);
143                                 return (metadataName.ToLower () == "fullpath") ? Utilities.Escape (metadata) : metadata;
144                         } else if (unevaluatedMetadata.Contains (metadataName))
145                                 return (string) unevaluatedMetadata [metadataName];
146                         else
147                                 return String.Empty;
148                 }
149                 
150                 public bool HasMetadata (string metadataName)
151                 {
152                         if (ReservedNameUtils.IsReservedMetadataName (metadataName))
153                                 return true;
154                         else
155                                 return evaluatedMetadata.Contains (metadataName);
156                 }
157
158                 public void RemoveMetadata (string metadataName)
159                 {
160                         if (metadataName == null)
161                                 throw new ArgumentNullException ("metadataName");
162                         
163                         if (ReservedNameUtils.IsReservedMetadataName (metadataName))
164                                 throw new ArgumentException (String.Format ("\"{0}\" is a reserved item meta-data, and cannot be modified or deleted.",
165                                         metadataName));
166
167                         if (FromXml) {
168                                 if (unevaluatedMetadata.Contains (metadataName)) {
169                                         XmlNode node = itemElement [metadataName];
170                                         itemElement.RemoveChild (node);
171                                 }
172                         } else if (HasParent) {
173                                 if (parent_item.child_items.Count > 1)
174                                         SplitParentItem ();
175                                 parent_item.RemoveMetadata (metadataName);
176                         } 
177                         
178                         DeleteMetadata (metadataName);
179                 }
180
181                 public void SetMetadata (string metadataName,
182                                          string metadataValue)
183                 {
184                         SetMetadata (metadataName, metadataValue, false);
185                 }
186                 
187                 public void SetMetadata (string metadataName,
188                                          string metadataValue,
189                                          bool treatMetadataValueAsLiteral)
190                 {
191                         if (metadataName == null)
192                                 throw new ArgumentNullException ("metadataName");
193                         
194                         if (metadataValue == null)
195                                 throw new ArgumentNullException ("metadataValue");
196                         
197                         if (ReservedNameUtils.IsReservedMetadataName (metadataName))
198                                 throw new ArgumentException (String.Format ("\"{0}\" is a reserved item meta-data, and cannot be modified or deleted.",
199                                         metadataName));
200
201                         if (treatMetadataValueAsLiteral && !HasParent)
202                                 metadataValue = Utilities.Escape (metadataValue);
203
204                         if (FromXml) {
205                                 XmlElement element = itemElement [metadataName];
206                                 if (element == null) {
207                                         element = itemElement.OwnerDocument.CreateElement (metadataName, Project.XmlNamespace);
208                                         element.InnerText = metadataValue;
209                                         itemElement.AppendChild (element);
210                                 } else
211                                         element.InnerText = metadataValue;
212                         } else if (HasParent) {
213                                 if (parent_item.child_items.Count > 1)
214                                         SplitParentItem ();
215                                 parent_item.SetMetadata (metadataName, metadataValue, treatMetadataValueAsLiteral);
216                         }
217                         if (FromXml || HasParent) {
218                                 parent_item_group.ParentProject.MarkProjectAsDirty ();
219                                 parent_item_group.ParentProject.NeedToReevaluate ();
220                         }
221                         
222                         DeleteMetadata (metadataName);
223                         AddMetadata (metadataName, metadataValue);
224                 }
225
226                 void AddMetadata (string name, string value)
227                 {
228                         if (parent_item_group != null) {
229                                 Expression e = new Expression ();
230                                 e.Parse (value, true);
231                                 evaluatedMetadata.Add (name, (string) e.ConvertTo (parent_item_group.ParentProject, typeof (string)));
232                         } else
233                                 evaluatedMetadata.Add (name, Utilities.Unescape (value));
234                                 
235                                 unevaluatedMetadata.Add (name, value);
236                 }
237
238                 void DeleteMetadata (string name)
239                 {
240                         if (evaluatedMetadata.Contains (name))
241                                 evaluatedMetadata.Remove (name);
242                         
243                         if (unevaluatedMetadata.Contains (name))
244                                 unevaluatedMetadata.Remove (name);
245                 }
246
247                 internal void Evaluate (Project project, bool evaluatedTo)
248                 {
249                         // FIXME: maybe make Expression.ConvertTo (null, ...) work as Utilities.Unescape ()?
250                         if (project == null) {
251                                 this.finalItemSpec = Utilities.Unescape (Include);
252                                 return;
253                         }
254                         
255                         foreach (XmlElement xe in itemElement.ChildNodes)
256                                 AddMetadata (xe.Name, xe.InnerText);
257
258                         DirectoryScanner directoryScanner;
259                         Expression includeExpr, excludeExpr;
260                         string includes, excludes;
261
262                         includeExpr = new Expression ();
263                         includeExpr.Parse (Include, true);
264                         excludeExpr = new Expression ();
265                         excludeExpr.Parse (Exclude, true);
266                         
267                         includes = (string) includeExpr.ConvertTo (project, typeof (string));
268                         excludes = (string) excludeExpr.ConvertTo (project, typeof (string));
269
270                         this.finalItemSpec = includes;
271                         
272                         directoryScanner = new DirectoryScanner ();
273                         
274                         directoryScanner.Includes = includes;
275                         directoryScanner.Excludes = excludes;
276
277                         if (project.FullFileName != String.Empty)
278                                 directoryScanner.BaseDirectory = new DirectoryInfo (Path.GetDirectoryName (project.FullFileName));
279                         else
280                                 directoryScanner.BaseDirectory = new DirectoryInfo (Directory.GetCurrentDirectory ());
281                         
282                         directoryScanner.Scan ();
283                         
284                         foreach (string matchedFile in directoryScanner.MatchedFilenames)
285                                 AddEvaluatedItem (project, evaluatedTo, matchedFile);
286                 }
287                 
288                 void AddEvaluatedItem (Project project, bool evaluatedTo, string itemSpec)
289                 {
290                         BuildItemGroup big;                     
291                         BuildItem bi = new BuildItem (this);
292                         bi.finalItemSpec = itemSpec;
293
294                         if (evaluatedTo) {
295                                 project.EvaluatedItems.AddItem (bi);
296         
297                                 if (!project.EvaluatedItemsByName.ContainsKey (bi.Name)) {
298                                         big = new BuildItemGroup (null, project, null, true);
299                                         project.EvaluatedItemsByName.Add (bi.Name, big);
300                                 } else {
301                                         big = project.EvaluatedItemsByName [bi.Name];
302                                 }
303
304                                 big.AddItem (bi);
305                         }
306
307                         if (!project.EvaluatedItemsByNameIgnoringCondition.ContainsKey (bi.Name)) {
308                                 big = new BuildItemGroup (null, project, null, true);
309                                 project.EvaluatedItemsByNameIgnoringCondition.Add (bi.Name, big);
310                         } else {
311                                 big = project.EvaluatedItemsByNameIgnoringCondition [bi.Name];
312                         }
313
314                         big.AddItem (bi);
315                 }
316                 
317                 internal string ConvertToString (Expression transform)
318                 {
319                         return GetItemSpecFromTransform (transform);
320                 }
321                 
322                 internal ITaskItem ConvertToITaskItem (Expression transform)
323                 {
324                         TaskItem taskItem;
325                         taskItem = new TaskItem (GetItemSpecFromTransform (transform), evaluatedMetadata);
326                         return taskItem;
327                 }
328
329                 internal void Detach ()
330                 {
331                         if (FromXml)
332                                 itemElement.ParentNode.RemoveChild (itemElement);
333                         else if (HasParent) {
334                                 if (parent_item.child_items.Count > 1)
335                                         SplitParentItem ();
336                                 parent_item.Detach ();
337                         }
338                 }
339
340                 string GetItemSpecFromTransform (Expression transform)
341                 {
342                         StringBuilder sb;
343                 
344                         if (transform == null)
345                                 return finalItemSpec;
346                         else {
347                                 sb = new StringBuilder ();
348                                 foreach (object o in transform.Collection) {
349                                         if (o is string) {
350                                                 sb.Append ((string)o);
351                                         } else if (o is PropertyReference) {
352                                                 sb.Append (((PropertyReference)o).ConvertToString (parent_item_group.ParentProject));
353                                         } else if (o is ItemReference) {
354                                                 sb.Append (((ItemReference)o).ConvertToString (parent_item_group.ParentProject));
355                                         } else if (o is MetadataReference) {
356                                                 sb.Append (GetMetadata (((MetadataReference)o).MetadataName));
357                                         }
358                                 }
359                                 return sb.ToString ();
360                         }
361                 }
362
363                 void SplitParentItem ()
364                 {
365                         BuildItem parent = parent_item;
366                         List <BuildItem> list = new List <BuildItem> ();
367                         XmlElement insertAfter = parent.itemElement;
368                         foreach (BuildItem bi in parent.child_items) {
369                                 BuildItem added = InsertElementAfter (parent, bi, insertAfter);
370                                 insertAfter = added.itemElement;
371                                 list.Add (added);
372                         }
373                         parent.parent_item_group.ReplaceWith (parent, list);
374                         parent.itemElement.ParentNode.RemoveChild (parent.itemElement);                 
375                 }
376
377                 static BuildItem InsertElementAfter (BuildItem parent, BuildItem child, XmlElement insertAfter)
378                 {
379                         BuildItem newParent;
380
381                         XmlDocument doc = parent.itemElement.OwnerDocument;
382                         XmlElement newElement = doc.CreateElement (child.Name, Project.XmlNamespace);
383                         newElement.SetAttribute ("Include", child.FinalItemSpec);
384                         if (parent.itemElement.HasAttribute ("Condition"))
385                                 newElement.SetAttribute ("Condition", parent.itemElement.GetAttribute ("Condition"));
386                         foreach (XmlElement xe in parent.itemElement)
387                                 newElement.AppendChild (xe.Clone ());
388                         parent.itemElement.ParentNode.InsertAfter (newElement, insertAfter);
389
390                         newParent = new BuildItem (newElement, parent.parent_item_group);
391                         newParent.child_items.AddItem (child);
392                         child.parent_item = newParent;
393
394                         return newParent;
395                 }
396
397                 public string Condition {
398                         get {
399                                 if (FromXml)
400                                         return itemElement.GetAttribute ("Condition");
401                                 else
402                                         return String.Empty;
403                         }
404                         set {
405                                 if (FromXml)
406                                         itemElement.SetAttribute ("Condition", value);
407                                 else if (!HasParent)
408                                         throw new InvalidOperationException ("Cannot set a condition on an object not represented by an XML element in the project file.");
409                         }
410                 }
411
412                 public string Exclude {
413                         get {
414                                 if (FromXml)
415                                         return itemElement.GetAttribute ("Exclude");
416                                 else
417                                         return String.Empty;
418                         }
419                         set {
420                                 if (FromXml)
421                                         itemElement.SetAttribute ("Exclude", value);
422                                 else
423                                         throw new InvalidOperationException ("Assigning the \"Exclude\" attribute of a virtual item is not allowed.");
424                         }
425                 }
426
427                 public string FinalItemSpec {
428                         get { return finalItemSpec; }
429                 }
430
431                 public string Include {
432                         get {
433                                 if (FromXml)
434                                         return itemElement.GetAttribute ("Include");
435                                 else if (HasParent)
436                                         return parent_item.Include;
437                                 else
438                                         return itemInclude;
439                         }
440                         set {
441                                 if (FromXml)
442                                         itemElement.SetAttribute ("Include", value);
443                                 else if (HasParent) {
444                                         if (parent_item.child_items.Count > 1)
445                                                 SplitParentItem ();
446                                         parent_item.Include = value;
447                                 } else
448                                         itemInclude = value;
449                         }
450                 }
451
452                 public bool IsImported {
453                         get { return isImported; }
454                 }
455
456                 public string Name {
457                         get {
458                                 if (FromXml)
459                                         return itemElement.Name;
460                                 else if (HasParent)
461                                         return parent_item.Name;
462                                 else
463                                         return name;
464                         }
465                         set {
466                                 if (FromXml) {
467                                         XmlElement newElement = itemElement.OwnerDocument.CreateElement (value, Project.XmlNamespace);
468                                         newElement.SetAttribute ("Include", itemElement.GetAttribute ("Include"));
469                                         newElement.SetAttribute ("Condition", itemElement.GetAttribute ("Condition"));
470                                         foreach (XmlElement xe in itemElement)
471                                                 newElement.AppendChild (xe.Clone ());
472                                         itemElement.ParentNode.ReplaceChild (newElement, itemElement);
473                                         itemElement = newElement;
474                                 } else if (HasParent) {
475                                         if (parent_item.child_items.Count > 1)
476                                                 SplitParentItem ();
477                                         parent_item.Name = value;
478                                 } else
479                                         name = value;
480                         }
481                 }
482                 
483                 internal bool FromXml {
484                         get { return itemElement != null; }
485                 }
486                 
487                 internal bool HasParent {
488                         get { return parent_item != null; }
489                 }
490
491                 internal BuildItem ParentItem {
492                         get { return parent_item; }
493                 }
494
495                 internal BuildItemGroup ParentItemGroup {
496                         get { return parent_item_group; }
497                 }
498         }
499 }
500
501 #endif