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;
29 using System.Collections.Generic;
34 using XPI = System.Xml.Linq.XProcessingInstruction;
36 namespace System.Xml.Linq
38 public abstract class XNode : XObject
40 public static int CompareDocumentOrder (XNode n1, XNode n2)
42 return order_comparer.Compare (n1, n2);
45 public static bool DeepEquals (XNode n1, XNode n2)
47 return eq_comparer.Equals (n1, n2);
50 static XNodeEqualityComparer eq_comparer =
51 new XNodeEqualityComparer ();
52 static XNodeDocumentOrderComparer order_comparer =
53 new XNodeDocumentOrderComparer ();
62 public static XNodeDocumentOrderComparer DocumentOrderComparer {
63 get { return order_comparer; }
66 public static XNodeEqualityComparer EqualityComparer {
67 get { return eq_comparer; }
70 public XNode PreviousNode {
71 get { return previous; }
72 internal set { previous = value; }
75 public XNode NextNode {
77 internal set { next = value; }
80 public string ToString (SaveOptions options)
82 StringWriter sw = new StringWriter ();
83 XmlWriterSettings s = new XmlWriterSettings ();
84 s.ConformanceLevel = ConformanceLevel.Auto;
85 s.Indent = options != SaveOptions.DisableFormatting;
86 XmlWriter xw = XmlWriter.Create (sw, s);
89 return sw.ToString ();
92 public void AddAfterSelf (object content)
95 throw new InvalidOperationException ();
98 foreach (object o in XUtil.ExpandArray (content)) {
99 if (o == null || Owner.OnAddingObject (o, true, here, false))
101 XNode n = XUtil.ToNode (o);
102 n = (XNode) XUtil.GetDetachedObject (n);
108 orgNext.previous = n;
115 public void AddAfterSelf (params object [] content)
118 throw new InvalidOperationException ();
119 AddAfterSelf ((object) content);
122 public void AddBeforeSelf (object content)
125 throw new InvalidOperationException ();
126 foreach (object o in XUtil.ExpandArray (content)) {
127 if (o == null || Owner.OnAddingObject (o, true, previous, true))
129 XNode n = XUtil.ToNode (o);
130 n = (XNode) XUtil.GetDetachedObject (n);
132 n.previous = previous;
134 if (previous != null)
137 if (Owner.FirstNode == this)
142 public void AddBeforeSelf (params object [] content)
145 throw new InvalidOperationException ();
146 AddBeforeSelf ((object) content);
149 public static XNode ReadFrom (XmlReader reader)
151 return ReadFrom (reader, LoadOptions.None);
154 internal static XNode ReadFrom (XmlReader r, LoadOptions options)
156 switch (r.NodeType) {
157 case XmlNodeType.Element:
158 return XElement.LoadCore (r, options);
159 case XmlNodeType.Whitespace:
160 case XmlNodeType.SignificantWhitespace:
161 case XmlNodeType.Text:
162 XText t = new XText (r.Value);
163 t.FillLineInfoAndBaseUri (r, options);
166 case XmlNodeType.CDATA:
167 XCData c = new XCData (r.Value);
168 c.FillLineInfoAndBaseUri (r, options);
171 case XmlNodeType.ProcessingInstruction:
172 XPI pi = new XPI (r.Name, r.Value);
173 pi.FillLineInfoAndBaseUri (r, options);
176 case XmlNodeType.Comment:
177 XComment cm = new XComment (r.Value);
178 cm.FillLineInfoAndBaseUri (r, options);
181 case XmlNodeType.DocumentType:
182 XDocumentType d = new XDocumentType (r.Name,
183 r.GetAttribute ("PUBLIC"),
184 r.GetAttribute ("SYSTEM"),
186 d.FillLineInfoAndBaseUri (r, options);
190 throw new InvalidOperationException (String.Format ("Node type {0} is not supported", r.NodeType));
194 public void Remove ()
197 throw new InvalidOperationException ("Owner is missing");
199 if (Owner.FirstNode == this)
200 Owner.FirstNode = next;
201 if (Owner.LastNode == this)
202 Owner.LastNode = previous;
203 if (previous != null)
204 previous.next = next;
206 next.previous = previous;
212 public override string ToString ()
214 return ToString (SaveOptions.None);
217 public abstract void WriteTo (XmlWriter writer);
219 public IEnumerable<XElement> Ancestors ()
221 for (XElement el = Parent; el != null; el = el.Parent)
225 public IEnumerable<XElement> Ancestors (XName name)
227 foreach (XElement el in Ancestors ())
232 public XmlReader CreateReader ()
234 return new XNodeReader (this);
238 public XmlReader CreateReader (ReaderOptions readerOptions)
240 var r = new XNodeReader (this);
241 if ((readerOptions & ReaderOptions.OmitDuplicateNamespaces) != 0)
242 r.OmitDuplicateNamespaces = true;
248 public IEnumerable<XElement> ElementsAfterSelf ()
250 foreach (XNode n in NodesAfterSelf ())
252 yield return (XElement) n;
255 public IEnumerable<XElement> ElementsAfterSelf (XName name)
257 foreach (XElement el in ElementsAfterSelf ())
262 public IEnumerable<XElement> ElementsBeforeSelf ()
264 foreach (XNode n in NodesBeforeSelf ())
266 yield return (XElement) n;
269 public IEnumerable<XElement> ElementsBeforeSelf (XName name)
271 foreach (XElement el in ElementsBeforeSelf ())
276 public bool IsAfter (XNode node)
278 return XNode.DocumentOrderComparer.Compare (this, node) > 0;
281 public bool IsBefore (XNode node)
283 return XNode.DocumentOrderComparer.Compare (this, node) < 0;
286 public IEnumerable<XNode> NodesAfterSelf ()
290 for (XNode n = NextNode; n != null; n = n.NextNode)
294 public IEnumerable<XNode> NodesBeforeSelf ()
298 for (XNode n = Owner.FirstNode; n != this; n = n.NextNode)
302 public void ReplaceWith (object content)
305 throw new InvalidOperationException ();
307 XNode here = previous;
308 XNode orgNext = next;
309 XContainer orgOwner = Owner;
311 foreach (object o in XUtil.ExpandArray (content)) {
312 if (o == null || orgOwner.OnAddingObject (o, true, here, false))
314 XNode n = XUtil.ToNode (o);
315 n = (XNode) XUtil.GetDetachedObject (n);
316 n.SetOwner (orgOwner);
321 orgOwner.FirstNode = n;
324 orgNext.previous = n;
326 orgOwner.LastNode = n;
331 public void ReplaceWith (params object [] content)
334 throw new InvalidOperationException ();
335 ReplaceWith ((object) content);