New test.
[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                 XmlAttribute            condition;
42                 bool                    isImported;
43                 List <BuildItem>        buildItems;
44                 GroupingCollection      parentCollection;
45                 Project                 parentProject;
46                 XmlElement              itemGroupElement;
47
48                 public BuildItemGroup ()
49                         : this (null, null)
50                 {
51                 }
52                 
53                 internal BuildItemGroup (XmlElement xmlElement, Project project)
54                 {
55                         this.itemGroupElement = xmlElement;
56                         this.buildItems = new List <BuildItem> ();
57                         this.isImported = false;
58                         this.parentProject = project;
59                         
60                         if (!FromXml)
61                                 return;
62
63                         this.condition = xmlElement.GetAttributeNode ("Condition");
64                         foreach (XmlNode xn in xmlElement.ChildNodes) {
65                                 if (xn is XmlElement == false)
66                                         continue;
67                                         
68                                 XmlElement xe = (XmlElement) xn;
69                                 BuildItem bi = new BuildItem (xe, this);
70                                 buildItems.Add (bi);
71                         }
72                 }
73
74                 internal void Evaluate ()
75                 {
76                         foreach (BuildItem bi in buildItems) {
77                                 if (bi.Condition == String.Empty)
78                                         bi.Evaluate ();
79                                 else {
80                                         ConditionExpression ce = ConditionParser.ParseCondition (bi.Condition);
81                                         if (ce.BoolEvaluate (parentProject))
82                                                 bi.Evaluate ();
83                                 }
84                         }
85                 }
86
87                 public BuildItem AddNewItem (string itemName,
88                                              string itemInclude)
89                 {
90                         return AddNewItem (itemName, itemInclude, false);
91                 }
92                 
93                 // FIXME: use expression
94                 [MonoTODO]
95                 public BuildItem AddNewItem (string itemName,
96                                              string itemInclude,
97                                              bool treatItemIncludeAsLiteral)
98                 {
99                         BuildItem bi = new BuildItem (itemName, itemInclude);
100                         buildItems.Add (bi);
101                         return bi;
102                 }
103                 
104                 internal void AddItem (BuildItem buildItem)
105                 {
106                         buildItems.Add (buildItem);
107                 }
108                 
109                 internal void AddItem (string name, ITaskItem taskItem)
110                 {
111                         BuildItem buildItem;
112                         buildItem = new BuildItem (name, taskItem);
113                         buildItems.Add (buildItem);
114                 }
115
116                 [MonoTODO]
117                 public void Clear ()
118                 {
119                         //FIXME: should this remove all build items?
120                         buildItems = new List <BuildItem> ();
121                 }
122
123                 [MonoTODO]
124                 public BuildItemGroup Clone (bool deepClone)
125                 {
126                         BuildItemGroup big = new BuildItemGroup ();
127                         // FIXME: add copying of items
128                         return big;
129                 }
130
131                 public IEnumerator GetEnumerator ()
132                 {
133                         return buildItems.GetEnumerator ();
134                 }
135
136                 [MonoTODO]
137                 public void RemoveItem (BuildItem itemToRemove)
138                 {
139                         buildItems.Remove (itemToRemove);
140                 }
141
142                 [MonoTODO]
143                 public void RemoveItemAt (int index)
144                 {
145                         buildItems.RemoveAt (index);
146                 }
147
148                 public BuildItem[] ToArray ()
149                 {
150                         return buildItems.ToArray ();
151                 }
152                 
153                 internal string ConvertToString (OldExpression transform,
154                                                  OldExpression separator)
155                 {
156                         string separatorString;
157                         
158                         if (separator == null)
159                                 separatorString = ";";
160                         else
161                                 separatorString = (string) separator.ConvertTo (typeof (string));
162                 
163                         string[] items = new string [buildItems.Count];
164                         int i = 0;
165                         foreach (BuildItem bi in  buildItems)
166                                 items [i++] = bi.ConvertToString (transform);
167                         return String.Join (separatorString, items);
168                 }
169                 
170                 internal ITaskItem[] ConvertToITaskItemArray (OldExpression transform)
171                 {
172                         ITaskItem[] array = new ITaskItem [buildItems.Count];
173                         int i = 0;
174                         foreach (BuildItem item in buildItems)
175                                 array [i++] = item.ConvertToITaskItem (transform);
176                         return array;
177                 }
178
179                 public string Condition {
180                         get {
181                                 if (condition != null)
182                                         return condition.Value;
183                                 else
184                                         return String.Empty;
185                         }
186                         set {
187                                 if (condition != null)
188                                         condition.Value = value;
189                         }
190                 }
191
192                 public int Count {
193                         get {
194                                 if (buildItems != null)
195                                         return buildItems.Count;
196                                 else
197                                         return 0;
198                         }
199                 }
200
201                 public bool IsImported {
202                         get {
203                                 return isImported;
204                         }
205                 }
206
207                 public BuildItem this [int index] {
208                         get {
209                                 return buildItems [index];
210                         }
211                 }
212                 
213                 internal GroupingCollection GroupingCollection {
214                         get { return parentCollection; }
215                         set { parentCollection = value; }
216                 }
217                 
218                 internal Project Project {
219                         get { return parentProject; }
220                 }
221
222                 internal bool FromXml {
223                         get {
224                                 return itemGroupElement != null;
225                         }
226                 }
227         }
228 }
229
230 #endif