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 ();
96 foreach (XNode n in XUtil.ToNodes (content)) {
103 if (Parent.LastNode == this)
108 public void AddAfterSelf (params object [] content)
111 throw new InvalidOperationException ();
113 foreach (object o in XUtil.ShrinkArray (content)) {
119 public void AddBeforeSelf (object content)
122 throw new InvalidOperationException ();
123 foreach (XNode n in XUtil.ToNodes (content)) {
125 n.previous = previous;
127 if (previous != null)
130 if (Parent.FirstNode == this)
131 Parent.FirstNode = n;
135 public void AddBeforeSelf (params object [] content)
138 throw new InvalidOperationException ();
139 foreach (object o in XUtil.ShrinkArray (content))
143 public static XNode ReadFrom (XmlReader r)
145 return ReadFrom (r, LoadOptions.None);
148 internal static XNode ReadFrom (XmlReader r, LoadOptions options)
150 switch (r.NodeType) {
151 case XmlNodeType.Element:
152 return XElement.LoadCore (r, options);
153 case XmlNodeType.Whitespace:
154 case XmlNodeType.SignificantWhitespace:
155 case XmlNodeType.Text:
156 XText t = new XText (r.Value);
157 t.FillLineInfoAndBaseUri (r, options);
160 case XmlNodeType.CDATA:
161 XCData c = new XCData (r.Value);
162 c.FillLineInfoAndBaseUri (r, options);
165 case XmlNodeType.ProcessingInstruction:
166 XPI pi = new XPI (r.Name, r.Value);
167 pi.FillLineInfoAndBaseUri (r, options);
170 case XmlNodeType.Comment:
171 XComment cm = new XComment (r.Value);
172 cm.FillLineInfoAndBaseUri (r, options);
175 case XmlNodeType.DocumentType:
176 XDocumentType d = new XDocumentType (r.Name,
177 r.GetAttribute ("PUBLIC"),
178 r.GetAttribute ("System"),
180 d.FillLineInfoAndBaseUri (r, options);
184 throw new InvalidOperationException (String.Format ("Node type {0} is not supported", r.NodeType));
188 public void Remove ()
191 throw new InvalidOperationException ("Parent is missing");
193 if (Parent.FirstNode == this)
194 Parent.FirstNode = next;
195 if (Parent.LastNode == this)
196 Parent.LastNode = previous;
197 if (previous != null)
198 previous.next = next;
200 next.previous = previous;
206 public override string ToString ()
208 return ToString (SaveOptions.None);
211 public abstract void WriteTo (XmlWriter w);
213 public IEnumerable<XElement> Ancestors ()
215 for (XElement el = Parent; el != null; el = el.Parent)
219 public IEnumerable<XElement> Ancestors (XName name)
221 foreach (XElement el in Ancestors ())
226 public XmlReader CreateReader ()
228 return new XNodeReader (this);
231 public IEnumerable<XElement> ElementsAfterSelf ()
233 foreach (XNode n in NodesAfterSelf ())
235 yield return (XElement) n;
238 public IEnumerable<XElement> ElementsAfterSelf (XName name)
240 foreach (XElement el in ElementsAfterSelf ())
245 public IEnumerable<XElement> ElementsBeforeSelf ()
247 foreach (XNode n in NodesBeforeSelf ())
249 yield return (XElement) n;
252 public IEnumerable<XElement> ElementsBeforeSelf (XName name)
254 foreach (XElement el in ElementsBeforeSelf ())
259 public bool IsAfter (XNode other)
261 return XNode.DocumentOrderComparer.Compare (this, other) > 0;
264 public bool IsBefore (XNode other)
266 return XNode.DocumentOrderComparer.Compare (this, other) < 0;
269 public IEnumerable<XNode> NodesAfterSelf ()
273 for (XNode n = NextNode; n != null; n = n.NextNode)
277 public IEnumerable<XNode> NodesBeforeSelf ()
279 for (XNode n = Parent.FirstNode; n != this; n = n.NextNode)
283 public void ReplaceWith (object item)
289 public void ReplaceWith (params object [] items)
291 AddAfterSelf (items);