LocationPathPattern.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. internal LocationPathPattern patternPrevious;
  19. internal bool isAncestor;
  20. internal NodeTest nodeTest;
  21. ExprFilter filter;
  22. public LocationPathPattern (NodeTest nodeTest)
  23. {
  24. this.nodeTest = nodeTest;
  25. }
  26. public LocationPathPattern (ExprFilter filter)
  27. {
  28. this.filter = filter;
  29. while (! (filter.expr is NodeTest))
  30. filter = (ExprFilter)filter.expr;
  31. this.nodeTest = (NodeTest)filter.expr;
  32. }
  33. internal void SetPreviousPattern (Pattern prev, bool isAncestor)
  34. {
  35. LocationPathPattern toSet = LastPathPattern;
  36. toSet.patternPrevious = (LocationPathPattern)prev;
  37. toSet.isAncestor = isAncestor;
  38. }
  39. public override double DefaultPriority {
  40. get {
  41. if (patternPrevious == null && filter == null) {
  42. NodeNameTest t = nodeTest as NodeNameTest;
  43. if (t != null) {
  44. if (t.Name.Name == "*")
  45. return -.25;
  46. return 0;
  47. }
  48. return -.5;
  49. }
  50. return .5;
  51. }
  52. }
  53. public override bool Matches (XPathNavigator node, XsltContext ctx)
  54. {
  55. if (! nodeTest.Match (ctx, node))
  56. return false;
  57. if (nodeTest is NodeTypeTest) {
  58. // node () is different in xslt patterns
  59. if (((NodeTypeTest)nodeTest).type == XPathNodeType.All &&
  60. (node.NodeType == XPathNodeType.Root ||
  61. node.NodeType == XPathNodeType.Attribute)
  62. )
  63. return false;
  64. }
  65. if (filter == null && patternPrevious == null)
  66. return true;
  67. if (patternPrevious != null) {
  68. if (!isAncestor) {
  69. XPathNavigator parent = node.Clone ();
  70. parent.MoveToParent ();
  71. if (!patternPrevious.Matches (parent, ctx))
  72. return false;
  73. } else {
  74. XPathNavigator anc = node.Clone ();
  75. while (true) {
  76. if (!anc.MoveToParent ())
  77. return false;
  78. if (patternPrevious.Matches (anc, ctx))
  79. break;
  80. }
  81. }
  82. }
  83. if (filter == null)
  84. return true;
  85. XPathNavigator p = node.Clone ();
  86. p.MoveToParent ();
  87. BaseIterator matches = filter.EvaluateNodeSet (new NullIterator (p, ctx));
  88. while (matches.MoveNext ()) {
  89. if (node.IsSamePosition (matches.Current))
  90. return true;
  91. }
  92. return false;
  93. }
  94. public override string ToString ()
  95. {
  96. string ret = "";
  97. if (patternPrevious != null) ret = patternPrevious.ToString () + (isAncestor ? "//" : "/");
  98. if (filter != null) ret += filter.ToString ();
  99. else ret += nodeTest.ToString ();
  100. return ret;
  101. }
  102. public LocationPathPattern LastPathPattern {
  103. get {
  104. LocationPathPattern ret = this;
  105. while (ret.patternPrevious != null)
  106. ret = ret.patternPrevious;
  107. return ret;
  108. }
  109. }
  110. }
  111. }