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 ();
48 public void Add (ItemReference itemReference)
50 objects.Add (itemReference);
53 public void Add (MetadataReference metadataReference)
55 objects.Add (metadataReference);
58 public void Add (PropertyReference propertyReference)
60 objects.Add (propertyReference);
63 public void Add (string s)
68 public object ConvertTo (Project project, Type type)
71 if (type == typeof (ITaskItem[]))
72 return ConvertToITaskItemArray (project);
74 return ConvertToArray (project, type);
76 if (type == typeof (ITaskItem))
77 return ConvertToITaskItem (project);
79 return ConvertToNonArray (project, type);
83 public IEnumerator GetEnumerator ()
85 foreach (object o in objects)
89 object ConvertToNonArray (Project project, Type type)
91 return ConvertToObject (ConvertToString (project), type);
94 object ConvertToArray (Project project, Type type)
96 string[] rawTable = ConvertToString (project).Split (';');
99 if (type == typeof (bool[])) {
100 bool[] array = new bool [rawTable.Length];
101 foreach (string raw in rawTable)
102 array [i++] = (bool) ConvertToObject (raw, type.GetElementType ());
104 } else if (type == typeof (string[])) {
105 string[] array = new string [rawTable.Length];
106 foreach (string raw in rawTable)
107 array [i++] = (string) ConvertToObject (raw, type.GetElementType ());
109 } else if (type == typeof (int[])) {
110 int[] array = new int [rawTable.Length];
111 foreach (string raw in rawTable)
112 array [i++] = (int) ConvertToObject (raw, type.GetElementType ());
114 } else if (type == typeof (uint[])) {
115 uint[] array = new uint [rawTable.Length];
116 foreach (string raw in rawTable)
117 array [i++] = (uint) ConvertToObject (raw, type.GetElementType ());
119 } else if (type == typeof (DateTime[])) {
120 DateTime[] array = new DateTime [rawTable.Length];
121 foreach (string raw in rawTable)
122 array [i++] = (DateTime) ConvertToObject (raw, type.GetElementType ());
125 throw new Exception ("Invalid type");
128 object ConvertToObject (string raw, Type type)
130 if (type == typeof (bool)) {
131 return Boolean.Parse (raw);
132 } else if (type == typeof (string)) {
134 } else if (type == typeof (int)) {
135 return Int32.Parse (raw);
136 } else if (type == typeof (uint)) {
137 return UInt32.Parse (raw);
138 } else if (type == typeof (DateTime)) {
139 return DateTime.Parse (raw);
141 throw new Exception (String.Format ("Unknown type: {0}", type.ToString ()));
144 string ConvertToString (Project project)
146 StringBuilder sb = new StringBuilder ();
148 foreach (object o in objects) {
150 sb.Append ((string) o);
151 } else if (o is ItemReference) {
152 ItemReference ir = (ItemReference) o;
153 sb.Append (ir.ConvertToString (project));
154 } else if (o is PropertyReference) {
155 PropertyReference pr = (PropertyReference) o;
156 sb.Append (pr.ConvertToString (project));
157 } else if (o is MetadataReference) {
158 // FIXME: we don't handle them yet
160 throw new Exception ("Invalid type in objects collection.");
163 return sb.ToString ();
166 ITaskItem ConvertToITaskItem (Project project)
171 throw new Exception ("Cannot cast empty expression to ITaskItem.");
173 if (objects [0] is ItemReference) {
174 ItemReference ir = (ItemReference) objects [0];
175 ITaskItem[] array = ir.ConvertToITaskItemArray (project);
176 if (array.Length == 1) {
179 throw new Exception ("TaskItem array too long");
182 item = new TaskItem (ConvertToString (project));
187 ITaskItem[] ConvertToITaskItemArray (Project project)
189 List <ITaskItem> finalItems = new List <ITaskItem> ();
190 ArrayList tempItems = new ArrayList ();
193 foreach (object o in objects) {
194 if (o is ItemReference) {
196 } else if (o is PropertyReference) {
197 PropertyReference pr = (PropertyReference) o;
198 tempItems.Add (pr.ConvertToString (project));
199 } else if (o is MetadataReference) {
200 // FIXME: not handled yet
201 } else if (o is string) {
204 throw new Exception ("Invalid type in objects collection.");
207 foreach (object o in tempItems) {
208 if (o is ItemReference) {
209 ItemReference ir = (ItemReference) o;
210 array = ir.ConvertToITaskItemArray (project);
212 finalItems.AddRange (array);
213 } else if (o is string) {
214 string s = (string) o;
215 array = ConvertToITaskItemArrayFromString (project, s);
216 finalItems.AddRange (array);
218 throw new Exception ("Invalid type in tempItems collection.");
222 return finalItems.ToArray ();
225 ITaskItem [] ConvertToITaskItemArrayFromString (Project project, string source)
227 List <ITaskItem> items = new List <ITaskItem> ();
228 string [] splitSource = source.Split (';');
229 foreach (string s in splitSource) {
230 if (s != String.Empty) {
231 items.Add (new TaskItem (s));
234 return items.ToArray ();