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 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939
|
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace System.ServiceModel.Dispatcher
{
using System.Runtime;
using System.Xml;
using System.Xml.XPath;
#if NO
//
// A message is just one more source of Xml data. To filter a message, we create a navigator over it that
// surfaces its contained Xml to the filter engine.
// In M5.1, we navigate messages by first writing them into a message document. This turns the message into an
// Xml DOM. we then get a navigator from the DOM.
// In M5.2, we'll navigate messages without requiring this step.
//
internal class MessageNavigator : GenericSeekableNavigator
{
StringBuilder builder;
int headerCollectionVersion;
bool loaded;
bool navigatesBody;
internal MessageNavigator(MessageNavigator nav)
: base(nav)
{
this.headerCollectionVersion = nav.headerCollectionVersion;
this.loaded = nav.loaded;
this.navigatesBody = nav.navigatesBody;
}
internal MessageNavigator(Message message, bool navigateBody)
: base()
{
this.navigatesBody = navigateBody;
this.Load(message, this.navigatesBody);
}
internal bool IsLoaded
{
get
{
return this.loaded;
}
}
internal bool NavigatesBody
{
get
{
return this.navigatesBody;
}
}
internal override void Clear()
{
base.Clear();
this.loaded = false;
this.navigatesBody = false;
}
public override XPathNavigator Clone()
{
return new MessageNavigator(this);
}
internal MessageNavigator Ensure(Message message, bool navigateBody)
{
// Rebuild the navigator if:
// If this navigator does not navigate on bodies and now we need to (or vice versa)
// Or the header collection changed under us
if (this.navigatesBody != navigateBody || message.Headers.CollectionVersion != this.headerCollectionVersion)
{
this.Load(message, navigateBody);
}
else
{
this.MoveToRoot();
}
return this;
}
// To load a message into a message document, we write the message into a buffer and then let XPathDocument
// load that buffer
internal void Load(Message message, bool navigatesBody)
{
if (null == this.builder)
{
this.builder = new StringBuilder(1024);
}
StringWriter stringWriter = new StringWriter(this.builder);
XmlWriter writer = new XmlTextWriter(stringWriter);
message.WriteMessage(writer, navigatesBody);
writer.Close();
StringReader reader = new StringReader(this.builder.ToString());
XPathDocument messageDoc = new XPathDocument(reader);
reader.Close();
this.builder.Length = 0;
this.Init(messageDoc.CreateNavigator());
this.loaded = true;
this.navigatesBody = navigatesBody;
this.headerCollectionVersion = message.Headers.CollectionVersion;
}
}
#endif
// To prevent XPaths from running forever etc, users can specify limits on:
// the # of nodes a filter or filter table should inspect
//
// This file contains navigators that impose these limits
internal interface INodeCounter
{
int CounterMarker { get; set; }
int MaxCounter { set; }
int ElapsedCount(int marker);
void Increase();
void IncreaseBy(int count);
}
internal class DummyNodeCounter : INodeCounter
{
internal static DummyNodeCounter Dummy = new DummyNodeCounter();
public int CounterMarker
{
get { return 0; }
set { }
}
public int MaxCounter
{
set { }
}
public int ElapsedCount(int marker) { return 0; }
public void Increase() { }
public void IncreaseBy(int count) { }
}
/// <summary>
/// Seekable navigators that wrap other navigators and doesn't exceed node counting limits
/// </summary>
internal class SafeSeekableNavigator : SeekableXPathNavigator, INodeCounter
{
SeekableXPathNavigator navigator;
SafeSeekableNavigator counter;
int nodeCount;
int nodeCountMax;
internal SafeSeekableNavigator(SafeSeekableNavigator nav)
{
this.navigator = (SeekableXPathNavigator)nav.navigator.Clone();
this.counter = nav.counter;
}
internal SafeSeekableNavigator(SeekableXPathNavigator navigator, int nodeCountMax)
{
this.navigator = navigator;
this.counter = this;
this.nodeCount = nodeCountMax;
this.nodeCountMax = nodeCountMax;
}
public override string BaseURI
{
get
{
return this.navigator.BaseURI;
}
}
public int CounterMarker
{
get
{
return this.counter.nodeCount;
}
set
{
this.counter.nodeCount = value;
}
}
public int MaxCounter
{
set
{
this.counter.nodeCountMax = value;
}
}
/// <summary>
/// Setting the current position moves this navigator to the location specified by the given position
/// </summary>
public override long CurrentPosition
{
get
{
return this.navigator.CurrentPosition;
}
set
{
this.navigator.CurrentPosition = value;
}
}
public override bool HasAttributes
{
get
{
return this.navigator.HasAttributes;
}
}
public override bool HasChildren
{
get
{
return this.navigator.HasChildren;
}
}
public override bool IsEmptyElement
{
get
{
return this.navigator.IsEmptyElement;
}
}
public override string LocalName
{
get
{
return this.navigator.LocalName;
}
}
public override string Name
{
get
{
return this.navigator.Name;
}
}
public override string NamespaceURI
{
get
{
return this.navigator.NamespaceURI;
}
}
public override XmlNameTable NameTable
{
get
{
return this.navigator.NameTable;
}
}
public override XPathNodeType NodeType
{
get
{
return this.navigator.NodeType;
}
}
public override string Prefix
{
get
{
return this.navigator.Prefix;
}
}
public override string Value
{
get
{
return this.navigator.Value;
}
}
public override string XmlLang
{
get
{
return this.navigator.XmlLang;
}
}
public override XPathNavigator Clone()
{
return new SafeSeekableNavigator(this);
}
#if NO
internal SafeNavigator CreateSafeXPathNavigator()
{
return new SafeNavigator(this, this.navigator);
}
#endif
public override XmlNodeOrder ComparePosition(XPathNavigator navigator)
{
if (navigator == null)
{
return XmlNodeOrder.Unknown;
}
SafeSeekableNavigator nav = navigator as SafeSeekableNavigator;
if (nav != null)
{
return this.navigator.ComparePosition(nav.navigator);
}
return XmlNodeOrder.Unknown;
}
public override XmlNodeOrder ComparePosition(long x, long y)
{
return this.navigator.ComparePosition(x, y);
}
public int ElapsedCount(int marker)
{
return marker - this.counter.nodeCount;
}
public override string GetLocalName(long nodePosition)
{
return this.navigator.GetLocalName(nodePosition);
}
public override string GetName(long nodePosition)
{
return this.navigator.GetName(nodePosition);
}
public override string GetNamespace(long nodePosition)
{
return this.navigator.GetNamespace(nodePosition);
}
public override XPathNodeType GetNodeType(long nodePosition)
{
return this.navigator.GetNodeType(nodePosition);
}
public override string GetValue(long nodePosition)
{
return this.navigator.GetValue(nodePosition);
}
public override string GetNamespace(string name)
{
this.IncrementNodeCount();
return this.navigator.GetNamespace(name);
}
public override string GetAttribute(string localName, string namespaceURI)
{
this.IncrementNodeCount();
return this.navigator.GetAttribute(localName, namespaceURI);
}
public void Increase()
{
this.IncrementNodeCount();
}
public void IncreaseBy(int count)
{
this.counter.nodeCount -= (count - 1);
Increase();
}
internal void IncrementNodeCount()
{
if (this.counter.nodeCount > 0)
{
this.counter.nodeCount--;
}
else
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XPathNavigatorException(SR.GetString(SR.FilterNodeQuotaExceeded, this.counter.nodeCountMax)));
}
}
#if NO
internal virtual void Init(SeekableXPathNavigator navigator, int nodeCountMax)
{
this.navigator = navigator;
this.nodeCount = nodeCountMax;
this.counter = this;
}
#endif
public override bool IsDescendant(XPathNavigator navigator)
{
if (navigator == null)
{
return false;
}
SafeSeekableNavigator nav = navigator as SafeSeekableNavigator;
if (nav != null)
{
return this.navigator.IsDescendant(nav.navigator);
}
return false;
}
public override bool IsSamePosition(XPathNavigator other)
{
if (other == null)
{
return false;
}
SafeSeekableNavigator nav = other as SafeSeekableNavigator;
if (nav != null)
{
return this.navigator.IsSamePosition(nav.navigator);
}
return false;
}
public override void MoveToRoot()
{
this.IncrementNodeCount();
this.navigator.MoveToRoot();
}
public override bool MoveToNextNamespace(XPathNamespaceScope namespaceScope)
{
this.IncrementNodeCount();
return this.navigator.MoveToNextNamespace(namespaceScope);
}
public override bool MoveToNextAttribute()
{
this.IncrementNodeCount();
return this.navigator.MoveToNextAttribute();
}
public override bool MoveToPrevious()
{
this.IncrementNodeCount();
return this.navigator.MoveToPrevious();
}
public override bool MoveToFirstAttribute()
{
this.IncrementNodeCount();
return this.navigator.MoveToFirstAttribute();
}
public override bool MoveToNamespace(string name)
{
this.IncrementNodeCount();
return this.navigator.MoveToNamespace(name);
}
public override bool MoveToParent()
{
this.IncrementNodeCount();
return this.navigator.MoveToParent();
}
public override bool MoveTo(XPathNavigator other)
{
if (other == null)
{
return false;
}
this.IncrementNodeCount();
SafeSeekableNavigator nav = other as SafeSeekableNavigator;
if (nav != null)
{
return this.navigator.MoveTo(nav.navigator);
}
return false;
}
public override bool MoveToId(string id)
{
this.IncrementNodeCount();
return this.navigator.MoveToId(id);
}
public override bool MoveToFirstChild()
{
this.IncrementNodeCount();
return this.navigator.MoveToFirstChild();
}
public override bool MoveToFirstNamespace(XPathNamespaceScope namespaceScope)
{
this.IncrementNodeCount();
return this.navigator.MoveToFirstNamespace(namespaceScope);
}
public override bool MoveToAttribute(string localName, string namespaceURI)
{
this.IncrementNodeCount();
return this.navigator.MoveToAttribute(localName, namespaceURI);
}
public override bool MoveToNext()
{
this.IncrementNodeCount();
return this.navigator.MoveToNext();
}
public override bool MoveToFirst()
{
this.IncrementNodeCount();
return this.navigator.MoveToFirst();
}
}
/// <summary>
/// The filter engine works with seekable navigators. This class takes a generic XPathNavigator implementation
/// and transforms it into a seekable navigator. Seekable navigators associate a 'position' to every node in a DOM.
///
/// This class maintains a (position, navigator) map. Cloning navigators is unavoidable - XPathNavigator offers
/// no other way to snapshot its current position. However, caching allows memory allocations to be avoided - but
/// only once the navigator is warmed up.
/// </summary>
internal class GenericSeekableNavigator : SeekableXPathNavigator
{
QueryBuffer<XPathNavigator> nodes;
long currentPosition;
XPathNavigator navigator;
GenericSeekableNavigator dom;
#if NO
internal GenericSeekableNavigator()
{
this.nodes = new QueryBuffer<XPathNavigator>(4);
this.currentPosition = -1;
}
#endif
internal GenericSeekableNavigator(XPathNavigator navigator)
{
this.navigator = navigator;
this.nodes = new QueryBuffer<XPathNavigator>(4);
this.currentPosition = -1;
this.dom = this;
}
internal GenericSeekableNavigator(GenericSeekableNavigator navigator)
{
this.navigator = navigator.navigator.Clone();
this.nodes = default(QueryBuffer<XPathNavigator>);
this.currentPosition = navigator.currentPosition;
this.dom = navigator.dom;
}
public override string BaseURI
{
get
{
return this.navigator.BaseURI;
}
}
public override bool HasAttributes
{
get
{
return this.navigator.HasAttributes;
}
}
public override bool HasChildren
{
get
{
return this.navigator.HasChildren;
}
}
#if NO
internal XPathNavigator InternalNavigator
{
get
{
return this.navigator;
}
}
#endif
public override bool IsEmptyElement
{
get
{
return this.navigator.IsEmptyElement;
}
}
public override string LocalName
{
get
{
return this.navigator.LocalName;
}
}
public override string Name
{
get
{
return this.navigator.Name;
}
}
public override string NamespaceURI
{
get
{
return this.navigator.NamespaceURI;
}
}
public override XmlNameTable NameTable
{
get
{
return this.navigator.NameTable;
}
}
public override XPathNodeType NodeType
{
get
{
return this.navigator.NodeType;
}
}
public override string Prefix
{
get
{
return this.navigator.Prefix;
}
}
public override string Value
{
get
{
return this.navigator.Value;
}
}
public override string XmlLang
{
get
{
return this.navigator.XmlLang;
}
}
/// <summary>
/// Setting the current position moves this navigator to the location specified by the given position
/// </summary>
public override long CurrentPosition
{
get
{
if (-1 == this.currentPosition)
{
this.SnapshotNavigator();
}
return this.currentPosition;
}
set
{
this.navigator.MoveTo(this[value]);
this.currentPosition = value;
}
}
/// <summary>
/// Return the XPathNavigator that has the given position
/// </summary>
internal XPathNavigator this[long nodePosition]
{
get
{
int pos = (int)nodePosition;
Fx.Assert(this.dom.nodes.IsValidIndex(pos) && null != this.dom.nodes[pos], "");
return this.dom.nodes[pos];
}
}
#if NO
internal virtual void Clear()
{
this.navigator = null;
this.currentPosition = -1;
}
#endif
public override XPathNavigator Clone()
{
return new GenericSeekableNavigator(this);
}
public override XmlNodeOrder ComparePosition(XPathNavigator navigator)
{
if (navigator == null)
{
return XmlNodeOrder.Unknown;
}
GenericSeekableNavigator nav = navigator as GenericSeekableNavigator;
if (nav != null)
{
return this.navigator.ComparePosition(nav.navigator);
}
return XmlNodeOrder.Unknown;
}
public override XmlNodeOrder ComparePosition(long x, long y)
{
XPathNavigator nodeX = this[x];
XPathNavigator nodeY = this[y];
return nodeX.ComparePosition(nodeY);
}
public override string GetLocalName(long nodePosition)
{
return this[nodePosition].LocalName;
}
public override string GetName(long nodePosition)
{
return this[nodePosition].Name;
}
public override string GetNamespace(long nodePosition)
{
return this[nodePosition].NamespaceURI;
}
public override XPathNodeType GetNodeType(long nodePosition)
{
return this[nodePosition].NodeType;
}
public override string GetValue(long nodePosition)
{
return this[nodePosition].Value;
}
public override string GetNamespace(string name)
{
return this.navigator.GetNamespace(name);
}
public override string GetAttribute(string localName, string namespaceURI)
{
return this.navigator.GetAttribute(localName, namespaceURI);
}
#if NO
internal void Init(XPathNavigator navigator)
{
Fx.Assert(null != navigator, "");
this.navigator = navigator;
this.currentPosition = -1;
}
#endif
public override bool IsDescendant(XPathNavigator navigator)
{
if (navigator == null)
{
return false;
}
GenericSeekableNavigator nav = navigator as GenericSeekableNavigator;
if (null != nav)
{
return this.navigator.IsDescendant(nav.navigator);
}
return false;
}
public override bool IsSamePosition(XPathNavigator other)
{
GenericSeekableNavigator nav = other as GenericSeekableNavigator;
if (null != nav)
{
return this.navigator.IsSamePosition(nav.navigator);
}
return false;
}
public override void MoveToRoot()
{
this.currentPosition = -1;
this.navigator.MoveToRoot();
}
public override bool MoveToNextNamespace(XPathNamespaceScope namespaceScope)
{
this.currentPosition = -1;
return this.navigator.MoveToNextNamespace(namespaceScope);
}
public override bool MoveToNextAttribute()
{
this.currentPosition = -1;
return this.navigator.MoveToNextAttribute();
}
public override bool MoveToPrevious()
{
this.currentPosition = -1;
return this.navigator.MoveToPrevious();
}
public override bool MoveToFirstAttribute()
{
this.currentPosition = -1;
return this.navigator.MoveToFirstAttribute();
}
public override bool MoveToNamespace(string name)
{
this.currentPosition = -1;
return this.navigator.MoveToNamespace(name);
}
public override bool MoveToParent()
{
this.currentPosition = -1;
return this.navigator.MoveToParent();
}
public override bool MoveTo(XPathNavigator other)
{
GenericSeekableNavigator nav = other as GenericSeekableNavigator;
if (null != nav)
{
if (this.navigator.MoveTo(nav.navigator))
{
this.currentPosition = nav.currentPosition;
return true;
}
}
return false;
}
public override bool MoveToId(string id)
{
this.currentPosition = -1;
return this.navigator.MoveToId(id);
}
public override bool MoveToFirstChild()
{
this.currentPosition = -1;
return this.navigator.MoveToFirstChild();
}
public override bool MoveToFirstNamespace(XPathNamespaceScope namespaceScope)
{
this.currentPosition = -1;
return this.navigator.MoveToFirstNamespace(namespaceScope);
}
public override bool MoveToAttribute(string localName, string namespaceURI)
{
this.currentPosition = -1;
return this.navigator.MoveToAttribute(localName, namespaceURI);
}
public override bool MoveToNext()
{
this.currentPosition = -1;
return this.navigator.MoveToNext();
}
public override bool MoveToFirst()
{
this.currentPosition = -1;
return this.navigator.MoveToFirst();
}
internal void SnapshotNavigator()
{
this.currentPosition = this.dom.nodes.Count;
this.dom.nodes.Add(this.navigator.Clone());
/*
if (this.currentPosition < this.nodes.Count)
{
// Use a cached navigator
XPathNavigator clonedNavigator = this.nodes[(int)this.currentPosition];
Fx.Assert(null != clonedNavigator, "");
clonedNavigator.MoveTo(this);
}
else
{
this.nodes.Add(this.navigator.Clone());
}
*/
}
#region IQueryBufferPool Members
#if NO
/// <summary>
/// Reset the pool by deleting it entirely and starting it over
/// </summary>
public void Reset()
{
this.nodes.count = 0;
this.nodes.TrimToCount();
}
public void Trim()
{
this.nodes.TrimToCount();
}
#endif
#endregion
}
}
|