LocationPathPattern.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //
  2. // Mono.Xml.XPath.LocationPathPattern
  3. //
  4. // Author:
  5. // Ben Maurer ([email protected])
  6. //
  7. // (C) 2003 Ben Maurer
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Collections;
  31. using System.IO;
  32. using System.Xml;
  33. using System.Xml.Schema;
  34. using System.Xml.XPath;
  35. using System.Xml.Xsl;
  36. namespace Mono.Xml.XPath {
  37. internal class LocationPathPattern : Pattern {
  38. LocationPathPattern patternPrevious;
  39. bool isAncestor;
  40. NodeTest nodeTest;
  41. ExprFilter filter;
  42. XPathNavigator previousNavigator;
  43. public LocationPathPattern (NodeTest nodeTest)
  44. {
  45. this.nodeTest = nodeTest;
  46. }
  47. public LocationPathPattern (ExprFilter filter)
  48. {
  49. this.filter = filter;
  50. while (! (filter.expr is NodeTest))
  51. filter = (ExprFilter)filter.expr;
  52. this.nodeTest = (NodeTest)filter.expr;
  53. }
  54. internal void SetPreviousPattern (Pattern prev, bool isAncestor)
  55. {
  56. LocationPathPattern toSet = LastPathPattern;
  57. toSet.patternPrevious = (LocationPathPattern)prev;
  58. toSet.isAncestor = isAncestor;
  59. }
  60. public override double DefaultPriority {
  61. get {
  62. if (patternPrevious == null && filter == null) {
  63. NodeNameTest t = nodeTest as NodeNameTest;
  64. if (t != null) {
  65. if (t.Name.Name == "*")
  66. return -.25;
  67. return 0;
  68. }
  69. return -.5;
  70. }
  71. return .5;
  72. }
  73. }
  74. public override bool Matches (XPathNavigator node, XsltContext ctx)
  75. {
  76. if (! nodeTest.Match (ctx, node))
  77. return false;
  78. if (nodeTest is NodeTypeTest) {
  79. // node () is different in xslt patterns
  80. if (((NodeTypeTest)nodeTest).type == XPathNodeType.All &&
  81. (node.NodeType == XPathNodeType.Root ||
  82. node.NodeType == XPathNodeType.Attribute)
  83. )
  84. return false;
  85. }
  86. if (filter == null && patternPrevious == null)
  87. return true;
  88. if (patternPrevious != null) {
  89. if (!isAncestor) {
  90. XPathNavigator parent = node.Clone ();
  91. parent.MoveToParent ();
  92. if (!patternPrevious.Matches (parent, ctx))
  93. return false;
  94. } else {
  95. XPathNavigator anc = node.Clone ();
  96. while (true) {
  97. if (!anc.MoveToParent ())
  98. return false;
  99. if (patternPrevious.Matches (anc, ctx))
  100. break;
  101. }
  102. }
  103. }
  104. if (filter == null)
  105. return true;
  106. // Optimization for non-positional predicate
  107. if (!filter.IsPositional && !(filter.expr is ExprFilter)) {
  108. return filter.pred.EvaluateBoolean (new NullIterator (node, ctx));
  109. }
  110. XPathNavigator p = null;
  111. if (previousNavigator == node) {
  112. p = previousNavigator;
  113. p.MoveTo (node);
  114. } else {
  115. p = node.Clone ();
  116. previousNavigator = p;
  117. }
  118. p.MoveToParent ();
  119. BaseIterator matches = filter.EvaluateNodeSet (new NullIterator (p, ctx));
  120. while (matches.MoveNext ()) {
  121. if (node.IsSamePosition (matches.Current))
  122. return true;
  123. }
  124. return false;
  125. }
  126. public override string ToString ()
  127. {
  128. string ret = "";
  129. if (patternPrevious != null) ret = patternPrevious.ToString () + (isAncestor ? "//" : "/");
  130. if (filter != null) ret += filter.ToString ();
  131. else ret += nodeTest.ToString ();
  132. return ret;
  133. }
  134. public LocationPathPattern LastPathPattern {
  135. get {
  136. LocationPathPattern ret = this;
  137. while (ret.patternPrevious != null)
  138. ret = ret.patternPrevious;
  139. return ret;
  140. }
  141. }
  142. }
  143. }