1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
|
//
// System.Xml.XmlNode
//
// Author:
// Kral Ferch <kral_ferch@hotmail.com>
// Atsushi Enomoto <ginga@kit.hi-ho.ne.jp>
//
// (C) 2002 Kral Ferch
// (C) 2002 Atsushi Enomoto
//
using System;
using System.Collections;
using System.IO;
using System.Text;
using System.Xml.XPath;
namespace System.Xml
{
public abstract class XmlNode : ICloneable, IEnumerable, IXPathNavigable
{
#region Fields
XmlDocument ownerDocument;
XmlNode parentNode;
#endregion
#region Constructors
internal XmlNode (XmlDocument ownerDocument)
{
this.ownerDocument = ownerDocument;
}
#endregion
#region Properties
public virtual XmlAttributeCollection Attributes {
get { return null; }
}
public virtual string BaseURI {
get {
// Isn't it conformant to W3C XML Base Recommendation?
// As far as I tested, there are not...
return (ParentNode != null) ? ParentNode.BaseURI : OwnerDocument.BaseURI;
}
}
public virtual XmlNodeList ChildNodes {
get {
return new XmlNodeListChildren (this);
}
}
public virtual XmlNode FirstChild {
get {
if (LastChild != null) {
return LastLinkedChild.NextLinkedSibling;
}
else {
return null;
}
}
}
public virtual bool HasChildNodes {
get { return LastChild != null; }
}
[MonoTODO("confirm whether this way is right for each not-overriden types.")]
public virtual string InnerText {
get {
StringBuilder builder = new StringBuilder ();
AppendChildValues (this, builder);
return builder.ToString ();
}
set { throw new NotImplementedException (); }
}
private void AppendChildValues (XmlNode parent, StringBuilder builder)
{
XmlNode node = parent.FirstChild;
while (node != null) {
if (node.NodeType == XmlNodeType.Text)
builder.Append (node.Value);
AppendChildValues (node, builder);
node = node.NextSibling;
}
}
[MonoTODO("Setter.")]
public virtual string InnerXml {
get {
StringWriter sw = new StringWriter ();
XmlTextWriter xtw = new XmlTextWriter (sw);
WriteContentTo (xtw);
return sw.GetStringBuilder ().ToString ();
}
set { throw new NotImplementedException (); }
}
public virtual bool IsReadOnly {
get { return false; }
}
[System.Runtime.CompilerServices.IndexerName("Item")]
public virtual XmlElement this [string name] {
get {
foreach (XmlNode node in ChildNodes) {
if ((node.NodeType == XmlNodeType.Element) &&
(node.Name == name)) {
return (XmlElement) node;
}
}
return null;
}
}
[System.Runtime.CompilerServices.IndexerName("Item")]
public virtual XmlElement this [string localname, string ns] {
get {
foreach (XmlNode node in ChildNodes) {
if ((node.NodeType == XmlNodeType.Element) &&
(node.LocalName == localname) &&
(node.NamespaceURI == ns)) {
return (XmlElement) node;
}
}
return null;
}
}
public virtual XmlNode LastChild {
get { return LastLinkedChild; }
}
internal virtual XmlLinkedNode LastLinkedChild {
get { return null; }
set { }
}
public abstract string LocalName { get; }
public abstract string Name { get; }
public virtual string NamespaceURI {
get { return String.Empty; }
}
public virtual XmlNode NextSibling {
get { return null; }
}
public abstract XmlNodeType NodeType { get; }
internal virtual XPathNodeType XPathNodeType {
get {
return (XPathNodeType) (-1);
}
}
public virtual string OuterXml {
get {
StringWriter sw = new StringWriter ();
XmlTextWriter xtw = new XmlTextWriter (sw);
WriteTo (xtw);
return sw.GetStringBuilder ().ToString ();
}
}
public virtual XmlDocument OwnerDocument {
get { return ownerDocument; }
}
public virtual XmlNode ParentNode {
get { return parentNode; }
}
public virtual string Prefix {
get { return String.Empty; }
set {}
}
public virtual XmlNode PreviousSibling {
get { return null; }
}
public virtual string Value {
get { return null; }
set { throw new InvalidOperationException ("This node does not have a value"); }
}
internal virtual string XmlLang {
get {
if(Attributes != null)
foreach(XmlAttribute attr in Attributes)
if(attr.Name == "xml:lang")
return attr.Value;
return (ParentNode != null) ? ParentNode.XmlLang : OwnerDocument.XmlLang;
}
}
internal virtual XmlSpace XmlSpace {
get {
if(Attributes != null) {
foreach(XmlAttribute attr in Attributes) {
if(attr.Name == "xml:space") {
switch(attr.Value) {
case "preserve": return XmlSpace.Preserve;
case "default": return XmlSpace.Default;
}
break;
}
}
}
return (ParentNode != null) ? ParentNode.XmlSpace : OwnerDocument.XmlSpace;
}
}
#endregion
#region Methods
public virtual XmlNode AppendChild (XmlNode newChild)
{
// I assume that AppendChild(n) equals to InsertAfter(n, this.LastChild) or InsertBefore(n, null)
return InsertBefore (newChild, null);
// Below are formerly used logic.
/* XmlDocument ownerDoc = (NodeType == XmlNodeType.Document) ? (XmlDocument)this : OwnerDocument;
if (NodeType == XmlNodeType.Document || NodeType == XmlNodeType.Element || NodeType == XmlNodeType.Attribute || NodeType == XmlNodeType.DocumentFragment) {
if (IsReadOnly)
throw new ArgumentException ("The specified node is readonly.");
if (newChild.OwnerDocument != ownerDoc)
throw new ArgumentException ("Can't append a node created by another document.");
// checking validity finished. then appending...
ownerDoc.onNodeInserting (newChild, this);
if(newChild.ParentNode != null)
newChild.ParentNode.RemoveChild(newChild);
if(newChild.NodeType == XmlNodeType.DocumentFragment)
{
int x = newChild.ChildNodes.Count;
for(int i=0; i<x; i++)
{
// When this logic became to remove children in order, then index will have never to increments.
XmlNode n = newChild.ChildNodes [0];
this.AppendChild(n); // recursively invokes events. (It is compatible with MS implementation.)
}
}
else
{
XmlLinkedNode newLinkedChild = (XmlLinkedNode) newChild;
XmlLinkedNode lastLinkedChild = LastLinkedChild;
newLinkedChild.parentNode = this;
if (lastLinkedChild != null)
{
newLinkedChild.NextLinkedSibling = lastLinkedChild.NextLinkedSibling;
lastLinkedChild.NextLinkedSibling = newLinkedChild;
}
else
newLinkedChild.NextLinkedSibling = newLinkedChild;
LastLinkedChild = newLinkedChild;
ownerDoc.onNodeInserted (newChild, newChild.ParentNode);
}
return newChild;
} else
throw new InvalidOperationException();
*/ }
public virtual XmlNode Clone ()
{
// By MS document, it is equivalent to CloneNode(true).
return this.CloneNode (true);
}
public abstract XmlNode CloneNode (bool deep);
[MonoTODO]
public XPathNavigator CreateNavigator ()
{
return new XmlDocumentNavigator (this);
}
public IEnumerator GetEnumerator ()
{
return new XmlNodeListChildren (this).GetEnumerator ();
}
[MonoTODO("performance problem.")]
public virtual string GetNamespaceOfPrefix (string prefix)
{
XmlNamespaceManager nsmgr = ConstructNamespaceManager ();
return nsmgr.LookupNamespace (prefix);
}
[MonoTODO("performance problem.")]
public virtual string GetPrefixOfNamespace (string namespaceURI)
{
XmlNamespaceManager nsmgr = ConstructNamespaceManager ();
return nsmgr.LookupPrefix (namespaceURI);
}
object ICloneable.Clone ()
{
return Clone ();
}
IEnumerator IEnumerable.GetEnumerator ()
{
return GetEnumerator ();
}
public virtual XmlNode InsertAfter (XmlNode newChild, XmlNode refChild)
{
// I assume that insertAfter(n1, n2) equals to InsertBefore(n1, n2.PreviousSibling).
// I took this way because rather than calling InsertAfter() from InsertBefore()
// because current implementation of 'NextSibling' looks faster than 'PreviousSibling'.
XmlNode argNode = null;
if(refChild != null)
argNode = refChild.NextSibling;
else if(ChildNodes.Count > 0)
argNode = FirstChild;
return InsertBefore (newChild, argNode);
}
[MonoTODO("If inserted node is entity reference, then check conforming entity. Wait for DTD implementation.")]
public virtual XmlNode InsertBefore (XmlNode newChild, XmlNode refChild)
{
XmlDocument ownerDoc = (NodeType == XmlNodeType.Document) ? (XmlDocument)this : OwnerDocument;
if (NodeType == XmlNodeType.Document ||
NodeType == XmlNodeType.Element ||
NodeType == XmlNodeType.Attribute ||
NodeType == XmlNodeType.DocumentFragment) {
if (IsReadOnly)
throw new ArgumentException ("The specified node is readonly.");
if (newChild.OwnerDocument != ownerDoc)
throw new ArgumentException ("Can't append a node created by another document.");
if (refChild != null && newChild.OwnerDocument != refChild.OwnerDocument)
throw new ArgumentException ("argument nodes are on the different documents.");
if (refChild != null && this == ownerDoc &&
ownerDoc.DocumentElement != null &&
(newChild is XmlElement ||
newChild is XmlCharacterData ||
newChild is XmlEntityReference))
throw new XmlException ("cannot insert this node to this position.");
// checking validity finished. then appending...
ownerDoc.onNodeInserting (newChild, this);
if(newChild.ParentNode != null)
newChild.ParentNode.RemoveChild (newChild);
if(newChild.NodeType == XmlNodeType.DocumentFragment) {
int x = newChild.ChildNodes.Count;
for(int i=0; i<x; i++) {
XmlNode n = newChild.ChildNodes [0];
this.InsertBefore (n, refChild); // recursively invokes events. (It is compatible with MS implementation.)
}
}
else {
XmlLinkedNode newLinkedChild = (XmlLinkedNode) newChild;
XmlLinkedNode lastLinkedChild = LastLinkedChild;
newLinkedChild.parentNode = this;
if(refChild == null) {
// append last, so:
// * set nextSibling of previous lastchild to newChild
// * set lastchild = newChild
// * set next of newChild to firstChild
if(LastLinkedChild != null) {
XmlLinkedNode formerFirst = FirstChild as XmlLinkedNode;
LastLinkedChild.NextLinkedSibling = newLinkedChild;
LastLinkedChild = newLinkedChild;
newLinkedChild.NextLinkedSibling = formerFirst;
}
else {
LastLinkedChild = newLinkedChild;
LastLinkedChild.NextLinkedSibling = newLinkedChild; // FirstChild
}
}
else {
// append not last, so:
// * if newchild is first, then set next of lastchild is newChild.
// otherwise, set next of previous sibling to newChild
// * set next of newChild to refChild
XmlLinkedNode prev = refChild.PreviousSibling as XmlLinkedNode;
if(prev == null)
LastLinkedChild.NextLinkedSibling = newLinkedChild;
else
prev.NextLinkedSibling = newLinkedChild;
newLinkedChild.NextLinkedSibling = refChild as XmlLinkedNode;
}
ownerDoc.onNodeInserted (newChild, newChild.ParentNode);
}
return newChild;
}
else
throw new InvalidOperationException ();
}
[MonoTODO]
public virtual void Normalize ()
{
throw new NotImplementedException ();
}
public virtual XmlNode PrependChild (XmlNode newChild)
{
return InsertAfter (newChild, null);
}
public virtual void RemoveAll ()
{
XmlNode next = null;
for (XmlNode node = FirstChild; node != null; node = next) {
next = node.NextSibling;
RemoveChild (node);
}
}
public virtual XmlNode RemoveChild (XmlNode oldChild)
{
XmlDocument ownerDoc = (NodeType == XmlNodeType.Document) ? (XmlDocument)this : OwnerDocument;
if(oldChild.ParentNode != this)
throw new XmlException ("specified child is not child of this node.");
ownerDoc.onNodeRemoving (oldChild, oldChild.ParentNode);
if (NodeType == XmlNodeType.Document || NodeType == XmlNodeType.Element || NodeType == XmlNodeType.Attribute || NodeType == XmlNodeType.DocumentFragment) {
if (IsReadOnly)
throw new ArgumentException ();
if (Object.ReferenceEquals (LastLinkedChild, LastLinkedChild.NextLinkedSibling) && Object.ReferenceEquals (LastLinkedChild, oldChild))
LastLinkedChild = null;
else {
XmlLinkedNode oldLinkedChild = (XmlLinkedNode)oldChild;
XmlLinkedNode beforeLinkedChild = LastLinkedChild;
while (!Object.ReferenceEquals (beforeLinkedChild.NextLinkedSibling, LastLinkedChild) && !Object.ReferenceEquals (beforeLinkedChild.NextLinkedSibling, oldLinkedChild))
beforeLinkedChild = beforeLinkedChild.NextLinkedSibling;
if (!Object.ReferenceEquals (beforeLinkedChild.NextLinkedSibling, oldLinkedChild))
throw new ArgumentException ();
beforeLinkedChild.NextLinkedSibling = oldLinkedChild.NextLinkedSibling;
oldLinkedChild.NextLinkedSibling = null;
}
ownerDoc.onNodeRemoved (oldChild, oldChild.ParentNode);
oldChild.parentNode = null; // clear parent 'after' above logic.
return oldChild;
}
else
throw new ArgumentException ();
}
[MonoTODO]
public virtual XmlNode ReplaceChild (XmlNode newChild, XmlNode oldChild)
{
throw new NotImplementedException ();
}
public XmlNodeList SelectNodes (string xpath)
{
return SelectNodes (xpath, null);
}
[MonoTODO]
public XmlNodeList SelectNodes (string xpath, XmlNamespaceManager nsmgr)
{
XPathNavigator nav = CreateNavigator ();
XPathExpression expr = nav.Compile (xpath);
if (nsmgr != null)
expr.SetContext (nsmgr);
XPathNodeIterator iter = nav.Select (expr);
ArrayList rgNodes = new ArrayList ();
while (iter.MoveNext ())
{
rgNodes.Add (((XmlDocumentNavigator) iter.Current).Node);
}
return new XmlNodeArrayList (rgNodes);
}
public XmlNode SelectSingleNode (string xpath)
{
return SelectSingleNode (xpath, null);
}
[MonoTODO]
public XmlNode SelectSingleNode (string xpath, XmlNamespaceManager nsmgr)
{
XPathNavigator nav = CreateNavigator ();
XPathExpression expr = nav.Compile (xpath);
if (nsmgr != null)
expr.SetContext (nsmgr);
XPathNodeIterator iter = nav.Select (expr);
if (!iter.MoveNext ())
return null;
return ((XmlDocumentNavigator) iter.Current).Node;
}
internal void SetParentNode (XmlNode parent)
{
parentNode = parent;
}
[MonoTODO]
public virtual bool Supports (string feature, string version)
{
throw new NotImplementedException ();
}
public abstract void WriteContentTo (XmlWriter w);
public abstract void WriteTo (XmlWriter w);
// It parses this and all the ancestor elements,
// find 'xmlns' declarations, stores and then return them.
// TODO: tests
internal XmlNamespaceManager ConstructNamespaceManager ()
{
XmlDocument doc = this is XmlDocument ? (XmlDocument)this : this.OwnerDocument;
XmlNamespaceManager nsmgr = new XmlNamespaceManager (doc.NameTable);
XmlElement el = null;
switch(this.NodeType) {
case XmlNodeType.Attribute:
el = ((XmlAttribute)this).OwnerElement;
break;
case XmlNodeType.Element:
el = this as XmlElement;
break;
default:
el = this.ParentNode as XmlElement;
break;
}
while(el != null) {
foreach(XmlAttribute attr in el.Attributes) {
if(attr.Prefix == "xmlns" || (attr.Name == "xmlns" && attr.Prefix == String.Empty)) {
if(nsmgr.LookupNamespace (attr.LocalName) == null )
nsmgr.AddNamespace (attr.LocalName, attr.Value);
}
}
// When reached to document, then it will set null value :)
el = el.ParentNode as XmlElement;
}
return nsmgr;
}
#endregion
}
}
|