New test.
[mono.git] / mcs / class / Microsoft.Build.Engine / Microsoft.Build.BuildEngine / ItemReference.cs
1 //
2 // ItemReference.cs: Represents "@(Reference)" in expression.
3 //
4 // Author:
5 //   Marek Sieradzki (marek.sieradzki@gmail.com)
6 // 
7 // (C) 2005 Marek Sieradzki
8 //
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:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
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.
27
28 #if NET_2_0
29
30 using System;
31 using System.Collections;
32 using Microsoft.Build.Framework;
33 using Microsoft.Build.Utilities;
34
35 namespace Microsoft.Build.BuildEngine {
36         internal class ItemReference {
37         
38                 string                  itemName;
39                 OldExpression           separator;
40                 OldExpression           transform;
41                 Project                 parentProject;
42                 
43                 /*
44                 public ItemReference (Project parentProject, string itemName,
45                                       LiteralExpression transform,
46                                       LiteralExpression separator)
47                 {
48                         this.parentProject = parentProject;
49                         this.itemName = itemName;
50                         this.transform = transform;
51                         this.separator = separator;
52                 }
53                 */
54         
55                 public ItemReference (Project parentProject)
56                 {
57                         if (parentProject == null)
58                                 throw new ArgumentNullException ("Parent Project needed.");
59                         this.parentProject = parentProject;
60                         this.itemName = null;
61                         this.separator = null;
62                 }
63                 
64                 public void ParseSource (string source)
65                 {
66                         string sourceWithoutParens;
67                         string separatorString = null;
68                         ApostropheState aState = ApostropheState.Out;
69                         ItemParsingState iState = ItemParsingState.Name;
70                         int c = -1;
71                         int itemNameEnd;
72                         int transformEnd = -1;
73                         int separatorStart = -1;
74                 
75                         if (source == null)
76                                 throw new ArgumentNullException ("source");
77                 
78                         if (source.Length < 3)
79                                 throw new ArgumentException ("Invalid item.");
80                         
81                         sourceWithoutParens = source.Substring (2, source.Length - 3);
82                         itemNameEnd = sourceWithoutParens.Length - 1;
83                         CharEnumerator it = sourceWithoutParens.GetEnumerator ();
84
85                         while (it.MoveNext ()) {
86                                 c++;
87                                 if (it.Current == '\'') {
88                                         if (aState == ApostropheState.In)
89                                                 aState = ApostropheState.Out;
90                                         else
91                                                 aState = ApostropheState.In;
92                                 }
93                                 if (it.Current == '-' && iState == ItemParsingState.Name && aState == ApostropheState.Out) {
94                                         iState = ItemParsingState.Transform1;
95                                         itemNameEnd = c - 1;
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) {
99                                         transformEnd = c;
100                                 } else if (iState == ItemParsingState.Transform2 && aState == ApostropheState.Out && it.Current == ',') {
101                                         transformEnd = c - 1;
102                                         separatorStart = c + 1;
103                                         break;
104                                 } else if (iState == ItemParsingState.Name && aState == ApostropheState.Out && it.Current == ',') {
105                                         separatorStart = c + 1;
106                                         itemNameEnd = c - 1;
107                                         break;
108                                 }
109                         }
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));
118                                 } else {
119                                         transform = new OldExpression (parentProject);
120                                         transform.ParseSource (sourceWithoutParens.Substring (itemNameEnd + 4,
121                                                 sourceWithoutParens.Length - itemNameEnd - 5));
122                                 }
123                         } else {
124                                 if (separatorStart != -1) {
125                                         separatorString = sourceWithoutParens.Substring (separatorStart + 1, sourceWithoutParens.Length
126                                                 - separatorStart - 2);
127                                 }
128                         }
129                         
130                         if (separatorString != null) {
131                                 separator = new OldExpression (parentProject);
132                                 separator.ParseSource (separatorString);
133                         }
134                 }
135                 
136                 public ITaskItem[] ConvertToITaskItemArray ()
137                 {
138                         if (itemName != String.Empty) {
139                                 BuildItemGroup big;
140                                 if (parentProject.EvaluatedItemsByName.Contains (itemName)) {
141                                         big = (BuildItemGroup)parentProject.EvaluatedItemsByName [itemName];
142                                         return big.ConvertToITaskItemArray (transform);
143                                 } else
144                                         return null;
145                         } else
146                                 return null;
147                 }
148                 
149                 public string ConvertToString ()
150                 {
151                         if (itemName != String.Empty) {
152                                 BuildItemGroup big;
153                                 if (parentProject.EvaluatedItemsByName.Contains (itemName)) {
154                                         big = (BuildItemGroup)parentProject.EvaluatedItemsByName [itemName];
155                                         
156                                         return big.ConvertToString (transform, separator);
157                                 } else
158                                         return String.Empty;
159                         } else
160                                 return String.Empty;
161                 }
162                 
163                 public string ItemName {
164                         get { return itemName; }
165                 }
166         }
167 }
168
169 #endif