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 {
39 OldExpression separator;
40 OldExpression transform;
41 Project parentProject;
44 public ItemReference (Project parentProject, string itemName,
45 LiteralExpression transform,
46 LiteralExpression separator)
48 this.parentProject = parentProject;
49 this.itemName = itemName;
50 this.transform = transform;
51 this.separator = separator;
55 public ItemReference (Project parentProject)
57 if (parentProject == null)
58 throw new ArgumentNullException ("Parent Project needed.");
59 this.parentProject = parentProject;
61 this.separator = null;
64 public void ParseSource (string source)
66 string sourceWithoutParens;
67 string separatorString = null;
68 ApostropheState aState = ApostropheState.Out;
69 ItemParsingState iState = ItemParsingState.Name;
72 int transformEnd = -1;
73 int separatorStart = -1;
76 throw new ArgumentNullException ("source");
78 if (source.Length < 3)
79 throw new ArgumentException ("Invalid item.");
81 sourceWithoutParens = source.Substring (2, source.Length - 3);
82 itemNameEnd = sourceWithoutParens.Length - 1;
83 CharEnumerator it = sourceWithoutParens.GetEnumerator ();
85 while (it.MoveNext ()) {
87 if (it.Current == '\'') {
88 if (aState == ApostropheState.In)
89 aState = ApostropheState.Out;
91 aState = ApostropheState.In;
93 if (it.Current == '-' && iState == ItemParsingState.Name && aState == ApostropheState.Out) {
94 iState = ItemParsingState.Transform1;
96 } else if (it.Current == '>' && iState == ItemParsingState.Transform1 && aState == ApostropheState.Out) {
97 iState = ItemParsingState.Transform2;
98 } else if (iState == ItemParsingState.Transform2 && aState == ApostropheState.Out && c == sourceWithoutParens.Length - 1) {
100 } else if (iState == ItemParsingState.Transform2 && aState == ApostropheState.Out && it.Current == ',') {
101 transformEnd = c - 1;
102 separatorStart = c + 1;
104 } else if (iState == ItemParsingState.Name && aState == ApostropheState.Out && it.Current == ',') {
105 separatorStart = c + 1;
110 itemName = sourceWithoutParens.Substring (0, itemNameEnd + 1);
111 if (transformEnd != -1) {
112 if (separatorStart != -1) {
113 separatorString = sourceWithoutParens.Substring (separatorStart + 1, sourceWithoutParens.Length
114 - separatorStart - 2);
115 transform = new OldExpression (parentProject);
116 transform.ParseSource (sourceWithoutParens.Substring (itemNameEnd + 4,
117 transformEnd - itemNameEnd - 4));
119 transform = new OldExpression (parentProject);
120 transform.ParseSource (sourceWithoutParens.Substring (itemNameEnd + 4,
121 sourceWithoutParens.Length - itemNameEnd - 5));
124 if (separatorStart != -1) {
125 separatorString = sourceWithoutParens.Substring (separatorStart + 1, sourceWithoutParens.Length
126 - separatorStart - 2);
130 if (separatorString != null) {
131 separator = new OldExpression (parentProject);
132 separator.ParseSource (separatorString);
136 public ITaskItem[] ConvertToITaskItemArray ()
138 if (itemName != String.Empty) {
140 if (parentProject.EvaluatedItemsByName.Contains (itemName)) {
141 big = (BuildItemGroup)parentProject.EvaluatedItemsByName [itemName];
142 return big.ConvertToITaskItemArray (transform);
149 public string ConvertToString ()
151 if (itemName != String.Empty) {
153 if (parentProject.EvaluatedItemsByName.Contains (itemName)) {
154 big = (BuildItemGroup)parentProject.EvaluatedItemsByName [itemName];
156 return big.ConvertToString (transform, separator);
163 public string ItemName {
164 get { return itemName; }