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.
34 using Microsoft.Build.Framework;
35 using Microsoft.Build.Utilities;
36 using Mono.XBuild.Utilities;
38 namespace Microsoft.Build.BuildEngine {
39 public class BuildProperty {
41 XmlElement propertyElement;
46 Project parentProject;
47 PropertyType propertyType;
54 public BuildProperty (string propertyName, string propertyValue)
55 : this (propertyName, propertyValue, PropertyType.Normal)
57 if (propertyName == null)
58 throw new ArgumentNullException ("propertyName");
59 if (propertyValue == null)
60 throw new ArgumentNullException ("propertyValue");
63 internal BuildProperty (string propertyName,
64 string propertyValue, PropertyType propertyType)
66 this.name = propertyName;
67 this.value = propertyValue;
68 this.finalValue = propertyValue;
69 this.propertyType = propertyType;
70 this.isImported = false;
73 internal BuildProperty (Project parentProject, XmlElement propertyElement)
75 if (propertyElement == null)
76 throw new ArgumentNullException ("propertyElement");
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;
87 public BuildProperty Clone (bool deepClone)
91 throw new NotImplementedException ();
93 return (BuildProperty) this.MemberwiseClone ();
96 throw new NotImplementedException ();
98 throw new InvalidOperationException ("A shallow clone of this object cannot be created.");
102 public static explicit operator string (BuildProperty propertyToCast)
104 if (propertyToCast == null)
107 return propertyToCast.ToString ();
110 public override string ToString ()
112 if (finalValue != null)
118 internal void Evaluate ()
120 BuildProperty evaluated = new BuildProperty (Name, Value);
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);
128 parentProject.EvaluatedProperties.AddProperty (evaluated);
131 // during property's eval phase, this is never reached, as PropertyReference
132 // handles the eval case
134 // during item's eval phase, we have expand: true, that's what we
137 // during non-eval, expand: true
138 // So, its always true here
139 internal string ConvertToString (Project project, ExpressionOptions options)
142 // found ref to @this while trying to ConvertToString
149 Expression exp = new Expression ();
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);
160 internal ITaskItem[] ConvertToITaskItemArray (Project project, ExpressionOptions options)
163 // found ref to @this while trying to ConvertToITaskItemArray
165 ITaskItem []items = new ITaskItem [1];
166 items [0] = new TaskItem (FinalValue);
172 Expression exp = new Expression ();
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);
183 internal bool FromXml {
185 return propertyElement != null;
189 public string Condition {
192 return propertyElement.GetAttribute ("Condition");
198 propertyElement.SetAttribute ("Condition", value);
200 throw new InvalidOperationException ("Cannot set a condition on an object not represented by an XML element in the project file.");
204 public string FinalValue {
206 if (finalValue == null)
213 public bool IsImported {
214 get { return isImported; }
221 public string Value {
228 propertyElement.InnerXml = value;
235 internal PropertyType PropertyType {
241 internal XmlElement XmlElement {
242 get { return propertyElement; }
246 internal enum PropertyType {