1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * $Id: HasPositionalPredChecker.java,v 1.4 2004/02/17 04:32:08 minchau Exp $
  18. */
  19. package com.sun.org.apache.xpath.internal.axes;
  20. import com.sun.org.apache.xpath.internal.Expression;
  21. import com.sun.org.apache.xpath.internal.ExpressionOwner;
  22. import com.sun.org.apache.xpath.internal.XPathVisitor;
  23. import com.sun.org.apache.xpath.internal.functions.FuncLast;
  24. import com.sun.org.apache.xpath.internal.functions.FuncPosition;
  25. import com.sun.org.apache.xpath.internal.functions.Function;
  26. import com.sun.org.apache.xpath.internal.objects.XNumber;
  27. import com.sun.org.apache.xpath.internal.operations.Div;
  28. import com.sun.org.apache.xpath.internal.operations.Minus;
  29. import com.sun.org.apache.xpath.internal.operations.Mod;
  30. import com.sun.org.apache.xpath.internal.operations.Mult;
  31. import com.sun.org.apache.xpath.internal.operations.Plus;
  32. import com.sun.org.apache.xpath.internal.operations.Quo;
  33. import com.sun.org.apache.xpath.internal.operations.Variable;
  34. public class HasPositionalPredChecker extends XPathVisitor
  35. {
  36. private boolean m_hasPositionalPred = false;
  37. private int m_predDepth = 0;
  38. /**
  39. * Process the LocPathIterator to see if it contains variables
  40. * or functions that may make it context dependent.
  41. * @param path LocPathIterator that is assumed to be absolute, but needs checking.
  42. * @return true if the path is confirmed to be absolute, false if it
  43. * may contain context dependencies.
  44. */
  45. public static boolean check(LocPathIterator path)
  46. {
  47. HasPositionalPredChecker hppc = new HasPositionalPredChecker();
  48. path.callVisitors(null, hppc);
  49. return hppc.m_hasPositionalPred;
  50. }
  51. /**
  52. * Visit a function.
  53. * @param owner The owner of the expression, to which the expression can
  54. * be reset if rewriting takes place.
  55. * @param func The function reference object.
  56. * @return true if the sub expressions should be traversed.
  57. */
  58. public boolean visitFunction(ExpressionOwner owner, Function func)
  59. {
  60. if((func instanceof FuncPosition) ||
  61. (func instanceof FuncLast))
  62. m_hasPositionalPred = true;
  63. return true;
  64. }
  65. // /**
  66. // * Visit a variable reference.
  67. // * @param owner The owner of the expression, to which the expression can
  68. // * be reset if rewriting takes place.
  69. // * @param var The variable reference object.
  70. // * @return true if the sub expressions should be traversed.
  71. // */
  72. // public boolean visitVariableRef(ExpressionOwner owner, Variable var)
  73. // {
  74. // m_hasPositionalPred = true;
  75. // return true;
  76. // }
  77. /**
  78. * Visit a predicate within a location path. Note that there isn't a
  79. * proper unique component for predicates, and that the expression will
  80. * be called also for whatever type Expression is.
  81. *
  82. * @param owner The owner of the expression, to which the expression can
  83. * be reset if rewriting takes place.
  84. * @param pred The predicate object.
  85. * @return true if the sub expressions should be traversed.
  86. */
  87. public boolean visitPredicate(ExpressionOwner owner, Expression pred)
  88. {
  89. m_predDepth++;
  90. if(m_predDepth == 1)
  91. {
  92. if((pred instanceof Variable) ||
  93. (pred instanceof XNumber) ||
  94. (pred instanceof Div) ||
  95. (pred instanceof Plus) ||
  96. (pred instanceof Minus) ||
  97. (pred instanceof Mod) ||
  98. (pred instanceof Quo) ||
  99. (pred instanceof Mult) ||
  100. (pred instanceof com.sun.org.apache.xpath.internal.operations.Number) ||
  101. (pred instanceof Function))
  102. m_hasPositionalPred = true;
  103. else
  104. pred.callVisitors(owner, this);
  105. }
  106. m_predDepth--;
  107. // Don't go have the caller go any further down the subtree.
  108. return false;
  109. }
  110. }