2 // ExpressionCollections.cs
5 // Marek Sieradzki (marek.sieradzki@gmail.com)
7 // (C) 2006 Marek Sieradzki
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
31 using System.Collections;
33 using Microsoft.Build.Framework;
34 using Microsoft.Build.Utilities;
36 namespace Microsoft.Build.BuildEngine {
38 internal class ExpressionCollection {
42 public ExpressionCollection ()
44 objects = new ArrayList ();
47 public void Add (ItemReference itemReference)
49 objects.Add (itemReference);
52 public void Add (MetadataReference metadataReference)
54 objects.Add (metadataReference);
57 public void Add (PropertyReference propertyReference)
59 objects.Add (propertyReference);
62 public void Add (string s)
67 public object ConvertTo (Type type)
69 if (type.IsArray == true) {
70 if (type == typeof (ITaskItem[]))
71 return ConvertToITaskItemArray ();
73 return ConvertToArray (type);
75 if (type == typeof (ITaskItem))
76 return ConvertToITaskItem ();
78 return ConvertToNonArray (type);
82 public IEnumerator GetEnumerator ()
84 foreach (object o in objects)
88 object ConvertToNonArray (Type type)
90 return ConvertToObject (ConvertToString (), type);
93 object ConvertToArray (Type type)
95 string[] rawTable = ToString ().Split (';');
98 if (type == typeof (bool[]) ||
99 type == typeof (string[]) ||
100 type == typeof (int[]) ||
101 type == typeof (uint[]) ||
102 type == typeof (DateTime[])) {
104 object[] array = new object [rawTable.Length];
105 foreach (string raw in rawTable)
106 array [i++] = ConvertToObject (raw, type.GetElementType ());
109 } else throw new Exception ("Invalid type.");
112 object ConvertToObject (string raw, Type type)
114 if (type == typeof (bool)) {
115 return Boolean.Parse (raw);
116 } else if (type == typeof (string)) {
118 } else if (type == typeof (int)) {
119 return Int32.Parse (raw);
120 } else if (type == typeof (uint)) {
121 return UInt32.Parse (raw);
122 } else if (type == typeof (DateTime)) {
123 return DateTime.Parse (raw);
125 throw new Exception (String.Format ("Unknown type: {0}", type.ToString ()));
129 string ConvertToString ()
131 StringBuilder sb = new StringBuilder ();
133 foreach (object o in objects) {
135 sb.Append ((string) o);
136 } else if (o is ItemReference) {
137 sb.Append (((ItemReference)o).ConvertToString ());
138 } else if (o is PropertyReference) {
139 sb.Append (((PropertyReference)o).ConvertToString ());
140 } else if (o is MetadataReference) {
141 // FIXME: we don't handle them yet
143 throw new Exception ("Invalid type in objects collection.");
146 return sb.ToString ();
149 ITaskItem ConvertToITaskItem ()
154 throw new Exception ("Cannot cast empty expression to ITaskItem.");
156 if (objects [0] is ItemReference) {
157 ItemReference ir = (ItemReference) objects [0];
158 ITaskItem[] array = ir.ConvertToITaskItemArray ();
159 if (array.Length == 1) {
162 throw new Exception ("TaskItem array too long");
165 item = new TaskItem (ConvertToString ());
170 ITaskItem[] ConvertToITaskItemArray ()
172 ArrayList finalItems = new ArrayList ();
173 ArrayList tempItems = new ArrayList ();
175 ITaskItem[] finalArray;
177 foreach (object o in objects) {
178 if (o is ItemReference) {
180 } else if (o is PropertyReference) {
181 PropertyReference pr = (PropertyReference) o;
182 tempItems.Add (pr.ConvertToString ());
183 } else if (o is MetadataReference) {
184 // FIXME: not handled yet
185 } else if (o is string) {
188 throw new Exception ("Invalid type in objects collection.");
191 foreach (object o in tempItems) {
192 if (o is ItemReference) {
193 ItemReference ir = (ItemReference) o;
194 array = ir.ConvertToITaskItemArray ();
196 foreach (ITaskItem item in array)
197 finalItems.Add (item);
198 } else if (o is string) {
199 string s = (string) o;
200 array = ConvertToITaskItemArrayFromString (s);
201 foreach (ITaskItem item in array)
202 finalItems.Add (item);
204 throw new Exception ("Invalid type in tempItems collection.");
208 finalArray = new ITaskItem [finalItems.Count];
210 foreach (ITaskItem item in finalItems)
211 finalArray [i++] = item;
215 ITaskItem[] ConvertToITaskItemArrayFromString (string source)
217 ArrayList tempItems = new ArrayList ();
218 ITaskItem[] finalArray;
219 string[] splittedSource = source.Split (';');
220 foreach (string s in splittedSource) {
221 if (s != String.Empty) {
222 tempItems.Add (new TaskItem (s));
225 finalArray = new ITaskItem [tempItems.Count];
227 foreach (ITaskItem item in tempItems)
228 finalArray [i++] = item;