New test.
[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                 private static string TemporaryTransform (object o, Type type)
41                 {
42                         string output = String.Empty;
43                         if (type == typeof (bool)) {
44                                 bool t = (bool) o;
45                                 output =  t.ToString (); 
46                         } else if (type == typeof (string)) {
47                                 string t = (string) o;
48                                 output =  t.ToString ();
49                         } else if (type == typeof (DateTime)) {
50                                 DateTime t = (DateTime) o;
51                                 output =  t.ToString ();
52                         } else if (type == typeof (int)) {
53                                 int t = (int) o;
54                                 output =  t.ToString ();
55                         } else if (type == typeof (uint)) {
56                                 uint t = (uint) o;
57                                 output =  t.ToString ();
58                         } else {
59                         }
60                         return output;
61                 }
62                 
63                 public static string TransformToString (object o, Type type)
64                 {
65                         return TemporaryTransform (o, type);
66                 }
67                 
68                 public static string TransformToString (object[] o, Type type)
69                 {
70                         ArrayList al = new ArrayList ();
71                         foreach (object obj in o ) {
72                                 if (type == typeof (bool[])) {
73                                         al.Add (TemporaryTransform (obj, typeof (bool)));
74                                 } else if (type == typeof (string[])) {
75                                         al.Add (TemporaryTransform (obj, typeof (string)));
76                                 } else if (type == typeof (DateTime[])) {
77                                         al.Add (TemporaryTransform (obj, typeof (DateTime)));
78                                 } else if (type == typeof (int[])) {
79                                         al.Add (TemporaryTransform (obj, typeof (int)));
80                                 } else if (type == typeof (uint[])) {
81                                         al.Add (TemporaryTransform (obj, typeof (uint)));
82                                 } else
83                                         throw new Exception (String.Format ("Invalid type: {0}", type.ToString ()));
84                         }
85                         string[] output = new string [al.Count];
86                         int i  = 0;
87                         foreach (string s in al)
88                                 output [i++] = s;
89                         return String.Join (";", output);
90                 }
91                 
92                 public static BuildProperty TransformToBuildProperty (string name, string items)
93                 {
94                         return new BuildProperty (name, items);
95                 }
96                 
97                 public static BuildProperty TransformToBuildProperty (string name, ITaskItem[] items)
98                 {
99                         BuildProperty buildProperty;
100                         buildProperty = new BuildProperty (name, TransformToString (items));
101                         return buildProperty;
102                 }
103                 
104                 public static BuildProperty TransformToBuildProperty (string name, ITaskItem item)
105                 {
106                         BuildProperty buildProperty;
107                         buildProperty = new BuildProperty (name, TransformToString (item));
108                         return buildProperty;
109                 }
110                 
111                 public static BuildItemGroup TransformToBuildItemGroup (string name, string items)
112                 {
113                         string[] splittedItems = items.Split (';');
114                         BuildItemGroup big = new BuildItemGroup ();
115                         foreach (string item in splittedItems)
116                                 big.AddItem (name, new TaskItem (item));
117                         return big;
118                 }
119                 
120                 public static BuildItemGroup TransformToBuildItemGroup (string name, ITaskItem[] items)
121                 {
122                         BuildItemGroup big = new BuildItemGroup ();
123                         foreach (ITaskItem item in items) {
124                                 big.AddItem (name, item);
125                         }
126                         return big;
127                 }
128                 
129                 public static BuildItemGroup TransformToBuildItemGroup (string name, ITaskItem item)
130                 {
131                         BuildItemGroup big = new BuildItemGroup ();
132                         big.AddItem (name, item);
133                         return big;
134                 }
135                 
136                 private static string TransformToString (ITaskItem[] items)
137                 {
138                         string[] text = new string [items.Length];
139                         int i = 0;
140                         foreach (ITaskItem item in items)
141                                 text [i++] = item.ItemSpec;
142                         return String.Join (";", text);
143                 }
144                 
145                 private static string TransformToString (ITaskItem item)
146                 {
147                         return item.ItemSpec;
148                 }
149         }
150 }
151
152 #endif