2007-03-05 Marek Sieradzki <marek.sieradzki@gmail.com>
[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                         /*if (type == typeof (bool)) {
60                                 bool t = (bool) o;
61                                 output =  t.ToString (); 
62                         } else if (type == typeof (string)) {
63                                 string t = (string) o;
64                                 output =  t.ToString ();
65                         } else if (type == typeof (DateTime)) {
66                                 DateTime t = (DateTime) o;
67                                 output =  t.ToString ();
68                         } else if (type == typeof (int)) {
69                                 int t = (int) o;
70                                 output =  t.ToString ();
71                         } else if (type == typeof (uint)) {
72                                 uint t = (uint) o;
73                                 output =  t.ToString ();
74                         }*/
75                         return output;
76                 }
77
78                 static string ToString (object [] o, Type type)
79                 {
80                 /*
81                         ArrayList al = new ArrayList ();
82                         foreach (object obj in o ) {
83                                 if (type == typeof (bool[])) {
84                                         al.Add (TemporaryTransform (obj, typeof (bool)));
85                                 } else if (type == typeof (string[])) {
86                                         al.Add (TemporaryTransform (obj, typeof (string)));
87                                 } else if (type == typeof (DateTime[])) {
88                                         al.Add (TemporaryTransform (obj, typeof (DateTime)));
89                                 } else if (type == typeof (int[])) {
90                                         al.Add (TemporaryTransform (obj, typeof (int)));
91                                 } else if (type == typeof (uint[])) {
92                                         al.Add (TemporaryTransform (obj, typeof (uint)));
93                                 } else
94                                         throw new Exception (String.Format ("Invalid type: {0}", type.ToString ()));
95                         }
96                         string[] output = new string [al.Count];
97                         int i  = 0;
98                         foreach (string s in al)
99                                 output [i++] = s;
100                         return String.Join (";", output);
101                         */
102
103                         List <string> list = new List <string> ();
104                         foreach (object obj in o)
105                                 list.Add (ToString (obj));
106                         return String.Join (";", list.ToArray ());
107                 }
108
109                 public static BuildProperty ToBuildProperty (object o, Type t, string name)
110                 {
111                         if (t == typeof (ITaskItem)) {
112                                 return new BuildProperty (name, ((ITaskItem) o).ItemSpec);
113                         } else if (t ==  typeof (ITaskItem [])) {
114                         // FIXME move Tostring here
115                                 return new BuildProperty (name, ToString ((ITaskItem []) o));
116                         } else if (t.IsArray) {
117                                 return new BuildProperty (name, ToString ((object []) o, t));
118                         } else {
119                                 return new BuildProperty (name, ToString (o));
120                         }
121                 }
122
123                 public static BuildItemGroup ToBuildItemGroup (object o, Type t, string name)
124                 {
125                         BuildItemGroup big = new BuildItemGroup ();
126
127                         if (t == typeof (ITaskItem)) {
128                                 big.AddItem (name, (ITaskItem) o);
129                         } else if (t ==  typeof (ITaskItem [])) {
130                                 foreach (ITaskItem i in (ITaskItem []) o)
131                                         big.AddItem (name, i);
132                         } else if (t.IsArray) {
133                                 return ToBuildItemGroup (name, ToString ((object []) o, t));
134                         } else {
135                                 return ToBuildItemGroup (name, ToString (o));
136                         }
137
138                         return big;
139                 }
140                 
141                 static BuildItemGroup ToBuildItemGroup (string name, string items)
142                 {
143                         string [] splitItems = items.Split (';');
144                         BuildItemGroup big = new BuildItemGroup ();
145                         foreach (string item in splitItems)
146                                 big.AddItem (name, new TaskItem (item));
147                         return big;
148                 }
149                 
150                 static string ToString (ITaskItem [] items)
151                 {
152                         string [] text = new string [items.Length];
153                         int i = 0;
154                         foreach (ITaskItem item in items)
155                                 text [i++] = item.ItemSpec;
156                         return String.Join (";", text);
157                 }
158         }
159 }
160
161 #endif