2 // BuildProperty.cs: Represents a property
5 // Marek Sieradzki (marek.sieradzki@gmail.com)
6 // Ankit Jain (jankit@novell.com)
8 // (C) 2005 Marek Sieradzki
9 // Copyright 2009 Novell, Inc (http://www.novell.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
36 using Microsoft.Build.Framework;
37 using Microsoft.Build.Utilities;
38 using Mono.XBuild.Utilities;
40 namespace Microsoft.Build.BuildEngine {
41 public class BuildProperty {
43 XmlElement propertyElement;
48 Project parentProject;
49 PropertyType propertyType;
56 public BuildProperty (string propertyName, string propertyValue)
57 : this (propertyName, propertyValue, PropertyType.Normal)
59 if (propertyName == null)
60 throw new ArgumentNullException ("propertyName");
61 if (propertyValue == null)
62 throw new ArgumentNullException ("propertyValue");
65 internal BuildProperty (string propertyName,
66 string propertyValue, PropertyType propertyType)
68 this.name = propertyName;
69 this.value = propertyValue;
70 this.finalValue = propertyValue;
71 this.propertyType = propertyType;
72 this.isImported = false;
75 internal BuildProperty (Project parentProject, XmlElement propertyElement)
77 if (propertyElement == null)
78 throw new ArgumentNullException ("propertyElement");
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;
89 public BuildProperty Clone (bool deepClone)
93 throw new NotImplementedException ();
95 return (BuildProperty) this.MemberwiseClone ();
98 throw new NotImplementedException ();
100 throw new InvalidOperationException ("A shallow clone of this object cannot be created.");
104 public static explicit operator string (BuildProperty propertyToCast)
106 if (propertyToCast == null)
109 return propertyToCast.ToString ();
112 public override string ToString ()
114 if (finalValue != null)
120 internal void Evaluate ()
122 BuildProperty evaluated = new BuildProperty (Name, Value);
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);
130 parentProject.EvaluatedProperties.AddProperty (evaluated);
133 // during property's eval phase, this is never reached, as PropertyReference
134 // handles the eval case
136 // during item's eval phase, we have expand: true, that's what we
139 // during non-eval, expand: true
140 // So, its always true here
141 internal string ConvertToString (Project project, ExpressionOptions options)
144 // found ref to @this while trying to ConvertToString
151 Expression exp = new Expression ();
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);
162 internal ITaskItem[] ConvertToITaskItemArray (Project project, ExpressionOptions options)
165 // found ref to @this while trying to ConvertToITaskItemArray
167 ITaskItem []items = new ITaskItem [1];
168 items [0] = new TaskItem (FinalValue);
174 Expression exp = new Expression ();
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);
185 internal bool FromXml {
187 return propertyElement != null;
191 public string Condition {
194 return propertyElement.GetAttribute ("Condition");
200 propertyElement.SetAttribute ("Condition", value);
202 throw new InvalidOperationException ("Cannot set a condition on an object not represented by an XML element in the project file.");
206 public string FinalValue {
208 if (finalValue == null)
215 public bool IsImported {
216 get { return isImported; }
223 public string Value {
230 propertyElement.InnerXml = value;
237 internal PropertyType PropertyType {
243 internal XmlElement XmlElement {
244 get { return propertyElement; }
248 internal enum PropertyType {