2007-02-03 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.Reflection;
33 using System.Text;
34 using Microsoft.Build.Framework;
35 using Microsoft.Build.Utilities;
36
37 namespace Microsoft.Build.BuildEngine {
38         internal class ChangeType {
39         
40                 static string TemporaryTransform (object o, Type type)
41                 {
42                         string output = null;
43
44                         if (type == typeof (string))
45                                 output = (string) o;
46                         else if (type == typeof (bool) ||
47                                  type == typeof (int) ||
48                                  type == typeof (uint) ||
49                                  type == typeof (float) ||
50                                  type == typeof (double) ||
51                                  type == typeof (DateTime)) {
52                                 output = o.ToString ();
53
54                         }
55                         /*if (type == typeof (bool)) {
56                                 bool t = (bool) o;
57                                 output =  t.ToString (); 
58                         } else if (type == typeof (string)) {
59                                 string t = (string) o;
60                                 output =  t.ToString ();
61                         } else if (type == typeof (DateTime)) {
62                                 DateTime t = (DateTime) o;
63                                 output =  t.ToString ();
64                         } else if (type == typeof (int)) {
65                                 int t = (int) o;
66                                 output =  t.ToString ();
67                         } else if (type == typeof (uint)) {
68                                 uint t = (uint) o;
69                                 output =  t.ToString ();
70                         }*/
71                         return output;
72                 }
73                 
74                 public static string TransformToString (object o, Type type)
75                 {
76                         return TemporaryTransform (o, type);
77                 }
78                 
79                 public static string TransformToString (object[] o, Type type)
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                 public static BuildProperty TransformToBuildProperty (string name, string items)
104                 {
105                         return new BuildProperty (name, items);
106                 }
107                 
108                 public static BuildProperty TransformToBuildProperty (string name, ITaskItem[] items)
109                 {
110                         BuildProperty buildProperty;
111                         buildProperty = new BuildProperty (name, TransformToString (items));
112                         return buildProperty;
113                 }
114                 
115                 public static BuildProperty TransformToBuildProperty (string name, ITaskItem item)
116                 {
117                         BuildProperty buildProperty;
118                         buildProperty = new BuildProperty (name, TransformToString (item));
119                         return buildProperty;
120                 }
121                 
122                 public static BuildItemGroup TransformToBuildItemGroup (string name, string items)
123                 {
124                         string[] splittedItems = items.Split (';');
125                         BuildItemGroup big = new BuildItemGroup ();
126                         foreach (string item in splittedItems)
127                                 big.AddItem (name, new TaskItem (item));
128                         return big;
129                 }
130                 
131                 public static BuildItemGroup TransformToBuildItemGroup (string name, ITaskItem[] items)
132                 {
133                         BuildItemGroup big = new BuildItemGroup ();
134                         foreach (ITaskItem item in items) {
135                                 big.AddItem (name, item);
136                         }
137                         return big;
138                 }
139                 
140                 public static BuildItemGroup TransformToBuildItemGroup (string name, ITaskItem item)
141                 {
142                         BuildItemGroup big = new BuildItemGroup ();
143                         big.AddItem (name, item);
144                         return big;
145                 }
146                 
147                 static string TransformToString (ITaskItem [] items)
148                 {
149                         string[] text = new string [items.Length];
150                         int i = 0;
151                         foreach (ITaskItem item in items)
152                                 text [i++] = item.ItemSpec;
153                         return String.Join (";", text);
154                 }
155                 
156                 static string TransformToString (ITaskItem item)
157                 {
158                         return item.ItemSpec;
159                 }
160         }
161 }
162
163 #endif