New test.
[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 (null,
54                                         "Parameter \"propertyName\" cannot be null.");
55                         if (propertyValue == null)
56                                 throw new ArgumentNullException (null,
57                                         "Parameter \"propertyValue\" cannot be null.");
58                 }
59
60                 internal BuildProperty (string propertyName,
61                                 string propertyValue, PropertyType propertyType)
62                 {
63                         this.name = propertyName;
64                         this.value = propertyValue;
65                         this.finalValue = propertyValue;
66                         this.propertyType = propertyType;
67                         this.isImported = false;
68                 }
69
70                 internal BuildProperty (Project parentProject, XmlElement propertyElement)
71                 {
72                         if (propertyElement == null)
73                                 throw new ArgumentNullException ("propertyElement");
74
75                         this.propertyElement = propertyElement;
76                         this.propertyType = PropertyType.Normal;
77                         this.parentProject = parentProject;
78                         this.name = propertyElement.Name;
79                         this.value = propertyElement.InnerText;
80                         this.isImported = false;
81                 }
82
83                 [MonoTODO]
84                 public BuildProperty Clone (bool deepClone)
85                 {
86                         if (deepClone) {
87                                 if (FromXml) 
88                                         throw new NotImplementedException ();
89                                 else
90                                         return (BuildProperty) this.MemberwiseClone ();
91                         } else {
92                                 if (FromXml)
93                                         throw new NotImplementedException ();
94                                 else
95                                         throw new InvalidOperationException ("A shallow clone of this object cannot be created.");
96                         }
97                 }
98
99                 public static explicit operator string (BuildProperty propertyToCast)
100                 {
101                         if (propertyToCast == null)
102                                 throw new ArgumentNullException ("propertyToCast");
103                         return propertyToCast.ToString ();
104                 }
105
106                 public override string ToString ()
107                 {
108                         if (finalValue != null)
109                                 return finalValue;
110                         else
111                                 return Value;
112                 }
113
114                 internal void Evaluate ()
115                 {
116                         if (FromXml) {
117                                 OldExpression exp = new OldExpression (parentProject);
118                                 exp.ParseSource (Value);
119                                 finalValue = (string) exp.ConvertTo (typeof (string));
120                                 parentProject.EvaluatedProperties.AddProperty (this);
121                         }
122                 }
123
124                 private bool FromXml {
125                         get {
126                                 return propertyElement != null;
127                         }
128                 }
129         
130                 public string Condition {
131                         get {
132                                 if (FromXml)
133                                         return propertyElement.GetAttribute ("Condition");
134                                 else
135                                         return String.Empty;
136                         }
137                         set {
138                                 if (FromXml)
139                                         propertyElement.SetAttribute ("Condition", value);
140                         }
141                 }
142
143                 public string FinalValue {
144                         get {
145                                 if (finalValue == null)
146                                         return this.@value;
147                                 else
148                                         return finalValue;
149                         }
150                 }
151                 
152                 public bool IsImported {
153                         get { return isImported; }
154                 }
155
156                 public string Name {
157                         get { return name; }
158                 }
159
160                 public string Value {
161                         get {
162                                 return value;
163                         }
164                         set {
165                                 this.@value = value;
166                                 if (FromXml) {
167                                         propertyElement.InnerText = value;
168                                 } else {
169                                         finalValue = value;
170                                 }
171                         }
172                 }
173
174                 internal PropertyType PropertyType {
175                         get {
176                                 return propertyType;
177                         }
178                 }
179         }
180
181         internal enum PropertyType {
182                 Reserved,
183                 Global,
184                 Normal,
185                 Environment
186         }
187 }
188
189 #endif