| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442 |
- //
- // System.Configuration.ConfigurationElement.cs
- //
- // Authors:
- // Duncan Mak ([email protected])
- // Lluis Sanchez Gual ([email protected])
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
- //
- #if NET_2_0 && XML_DEP
- #if XML_DEP
- using System.Collections;
- using System.Xml;
- using System.Reflection;
- using System.IO;
- using System.ComponentModel;
- namespace System.Configuration
- {
- public abstract class ConfigurationElement
- {
- static Hashtable elementMaps = new Hashtable ();
- Hashtable values;
- string rawXml;
- bool modified;
- ElementMap map;
-
- protected ConfigurationElement ()
- {
- map = GetMap (GetType());
- }
-
- internal string RawXml {
- get { return rawXml; }
- set { rawXml = value; }
- }
- protected internal virtual ConfigurationPropertyCollection CollectionKeyProperties {
- get {
- return map.KeyProperties;
- }
- }
- protected internal object this [ConfigurationProperty property] {
- get {
- if (values == null || !values.ContainsKey (property)) {
- if (property.IsElement) {
- object elem = Activator.CreateInstance (property.Type);
- this [property] = elem;
- return elem;
- }
- else
- return property.DefaultValue;
- }
- else
- return values [property];
- }
- set {
- if (object.Equals (value, property.DefaultValue)) {
- if (values == null) return;
- values.Remove (property);
- }
- else {
- if (values == null) values = new Hashtable ();
- values [property] = value;
- }
- modified = true;
- }
- }
- protected internal object this [string property_name] {
- get {
- ConfigurationProperty prop = map.Properties [property_name];
- if (prop == null) throw new InvalidOperationException ("Property '" + property_name + "' not found in configuration section");
- return this [prop];
- }
- set {
- ConfigurationProperty prop = map.Properties [property_name];
- if (prop == null) throw new InvalidOperationException ("Property '" + property_name + "' not found in configuration section");
- this [prop] = value;
- }
- }
- protected internal virtual ConfigurationPropertyCollection Properties {
- get {
- return map.Properties;
- }
- }
- [MonoTODO]
- public override bool Equals (object compareTo)
- {
- return base.Equals (compareTo);
- }
- [MonoTODO]
- public override int GetHashCode ()
- {
- return base.GetHashCode ();
- }
- public bool HasValue (string key)
- {
- ConfigurationProperty prop = map.Properties [key];
- if (prop == null) return false;
- if (values == null) return false;
- return values.ContainsKey (prop);
- }
- [MonoTODO]
- public string PropertyFileName ()
- {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- public int PropertyLineNumber ()
- {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- protected internal virtual void Deserialize (
- XmlReader reader, bool serialize_collection_key)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- protected virtual bool HandleUnrecognizedAttribute (
- string name, string value)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- protected virtual bool HandleUnrecognizedElement (
- string element, XmlReader reader)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- protected internal virtual void InitializeDefault ()
- {
- values = null;
- }
- protected internal virtual bool IsModified ()
- {
- return modified;
- }
- [MonoTODO]
- protected internal virtual void ReadXml (XmlReader reader, object context)
- {
- Hashtable readProps = new Hashtable ();
-
- reader.MoveToContent ();
- if (!map.HasProperties) {
- reader.Skip ();
- return;
- }
-
- while (reader.MoveToNextAttribute ())
- {
- ConfigurationProperty prop = map.Properties [reader.LocalName];
- if (prop == null)
- throw new ConfigurationException ("Unrecognized attribute '" + reader.LocalName + "'.");
-
- if (readProps.Contains (prop))
- throw new ConfigurationException ("The attribute '" + prop.Name + "' may only appear once in this element.");
-
- object val = prop.ConvertFromString (reader.Value);
- if (!object.Equals (val, prop.DefaultValue))
- this [prop] = val;
- readProps [prop] = prop;
- }
-
- reader.MoveToElement ();
- if (reader.IsEmptyElement) {
- reader.Skip ();
- return;
- }
-
- reader.ReadStartElement ();
- reader.MoveToContent ();
-
- while (reader.NodeType != XmlNodeType.EndElement)
- {
- if (reader.NodeType != XmlNodeType.Element) {
- reader.Skip ();
- continue;
- }
-
- ConfigurationProperty prop = map.Properties [reader.LocalName];
- if (prop == null)
- throw new ConfigurationException ("Unrecognized element '" + reader.LocalName + "'.");
-
- if (!prop.IsElement)
- throw new ConfigurationException ("Property '" + prop.Name + "' is not a ConfigurationElement.");
-
- if (readProps.Contains (prop))
- throw new ConfigurationException ("The element <" + prop.Name + "> may only appear once in this section.");
-
- ConfigurationElement val = this [prop] as ConfigurationElement;
- val.ReadXml (reader, context);
- readProps [prop] = prop;
- }
- modified = false;
- }
- [MonoTODO]
- protected internal virtual void Reset (ConfigurationElement parent_element, object context)
- {
- if (parent_element != null) {
- if (!map.HasProperties) return;
- values = null;
- foreach (ConfigurationProperty prop in map.Properties) {
- if (parent_element.HasValue (prop.Name))
- this [prop] = parent_element [prop.Name];
- }
- }
- else
- InitializeDefault ();
- }
- protected internal virtual void ResetModified ()
- {
- modified = false;
- }
- [MonoTODO ("Return value?")]
- protected internal virtual bool Serialize (XmlWriter writer, bool serializeCollectionKey)
- {
- if (values == null || !map.HasProperties) return true;
-
- ArrayList elems = new ArrayList ();
- foreach (DictionaryEntry entry in values)
- {
- ConfigurationProperty prop = (ConfigurationProperty) entry.Key;
- if (serializeCollectionKey && !prop.IsKey) continue;
- if (prop.IsElement) continue;
-
- if (!object.Equals (entry.Value, prop.DefaultValue))
- writer.WriteAttributeString (prop.Name, prop.ConvertToString (entry.Value));
- }
- if (serializeCollectionKey) return true;
-
- foreach (DictionaryEntry entry in values)
- {
- ConfigurationProperty prop = (ConfigurationProperty) entry.Key;
- if (!prop.IsElement) continue;
-
- ConfigurationElement val = entry.Value as ConfigurationElement;
- if (val != null)
- val.SerializeToXmlElement (writer, prop.Name);
- }
- return true;
- }
-
- [MonoTODO]
- protected internal virtual bool SerializeAttributeOnRemove (
- ConfigurationProperty property)
- {
- throw new NotImplementedException ();
- }
- protected internal virtual bool SerializeToXmlElement (
- XmlWriter writer, string elementName)
- {
- writer.WriteStartElement (elementName);
- Serialize (writer, false);
- writer.WriteEndElement ();
- return true;
- }
- protected internal virtual void UnMerge (
- ConfigurationElement source, ConfigurationElement parent,
- bool serializeCollectionKey, object context,
- ConfigurationUpdateMode updateMode)
- {
- if (source.map != parent.map)
- throw new ConfigurationException ("Can't unmerge two elements of different type");
-
- ElementMap map = source.map;
- if (!map.HasProperties) return;
-
- foreach (ConfigurationProperty prop in map.Properties) {
- if (source.HasValue (prop.Name)) {
- object sourceValue = source [prop];
- if (!parent.HasValue (prop.Name)) {
- this [prop] = sourceValue;
- }
- else if (sourceValue != null) {
- object parentValue = parent [prop];
- if (parentValue != null && prop.IsElement) {
- ConfigurationElement copy = (ConfigurationElement) Activator.CreateInstance (prop.Type);
- copy.UnMerge ((ConfigurationElement) sourceValue, (ConfigurationElement) parentValue, serializeCollectionKey, context, updateMode);
- this [prop] = copy;
- }
- else {
- if (!object.Equals (sourceValue, parentValue))
- this [prop] = sourceValue;
- }
- }
- }
- }
- }
- [MonoTODO]
- protected virtual void ValidateRequiredProperties (
- ConfigurationPropertyCollection properties,
- bool serialize_collection_key)
- {
- throw new NotImplementedException ();
- }
- protected internal virtual string WriteXml (
- ConfigurationElement parent,
- object context, string name,
- ConfigurationUpdateMode updateMode)
- {
- ConfigurationElement elem;
- if (parent != null) {
- elem = (ConfigurationElement) Activator.CreateInstance (GetType());
- elem.UnMerge (this, parent, false, context, updateMode);
- }
- else
- elem = this;
-
- StringWriter sw = new StringWriter ();
- XmlTextWriter tw = new XmlTextWriter (sw);
- tw.Formatting = Formatting.Indented;
- elem.SerializeToXmlElement (tw, name);
- tw.Close ();
- return sw.ToString ();
- }
-
- internal static ElementMap GetMap (Type t)
- {
- lock (elementMaps) {
- ElementMap map = elementMaps [t] as ElementMap;
- if (map != null) return map;
-
- if (typeof(ConfigurationElementCollection).IsAssignableFrom (t))
- map = new CollectionElementMap (t);
- else
- map = new ElementMap (t);
- elementMaps [t] = map;
- return map;
- }
- }
- }
-
- internal class ElementMap
- {
- ConfigurationPropertyCollection properties;
- ConfigurationPropertyCollection keyProperties;
-
- public ElementMap (Type t)
- {
- ReflectProperties (t);
- }
-
- protected void ReflectProperties (Type t)
- {
- PropertyInfo[] props = t.GetProperties ();
- foreach (PropertyInfo prop in props)
- {
- ConfigurationPropertyAttribute at = (ConfigurationPropertyAttribute) Attribute.GetCustomAttribute (prop, typeof(ConfigurationPropertyAttribute)) as ConfigurationPropertyAttribute;
- if (at == null) continue;
- string name = at.Name != null ? at.Name : prop.Name;
-
- ConfigurationValidationAttribute validator = (ConfigurationValidationAttribute) Attribute.GetCustomAttribute (t, typeof(ConfigurationValidationAttribute)) as ConfigurationValidationAttribute;
- TypeConverter converter = TypeDescriptor.GetConverter (prop.PropertyType);
- ConfigurationProperty cp = new ConfigurationProperty (name, prop.PropertyType, at.DefaultValue, converter, validator, at.Flags);
-
- if (properties == null) properties = new ConfigurationPropertyCollection ();
- properties.Add (cp);
- }
- }
-
- public bool HasProperties
- {
- get { return properties != null && properties.Count > 0; }
- }
-
- public ConfigurationPropertyCollection Properties
- {
- get {
- if (properties == null) properties = new ConfigurationPropertyCollection ();
- return properties;
- }
- }
-
- public ConfigurationPropertyCollection KeyProperties {
- get {
- if (keyProperties == null) {
- keyProperties = new ConfigurationPropertyCollection ();
-
- if (properties != null)
- foreach (ConfigurationProperty p in properties)
- if (p.IsKey) keyProperties.Add (p);
- }
- return keyProperties;
- }
- }
- }
-
- internal class CollectionElementMap: ElementMap
- {
- public CollectionElementMap (Type t): base (t)
- {
- }
- }
- }
- #endif
- #endif
|