New test.
[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.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 = true;
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 ();
84                                         if (propertyValue == null) {
85                                                 propertyValue = property.DefaultValue;
86                                                 defaulted = true;
87                                         }
88                                         needPropertyValue = false;
89                                 }
90
91 #if notyet
92                                 /* LAMESPEC: the msdn2 docs say that
93                                  * for object types this
94                                  * pessimistically sets Dirty == true.
95                                  * tests, however, point out that that
96                                  * is not the case. */
97                                 if (!property.PropertyType.IsValueType)
98                                         dirty = true;
99 #endif
100
101                                 return propertyValue;
102                         }
103                         set {
104                                 propertyValue = value;
105                                 dirty = true;
106                                 needPropertyValue = false;
107                                 needSerializedValue = true;
108                                 defaulted = false;
109                         }
110                 }
111
112                 [MonoTODO ("string type converter?")]
113                 public object SerializedValue {
114                         get {
115                                 if (needSerializedValue) {
116                                         needSerializedValue = false;
117
118                                         switch (property.SerializeAs)
119                                         {
120                                         case SettingsSerializeAs.String:
121                                                 /* the docs say use a string type converter.. this means what? */
122                                                 serializedValue = propertyValue.ToString();
123                                                 break;
124 #if (XML_DEP)
125                                         case SettingsSerializeAs.Xml:
126                                                 XmlSerializer serializer = new XmlSerializer (propertyValue.GetType());
127                                                 StringWriter w = new StringWriter();
128
129                                                 serializer.Serialize (w, propertyValue);
130                                                 serializedValue = w.ToString();
131                                                 break;
132 #endif
133                                         case SettingsSerializeAs.Binary:
134                                                 BinaryFormatter bf = new BinaryFormatter ();
135                                                 MemoryStream ms = new MemoryStream ();
136                                                 bf.Serialize (ms, propertyValue);
137                                                 serializedValue = ms.ToArray();
138                                                 break;
139                                         default:
140                                                 serializedValue = null;
141                                                 break;
142                                         }
143
144                                 }
145
146                                 return serializedValue;
147                         }
148                         set {
149                                 serializedValue = value;
150                                 needPropertyValue = true;
151                         }
152                 }
153
154                 public bool UsingDefaultValue {
155                         get {
156                                 return defaulted;
157                         }
158                 }
159
160                 private object GetDeserializedValue ()
161                 {
162                         if (serializedValue == null)
163                                 return null;
164
165                         object deserializedObject = null;
166
167                         try {
168                                 switch (property.SerializeAs) {
169                                         case SettingsSerializeAs.String:
170                                                 if (((string) serializedValue).Length > 0)
171                                                         deserializedObject = TypeDescriptor.GetConverter (property.PropertyType).ConvertFromString ((string) serializedValue);
172                                                 break;
173 #if (XML_DEP)
174                                         case SettingsSerializeAs.Xml:
175                                                 XmlSerializer serializer = new XmlSerializer (property.PropertyType);
176                                                 StringReader str = new StringReader ((string) serializedValue);
177                                                 deserializedObject = serializer.Deserialize (XmlReader.Create (str));
178                                                 break;
179 #endif
180                                         case SettingsSerializeAs.Binary:
181                                                 BinaryFormatter bf = new BinaryFormatter ();
182                                                 MemoryStream ms = new MemoryStream ((byte []) serializedValue);
183                                                 deserializedObject = bf.Deserialize (ms);
184                                                 break;
185                                 }
186                         }
187                         catch (Exception e) {
188                                 if (property.ThrowOnErrorDeserializing)
189                                         throw e;
190                         }
191
192                         return deserializedObject;
193                 }
194
195                 SettingsProperty property;
196                 object propertyValue;
197                 object serializedValue;
198                 bool needSerializedValue;
199                 bool needPropertyValue;
200                 bool dirty;
201                 bool defaulted = false;
202                 bool deserialized;
203         }
204
205 }
206
207 #endif