Improve expression parsing, esp for concat'ing expressions.
[mono.git] / mcs / class / Microsoft.Build.Engine / Microsoft.Build.BuildEngine / ChangeType.cs
1 //
2 // ChangeType.cs: Changes types for output properties or 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.Collections;
32 using System.Collections.Generic;
33 using System.Reflection;
34 using System.Text;
35 using Microsoft.Build.Framework;
36 using Microsoft.Build.Utilities;
37
38 namespace Microsoft.Build.BuildEngine {
39         internal class ChangeType {
40                 //removed Type type
41                 // FIXME throw exception here
42                 static string ToString (object o)
43                 {
44                         string output = null;
45                         Type type = o.GetType ();
46
47                         // FIXME: there are more types
48                         if (type == typeof (string))
49                                 output = (string) o;
50                         else if (type == typeof (bool) ||
51                                  type == typeof (int) ||
52                                  type == typeof (uint) ||
53                                  type == typeof (float) ||
54                                  type == typeof (double) ||
55                                  type == typeof (DateTime)) {
56                                 output = o.ToString ();
57
58                         }
59                         return output;
60                 }
61
62                 static string ToString (object [] o, Type type)
63                 {
64                 /*
65                         ArrayList al = new ArrayList ();
66                         foreach (object obj in o ) {
67                                 if (type == typeof (bool[])) {
68                                         al.Add (TemporaryTransform (obj, typeof (bool)));
69                                 } else if (type == typeof (string[])) {
70                                         al.Add (TemporaryTransform (obj, typeof (string)));
71                                 } else if (type == typeof (DateTime[])) {
72                                         al.Add (TemporaryTransform (obj, typeof (DateTime)));
73                                 } else if (type == typeof (int[])) {
74                                         al.Add (TemporaryTransform (obj, typeof (int)));
75                                 } else if (type == typeof (uint[])) {
76                                         al.Add (TemporaryTransform (obj, typeof (uint)));
77                                 } else
78                                         throw new Exception (String.Format ("Invalid type: {0}", type.ToString ()));
79                         }
80                         string[] output = new string [al.Count];
81                         int i  = 0;
82                         foreach (string s in al)
83                                 output [i++] = s;
84                         return String.Join (";", output);
85                         */
86
87                         List <string> list = new List <string> ();
88                         foreach (object obj in o)
89                                 list.Add (ToString (obj));
90                         return String.Join (";", list.ToArray ());
91                 }
92
93                 public static BuildProperty ToBuildProperty (object o, Type t, string name)
94                 {
95                         if (t == typeof (ITaskItem)) {
96                                 return new BuildProperty (name, ((ITaskItem) o).ItemSpec);
97                         } else if (t ==  typeof (ITaskItem [])) {
98                         // FIXME move Tostring here
99                                 return new BuildProperty (name, ToString ((ITaskItem []) o));
100                         } else if (t.IsArray) {
101                                 return new BuildProperty (name, ToString ((object []) o, t));
102                         } else {
103                                 return new BuildProperty (name, ToString (o));
104                         }
105                 }
106
107                 public static BuildItemGroup ToBuildItemGroup (object o, Type t, string name)
108                 {
109                         BuildItemGroup big = new BuildItemGroup ();
110
111                         if (t == typeof (ITaskItem)) {
112                                 big.AddItem (name, (ITaskItem) o);
113                         } else if (t ==  typeof (ITaskItem [])) {
114                                 foreach (ITaskItem i in (ITaskItem []) o)
115                                         big.AddItem (name, i);
116                         } else if (t.IsArray) {
117                                 return ToBuildItemGroup (name, ToString ((object []) o, t), true);
118                         } else {
119                                 return ToBuildItemGroup (name, ToString (o), false);
120                         }
121
122                         return big;
123                 }
124                 
125                 static BuildItemGroup ToBuildItemGroup (string name, string items, bool split)
126                 {
127                         BuildItemGroup big = new BuildItemGroup ();
128                         if (split) {
129                                 string [] splitItems = items.Split (';');
130                                 foreach (string item in splitItems)
131                                         big.AddItem (name, new TaskItem (item));
132                         } else {
133                                 big.AddItem (name, new TaskItem (items));
134                         }
135
136                         return big;
137                 }
138                 
139                 static string ToString (ITaskItem [] items)
140                 {
141                         string [] text = new string [items.Length];
142                         int i = 0;
143                         foreach (ITaskItem item in items)
144                                 text [i++] = item.ItemSpec;
145                         return String.Join (";", text);
146                 }
147         }
148 }
149
150 #endif