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. package org.apache.commons.jxpath.ri.axes;
  17. import org.apache.commons.jxpath.ri.EvalContext;
  18. import org.apache.commons.jxpath.ri.compiler.NodeTest;
  19. import org.apache.commons.jxpath.ri.model.NodePointer;
  20. /**
  21. * EvalContext that walks the "parent::" axis.
  22. *
  23. * @author Dmitri Plotnikov
  24. * @version $Revision: 1.15 $ $Date: 2004/03/25 03:49:50 $
  25. */
  26. public class ParentContext extends EvalContext {
  27. private NodeTest nodeTest;
  28. private boolean setStarted = false;
  29. private NodePointer currentNodePointer;
  30. public ParentContext(EvalContext parentContext, NodeTest nodeTest) {
  31. super(parentContext);
  32. this.nodeTest = nodeTest;
  33. }
  34. public NodePointer getCurrentNodePointer() {
  35. return currentNodePointer;
  36. }
  37. public int getCurrentPosition() {
  38. return 1;
  39. }
  40. public int getDocumentOrder() {
  41. return -1;
  42. }
  43. public void reset() {
  44. super.reset();
  45. setStarted = false;
  46. }
  47. public boolean setPosition(int position) {
  48. super.setPosition(position);
  49. return position == 1;
  50. }
  51. public boolean nextNode() {
  52. // Each set contains exactly one node: the parent
  53. if (setStarted) {
  54. return false;
  55. }
  56. setStarted = true;
  57. NodePointer thisLocation = parentContext.getCurrentNodePointer();
  58. currentNodePointer = thisLocation.getImmediateParentPointer();
  59. while (currentNodePointer != null
  60. && currentNodePointer.isContainer()) {
  61. currentNodePointer = currentNodePointer.getImmediateParentPointer();
  62. }
  63. if (currentNodePointer != null
  64. && currentNodePointer.testNode(nodeTest)) {
  65. position++;
  66. return true;
  67. }
  68. return false;
  69. }
  70. }