2 // ItemReference.cs: Represents "@(Reference)" in expression.
5 // Marek Sieradzki (marek.sieradzki@gmail.com)
7 // (C) 2005 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 Microsoft.Build.Framework;
33 using Microsoft.Build.Utilities;
35 namespace Microsoft.Build.BuildEngine {
36 internal class ItemReference {
43 public ItemReference (Expression parent)
46 throw new Exception ("Parent Expression needed to find project.");
52 public ItemReference (Expression parent, string source)
58 public void ParseSource (string source)
60 string sourceWithoutParens;
61 ApostropheState aState = ApostropheState.Out;
62 ItemParsingState iState = ItemParsingState.Name;
65 int transformEnd = -1;
66 int separatorStart = -1;
69 throw new ArgumentNullException ("source");
71 if (source.Length < 3)
72 throw new ArgumentException ("Invalid item.");
74 sourceWithoutParens = source.Substring (2, source.Length - 3);
75 itemNameEnd = sourceWithoutParens.Length - 1;
76 CharEnumerator it = sourceWithoutParens.GetEnumerator ();
78 while (it.MoveNext ()) {
80 if (it.Current == '\'') {
81 if (aState == ApostropheState.In)
82 aState = ApostropheState.Out;
84 aState = ApostropheState.In;
86 if (it.Current == '-' && iState == ItemParsingState.Name && aState == ApostropheState.Out) {
87 iState = ItemParsingState.Transform1;
89 } else if (it.Current == '>' && iState == ItemParsingState.Transform1 && aState == ApostropheState.Out) {
90 iState = ItemParsingState.Transform2;
91 } else if (iState == ItemParsingState.Transform2 && aState == ApostropheState.Out && c == sourceWithoutParens.Length - 1) {
93 } else if (iState == ItemParsingState.Transform2 && aState == ApostropheState.Out && it.Current == ',') {
95 separatorStart = c + 1;
97 } else if (iState == ItemParsingState.Name && aState == ApostropheState.Out && it.Current == ',') {
98 separatorStart = c + 1;
103 itemName = sourceWithoutParens.Substring (0, itemNameEnd + 1);
104 if (transformEnd != -1) {
105 if (separatorStart != -1) {
106 separator = sourceWithoutParens.Substring (separatorStart + 1, sourceWithoutParens.Length
107 - separatorStart - 2);
108 transform = new Expression (parent.Project, sourceWithoutParens.Substring (itemNameEnd + 4,
109 transformEnd - itemNameEnd - 4));
111 transform = new Expression (parent.Project, sourceWithoutParens.Substring (itemNameEnd + 4,
112 sourceWithoutParens.Length - itemNameEnd - 5));
115 if (separatorStart != -1) {
116 separator = sourceWithoutParens.Substring (separatorStart + 1, sourceWithoutParens.Length
117 - separatorStart - 2);
122 public ITaskItem[] ToITaskItemArray ()
124 if (itemName != String.Empty) {
125 Project p = parent.Project;
127 if (p.EvaluatedItemsByName.Contains (itemName)) {
128 big = (BuildItemGroup)p.EvaluatedItemsByName [itemName];
129 return big.ToITaskItemArray (transform);
136 public new string ToString ()
138 if (itemName != String.Empty) {
139 Project p = parent.Project;
141 if (p.EvaluatedItemsByName.Contains (itemName)) {
142 big = (BuildItemGroup)p.EvaluatedItemsByName [itemName];
143 return big.ToString (transform, separator);
150 public string ItemName {
151 get { return itemName; }
154 public string Separator {
155 get { return separator; }