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.
33 using System.Collections.Generic;
35 using Microsoft.Build.Framework;
36 using Microsoft.Build.Utilities;
37 using Mono.XBuild.Utilities;
39 namespace Microsoft.Build.BuildEngine {
40 public class BuildProperty {
42 XmlElement propertyElement;
47 Project parentProject;
48 PropertyType propertyType;
55 public BuildProperty (string propertyName, string propertyValue)
56 : this (propertyName, propertyValue, PropertyType.Normal)
58 if (propertyName == null)
59 throw new ArgumentNullException ("propertyName");
60 if (propertyValue == null)
61 throw new ArgumentNullException ("propertyValue");
64 internal BuildProperty (string propertyName,
65 string propertyValue, PropertyType propertyType)
67 this.name = propertyName;
68 this.value = propertyValue;
69 this.finalValue = propertyValue;
70 this.propertyType = propertyType;
71 this.isImported = false;
74 internal BuildProperty (Project parentProject, XmlElement propertyElement)
76 if (propertyElement == null)
77 throw new ArgumentNullException ("propertyElement");
79 this.propertyElement = propertyElement;
80 this.propertyType = PropertyType.Normal;
81 this.parentProject = parentProject;
82 this.name = propertyElement.Name;
83 this.value = MSBuildUtils.UnescapeFromXml (propertyElement.InnerXml);
84 this.isImported = false;
88 public BuildProperty Clone (bool deepClone)
92 throw new NotImplementedException ();
94 return (BuildProperty) this.MemberwiseClone ();
97 throw new NotImplementedException ();
99 throw new InvalidOperationException ("A shallow clone of this object cannot be created.");
103 public static explicit operator string (BuildProperty propertyToCast)
105 if (propertyToCast == null)
108 return propertyToCast.ToString ();
111 public override string ToString ()
113 if (finalValue != null)
119 internal void Evaluate ()
121 BuildProperty evaluated = new BuildProperty (Name, Value);
123 // In evaluate phase, properties are not expanded
124 evaluated.finalValue = Expression.ParseAs<string> (Value, ParseOptions.None,
125 parentProject, ExpressionOptions.DoNotExpandItemRefs);
127 parentProject.EvaluatedProperties.AddProperty (evaluated);
130 // during property's eval phase, this is never reached, as PropertyReference
131 // handles the eval case
133 // during item's eval phase, we have expand: true, that's what we
136 // during non-eval, expand: true
137 // So, its always true here
138 internal string ConvertToString (Project project, ExpressionOptions options)
141 // found ref to @this while trying to ConvertToString
148 Expression exp = new Expression ();
150 // in non-evaluation phase, properties are always expanded
151 exp.Parse (FinalValue, options == ExpressionOptions.ExpandItemRefs ?
152 ParseOptions.AllowItems : ParseOptions.None);
153 return (string) exp.ConvertTo (project, typeof (string), options);
159 internal ITaskItem[] ConvertToITaskItemArray (Project project, ExpressionOptions options)
162 // found ref to @this while trying to ConvertToITaskItemArray
164 ITaskItem []items = new ITaskItem [1];
165 items [0] = new TaskItem (FinalValue);
171 Expression exp = new Expression ();
173 // in non-evaluation phase, properties are always expanded
174 exp.Parse (FinalValue, ParseOptions.Split | (options == ExpressionOptions.ExpandItemRefs ?
175 ParseOptions.AllowItems : ParseOptions.None));
176 return (ITaskItem[]) exp.ConvertTo (project, typeof (ITaskItem[]), options);
182 internal bool FromXml {
184 return propertyElement != null;
188 public string Condition {
191 return propertyElement.GetAttribute ("Condition");
197 propertyElement.SetAttribute ("Condition", value);
199 throw new InvalidOperationException ("Cannot set a condition on an object not represented by an XML element in the project file.");
203 public string FinalValue {
205 if (finalValue == null)
212 public bool IsImported {
213 get { return isImported; }
220 public string Value {
227 propertyElement.InnerXml = value;
234 internal PropertyType PropertyType {
240 internal XmlElement XmlElement {
241 get { return propertyElement; }
244 internal IEnumerable<string> GetAttributes ()
248 foreach (XmlAttribute attr in propertyElement.Attributes)
249 yield return attr.Value;
253 internal enum PropertyType {