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;
31 namespace System.Xml.Linq
33 public abstract class XObject : IXmlLineInfo
40 List<object> annotations;
44 public event EventHandler<XObjectChangeEventArgs> Changing;
45 public event EventHandler<XObjectChangeEventArgs> Changed;
47 public string BaseUri {
48 get { return baseuri; }
49 internal set { baseuri = value; }
52 public XDocument Document {
54 if (this is XDocument)
55 return (XDocument) this;
57 for (XContainer e = owner; e != null; e = e.owner)
64 public abstract XmlNodeType NodeType { get; }
66 public XElement Parent {
67 get { return owner as XElement; }
70 internal XContainer Owner {
74 internal void SetOwner (XContainer node)
79 public void AddAnnotation (object annotation)
81 if (annotation == null)
82 throw new ArgumentNullException ("annotation");
83 if (annotations == null)
84 annotations = new List<object> ();
85 annotations.Add (annotation);
88 public T Annotation<T> () where T : class
90 return (T) Annotation (typeof (T));
93 public object Annotation (Type type)
95 if (annotations != null)
96 foreach (object o in annotations)
97 if (o.GetType () == type)
99 throw new ArgumentException ();
102 public IEnumerable<T> Annotations<T> () where T : class
104 foreach (T o in Annotations (typeof (T)))
108 public IEnumerable<object> Annotations (Type type)
110 if (annotations == null)
112 foreach (object o in annotations)
113 if (o.GetType () == type)
117 public void RemoveAnnotations<T> () where T : class
119 RemoveAnnotations (typeof (T));
122 public void RemoveAnnotations (Type type)
124 if (annotations == null)
126 for (int i = 0; i < annotations.Count; i++)
127 if (annotations [i].GetType () == type)
128 annotations.RemoveAt (i);
131 internal int LineNumber {
133 set { line = value; }
136 internal int LinePosition {
137 get { return column; }
138 set { column = value; }
141 int IXmlLineInfo.LineNumber {
142 get { return LineNumber; }
145 int IXmlLineInfo.LinePosition {
146 get { return LinePosition; }
149 bool IXmlLineInfo.HasLineInfo ()
154 internal void FillLineInfoAndBaseUri (XmlReader r, LoadOptions options)
156 if ((options & LoadOptions.SetLineInfo) != LoadOptions.None) {
157 IXmlLineInfo li = r as IXmlLineInfo;
158 if (li != null && li.HasLineInfo ()) {
159 LineNumber = li.LineNumber;
160 LinePosition = li.LinePosition;
163 if ((options & LoadOptions.SetBaseUri) != LoadOptions.None)