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