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