PageRenderTime 46ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/System.Xml.Linq/System.Xml.Linq/XObject.cs

https://bitbucket.org/danipen/mono
C# | 223 lines | 163 code | 35 blank | 25 comment | 32 complexity | 88e88e76fb53bbc92e119fbb0f9a0232 MD5 | raw file
Possible License(s): Unlicense, Apache-2.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  1. //
  2. // Authors:
  3. // Atsushi Enomoto
  4. //
  5. // Copyright 2007 Novell (http://www.novell.com)
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining
  8. // a copy of this software and associated documentation files (the
  9. // "Software"), to deal in the Software without restriction, including
  10. // without limitation the rights to use, copy, modify, merge, publish,
  11. // distribute, sublicense, and/or sell copies of the Software, and to
  12. // permit persons to whom the Software is furnished to do so, subject to
  13. // the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be
  16. // included in all copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  22. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  23. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  24. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. //
  26. using System;
  27. using System.Collections.Generic;
  28. using System.Linq;
  29. using System.Xml;
  30. namespace System.Xml.Linq
  31. {
  32. public abstract class XObject : IXmlLineInfo
  33. {
  34. internal XObject ()
  35. {
  36. }
  37. XContainer owner;
  38. List<object> annotations;
  39. string baseuri;
  40. int line, column;
  41. public event EventHandler<XObjectChangeEventArgs> Changing;
  42. public event EventHandler<XObjectChangeEventArgs> Changed;
  43. public string BaseUri {
  44. get { return baseuri; }
  45. internal set { baseuri = value; }
  46. }
  47. public XDocument Document {
  48. get {
  49. if (this is XDocument)
  50. return (XDocument) this;
  51. for (XContainer e = owner; e != null; e = e.owner)
  52. if (e is XDocument)
  53. return (XDocument) e;
  54. return null;
  55. }
  56. }
  57. public abstract XmlNodeType NodeType { get; }
  58. public XElement Parent {
  59. get { return owner as XElement; }
  60. }
  61. internal XContainer Owner {
  62. get { return owner; }
  63. }
  64. internal void SetOwner (XContainer node)
  65. {
  66. owner = node;
  67. }
  68. public void AddAnnotation (object annotation)
  69. {
  70. if (annotation == null)
  71. throw new ArgumentNullException ("annotation");
  72. if (annotations == null)
  73. annotations = new List<object> ();
  74. annotations.Add (annotation);
  75. }
  76. public T Annotation<T> () where T : class
  77. {
  78. return (T) Annotation (typeof (T));
  79. }
  80. public object Annotation (Type type)
  81. {
  82. return Annotations (type).FirstOrDefault ();
  83. }
  84. public IEnumerable<T> Annotations<T> () where T : class
  85. {
  86. foreach (T o in Annotations (typeof (T)))
  87. yield return o;
  88. }
  89. public IEnumerable<object> Annotations (Type type)
  90. {
  91. if (type == null)
  92. throw new ArgumentNullException ("type");
  93. if (annotations == null)
  94. yield break;
  95. foreach (object o in annotations)
  96. if (type.IsAssignableFrom (o.GetType ()))
  97. yield return o;
  98. }
  99. public void RemoveAnnotations<T> () where T : class
  100. {
  101. RemoveAnnotations (typeof (T));
  102. }
  103. public void RemoveAnnotations (Type type)
  104. {
  105. if (annotations == null)
  106. return;
  107. for (int i = 0; i < annotations.Count; i++)
  108. if (type.IsAssignableFrom (annotations [i].GetType ()))
  109. annotations.RemoveAt (i);
  110. }
  111. internal int LineNumber {
  112. get { return line; }
  113. set { line = value; }
  114. }
  115. internal int LinePosition {
  116. get { return column; }
  117. set { column = value; }
  118. }
  119. int IXmlLineInfo.LineNumber {
  120. get { return LineNumber; }
  121. }
  122. int IXmlLineInfo.LinePosition {
  123. get { return LinePosition; }
  124. }
  125. bool IXmlLineInfo.HasLineInfo ()
  126. {
  127. return line > 0;
  128. }
  129. internal void FillLineInfoAndBaseUri (XmlReader r, LoadOptions options)
  130. {
  131. if ((options & LoadOptions.SetLineInfo) != LoadOptions.None) {
  132. IXmlLineInfo li = r as IXmlLineInfo;
  133. if (li != null && li.HasLineInfo ()) {
  134. LineNumber = li.LineNumber;
  135. LinePosition = li.LinePosition;
  136. }
  137. }
  138. if ((options & LoadOptions.SetBaseUri) != LoadOptions.None)
  139. BaseUri = r.BaseURI;
  140. }
  141. internal void OnAddingObject (object addedObject)
  142. {
  143. OnChanging (addedObject, new XObjectChangeEventArgs (XObjectChange.Add));
  144. }
  145. internal void OnAddedObject (object addedObject)
  146. {
  147. OnChanged (addedObject, new XObjectChangeEventArgs (XObjectChange.Add));
  148. }
  149. internal void OnNameChanging (object renamedObject)
  150. {
  151. OnChanging (renamedObject, new XObjectChangeEventArgs (System.Xml.Linq.XObjectChange.Name));
  152. }
  153. internal void OnNameChanged (object renamedObject)
  154. {
  155. OnChanged (renamedObject, new XObjectChangeEventArgs (System.Xml.Linq.XObjectChange.Name));
  156. }
  157. internal void OnRemovingObject (object removedObject)
  158. {
  159. OnChanging (removedObject, new XObjectChangeEventArgs (XObjectChange.Remove));
  160. }
  161. internal void OnRemovedObject (object removedObject)
  162. {
  163. OnChanged (removedObject, new XObjectChangeEventArgs (XObjectChange.Remove));
  164. }
  165. internal void OnValueChanging (object changedObject)
  166. {
  167. OnChanging (changedObject, new XObjectChangeEventArgs (XObjectChange.Value));
  168. }
  169. internal void OnValueChanged (object changedObject)
  170. {
  171. OnChanged (changedObject, new XObjectChangeEventArgs (XObjectChange.Value));
  172. }
  173. void OnChanging (object sender, XObjectChangeEventArgs args)
  174. {
  175. if (Changing != null)
  176. Changing (sender, args);
  177. if (Parent != null)
  178. Parent.OnChanging (sender, args);
  179. }
  180. void OnChanged (object sender, XObjectChangeEventArgs args)
  181. {
  182. if (Changed != null)
  183. Changed (sender, args);
  184. if (Parent != null)
  185. Parent.OnChanged (sender, args);
  186. }
  187. }
  188. }