2 // ExpressionCollection.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;
32 using System.Collections.Generic;
34 using Microsoft.Build.Framework;
35 using Microsoft.Build.Utilities;
37 namespace Microsoft.Build.BuildEngine {
39 internal class ExpressionCollection {
43 public ExpressionCollection ()
45 objects = new ArrayList ();
49 get { return objects.Count; }
52 public void Add (ItemReference itemReference)
54 objects.Add (itemReference);
57 public void Add (MetadataReference metadataReference)
59 objects.Add (metadataReference);
62 public void Add (PropertyReference propertyReference)
64 objects.Add (propertyReference);
67 public void Add (string s)
72 public object ConvertTo (Project project, Type type)
75 if (type == typeof (ITaskItem[]))
76 return ConvertToITaskItemArray (project);
78 return ConvertToArray (project, type);
80 if (type == typeof (ITaskItem))
81 return ConvertToITaskItem (project);
83 return ConvertToNonArray (project, type);
87 public IEnumerator GetEnumerator ()
89 foreach (object o in objects)
93 object ConvertToNonArray (Project project, Type type)
95 return ConvertToObject (ConvertToString (project), type);
98 object ConvertToArray (Project project, Type type)
100 string [] rawTable = ConvertToString (project).Split (';');
102 Array arr = Array.CreateInstance (type.GetElementType (), rawTable.Length);
103 for (int i = 0; i < arr.Length; i++)
104 arr.SetValue (ConvertToObject (rawTable [i], type.GetElementType ()), i);
108 object ConvertToObject (string raw, Type type)
110 if (type == typeof (bool))
111 return Boolean.Parse (raw);
112 else if (type == typeof (string))
114 else if (type == typeof (int))
115 return Int32.Parse (raw);
116 else if (type == typeof (uint))
117 return UInt32.Parse (raw);
118 else if (type == typeof (DateTime))
119 return DateTime.Parse (raw);
121 throw new Exception (String.Format ("Unknown type: {0}", type.ToString ()));
123 string ConvertToString (Project project)
125 StringBuilder sb = new StringBuilder ();
127 foreach (object o in objects) {
129 sb.Append ((string) o);
130 } else if (o is ItemReference) {
131 ItemReference ir = (ItemReference) o;
132 sb.Append (ir.ConvertToString (project));
133 } else if (o is PropertyReference) {
134 PropertyReference pr = (PropertyReference) o;
135 sb.Append (pr.ConvertToString (project));
136 } else if (o is MetadataReference) {
137 MetadataReference mr = (MetadataReference) o;
138 sb.Append (mr.ConvertToString (project));
140 throw new Exception ("Invalid type in objects collection.");
143 return sb.ToString ();
146 ITaskItem ConvertToITaskItem (Project project)
151 throw new Exception ("Cannot cast empty expression to ITaskItem.");
153 if (objects [0] is ItemReference) {
154 ItemReference ir = (ItemReference) objects [0];
155 ITaskItem[] array = ir.ConvertToITaskItemArray (project);
156 if (array.Length == 1) {
159 throw new Exception ("TaskItem array too long");
162 item = new TaskItem (ConvertToString (project));
167 ITaskItem[] ConvertToITaskItemArray (Project project)
169 List <ITaskItem> finalItems = new List <ITaskItem> ();
170 ArrayList tempItems = new ArrayList ();
173 foreach (object o in objects) {
174 if (o is ItemReference) {
176 } else if (o is PropertyReference) {
177 PropertyReference pr = (PropertyReference) o;
178 tempItems.Add (pr.ConvertToString (project));
179 } else if (o is MetadataReference) {
180 // FIXME: not handled yet
181 } else if (o is string) {
184 throw new Exception ("Invalid type in objects collection.");
187 foreach (object o in tempItems) {
188 if (o is ItemReference) {
189 ItemReference ir = (ItemReference) o;
190 array = ir.ConvertToITaskItemArray (project);
192 finalItems.AddRange (array);
193 } else if (o is string) {
194 string s = (string) o;
195 array = ConvertToITaskItemArrayFromString (project, s);
196 finalItems.AddRange (array);
198 throw new Exception ("Invalid type in tempItems collection.");
202 return finalItems.ToArray ();
205 ITaskItem [] ConvertToITaskItemArrayFromString (Project project, string source)
207 List <ITaskItem> items = new List <ITaskItem> ();
208 string [] splitSource = source.Split (';');
209 foreach (string s in splitSource) {
210 if (s != String.Empty) {
211 items.Add (new TaskItem (s));
214 return items.ToArray ();