LocationPathPattern.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //
  2. // Mono.Xml.XPath.LocationPathPattern
  3. //
  4. // Author:
  5. // Ben Maurer ([email protected])
  6. //
  7. // (C) 2003 Ben Maurer
  8. //
  9. using System;
  10. using System.Collections;
  11. using System.IO;
  12. using System.Xml;
  13. using System.Xml.Schema;
  14. using System.Xml.XPath;
  15. using System.Xml.Xsl;
  16. namespace Mono.Xml.XPath {
  17. internal class LocationPathPattern : Pattern {
  18. LocationPathPattern patternPrevious;
  19. bool isAncestor;
  20. NodeTest nodeTest;
  21. ExprFilter filter;
  22. XPathNavigator previousNavigator;
  23. public LocationPathPattern (NodeTest nodeTest)
  24. {
  25. this.nodeTest = nodeTest;
  26. }
  27. public LocationPathPattern (ExprFilter filter)
  28. {
  29. this.filter = filter;
  30. while (! (filter.expr is NodeTest))
  31. filter = (ExprFilter)filter.expr;
  32. this.nodeTest = (NodeTest)filter.expr;
  33. }
  34. internal void SetPreviousPattern (Pattern prev, bool isAncestor)
  35. {
  36. LocationPathPattern toSet = LastPathPattern;
  37. toSet.patternPrevious = (LocationPathPattern)prev;
  38. toSet.isAncestor = isAncestor;
  39. }
  40. public override double DefaultPriority {
  41. get {
  42. if (patternPrevious == null && filter == null) {
  43. NodeNameTest t = nodeTest as NodeNameTest;
  44. if (t != null) {
  45. if (t.Name.Name == "*")
  46. return -.25;
  47. return 0;
  48. }
  49. return -.5;
  50. }
  51. return .5;
  52. }
  53. }
  54. public override bool Matches (XPathNavigator node, XsltContext ctx)
  55. {
  56. if (! nodeTest.Match (ctx, node))
  57. return false;
  58. if (nodeTest is NodeTypeTest) {
  59. // node () is different in xslt patterns
  60. if (((NodeTypeTest)nodeTest).type == XPathNodeType.All &&
  61. (node.NodeType == XPathNodeType.Root ||
  62. node.NodeType == XPathNodeType.Attribute)
  63. )
  64. return false;
  65. }
  66. if (filter == null && patternPrevious == null)
  67. return true;
  68. if (patternPrevious != null) {
  69. if (!isAncestor) {
  70. XPathNavigator parent = node.Clone ();
  71. parent.MoveToParent ();
  72. if (!patternPrevious.Matches (parent, ctx))
  73. return false;
  74. } else {
  75. XPathNavigator anc = node.Clone ();
  76. while (true) {
  77. if (!anc.MoveToParent ())
  78. return false;
  79. if (patternPrevious.Matches (anc, ctx))
  80. break;
  81. }
  82. }
  83. }
  84. if (filter == null)
  85. return true;
  86. // Optimization for non-positional predicate
  87. if (!filter.IsPositional && !(filter.expr is ExprFilter)) {
  88. return filter.pred.EvaluateBoolean (new NullIterator (node, ctx));
  89. }
  90. XPathNavigator p = null;
  91. if (previousNavigator == node) {
  92. p = previousNavigator;
  93. p.MoveTo (node);
  94. } else {
  95. p = node.Clone ();
  96. previousNavigator = p;
  97. }
  98. p.MoveToParent ();
  99. BaseIterator matches = filter.EvaluateNodeSet (new NullIterator (p, ctx));
  100. while (matches.MoveNext ()) {
  101. if (node.IsSamePosition (matches.Current))
  102. return true;
  103. }
  104. return false;
  105. }
  106. public override string ToString ()
  107. {
  108. string ret = "";
  109. if (patternPrevious != null) ret = patternPrevious.ToString () + (isAncestor ? "//" : "/");
  110. if (filter != null) ret += filter.ToString ();
  111. else ret += nodeTest.ToString ();
  112. return ret;
  113. }
  114. public LocationPathPattern LastPathPattern {
  115. get {
  116. LocationPathPattern ret = this;
  117. while (ret.patternPrevious != null)
  118. ret = ret.patternPrevious;
  119. return ret;
  120. }
  121. }
  122. }
  123. }