LocationPathPattern.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. using Mono.Xml.Xsl;
  37. namespace Mono.Xml.XPath {
  38. internal class LocationPathPattern : Pattern {
  39. LocationPathPattern patternPrevious;
  40. bool isAncestor;
  41. NodeTest nodeTest;
  42. ExprFilter filter;
  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 == "*" || t.Name.Name.Length == 0)
  66. return -.25;
  67. return 0;
  68. }
  69. return -.5;
  70. }
  71. return .5;
  72. }
  73. }
  74. public override XPathNodeType EvaluatedNodeType {
  75. get { return nodeTest.EvaluatedNodeType; }
  76. }
  77. public override bool Matches (XPathNavigator node, XsltContext ctx)
  78. {
  79. if (! nodeTest.Match (ctx, node))
  80. return false;
  81. if (nodeTest is NodeTypeTest) {
  82. // node () is different in xslt patterns
  83. if (((NodeTypeTest)nodeTest).type == XPathNodeType.All &&
  84. (node.NodeType == XPathNodeType.Root ||
  85. node.NodeType == XPathNodeType.Attribute)
  86. )
  87. return false;
  88. }
  89. if (filter == null && patternPrevious == null)
  90. return true;
  91. XPathNavigator tmpNav;
  92. if (patternPrevious != null) {
  93. tmpNav = ((XsltCompiledContext) ctx).GetNavCache (this, node);
  94. if (!isAncestor) {
  95. tmpNav.MoveToParent ();
  96. if (!patternPrevious.Matches (tmpNav, ctx))
  97. return false;
  98. } else {
  99. while (true) {
  100. if (!tmpNav.MoveToParent ())
  101. return false;
  102. if (patternPrevious.Matches (tmpNav, ctx))
  103. break;
  104. }
  105. }
  106. }
  107. if (filter == null)
  108. return true;
  109. // Optimization for non-positional predicate
  110. if (!filter.IsPositional && !(filter.expr is ExprFilter)) {
  111. return filter.pred.EvaluateBoolean (new NullIterator (node, ctx));
  112. }
  113. tmpNav = ((XsltCompiledContext) ctx).GetNavCache (this, node);
  114. tmpNav.MoveToParent ();
  115. BaseIterator matches = filter.EvaluateNodeSet (new NullIterator (tmpNav, ctx));
  116. while (matches.MoveNext ()) {
  117. if (node.IsSamePosition (matches.Current))
  118. return true;
  119. }
  120. return false;
  121. }
  122. public override string ToString ()
  123. {
  124. string ret = "";
  125. if (patternPrevious != null) ret = patternPrevious.ToString () + (isAncestor ? "//" : "/");
  126. if (filter != null) ret += filter.ToString ();
  127. else ret += nodeTest.ToString ();
  128. return ret;
  129. }
  130. public LocationPathPattern LastPathPattern {
  131. get {
  132. LocationPathPattern ret = this;
  133. while (ret.patternPrevious != null)
  134. ret = ret.patternPrevious;
  135. return ret;
  136. }
  137. }
  138. }
  139. }