2 // System.Web.UI.WebControls.SettingValueElement.cs
5 // Chris Toshok (toshok@ximian.com)
7 // (C) 2005 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
30 using System.Reflection;
35 namespace System.Configuration
37 public sealed class SettingValueElement
38 #if (CONFIGURATION_DEP)
39 : ConfigurationElement
45 #if (CONFIGURATION_DEP)
52 public SettingValueElement ()
56 #if (CONFIGURATION_DEP)
58 protected override ConfigurationPropertyCollection Properties {
60 return base.Properties;
66 public XmlNode ValueXml {
71 #if (CONFIGURATION_DEP)
73 protected override void DeserializeElement (XmlReader reader, bool serializeCollectionKey)
75 original = new XmlDocument ().ReadNode (reader);
76 node = original.CloneNode (true);
81 public override bool Equals (object settingValue)
83 throw new NotImplementedException ();
86 public override int GetHashCode ()
88 throw new NotImplementedException ();
91 #if (CONFIGURATION_DEP)
92 protected override bool IsModified ()
94 return original != node;
97 protected override void Reset (ConfigurationElement parentElement)
102 protected override void ResetModified ()
108 #if (XML_DEP) && (CONFIGURATION_DEP)
109 protected override bool SerializeToXmlElement (XmlWriter writer, string elementName)
113 node.WriteTo (writer);
118 #if (CONFIGURATION_DEP)
119 protected override void Unmerge (ConfigurationElement sourceElement, ConfigurationElement parentElement, ConfigurationSaveMode saveMode)
121 if (parentElement != null && sourceElement.GetType() != parentElement.GetType())
122 throw new ConfigurationErrorsException ("Can't unmerge two elements of different type");
124 bool isMinimalOrModified = saveMode == ConfigurationSaveMode.Minimal ||
125 saveMode == ConfigurationSaveMode.Modified;
127 foreach (PropertyInformation prop in sourceElement.ElementInformation.Properties)
129 if (prop.ValueOrigin == PropertyValueOrigin.Default)
132 PropertyInformation unmergedProp = ElementInformation.Properties [prop.Name];
134 object sourceValue = prop.Value;
135 if (parentElement == null || !HasValue (parentElement, prop.Name)) {
136 unmergedProp.Value = sourceValue;
140 if (sourceValue == null)
143 object parentValue = GetItem (parentElement, prop.Name);
144 if (!PropertyIsElement (prop)) {
145 if (!object.Equals (sourceValue, parentValue) ||
146 (saveMode == ConfigurationSaveMode.Full) ||
147 (saveMode == ConfigurationSaveMode.Modified && prop.ValueOrigin == PropertyValueOrigin.SetHere))
148 unmergedProp.Value = sourceValue;
152 var sourceElem = (ConfigurationElement) sourceValue;
153 if (isMinimalOrModified && !ElementIsModified (sourceElem))
155 if (parentValue == null) {
156 unmergedProp.Value = sourceValue;
160 var parentElem = (ConfigurationElement) parentValue;
161 ConfigurationElement copy = (ConfigurationElement) unmergedProp.Value;
162 ElementUnmerge (copy, sourceElem, parentElem, saveMode);
166 bool HasValue (ConfigurationElement element, string propName)
168 PropertyInformation info = element.ElementInformation.Properties [propName];
169 return info != null && info.ValueOrigin != PropertyValueOrigin.Default;
172 object GetItem (ConfigurationElement element, string property)
174 PropertyInformation pi = ElementInformation.Properties [property];
176 throw new InvalidOperationException ("Property '" + property + "' not found in configuration element");
181 bool PropertyIsElement (PropertyInformation prop)
183 return (typeof(ConfigurationElement).IsAssignableFrom (prop.Type));
186 bool ElementIsModified (ConfigurationElement element)
188 return (bool) element.GetType ().GetMethod ("IsModified", BindingFlags.NonPublic | BindingFlags.Instance).Invoke (element, new object [0]);
191 void ElementUnmerge (ConfigurationElement target, ConfigurationElement sourceElement, ConfigurationElement parentElement, ConfigurationSaveMode saveMode)
193 target.GetType ().GetMethod ("Unmerge", BindingFlags.NonPublic | BindingFlags.Instance).Invoke (target, new object [] {sourceElement, parentElement, saveMode});