Forgot this in changelog
[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 // 
7 // (C) 2005 Marek Sieradzki
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
28 #if NET_2_0
29
30 using System;
31 using System.Text;
32 using System.Xml;
33
34 namespace Microsoft.Build.BuildEngine {
35         public class BuildProperty {
36         
37                 XmlElement      propertyElement;
38                 string          finalValue;
39                 bool            isImported;
40                 string          value;
41                 string          name;
42                 Project         parentProject;
43                 PropertyType    propertyType;
44
45                 private BuildProperty ()
46                 {
47                 }
48
49                 public BuildProperty (string propertyName, string propertyValue)
50                         : this (propertyName, propertyValue, PropertyType.Normal)
51                 {
52                         if (propertyName == null)
53                                 throw new ArgumentNullException ("propertyName");
54                         if (propertyValue == null)
55                                 throw new ArgumentNullException ("propertyValue");
56                 }
57
58                 internal BuildProperty (string propertyName,
59                                 string propertyValue, PropertyType propertyType)
60                 {
61                         this.name = propertyName;
62                         this.value = propertyValue;
63                         this.finalValue = propertyValue;
64                         this.propertyType = propertyType;
65                         this.isImported = false;
66                 }
67
68                 internal BuildProperty (Project parentProject, XmlElement propertyElement)
69                 {
70                         if (propertyElement == null)
71                                 throw new ArgumentNullException ("propertyElement");
72
73                         this.propertyElement = propertyElement;
74                         this.propertyType = PropertyType.Normal;
75                         this.parentProject = parentProject;
76                         this.name = propertyElement.Name;
77                         this.value = propertyElement.InnerText;
78                         this.isImported = false;
79                 }
80
81                 [MonoTODO]
82                 public BuildProperty Clone (bool deepClone)
83                 {
84                         if (deepClone) {
85                                 if (FromXml) 
86                                         throw new NotImplementedException ();
87                                 else
88                                         return (BuildProperty) this.MemberwiseClone ();
89                         } else {
90                                 if (FromXml)
91                                         throw new NotImplementedException ();
92                                 else
93                                         throw new InvalidOperationException ("A shallow clone of this object cannot be created.");
94                         }
95                 }
96
97                 public static explicit operator string (BuildProperty propertyToCast)
98                 {
99                         if (propertyToCast == null)
100                                 return String.Empty;
101                         else
102                                 return propertyToCast.ToString ();
103                 }
104
105                 public override string ToString ()
106                 {
107                         if (finalValue != null)
108                                 return finalValue;
109                         else
110                                 return Value;
111                 }
112
113                 internal void Evaluate ()
114                 {
115                         if (FromXml) {
116                                 Expression exp = new Expression ();
117                                 exp.Parse (Value);
118                                 finalValue = (string) exp.ConvertTo (parentProject, typeof (string));
119                                 parentProject.EvaluatedProperties.AddProperty (this);
120                         }
121                 }
122
123                 private bool FromXml {
124                         get {
125                                 return propertyElement != null;
126                         }
127                 }
128         
129                 public string Condition {
130                         get {
131                                 if (FromXml)
132                                         return propertyElement.GetAttribute ("Condition");
133                                 else
134                                         return String.Empty;
135                         }
136                         set {
137                                 if (FromXml)
138                                         propertyElement.SetAttribute ("Condition", value);
139                                 else
140                                         throw new InvalidOperationException ("Cannot set a condition on an object not represented by an XML element in the project file.");
141                         }
142                 }
143
144                 public string FinalValue {
145                         get {
146                                 if (finalValue == null)
147                                         return this.@value;
148                                 else
149                                         return finalValue;
150                         }
151                 }
152                 
153                 public bool IsImported {
154                         get { return isImported; }
155                 }
156
157                 public string Name {
158                         get { return name; }
159                 }
160
161                 public string Value {
162                         get {
163                                 return value;
164                         }
165                         set {
166                                 this.@value = value;
167                                 if (FromXml) {
168                                         propertyElement.InnerText = value;
169                                 } else {
170                                         finalValue = value;
171                                 }
172                         }
173                 }
174
175                 internal PropertyType PropertyType {
176                         get {
177                                 return propertyType;
178                         }
179                 }
180         }
181
182         internal enum PropertyType {
183                 Reserved,
184                 Global,
185                 Normal,
186                 Environment
187         }
188 }
189
190 #endif