2007-01-14 Marek Sieradzki <marek.sieradzki@gmail.com>
[mono.git] / mcs / class / Microsoft.Build.Engine / Microsoft.Build.BuildEngine / BuildItemGroup.cs
1 //
2 // BuildItemGroup.cs: Represents a group of build items.
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.Reflection;
32 using System.Collections;
33 using System.Collections.Generic;
34 using System.Xml;
35 using Microsoft.Build.Framework;
36 using Microsoft.Build.Utilities;
37
38 namespace Microsoft.Build.BuildEngine {
39         public class BuildItemGroup : IEnumerable {
40         
41                 List <BuildItem>        buildItems;
42                 ImportedProject         importedProject;
43                 XmlElement              itemGroupElement;
44                 GroupingCollection      parentCollection;
45                 Project                 parentProject;
46                 bool                    read_only;
47
48                 public BuildItemGroup ()
49                         : this (null, null, null, false)
50                 {
51                 }
52                 
53                 internal BuildItemGroup (XmlElement xmlElement, Project project, ImportedProject importedProject, bool readOnly)
54                 {
55                         this.buildItems = new List <BuildItem> ();
56                         this.importedProject = importedProject;
57                         this.itemGroupElement = xmlElement;
58                         this.parentProject = project;
59                         this.read_only = readOnly;
60                         
61                         if (!FromXml)
62                                 return;
63
64                         foreach (XmlNode xn in xmlElement.ChildNodes) {
65                                 if (!(xn is XmlElement))
66                                         continue;
67                                         
68                                 XmlElement xe = (XmlElement) xn;
69                                 BuildItem bi = new BuildItem (xe, this);
70                                 buildItems.Add (bi);
71                         }
72                 }
73
74                 public BuildItem AddNewItem (string itemName,
75                                              string itemInclude)
76                 {
77                         return AddNewItem (itemName, itemInclude, false);
78                 }
79                 
80                 [MonoTODO]
81                 public BuildItem AddNewItem (string itemName,
82                                              string itemInclude,
83                                              bool treatItemIncludeAsLiteral)
84                 {
85                         if (treatItemIncludeAsLiteral)
86                                 itemInclude = Utilities.Escape (itemInclude);
87
88                         BuildItem bi = new BuildItem (itemName, itemInclude);
89
90                         bi.Evaluate (null, true);
91
92                         if (!read_only)
93                                 buildItems.Add (bi);
94
95                         return bi;
96                 }
97                 
98                 public void Clear ()
99                 {
100                         if (FromXml)
101                                 itemGroupElement.RemoveAll ();
102                         
103                         buildItems = new List <BuildItem> ();
104
105                         if (parentProject != null) {
106                                 parentProject.MarkProjectAsDirty ();
107                                 parentProject.NeedToReevaluate ();
108                         }
109                 }
110
111                 [MonoTODO]
112                 public BuildItemGroup Clone (bool deepClone)
113                 {
114                         if (deepClone) {
115                                 if (FromXml)
116                                         throw new NotImplementedException ();
117                                 else
118                                         throw new NotImplementedException ();
119                         } else {
120                                 if (FromXml)
121                                         throw new InvalidOperationException ("A shallow clone of this object cannot be created.");
122                                 else
123                                         throw new NotImplementedException ();
124                         }
125                 }
126
127                 public IEnumerator GetEnumerator ()
128                 {
129                         return buildItems.GetEnumerator ();
130                 }
131
132                 public void RemoveItem (BuildItem itemToRemove)
133                 {
134                         if (itemToRemove == null)
135                                 return;
136
137                         itemToRemove.Detach ();
138
139                         buildItems.Remove (itemToRemove);
140                 }
141
142                 public void RemoveItemAt (int index)
143                 {
144                         BuildItem item = buildItems [index];
145
146                         RemoveItem (item);
147                 }
148
149                 public BuildItem[] ToArray ()
150                 {
151                         return buildItems.ToArray ();
152                 }
153
154                 internal void AddItem (BuildItem buildItem)
155                 {
156                         buildItems.Add (buildItem);
157                 }
158
159                 internal void AddItem (string name, ITaskItem taskItem)
160                 {
161                         BuildItem buildItem;
162                         buildItem = new BuildItem (name, taskItem);
163                         buildItems.Add (buildItem);
164                 }
165
166                 internal string ConvertToString (Expression transform,
167                                                  Expression separator)
168                 {
169                         string separatorString;
170                         
171                         if (separator == null)
172                                 separatorString = ";";
173                         else
174                                 separatorString = (string) separator.ConvertTo (parentProject, typeof (string));
175                 
176                         string[] items = new string [buildItems.Count];
177                         int i = 0;
178                         foreach (BuildItem bi in  buildItems)
179                                 items [i++] = bi.ConvertToString (transform);
180                         return String.Join (separatorString, items);
181                 }
182
183                 internal ITaskItem[] ConvertToITaskItemArray (Expression transform)
184                 {
185                         ITaskItem[] array = new ITaskItem [buildItems.Count];
186                         int i = 0;
187                         foreach (BuildItem item in buildItems)
188                                 array [i++] = item.ConvertToITaskItem (transform);
189                         return array;
190                 }
191
192                 internal void Detach ()
193                 {
194                         if (!FromXml)
195                                 throw new InvalidOperationException ();
196
197                         itemGroupElement.ParentNode.RemoveChild (itemGroupElement);
198                 }
199
200                 internal void Evaluate ()
201                 {
202                         foreach (BuildItem bi in buildItems) {
203                                 if (bi.Condition == String.Empty)
204                                         bi.Evaluate (parentProject, true);
205                                 else {
206                                         ConditionExpression ce = ConditionParser.ParseCondition (bi.Condition);
207                                         bi.Evaluate (parentProject, ce.BoolEvaluate (parentProject));
208                                 }
209                         }
210                 }               
211
212                 internal void ReplaceWith (BuildItem item, List <BuildItem> list)
213                 {
214                         int index = buildItems.IndexOf (item);
215                         buildItems.RemoveAt (index);
216                         buildItems.InsertRange (index, list);
217                 }
218                 
219                 public string Condition {
220                         get {
221                                 if (FromXml)
222                                         return itemGroupElement.GetAttribute ("Condition");
223                                 else
224                                         return String.Empty;
225                         }
226                         set {
227                                 if (FromXml)
228                                         itemGroupElement.SetAttribute ("Condition", value);
229                                 else
230                                         throw new InvalidOperationException ("Cannot set a condition on an object not represented by an XML element in the project file.");
231                         }
232                 }
233
234                 public int Count {
235                         get { return buildItems.Count; }
236                 }
237
238                 public bool IsImported {
239                         get { return importedProject != null; }
240                 }
241
242                 
243                 [MonoTODO]
244                 public BuildItem this [int index] {
245                         get {
246                                 return buildItems [index];
247                         }
248                 }
249                 
250                 internal GroupingCollection GroupingCollection {
251                         get { return parentCollection; }
252                         set { parentCollection = value; }
253                 }
254                 
255                 internal Project Project {
256                         get { return parentProject; }
257                 }
258
259                 internal bool FromXml {
260                         get {
261                                 return itemGroupElement != null;
262                         }
263                 }
264
265                 internal XmlElement XmlElement {
266                         get {
267                                 return itemGroupElement;
268                         }       
269                 }
270         }
271 }
272
273 #endif