2005-09-22 Chris Toshok <toshok@ximian.com>
[mono.git] / mcs / class / System / System.Configuration / SettingsPropertyValue.cs
1 //
2 // System.Web.UI.WebControls.SettingsPropertyValue.cs
3 //
4 // Authors:
5 //      Chris Toshok (toshok@ximian.com)
6 //
7 // (C) 2005 Novell, Inc (http://www.novell.com)
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
29 #if NET_2_0
30 using System;
31 using System.IO;
32 using System.Runtime.Serialization.Formatters.Binary;
33 using System.Xml.Serialization;
34
35 namespace System.Configuration
36 {
37
38         public class SettingsPropertyValue
39         {
40                 public SettingsPropertyValue (SettingsProperty property)
41                 {
42                         this.property = property;
43                         needPropertyValue = true;
44                 }
45
46                 public bool Deserialized {
47                         get {
48                                 return deserialized;
49                         }
50                         set {
51                                 deserialized = true;
52                         }
53                 }
54
55                 public bool IsDirty {
56                         get {
57                                 return dirty;
58                         }
59                         set {
60                                 dirty = value;
61                         }
62                 }
63
64                 public string Name {
65                         get {
66                                 return property.Name;
67                         }
68                 }
69
70                 public SettingsProperty Property {
71                         get {
72                                 return property;
73                         }
74                 }
75
76                 public object PropertyValue {
77                         get {
78                                 if (needPropertyValue) {
79                                         needPropertyValue = false;
80                                         propertyValue = property.DefaultValue;
81                                         defaulted = true;
82                                 }
83
84 #if notyet
85                                 /* LAMESPEC: the msdn2 docs say that
86                                  * for object types this
87                                  * pessimistically sets Dirty == true.
88                                  * tests, however, point out that that
89                                  * is not the case. */
90                                 if (!property.PropertyType.IsValueType)
91                                         dirty = true;
92 #endif
93
94                                 return propertyValue;
95                         }
96                         set {
97                                 propertyValue = value;
98                                 dirty = true;
99                                 needPropertyValue = false;
100                                 needSerializedValue = true;
101                                 defaulted = false;
102                         }
103                 }
104
105                 [MonoTODO ("string type converter?")]
106                 public object SerializedValue {
107                         get {
108                                 if (needSerializedValue) {
109                                         needSerializedValue = false;
110
111                                         switch (property.SerializeAs)
112                                         {
113                                         case SettingsSerializeAs.String:
114                                                 /* the docs say use a string type converter.. this means what? */
115                                                 serializedValue = propertyValue.ToString();
116                                                 break;
117                                         case SettingsSerializeAs.Xml:
118                                                 XmlSerializer serializer = new XmlSerializer (propertyValue.GetType());
119                                                 StringWriter w = new StringWriter();
120
121                                                 serializer.Serialize (w, propertyValue);
122                                                 serializedValue = w.ToString();
123                                                 break;
124                                         case SettingsSerializeAs.Binary:
125                                                 BinaryFormatter bf = new BinaryFormatter ();
126                                                 MemoryStream ms = new MemoryStream ();
127                                                 bf.Serialize (ms, propertyValue);
128                                                 serializedValue = ms.ToArray();
129                                                 break;
130                                         default:
131                                                 serializedValue = null;
132                                                 break;
133                                         }
134
135                                 }
136
137                                 return serializedValue;
138                         }
139                         set {
140                                 serializedValue = value;
141                         }
142                 }
143
144                 public bool UsingDefaultValue {
145                         get {
146                                 return defaulted;
147                         }
148                 }
149
150                 SettingsProperty property;
151                 object propertyValue;
152                 object serializedValue;
153                 bool needSerializedValue;
154                 bool needPropertyValue;
155                 bool dirty;
156                 bool defaulted;
157                 bool deserialized;
158         }
159
160 }
161
162 #endif