5 // Copyright 2007 Novell (http://www.novell.com)
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:
15 // The above copyright notice and this permission notice shall be
16 // included in all copies or substantial portions of the Software.
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.
28 using System.Collections.Generic;
32 namespace System.Xml.Linq
34 public abstract class XObject : IXmlLineInfo
41 List<object> annotations;
45 public event EventHandler<XObjectChangeEventArgs> Changing;
46 public event EventHandler<XObjectChangeEventArgs> Changed;
48 public string BaseUri {
49 get { return baseuri; }
50 internal set { baseuri = value; }
53 public XDocument Document {
55 if (this is XDocument)
56 return (XDocument) this;
58 for (XContainer e = owner; e != null; e = e.owner)
65 public abstract XmlNodeType NodeType { get; }
67 public XElement Parent {
68 get { return owner as XElement; }
71 internal XContainer Owner {
75 internal void SetOwner (XContainer node)
80 public void AddAnnotation (object annotation)
82 if (annotation == null)
83 throw new ArgumentNullException ("annotation");
84 if (annotations == null)
85 annotations = new List<object> ();
86 annotations.Add (annotation);
89 public T Annotation<T> () where T : class
91 return (T) Annotation (typeof (T));
94 public object Annotation (Type type)
96 return Annotations (type).FirstOrDefault ();
99 public IEnumerable<T> Annotations<T> () where T : class
101 foreach (T o in Annotations (typeof (T)))
105 public IEnumerable<object> Annotations (Type type)
108 throw new ArgumentNullException ("type");
109 if (annotations == null)
111 foreach (object o in annotations)
112 if (type.IsAssignableFrom (o.GetType ()))
116 public void RemoveAnnotations<T> () where T : class
118 RemoveAnnotations (typeof (T));
121 public void RemoveAnnotations (Type type)
123 if (annotations == null)
125 for (int i = 0; i < annotations.Count; i++)
126 if (type.IsAssignableFrom (annotations [i].GetType ()))
127 annotations.RemoveAt (i);
130 internal int LineNumber {
132 set { line = value; }
135 internal int LinePosition {
136 get { return column; }
137 set { column = value; }
140 int IXmlLineInfo.LineNumber {
141 get { return LineNumber; }
144 int IXmlLineInfo.LinePosition {
145 get { return LinePosition; }
148 bool IXmlLineInfo.HasLineInfo ()
153 internal void FillLineInfoAndBaseUri (XmlReader r, LoadOptions options)
155 if ((options & LoadOptions.SetLineInfo) != LoadOptions.None) {
156 IXmlLineInfo li = r as IXmlLineInfo;
157 if (li != null && li.HasLineInfo ()) {
158 LineNumber = li.LineNumber;
159 LinePosition = li.LinePosition;
162 if ((options & LoadOptions.SetBaseUri) != LoadOptions.None)
166 internal virtual void OnAddingObject ()
169 Parent.OnAddingObject ();
170 if (Changing != null)
171 Changing.Invoke (this, null);
174 internal virtual void OnAddedObject ()
177 Parent.OnAddedObject ();
179 Changed.Invoke (this, null);