2006-03-29 Marek Sieradzki <marek.sieradzki@gmail.com>
[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                 string          separator;
40                 Expression      parent;
41                 Expression      transform;
42         
43                 public ItemReference (Expression parent)
44                 {
45                         if (parent == null)
46                                 throw new Exception ("Parent Expression needed to find project.");
47                         this.parent = parent;
48                         this.itemName = null;
49                         this.separator = ";";
50                 }
51                 
52                 public ItemReference (Expression parent, string source)
53                         : this (parent)
54                 {
55                         ParseSource (source);
56                 }
57                 
58                 public void ParseSource (string source)
59                 {
60                         string sourceWithoutParens;
61                         ApostropheState aState = ApostropheState.Out;
62                         ItemParsingState iState = ItemParsingState.Name;
63                         int c = -1;
64                         int itemNameEnd;
65                         int transformEnd = -1;
66                         int separatorStart = -1;
67                 
68                         if (source == null)
69                                 throw new ArgumentNullException ("source");
70                 
71                         if (source.Length < 3)
72                                 throw new ArgumentException ("Invalid item.");
73                         
74                         sourceWithoutParens = source.Substring (2, source.Length - 3);
75                         itemNameEnd = sourceWithoutParens.Length - 1;
76                         CharEnumerator it = sourceWithoutParens.GetEnumerator ();
77
78                         while (it.MoveNext ()) {
79                                 c++;
80                                 if (it.Current == '\'') {
81                                         if (aState == ApostropheState.In)
82                                                 aState = ApostropheState.Out;
83                                         else
84                                                 aState = ApostropheState.In;
85                                 }
86                                 if (it.Current == '-' && iState == ItemParsingState.Name && aState == ApostropheState.Out) {
87                                         iState = ItemParsingState.Transform1;
88                                         itemNameEnd = c - 1;
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) {
92                                         transformEnd = c;
93                                 } else if (iState == ItemParsingState.Transform2 && aState == ApostropheState.Out && it.Current == ',') {
94                                         transformEnd = c - 1;
95                                         separatorStart = c + 1;
96                                         break;
97                                 } else if (iState == ItemParsingState.Name && aState == ApostropheState.Out && it.Current == ',') {
98                                         separatorStart = c + 1;
99                                         itemNameEnd = c - 1;
100                                         break;
101                                 }
102                         }
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));
110                                 } else {
111                                         transform = new Expression (parent.Project, sourceWithoutParens.Substring (itemNameEnd + 4,
112                                         sourceWithoutParens.Length - itemNameEnd - 5));
113                                 }
114                         } else {
115                                 if (separatorStart != -1) {
116                                         separator = sourceWithoutParens.Substring (separatorStart + 1, sourceWithoutParens.Length
117                                                 - separatorStart - 2);
118                                 }
119                         }
120                 }
121                 
122                 public ITaskItem[] ToITaskItemArray ()
123                 {
124                         if (itemName != String.Empty) {
125                                 Project p = parent.Project;
126                                 BuildItemGroup big;
127                                 if (p.EvaluatedItemsByName.Contains (itemName)) {
128                                         big = (BuildItemGroup)p.EvaluatedItemsByName [itemName];
129                                         return big.ToITaskItemArray (transform);
130                                 } else
131                                         return null;
132                         } else
133                                 return null;
134                 }
135                 
136                 public new string ToString ()
137                 {
138                         if (itemName != String.Empty) {
139                                 Project p = parent.Project;
140                                 BuildItemGroup big;
141                                 if (p.EvaluatedItemsByName.Contains (itemName)) {
142                                         big = (BuildItemGroup)p.EvaluatedItemsByName [itemName];
143                                         return big.ToString (transform, separator);
144                                 } else
145                                         return String.Empty;
146                         } else
147                                 return String.Empty;
148                 }
149                 
150                 public string ItemName {
151                         get { return itemName; }
152                 }
153                 
154                 public string Separator {
155                         get { return separator; }
156                 }
157         }
158 }
159
160 #endif