Bug 15572. Lookup KnownTypeCollection element types in MSSimpleNamespace
[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 using System;
31 using System.Text;
32 using System.Xml;
33
34 using Microsoft.Build.Framework;
35 using Microsoft.Build.Utilities;
36 using Mono.XBuild.Utilities;
37
38 namespace Microsoft.Build.BuildEngine {
39         public class BuildProperty {
40         
41                 XmlElement      propertyElement;
42                 string          finalValue;
43                 bool            isImported;
44                 string          value;
45                 string          name;
46                 Project         parentProject;
47                 PropertyType    propertyType;
48                 bool            converting;
49
50                 BuildProperty ()
51                 {
52                 }
53
54                 public BuildProperty (string propertyName, string propertyValue)
55                         : this (propertyName, propertyValue, PropertyType.Normal)
56                 {
57                         if (propertyName == null)
58                                 throw new ArgumentNullException ("propertyName");
59                         if (propertyValue == null)
60                                 throw new ArgumentNullException ("propertyValue");
61                 }
62
63                 internal BuildProperty (string propertyName,
64                                 string propertyValue, PropertyType propertyType)
65                 {
66                         this.name = propertyName;
67                         this.value = propertyValue;
68                         this.finalValue = propertyValue;
69                         this.propertyType = propertyType;
70                         this.isImported = false;
71                 }
72
73                 internal BuildProperty (Project parentProject, XmlElement propertyElement)
74                 {
75                         if (propertyElement == null)
76                                 throw new ArgumentNullException ("propertyElement");
77
78                         this.propertyElement = propertyElement;
79                         this.propertyType = PropertyType.Normal;
80                         this.parentProject = parentProject;
81                         this.name = propertyElement.Name;
82                         this.value = MSBuildUtils.UnescapeFromXml (propertyElement.InnerXml);
83                         this.isImported = false;
84                 }
85
86                 [MonoTODO]
87                 public BuildProperty Clone (bool deepClone)
88                 {
89                         if (deepClone) {
90                                 if (FromXml) 
91                                         throw new NotImplementedException ();
92                                 else
93                                         return (BuildProperty) this.MemberwiseClone ();
94                         } else {
95                                 if (FromXml)
96                                         throw new NotImplementedException ();
97                                 else
98                                         throw new InvalidOperationException ("A shallow clone of this object cannot be created.");
99                         }
100                 }
101
102                 public static explicit operator string (BuildProperty propertyToCast)
103                 {
104                         if (propertyToCast == null)
105                                 return String.Empty;
106                         else
107                                 return propertyToCast.ToString ();
108                 }
109
110                 public override string ToString ()
111                 {
112                         if (finalValue != null)
113                                 return finalValue;
114                         else
115                                 return Value;
116                 }
117
118                 internal void Evaluate ()
119                 {
120                         BuildProperty evaluated = new BuildProperty (Name, Value);
121
122                         // In evaluate phase, properties are not expanded
123                         Expression exp = new Expression ();
124                         exp.Parse (Value, ParseOptions.None);
125                         evaluated.finalValue = (string) exp.ConvertTo (parentProject, typeof (string),
126                                         ExpressionOptions.DoNotExpandItemRefs);
127
128                         parentProject.EvaluatedProperties.AddProperty (evaluated);
129                 }
130
131                 // during property's eval phase, this is never reached, as PropertyReference
132                 // handles the eval case
133                 //
134                 // during item's eval phase, we have expand: true, that's what we
135                 // do here..
136                 //
137                 // during non-eval, expand: true
138                 // So, its always true here
139                 internal string ConvertToString (Project project, ExpressionOptions options)
140                 {
141                         if (converting) {
142                                 // found ref to @this while trying to ConvertToString
143                                 // for @this!
144                                 return FinalValue;
145                         }
146
147                         converting = true;
148                         try {
149                                 Expression exp = new Expression ();
150
151                                 // in non-evaluation phase, properties are always expanded
152                                 exp.Parse (FinalValue, options == ExpressionOptions.ExpandItemRefs ?
153                                                         ParseOptions.AllowItems : ParseOptions.None);
154                                 return (string) exp.ConvertTo (project, typeof (string), options);
155                         } finally {
156                                 converting = false;
157                         }
158                 }
159
160                 internal ITaskItem[] ConvertToITaskItemArray (Project project, ExpressionOptions options)
161                 {
162                         if (converting) {
163                                 // found ref to @this while trying to ConvertToITaskItemArray
164                                 // for @this!
165                                 ITaskItem []items = new ITaskItem [1];
166                                 items [0] = new TaskItem (FinalValue);
167                                 return items;
168                         }
169
170                         converting = true;
171                         try {
172                                 Expression exp = new Expression ();
173
174                                 // in non-evaluation phase, properties are always expanded
175                                 exp.Parse (FinalValue, ParseOptions.Split | (options == ExpressionOptions.ExpandItemRefs ?
176                                                         ParseOptions.AllowItems : ParseOptions.None));
177                                 return (ITaskItem[]) exp.ConvertTo (project, typeof (ITaskItem[]), options);
178                         } finally {
179                                 converting = false;
180                         }
181                 }
182
183                 internal bool FromXml {
184                         get {
185                                 return propertyElement != null;
186                         }
187                 }
188         
189                 public string Condition {
190                         get {
191                                 if (FromXml)
192                                         return propertyElement.GetAttribute ("Condition");
193                                 else
194                                         return String.Empty;
195                         }
196                         set {
197                                 if (FromXml)
198                                         propertyElement.SetAttribute ("Condition", value);
199                                 else
200                                         throw new InvalidOperationException ("Cannot set a condition on an object not represented by an XML element in the project file.");
201                         }
202                 }
203
204                 public string FinalValue {
205                         get {
206                                 if (finalValue == null)
207                                         return this.@value;
208                                 else
209                                         return finalValue;
210                         }
211                 }
212                 
213                 public bool IsImported {
214                         get { return isImported; }
215                 }
216
217                 public string Name {
218                         get { return name; }
219                 }
220
221                 public string Value {
222                         get {
223                                 return value;
224                         }
225                         set {
226                                 this.@value = value;
227                                 if (FromXml) {
228                                         propertyElement.InnerXml = value;
229                                 } else {
230                                         finalValue = value;
231                                 }
232                         }
233                 }
234
235                 internal PropertyType PropertyType {
236                         get {
237                                 return propertyType;
238                         }
239                 }
240
241                 internal XmlElement XmlElement {
242                         get { return propertyElement; }
243                 }
244         }
245
246         internal enum PropertyType {
247                 Reserved,
248                 Global,
249                 Normal,
250                 Environment
251         }
252 }