ConfigurationElement.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. //
  2. // System.Configuration.ConfigurationElement.cs
  3. //
  4. // Authors:
  5. // Duncan Mak ([email protected])
  6. // Lluis Sanchez Gual ([email protected])
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining
  9. // a copy of this software and associated documentation files (the
  10. // "Software"), to deal in the Software without restriction, including
  11. // without limitation the rights to use, copy, modify, merge, publish,
  12. // distribute, sublicense, and/or sell copies of the Software, and to
  13. // permit persons to whom the Software is furnished to do so, subject to
  14. // the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be
  17. // included in all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  23. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  24. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  25. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. //
  27. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  28. //
  29. #if NET_2_0 && XML_DEP
  30. #if XML_DEP
  31. using System.Collections;
  32. using System.Xml;
  33. using System.Reflection;
  34. using System.IO;
  35. using System.ComponentModel;
  36. namespace System.Configuration
  37. {
  38. public abstract class ConfigurationElement
  39. {
  40. static Hashtable elementMaps = new Hashtable ();
  41. Hashtable values;
  42. string rawXml;
  43. bool modified;
  44. ElementMap map;
  45. protected ConfigurationElement ()
  46. {
  47. map = GetMap (GetType());
  48. }
  49. internal string RawXml {
  50. get { return rawXml; }
  51. set { rawXml = value; }
  52. }
  53. protected internal virtual ConfigurationPropertyCollection CollectionKeyProperties {
  54. get {
  55. return map.KeyProperties;
  56. }
  57. }
  58. protected internal object this [ConfigurationProperty property] {
  59. get {
  60. if (values == null || !values.ContainsKey (property)) {
  61. if (property.IsElement) {
  62. object elem = Activator.CreateInstance (property.Type);
  63. this [property] = elem;
  64. return elem;
  65. }
  66. else
  67. return property.DefaultValue;
  68. }
  69. else
  70. return values [property];
  71. }
  72. set {
  73. if (object.Equals (value, property.DefaultValue)) {
  74. if (values == null) return;
  75. values.Remove (property);
  76. }
  77. else {
  78. if (values == null) values = new Hashtable ();
  79. values [property] = value;
  80. }
  81. modified = true;
  82. }
  83. }
  84. protected internal object this [string property_name] {
  85. get {
  86. ConfigurationProperty prop = map.Properties [property_name];
  87. if (prop == null) throw new InvalidOperationException ("Property '" + property_name + "' not found in configuration section");
  88. return this [prop];
  89. }
  90. set {
  91. ConfigurationProperty prop = map.Properties [property_name];
  92. if (prop == null) throw new InvalidOperationException ("Property '" + property_name + "' not found in configuration section");
  93. this [prop] = value;
  94. }
  95. }
  96. protected internal virtual ConfigurationPropertyCollection Properties {
  97. get {
  98. return map.Properties;
  99. }
  100. }
  101. [MonoTODO]
  102. public override bool Equals (object compareTo)
  103. {
  104. return base.Equals (compareTo);
  105. }
  106. [MonoTODO]
  107. public override int GetHashCode ()
  108. {
  109. return base.GetHashCode ();
  110. }
  111. public bool HasValue (string key)
  112. {
  113. ConfigurationProperty prop = map.Properties [key];
  114. if (prop == null) return false;
  115. if (values == null) return false;
  116. return values.ContainsKey (prop);
  117. }
  118. [MonoTODO]
  119. public string PropertyFileName ()
  120. {
  121. throw new NotImplementedException ();
  122. }
  123. [MonoTODO]
  124. public int PropertyLineNumber ()
  125. {
  126. throw new NotImplementedException ();
  127. }
  128. [MonoTODO]
  129. protected internal virtual void Deserialize (
  130. XmlReader reader, bool serialize_collection_key)
  131. {
  132. throw new NotImplementedException ();
  133. }
  134. [MonoTODO]
  135. protected virtual bool HandleUnrecognizedAttribute (
  136. string name, string value)
  137. {
  138. throw new NotImplementedException ();
  139. }
  140. [MonoTODO]
  141. protected virtual bool HandleUnrecognizedElement (
  142. string element, XmlReader reader)
  143. {
  144. throw new NotImplementedException ();
  145. }
  146. [MonoTODO]
  147. protected internal virtual void InitializeDefault ()
  148. {
  149. values = null;
  150. }
  151. protected internal virtual bool IsModified ()
  152. {
  153. return modified;
  154. }
  155. [MonoTODO]
  156. protected internal virtual void ReadXml (XmlReader reader, object context)
  157. {
  158. Hashtable readProps = new Hashtable ();
  159. reader.MoveToContent ();
  160. if (!map.HasProperties) {
  161. reader.Skip ();
  162. return;
  163. }
  164. while (reader.MoveToNextAttribute ())
  165. {
  166. ConfigurationProperty prop = map.Properties [reader.LocalName];
  167. if (prop == null)
  168. throw new ConfigurationException ("Unrecognized attribute '" + reader.LocalName + "'.");
  169. if (readProps.Contains (prop))
  170. throw new ConfigurationException ("The attribute '" + prop.Name + "' may only appear once in this element.");
  171. object val = prop.ConvertFromString (reader.Value);
  172. if (!object.Equals (val, prop.DefaultValue))
  173. this [prop] = val;
  174. readProps [prop] = prop;
  175. }
  176. reader.MoveToElement ();
  177. if (reader.IsEmptyElement) {
  178. reader.Skip ();
  179. return;
  180. }
  181. reader.ReadStartElement ();
  182. reader.MoveToContent ();
  183. while (reader.NodeType != XmlNodeType.EndElement)
  184. {
  185. if (reader.NodeType != XmlNodeType.Element) {
  186. reader.Skip ();
  187. continue;
  188. }
  189. ConfigurationProperty prop = map.Properties [reader.LocalName];
  190. if (prop == null)
  191. throw new ConfigurationException ("Unrecognized element '" + reader.LocalName + "'.");
  192. if (!prop.IsElement)
  193. throw new ConfigurationException ("Property '" + prop.Name + "' is not a ConfigurationElement.");
  194. if (readProps.Contains (prop))
  195. throw new ConfigurationException ("The element <" + prop.Name + "> may only appear once in this section.");
  196. ConfigurationElement val = this [prop] as ConfigurationElement;
  197. val.ReadXml (reader, context);
  198. readProps [prop] = prop;
  199. }
  200. modified = false;
  201. }
  202. [MonoTODO]
  203. protected internal virtual void Reset (ConfigurationElement parent_element, object context)
  204. {
  205. if (parent_element != null) {
  206. if (!map.HasProperties) return;
  207. values = null;
  208. foreach (ConfigurationProperty prop in map.Properties) {
  209. if (parent_element.HasValue (prop.Name))
  210. this [prop] = parent_element [prop.Name];
  211. }
  212. }
  213. else
  214. InitializeDefault ();
  215. }
  216. protected internal virtual void ResetModified ()
  217. {
  218. modified = false;
  219. }
  220. [MonoTODO ("Return value?")]
  221. protected internal virtual bool Serialize (XmlWriter writer, bool serializeCollectionKey)
  222. {
  223. if (values == null || !map.HasProperties) return true;
  224. ArrayList elems = new ArrayList ();
  225. foreach (DictionaryEntry entry in values)
  226. {
  227. ConfigurationProperty prop = (ConfigurationProperty) entry.Key;
  228. if (serializeCollectionKey && !prop.IsKey) continue;
  229. if (prop.IsElement) continue;
  230. if (!object.Equals (entry.Value, prop.DefaultValue))
  231. writer.WriteAttributeString (prop.Name, prop.ConvertToString (entry.Value));
  232. }
  233. if (serializeCollectionKey) return true;
  234. foreach (DictionaryEntry entry in values)
  235. {
  236. ConfigurationProperty prop = (ConfigurationProperty) entry.Key;
  237. if (!prop.IsElement) continue;
  238. ConfigurationElement val = entry.Value as ConfigurationElement;
  239. if (val != null)
  240. val.SerializeToXmlElement (writer, prop.Name);
  241. }
  242. return true;
  243. }
  244. [MonoTODO]
  245. protected internal virtual bool SerializeAttributeOnRemove (
  246. ConfigurationProperty property)
  247. {
  248. throw new NotImplementedException ();
  249. }
  250. protected internal virtual bool SerializeToXmlElement (
  251. XmlWriter writer, string elementName)
  252. {
  253. writer.WriteStartElement (elementName);
  254. Serialize (writer, false);
  255. writer.WriteEndElement ();
  256. return true;
  257. }
  258. protected internal virtual void UnMerge (
  259. ConfigurationElement source, ConfigurationElement parent,
  260. bool serializeCollectionKey, object context,
  261. ConfigurationUpdateMode updateMode)
  262. {
  263. if (source.map != parent.map)
  264. throw new ConfigurationException ("Can't unmerge two elements of different type");
  265. ElementMap map = source.map;
  266. if (!map.HasProperties) return;
  267. foreach (ConfigurationProperty prop in map.Properties) {
  268. if (source.HasValue (prop.Name)) {
  269. object sourceValue = source [prop];
  270. if (!parent.HasValue (prop.Name)) {
  271. this [prop] = sourceValue;
  272. }
  273. else if (sourceValue != null) {
  274. object parentValue = parent [prop];
  275. if (parentValue != null && prop.IsElement) {
  276. ConfigurationElement copy = (ConfigurationElement) Activator.CreateInstance (prop.Type);
  277. copy.UnMerge ((ConfigurationElement) sourceValue, (ConfigurationElement) parentValue, serializeCollectionKey, context, updateMode);
  278. this [prop] = copy;
  279. }
  280. else {
  281. if (!object.Equals (sourceValue, parentValue))
  282. this [prop] = sourceValue;
  283. }
  284. }
  285. }
  286. }
  287. }
  288. [MonoTODO]
  289. protected virtual void ValidateRequiredProperties (
  290. ConfigurationPropertyCollection properties,
  291. bool serialize_collection_key)
  292. {
  293. throw new NotImplementedException ();
  294. }
  295. protected internal virtual string WriteXml (
  296. ConfigurationElement parent,
  297. object context, string name,
  298. ConfigurationUpdateMode updateMode)
  299. {
  300. ConfigurationElement elem;
  301. if (parent != null) {
  302. elem = (ConfigurationElement) Activator.CreateInstance (GetType());
  303. elem.UnMerge (this, parent, false, context, updateMode);
  304. }
  305. else
  306. elem = this;
  307. StringWriter sw = new StringWriter ();
  308. XmlTextWriter tw = new XmlTextWriter (sw);
  309. tw.Formatting = Formatting.Indented;
  310. elem.SerializeToXmlElement (tw, name);
  311. tw.Close ();
  312. return sw.ToString ();
  313. }
  314. internal static ElementMap GetMap (Type t)
  315. {
  316. lock (elementMaps) {
  317. ElementMap map = elementMaps [t] as ElementMap;
  318. if (map != null) return map;
  319. if (typeof(ConfigurationElementCollection).IsAssignableFrom (t))
  320. map = new CollectionElementMap (t);
  321. else
  322. map = new ElementMap (t);
  323. elementMaps [t] = map;
  324. return map;
  325. }
  326. }
  327. }
  328. internal class ElementMap
  329. {
  330. ConfigurationPropertyCollection properties;
  331. ConfigurationPropertyCollection keyProperties;
  332. public ElementMap (Type t)
  333. {
  334. ReflectProperties (t);
  335. }
  336. protected void ReflectProperties (Type t)
  337. {
  338. PropertyInfo[] props = t.GetProperties ();
  339. foreach (PropertyInfo prop in props)
  340. {
  341. ConfigurationPropertyAttribute at = (ConfigurationPropertyAttribute) Attribute.GetCustomAttribute (prop, typeof(ConfigurationPropertyAttribute)) as ConfigurationPropertyAttribute;
  342. if (at == null) continue;
  343. string name = at.Name != null ? at.Name : prop.Name;
  344. ConfigurationValidationAttribute validator = (ConfigurationValidationAttribute) Attribute.GetCustomAttribute (t, typeof(ConfigurationValidationAttribute)) as ConfigurationValidationAttribute;
  345. TypeConverter converter = TypeDescriptor.GetConverter (prop.PropertyType);
  346. ConfigurationProperty cp = new ConfigurationProperty (name, prop.PropertyType, at.DefaultValue, converter, validator, at.Flags);
  347. if (properties == null) properties = new ConfigurationPropertyCollection ();
  348. properties.Add (cp);
  349. }
  350. }
  351. public bool HasProperties
  352. {
  353. get { return properties != null && properties.Count > 0; }
  354. }
  355. public ConfigurationPropertyCollection Properties
  356. {
  357. get {
  358. if (properties == null) properties = new ConfigurationPropertyCollection ();
  359. return properties;
  360. }
  361. }
  362. public ConfigurationPropertyCollection KeyProperties {
  363. get {
  364. if (keyProperties == null) {
  365. keyProperties = new ConfigurationPropertyCollection ();
  366. if (properties != null)
  367. foreach (ConfigurationProperty p in properties)
  368. if (p.IsKey) keyProperties.Add (p);
  369. }
  370. return keyProperties;
  371. }
  372. }
  373. }
  374. internal class CollectionElementMap: ElementMap
  375. {
  376. public CollectionElementMap (Type t): base (t)
  377. {
  378. }
  379. }
  380. }
  381. #endif
  382. #endif