Merge pull request #347 from JamesB7/master
[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 using System;
30 using System.Globalization;
31 using System.IO;
32 using System.ComponentModel;
33 using System.Runtime.Serialization.Formatters.Binary;
34 #if (XML_DEP)
35 using System.Xml;
36 using System.Xml.Serialization;
37 #endif
38
39 namespace System.Configuration
40 {
41
42         public class SettingsPropertyValue
43         {
44                 public SettingsPropertyValue (SettingsProperty property)
45                 {
46                         this.property = property;
47                         needPropertyValue = true;
48                 }
49
50                 public bool Deserialized {
51                         get {
52                                 return deserialized;
53                         }
54                         set {
55                                 deserialized = value;
56                         }
57                 }
58
59                 public bool IsDirty {
60                         get {
61                                 return dirty;
62                         }
63                         set {
64                                 dirty = value;
65                         }
66                 }
67
68                 public string Name {
69                         get {
70                                 return property.Name;
71                         }
72                 }
73
74                 public SettingsProperty Property {
75                         get {
76                                 return property;
77                         }
78                 }
79
80                 public object PropertyValue {
81                         get {
82                                 if (needPropertyValue) {
83                                         propertyValue = GetDeserializedValue (serializedValue);
84                                         if (propertyValue == null) {
85                                                 propertyValue = GetDeserializedDefaultValue ();
86                                                 defaulted = true;
87                                         }
88                                         needPropertyValue = false;
89                                 }
90
91                                 if (propertyValue != null &&
92                                         !(propertyValue is string) &&
93                                         !(propertyValue is DateTime) &&
94                                         !property.PropertyType.IsPrimitive)
95                                         dirty = true;
96
97                                 return propertyValue;
98                         }
99                         set {
100                                 propertyValue = value;
101                                 dirty = true;
102                                 needPropertyValue = false;
103                                 needSerializedValue = true;
104                                 defaulted = false;
105                         }
106                 }
107
108                 public object SerializedValue {
109                         get {
110                                 if (needSerializedValue) {
111                                         needSerializedValue = false;
112
113                                         switch (property.SerializeAs)
114                                         {
115                                         case SettingsSerializeAs.String:
116                                                 serializedValue = TypeDescriptor.GetConverter (property.PropertyType).ConvertToInvariantString (propertyValue);
117                                                 break;
118 #if (XML_DEP)
119                                         case SettingsSerializeAs.Xml:
120                                                 if (propertyValue != null) {
121                                                         XmlSerializer serializer = new XmlSerializer (propertyValue.GetType ());
122                                                         StringWriter w = new StringWriter(CultureInfo.InvariantCulture);
123         
124                                                         serializer.Serialize (w, propertyValue);
125                                                         serializedValue = w.ToString();
126                                                 }
127                                                 else
128                                                         serializedValue = null;
129                                                 break;
130 #endif
131                                         case SettingsSerializeAs.Binary:
132                                                 if (propertyValue != null) {
133                                                         BinaryFormatter bf = new BinaryFormatter ();
134                                                         MemoryStream ms = new MemoryStream ();
135                                                         bf.Serialize (ms, propertyValue);
136                                                         serializedValue = ms.ToArray();
137                                                 }
138                                                 else
139                                                         serializedValue = null;
140                                                 break;
141                                         default:
142                                                 serializedValue = null;
143                                                 break;
144                                         }
145
146                                 }
147
148                                 return serializedValue;
149                         }
150                         set {
151                                 serializedValue = value;
152                                 needPropertyValue = true;
153                         }
154                 }
155
156                 public bool UsingDefaultValue {
157                         get {
158                                 return defaulted;
159                         }
160                 }
161
162                 internal object Reset ()
163                 {
164                         propertyValue = GetDeserializedDefaultValue ();
165                         dirty = true;
166                         defaulted = true;
167                         needPropertyValue = true;
168                         return propertyValue;
169                 }
170
171                 private object GetDeserializedDefaultValue ()
172                 {
173                         if (property.DefaultValue == null)
174                                 if (property.PropertyType.IsValueType)
175                                         return Activator.CreateInstance (property.PropertyType);
176                                 else
177                                         return null;
178
179                         if (property.DefaultValue is string && ((string) property.DefaultValue).Length == 0)
180                                 if (property.PropertyType != typeof (string))
181                                         return Activator.CreateInstance (property.PropertyType);
182                                 else
183                                         return string.Empty;
184
185                         if (property.DefaultValue is string && ((string) property.DefaultValue).Length > 0)
186                                 return GetDeserializedValue (property.DefaultValue);
187
188                         if (!property.PropertyType.IsAssignableFrom (property.DefaultValue.GetType ())) {
189                                 TypeConverter converter = TypeDescriptor.GetConverter (property.PropertyType);
190                                 return converter.ConvertFrom (null, CultureInfo.InvariantCulture, property.DefaultValue);
191                         }
192                         return property.DefaultValue;
193                 }
194
195                 private object GetDeserializedValue (object serializedValue)
196                 {
197                         if (serializedValue == null)
198                                 return null;
199
200                         object deserializedObject = null;
201
202                         try {
203                                 switch (property.SerializeAs) {
204                                         case SettingsSerializeAs.String:
205                                                 if (serializedValue is string)
206                                                         deserializedObject = TypeDescriptor.GetConverter (property.PropertyType).ConvertFromInvariantString ((string) serializedValue);
207                                                 break;
208 #if (XML_DEP)
209                                         case SettingsSerializeAs.Xml:
210                                                 XmlSerializer serializer = new XmlSerializer (property.PropertyType);
211                                                 StringReader str = new StringReader ((string) serializedValue);
212                                                 deserializedObject = serializer.Deserialize (XmlReader.Create (str));
213                                                 break;
214 #endif
215                                         case SettingsSerializeAs.Binary:
216                                                 BinaryFormatter bf = new BinaryFormatter ();
217                                                 MemoryStream ms;
218                                                 if (serializedValue is string)
219                                                         ms = new MemoryStream (Convert.FromBase64String ((string) serializedValue));
220                                                 else
221                                                         ms = new MemoryStream ((byte []) serializedValue);
222                                                 deserializedObject = bf.Deserialize (ms);
223                                                 break;
224                                 }
225                         }
226                         catch (Exception e) {
227                                 if (property.ThrowOnErrorDeserializing)
228                                         throw e;
229                         }
230
231                         return deserializedObject;
232                 }
233
234                 readonly SettingsProperty property;
235                 object propertyValue;
236                 object serializedValue;
237                 bool needSerializedValue;
238                 bool needPropertyValue;
239                 bool dirty;
240                 bool defaulted = false;
241                 bool deserialized;
242         }
243
244 }
245