1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
|
//
// ExpressionCollection.cs
//
// Author:
// Marek Sieradzki (marek.sieradzki@gmail.com)
// Ankit Jain (jankit@novell.com)
//
// (C) 2006 Marek Sieradzki
// Copyright 2009 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#if NET_2_0
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
namespace Microsoft.Build.BuildEngine {
internal class ExpressionCollection {
IList objects;
static Dictionary<string, bool> boolValues;
static ExpressionCollection ()
{
string[] trueValuesArray = new string[] {"true", "on", "yes"};
string[] falseValuesArray = new string[] {"false", "off", "no"};
boolValues = new Dictionary<string, bool> (StringComparer.InvariantCultureIgnoreCase);
foreach (string s in trueValuesArray)
boolValues.Add (s, true);
foreach (string s in falseValuesArray)
boolValues.Add (s, false);
}
public ExpressionCollection ()
{
objects = new ArrayList ();
}
public int Count {
get { return objects.Count; }
}
public void Add (IReference reference)
{
objects.Add (reference);
}
public void Add (string s)
{
objects.Add (s);
}
public object ConvertTo (Project project, Type type, ExpressionOptions options)
{
if (type.IsArray) {
if (type == typeof (ITaskItem[]))
return ConvertToITaskItemArray (project, options);
else
return ConvertToArray (project, type, options);
} else {
if (type == typeof (ITaskItem))
return ConvertToITaskItem (project, options);
else
return ConvertToNonArray (project, type, options);
}
}
public IEnumerator GetEnumerator ()
{
foreach (object o in objects)
yield return o;
}
object ConvertToNonArray (Project project, Type type, ExpressionOptions options)
{
return ConvertToObject (ConvertToString (project, options), type, options);
}
object ConvertToArray (Project project, Type type, ExpressionOptions options)
{
ITaskItem[] items = ConvertToITaskItemArray (project, options);
Type element_type = type.GetElementType ();
Array arr = Array.CreateInstance (element_type, items.Length);
for (int i = 0; i < arr.Length; i ++)
arr.SetValue (ConvertToObject (items [i].ItemSpec, element_type, options), i);
return arr;
}
object ConvertToObject (string raw, Type type, ExpressionOptions options)
{
if (type == typeof (bool)) {
bool value;
if (boolValues.TryGetValue (raw, out value))
return value;
else
return false;
}
if (type == typeof (string))
return raw;
if (type.IsPrimitive)
return Convert.ChangeType (raw, type);
if (type == typeof (DateTime))
return DateTime.Parse (raw);
throw new Exception (String.Format ("Unsupported type: {0}", type));
}
string ConvertToString (Project project, ExpressionOptions options)
{
StringBuilder sb = new StringBuilder ();
foreach (object o in objects) {
string s = o as string;
if (s != null) {
sb.Append (s);
continue;
}
IReference br = o as IReference;
if (br != null)
sb.Append (br.ConvertToString (project, options));
else
throw new Exception ("BUG: Invalid type in objects collection.");
}
return sb.ToString ();
}
ITaskItem ConvertToITaskItem (Project project, ExpressionOptions options)
{
if (objects == null)
throw new Exception ("Cannot cast empty expression to ITaskItem.");
ITaskItem[] items = ConvertToITaskItemArray (project, options);
if (items.Length > 1)
//FIXME: msbuild gives better errors
throw new Exception (String.Format ("Exactly one item required, but got: {0}", items.Length));
if (items.Length == 0) return null;
return items [0];
}
// Concat rules (deduced)
// - ItemRef can concat only with a string ';' or PropertyRef ending in ';'
// - MetadataRef can concat with anything other than ItemRef
// - PropertyRef cannot be right after a ItemRef
// PropertyRef concats if it doesn't end in ';'
// - string cannot concat with ItemRef unless it is ';'.
// string concats if it ends in ';'
ITaskItem[] ConvertToITaskItemArray (Project project, ExpressionOptions options)
{
List <ITaskItem> finalItems = new List <ITaskItem> ();
object prev = null;
bool prev_can_concat = false;
foreach (object o in objects) {
bool can_concat = prev_can_concat;
string str = o as string;
if (str != null) {
string trimmed_str = str.Trim ();
if (!IsSemicolon (str) && trimmed_str.Length > 0 && prev != null && prev is ItemReference)
// non-empty, non-semicolon string after item ref
ThrowCantConcatError (prev, str);
if (trimmed_str.Length == 0 && prev is string && IsSemicolon ((string) prev)) {
// empty string after a ';', ignore it
continue;
}
// empty string _after_ a itemref, not an error
prev_can_concat = !(str.Length > 0 && str [str.Length - 1] == ';') && trimmed_str.Length > 0;
AddItemsToArray (finalItems,
ConvertToITaskItemArrayFromString (str),
can_concat);
prev = o;
continue;
}
IReference br = o as IReference;
if (br == null)
throw new Exception ("BUG: Invalid type in objects collection.");
if (o is ItemReference) {
if (prev != null && !(prev is string && (string)prev == ";"))
ThrowCantConcatError (prev, br);
prev_can_concat = true;
} else if (o is MetadataReference) {
if (prev != null && prev is ItemReference)
ThrowCantConcatError (prev, br);
prev_can_concat = true;
} else if (o is PropertyReference) {
if (prev != null && prev is ItemReference)
ThrowCantConcatError (prev, br);
string value = ((PropertyReference) o).GetValue (project);
prev_can_concat = !(value.Length > 0 && value [value.Length - 1] == ';');
}
AddItemsToArray (finalItems, br.ConvertToITaskItemArray (project, options), can_concat);
prev = o;
}
// Trim and Remove empty items
List<ITaskItem> toRemove = new List<ITaskItem> ();
for (int i = 0; i < finalItems.Count; i ++) {
string s = finalItems [i].ItemSpec.Trim ();
if (s.Length == 0)
toRemove.Add (finalItems [i]);
else
finalItems [i].ItemSpec = s;
}
foreach (ITaskItem ti in toRemove)
finalItems.Remove (ti);
return finalItems.ToArray ();
}
// concat's first item in @items to last item in @list if @concat is true
// else just adds all @items to @list
void AddItemsToArray (List<ITaskItem> list, ITaskItem[] items, bool concat)
{
if (items == null || items.Length == 0)
return;
int start_index = 1;
if (concat && list.Count > 0)
list [list.Count - 1].ItemSpec += items [0].ItemSpec;
else
start_index = 0;
for (int i = start_index; i < items.Length; i ++)
list.Add (items [i]);
}
ITaskItem [] ConvertToITaskItemArrayFromString (string source)
{
List <ITaskItem> items = new List <ITaskItem> ();
string [] splitSource = source.Split (new char [] {';'},
StringSplitOptions.RemoveEmptyEntries);
foreach (string s in splitSource)
items.Add (new TaskItem (s));
return items.ToArray ();
}
bool IsSemicolon (string str)
{
return str != null && str.Length == 1 && str [0] == ';';
}
void ThrowCantConcatError (object first, object second)
{
throw new Exception (String.Format (
"Can't concatenate Item list with other strings where an item list is " +
"expected ('{0}', '{1}'). Use semi colon to separate items.",
first.ToString (), second.ToString ()));
}
}
}
#endif
|