2 // ExpressionCollection.cs
5 // Marek Sieradzki (marek.sieradzki@gmail.com)
6 // Ankit Jain (jankit@novell.com)
8 // (C) 2006 Marek Sieradzki
9 // Copyright 2009 Novell, Inc (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Collections;
34 using System.Collections.Generic;
36 using Microsoft.Build.Framework;
37 using Microsoft.Build.Utilities;
39 namespace Microsoft.Build.BuildEngine {
41 internal class ExpressionCollection {
44 static Dictionary<string, bool> boolValues;
46 static ExpressionCollection ()
48 string[] trueValuesArray = new string[] {"true", "on", "yes"};
49 string[] falseValuesArray = new string[] {"false", "off", "no"};
51 boolValues = new Dictionary<string, bool> (StringComparer.InvariantCultureIgnoreCase);
52 foreach (string s in trueValuesArray)
53 boolValues.Add (s, true);
54 foreach (string s in falseValuesArray)
55 boolValues.Add (s, false);
58 public ExpressionCollection ()
60 objects = new ArrayList ();
64 get { return objects.Count; }
67 public void Add (IReference reference)
69 objects.Add (reference);
72 public void Add (string s)
77 public object ConvertTo (Project project, Type type)
80 if (type == typeof (ITaskItem[]))
81 return ConvertToITaskItemArray (project);
83 return ConvertToArray (project, type);
85 if (type == typeof (ITaskItem))
86 return ConvertToITaskItem (project);
88 return ConvertToNonArray (project, type);
92 public IEnumerator GetEnumerator ()
94 foreach (object o in objects)
98 object ConvertToNonArray (Project project, Type type)
100 return ConvertToObject (ConvertToString (project), type);
103 object ConvertToArray (Project project, Type type)
105 ITaskItem[] items = ConvertToITaskItemArray (project);
107 Type element_type = type.GetElementType ();
108 Array arr = Array.CreateInstance (element_type, items.Length);
109 for (int i = 0; i < arr.Length; i ++)
110 arr.SetValue (ConvertToObject (items [i].ItemSpec, element_type), i);
114 object ConvertToObject (string raw, Type type)
116 if (type == typeof (bool)) {
118 if (boolValues.TryGetValue (raw, out value))
124 if (type == typeof (string))
126 else if (type == typeof (int))
127 return Int32.Parse (raw);
128 else if (type == typeof (uint))
129 return UInt32.Parse (raw);
130 else if (type == typeof (DateTime))
131 return DateTime.Parse (raw);
133 throw new Exception (String.Format ("Unknown type: {0}", type.ToString ()));
136 string ConvertToString (Project project)
138 StringBuilder sb = new StringBuilder ();
140 foreach (object o in objects) {
141 string s = o as string;
147 IReference br = o as IReference;
149 sb.Append (br.ConvertToString (project));
151 throw new Exception ("BUG: Invalid type in objects collection.");
153 return sb.ToString ();
156 ITaskItem ConvertToITaskItem (Project project)
161 throw new Exception ("Cannot cast empty expression to ITaskItem.");
163 ITaskItem[] items = ConvertToITaskItemArray (project);
164 if (items.Length != 1)
165 //FIXME: msbuild gives better errors
166 throw new Exception (String.Format ("Too many items: {0}", items.Length));
171 // Concat rules (deduced)
172 // - ItemRef can concat only with a string ';' or PropertyRef ending in ';'
173 // - MetadataRef can concat with anything other than ItemRef
174 // - PropertyRef cannot be right after a ItemRef
175 // PropertyRef concats if it doesn't end in ';'
176 // - string cannot concat with ItemRef unless it is ';'.
177 // string concats if it ends in ';'
178 ITaskItem[] ConvertToITaskItemArray (Project project)
180 List <ITaskItem> finalItems = new List <ITaskItem> ();
183 bool prev_can_concat = false;
185 foreach (object o in objects) {
186 bool can_concat = prev_can_concat;
188 string str = o as string;
190 string trimmed_str = str.Trim ();
191 if (!IsSemicolon (str) && trimmed_str.Length > 0 && prev != null && prev is ItemReference)
192 // non-empty, non-semicolon string after item ref
193 ThrowCantConcatError (prev, str);
195 if (trimmed_str.Length == 0 && prev is string && IsSemicolon ((string) prev)) {
196 // empty string after a ';', ignore it
200 // empty string _after_ a itemref, not an error
201 prev_can_concat = !(str.Length > 0 && str [str.Length - 1] == ';') && trimmed_str.Length > 0;
202 AddItemsToArray (finalItems,
203 ConvertToITaskItemArrayFromString (str),
209 IReference br = o as IReference;
211 throw new Exception ("BUG: Invalid type in objects collection.");
213 if (o is ItemReference) {
214 if (prev != null && !(prev is string && (string)prev == ";"))
215 ThrowCantConcatError (prev, br);
217 prev_can_concat = true;
218 } else if (o is MetadataReference) {
219 if (prev != null && prev is ItemReference)
220 ThrowCantConcatError (prev, br);
222 prev_can_concat = true;
223 } else if (o is PropertyReference) {
224 if (prev != null && prev is ItemReference)
225 ThrowCantConcatError (prev, br);
227 string value = ((PropertyReference) o).GetValue (project);
228 prev_can_concat = !(value.Length > 0 && value [value.Length - 1] == ';');
231 AddItemsToArray (finalItems, br.ConvertToITaskItemArray (project), can_concat);
236 // Trim and Remove empty items
237 List<ITaskItem> toRemove = new List<ITaskItem> ();
238 for (int i = 0; i < finalItems.Count; i ++) {
239 string s = finalItems [i].ItemSpec.Trim ();
241 toRemove.Add (finalItems [i]);
243 finalItems [i].ItemSpec = s;
245 foreach (ITaskItem ti in toRemove)
246 finalItems.Remove (ti);
248 return finalItems.ToArray ();
251 // concat's first item in @items to last item in @list if @concat is true
252 // else just adds all @items to @list
253 void AddItemsToArray (List<ITaskItem> list, ITaskItem[] items, bool concat)
255 if (items == null || items.Length == 0)
259 if (concat && list.Count > 0)
260 list [list.Count - 1].ItemSpec += items [0].ItemSpec;
264 for (int i = start_index; i < items.Length; i ++)
265 list.Add (items [i]);
268 ITaskItem [] ConvertToITaskItemArrayFromString (string source)
270 List <ITaskItem> items = new List <ITaskItem> ();
271 string [] splitSource = source.Split (new char [] {';'},
272 StringSplitOptions.RemoveEmptyEntries);
274 foreach (string s in splitSource)
275 items.Add (new TaskItem (s));
277 return items.ToArray ();
280 bool IsSemicolon (string str)
282 return str != null && str.Length == 1 && str [0] == ';';
285 void ThrowCantConcatError (object first, object second)
287 throw new Exception (String.Format (
288 "Can't concatenate Item list with other strings where an item list is " +
289 "expected ('{0}', '{1}'). Use semi colon to separate items.",
290 first.ToString (), second.ToString ()));