fixed tests
[mono.git] / mcs / class / Microsoft.Build.Engine / Microsoft.Build.BuildEngine / ExpressionCollection.cs
1 //
2 // ExpressionCollection.cs
3 //
4 // Author:
5 //   Marek Sieradzki (marek.sieradzki@gmail.com)
6 // 
7 // (C) 2006 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.Text;
34 using Microsoft.Build.Framework;
35 using Microsoft.Build.Utilities;
36
37 namespace Microsoft.Build.BuildEngine {
38
39         internal class ExpressionCollection {
40         
41                 IList objects;
42         
43                 public ExpressionCollection ()
44                 {
45                         objects = new ArrayList ();
46                 }
47                 
48                 public void Add (ItemReference itemReference)
49                 {
50                         objects.Add (itemReference);
51                 }
52                 
53                 public void Add (MetadataReference metadataReference)
54                 {
55                         objects.Add (metadataReference);
56                 }
57                 
58                 public void Add (PropertyReference propertyReference)
59                 {
60                         objects.Add (propertyReference);
61                 }
62                 
63                 public void Add (string s)
64                 {
65                         objects.Add (s);
66                 }
67                 
68                 public object ConvertTo (Project project, Type type)
69                 {
70                         if (type.IsArray) {
71                                 if (type == typeof (ITaskItem[]))
72                                         return ConvertToITaskItemArray (project);
73                                 else
74                                         return ConvertToArray (project, type);
75                         } else {
76                                 if (type == typeof (ITaskItem))
77                                         return ConvertToITaskItem (project);
78                                 else
79                                         return ConvertToNonArray (project, type);
80                         }
81                 }
82                 
83                 public IEnumerator GetEnumerator ()
84                 {
85                         foreach (object o in objects)
86                                 yield return o;
87                 }
88                 
89                 object ConvertToNonArray (Project project, Type type)
90                 {
91                         return ConvertToObject (ConvertToString (project), type);
92                 }
93
94                 object ConvertToArray (Project project, Type type)
95                 {
96                         string[] rawTable = ConvertToString (project).Split (';');
97                         int i = 0;
98                         
99                         if (type == typeof (bool[])) {
100                                 bool[] array = new bool [rawTable.Length];
101                                 foreach (string raw in rawTable)
102                                         array [i++] = (bool) ConvertToObject (raw, type.GetElementType ());
103                                 return array;
104                         } else if (type == typeof (string[])) {
105                                 string[] array = new string [rawTable.Length];
106                                 foreach (string raw in rawTable)
107                                         array [i++] = (string) ConvertToObject (raw, type.GetElementType ());
108                                 return array;
109                         } else if (type == typeof (int[])) {
110                                 int[] array = new int [rawTable.Length];
111                                 foreach (string raw in rawTable)
112                                         array [i++] = (int) ConvertToObject (raw, type.GetElementType ());
113                                 return array;
114                         } else if (type == typeof (uint[])) {
115                                 uint[] array = new uint [rawTable.Length];
116                                 foreach (string raw in rawTable)
117                                         array [i++] = (uint) ConvertToObject (raw, type.GetElementType ());
118                                 return array;
119                         } else if (type == typeof (DateTime[])) {
120                                 DateTime[] array = new DateTime [rawTable.Length];
121                                 foreach (string raw in rawTable)
122                                         array [i++] = (DateTime) ConvertToObject (raw, type.GetElementType ());
123                                 return array;
124                         } else
125                                 throw new Exception ("Invalid type");
126                 }
127                 
128                 object ConvertToObject (string raw, Type type)
129                 {
130                         if (type == typeof (bool)) {
131                                 return Boolean.Parse (raw);
132                         } else if (type == typeof (string)) {
133                                 return raw;
134                         } else if (type == typeof (int)) {
135                                 return Int32.Parse (raw);
136                         } else if (type == typeof (uint)) {
137                                 return UInt32.Parse (raw);
138                         } else if (type == typeof (DateTime)) {
139                                 return DateTime.Parse (raw);
140                         } else {
141                                 throw new Exception (String.Format ("Unknown type: {0}", type.ToString ()));
142                         }
143                 }
144                 string ConvertToString (Project project)
145                 {
146                         StringBuilder sb = new StringBuilder ();
147                         
148                         foreach (object o in objects) {
149                                 if (o is string) {
150                                         sb.Append ((string) o);
151                                 } else if (o is ItemReference) {
152                                         ItemReference ir = (ItemReference) o;
153                                         sb.Append (ir.ConvertToString (project));
154                                 } else if (o is PropertyReference) {
155                                         PropertyReference pr = (PropertyReference) o;
156                                         sb.Append (pr.ConvertToString (project));
157                                 } else if (o is MetadataReference) {
158                                         // FIXME: we don't handle them yet
159                                 } else {
160                                         throw new Exception ("Invalid type in objects collection.");
161                                 }
162                         }
163                         return sb.ToString ();
164                 }
165
166                 ITaskItem ConvertToITaskItem (Project project)
167                 {
168                         ITaskItem item;
169                         
170                         if (objects == null)
171                                 throw new Exception ("Cannot cast empty expression to ITaskItem.");
172                         
173                         if (objects [0] is ItemReference) {
174                                 ItemReference ir = (ItemReference) objects [0];
175                                 ITaskItem[] array = ir.ConvertToITaskItemArray (project);
176                                 if (array.Length == 1) {
177                                         return array [0];
178                                 } else {
179                                         throw new Exception ("TaskItem array too long");
180                                 }
181                         } else {
182                                 item = new TaskItem (ConvertToString (project));
183                                 return item;
184                         }
185                 }
186                 
187                 ITaskItem[] ConvertToITaskItemArray (Project project)
188                 {
189                         List <ITaskItem> finalItems = new List <ITaskItem> ();
190                         ArrayList tempItems = new ArrayList ();
191                         ITaskItem[] array;
192                         
193                         foreach (object o in objects) {
194                                 if (o is ItemReference) {
195                                         tempItems.Add (o);
196                                 } else if (o is PropertyReference) {
197                                         PropertyReference pr = (PropertyReference) o;
198                                         tempItems.Add (pr.ConvertToString (project));
199                                 } else if (o is MetadataReference) {
200                                         // FIXME: not handled yet
201                                 } else if (o is string) {
202                                         tempItems.Add (o);
203                                 } else {
204                                         throw new Exception ("Invalid type in objects collection.");
205                                 }
206                         }
207                         foreach (object o in tempItems) {
208                                 if (o is ItemReference) {
209                                         ItemReference ir = (ItemReference) o;
210                                         array = ir.ConvertToITaskItemArray (project);
211                                         if (array != null)
212                                                 finalItems.AddRange (array);
213                                 } else if (o is string) {
214                                         string s = (string) o;
215                                         array = ConvertToITaskItemArrayFromString (project, s);
216                                         finalItems.AddRange (array);
217                                 } else {
218                                         throw new Exception ("Invalid type in tempItems collection.");
219                                 }
220                         }
221                         
222                         return finalItems.ToArray ();
223                 }
224                 
225                 ITaskItem [] ConvertToITaskItemArrayFromString (Project project, string source)
226                 {
227                         List <ITaskItem> items = new List <ITaskItem> ();
228                         string [] splitSource = source.Split (';');
229                         foreach (string s in splitSource) {
230                                 if (s != String.Empty) {
231                                         items.Add (new TaskItem (s));
232                                 }
233                         }
234                         return items.ToArray ();
235                 }
236         }
237 }
238
239 #endif