Handle fault messages in duplex callback channel.
[mono.git] / mcs / class / Microsoft.Build.Engine / Microsoft.Build.BuildEngine / BuildProperty.cs
1 //
2 // BuildProperty.cs: Represents a property
3 //
4 // Author:
5 //   Marek Sieradzki (marek.sieradzki@gmail.com)
6 //   Ankit Jain (jankit@novell.com)
7 // 
8 // (C) 2005 Marek Sieradzki
9 // Copyright 2009 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30 #if NET_2_0
31
32 using System;
33 using System.Text;
34 using System.Xml;
35
36 using Microsoft.Build.Framework;
37 using Microsoft.Build.Utilities;
38 using Mono.XBuild.Utilities;
39
40 namespace Microsoft.Build.BuildEngine {
41         public class BuildProperty {
42         
43                 XmlElement      propertyElement;
44                 string          finalValue;
45                 bool            isImported;
46                 string          value;
47                 string          name;
48                 Project         parentProject;
49                 PropertyType    propertyType;
50                 bool            converting;
51
52                 BuildProperty ()
53                 {
54                 }
55
56                 public BuildProperty (string propertyName, string propertyValue)
57                         : this (propertyName, propertyValue, PropertyType.Normal)
58                 {
59                         if (propertyName == null)
60                                 throw new ArgumentNullException ("propertyName");
61                         if (propertyValue == null)
62                                 throw new ArgumentNullException ("propertyValue");
63                 }
64
65                 internal BuildProperty (string propertyName,
66                                 string propertyValue, PropertyType propertyType)
67                 {
68                         this.name = propertyName;
69                         this.value = propertyValue;
70                         this.finalValue = propertyValue;
71                         this.propertyType = propertyType;
72                         this.isImported = false;
73                 }
74
75                 internal BuildProperty (Project parentProject, XmlElement propertyElement)
76                 {
77                         if (propertyElement == null)
78                                 throw new ArgumentNullException ("propertyElement");
79
80                         this.propertyElement = propertyElement;
81                         this.propertyType = PropertyType.Normal;
82                         this.parentProject = parentProject;
83                         this.name = propertyElement.Name;
84                         this.value = MSBuildUtils.UnescapeFromXml (propertyElement.InnerXml);
85                         this.isImported = false;
86                 }
87
88                 [MonoTODO]
89                 public BuildProperty Clone (bool deepClone)
90                 {
91                         if (deepClone) {
92                                 if (FromXml) 
93                                         throw new NotImplementedException ();
94                                 else
95                                         return (BuildProperty) this.MemberwiseClone ();
96                         } else {
97                                 if (FromXml)
98                                         throw new NotImplementedException ();
99                                 else
100                                         throw new InvalidOperationException ("A shallow clone of this object cannot be created.");
101                         }
102                 }
103
104                 public static explicit operator string (BuildProperty propertyToCast)
105                 {
106                         if (propertyToCast == null)
107                                 return String.Empty;
108                         else
109                                 return propertyToCast.ToString ();
110                 }
111
112                 public override string ToString ()
113                 {
114                         if (finalValue != null)
115                                 return finalValue;
116                         else
117                                 return Value;
118                 }
119
120                 internal void Evaluate ()
121                 {
122                         BuildProperty evaluated = new BuildProperty (Name, Value);
123
124                         // In evaluate phase, properties are not expanded
125                         Expression exp = new Expression ();
126                         exp.Parse (Value, ParseOptions.None);
127                         evaluated.finalValue = (string) exp.ConvertTo (parentProject, typeof (string),
128                                         ExpressionOptions.DoNotExpandItemRefs);
129
130                         parentProject.EvaluatedProperties.AddProperty (evaluated);
131                 }
132
133                 // during property's eval phase, this is never reached, as PropertyReference
134                 // handles the eval case
135                 //
136                 // during item's eval phase, we have expand: true, that's what we
137                 // do here..
138                 //
139                 // during non-eval, expand: true
140                 // So, its always true here
141                 internal string ConvertToString (Project project, ExpressionOptions options)
142                 {
143                         if (converting) {
144                                 // found ref to @this while trying to ConvertToString
145                                 // for @this!
146                                 return FinalValue;
147                         }
148
149                         converting = true;
150                         try {
151                                 Expression exp = new Expression ();
152
153                                 // in non-evaluation phase, properties are always expanded
154                                 exp.Parse (FinalValue, options == ExpressionOptions.ExpandItemRefs ?
155                                                         ParseOptions.AllowItems : ParseOptions.None);
156                                 return (string) exp.ConvertTo (project, typeof (string), options);
157                         } finally {
158                                 converting = false;
159                         }
160                 }
161
162                 internal ITaskItem[] ConvertToITaskItemArray (Project project, ExpressionOptions options)
163                 {
164                         if (converting) {
165                                 // found ref to @this while trying to ConvertToITaskItemArray
166                                 // for @this!
167                                 ITaskItem []items = new ITaskItem [1];
168                                 items [0] = new TaskItem (FinalValue);
169                                 return items;
170                         }
171
172                         converting = true;
173                         try {
174                                 Expression exp = new Expression ();
175
176                                 // in non-evaluation phase, properties are always expanded
177                                 exp.Parse (FinalValue, ParseOptions.Split | (options == ExpressionOptions.ExpandItemRefs ?
178                                                         ParseOptions.AllowItems : ParseOptions.None));
179                                 return (ITaskItem[]) exp.ConvertTo (project, typeof (ITaskItem[]), options);
180                         } finally {
181                                 converting = false;
182                         }
183                 }
184
185                 internal bool FromXml {
186                         get {
187                                 return propertyElement != null;
188                         }
189                 }
190         
191                 public string Condition {
192                         get {
193                                 if (FromXml)
194                                         return propertyElement.GetAttribute ("Condition");
195                                 else
196                                         return String.Empty;
197                         }
198                         set {
199                                 if (FromXml)
200                                         propertyElement.SetAttribute ("Condition", value);
201                                 else
202                                         throw new InvalidOperationException ("Cannot set a condition on an object not represented by an XML element in the project file.");
203                         }
204                 }
205
206                 public string FinalValue {
207                         get {
208                                 if (finalValue == null)
209                                         return this.@value;
210                                 else
211                                         return finalValue;
212                         }
213                 }
214                 
215                 public bool IsImported {
216                         get { return isImported; }
217                 }
218
219                 public string Name {
220                         get { return name; }
221                 }
222
223                 public string Value {
224                         get {
225                                 return value;
226                         }
227                         set {
228                                 this.@value = value;
229                                 if (FromXml) {
230                                         propertyElement.InnerXml = value;
231                                 } else {
232                                         finalValue = value;
233                                 }
234                         }
235                 }
236
237                 internal PropertyType PropertyType {
238                         get {
239                                 return propertyType;
240                         }
241                 }
242
243                 internal XmlElement XmlElement {
244                         get { return propertyElement; }
245                 }
246         }
247
248         internal enum PropertyType {
249                 Reserved,
250                 Global,
251                 Normal,
252                 Environment
253         }
254 }
255
256 #endif