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.Pointer;
  18. import org.apache.commons.jxpath.ri.EvalContext;
  19. import org.apache.commons.jxpath.ri.compiler.NodeTest;
  20. import org.apache.commons.jxpath.ri.model.NodePointer;
  21. /**
  22. * EvalContext that returns the current node from the parent context if the
  23. * test succeeds.
  24. *
  25. * @author Dmitri Plotnikov
  26. * @version $Revision: 1.12 $ $Date: 2004/02/29 14:17:37 $
  27. */
  28. public class SelfContext extends EvalContext {
  29. private NodeTest nodeTest;
  30. private boolean startedSet = false;
  31. private NodePointer nodePointer;
  32. public SelfContext(EvalContext parentContext, NodeTest nodeTest) {
  33. super(parentContext);
  34. this.nodeTest = nodeTest;
  35. }
  36. public Pointer getSingleNodePointer() {
  37. return parentContext.getSingleNodePointer();
  38. }
  39. public NodePointer getCurrentNodePointer() {
  40. if (position == 0) {
  41. if (!setPosition(1)) {
  42. return null;
  43. }
  44. }
  45. return nodePointer;
  46. }
  47. public boolean nextNode() {
  48. return setPosition(getCurrentPosition() + 1);
  49. }
  50. public void reset() {
  51. super.reset();
  52. startedSet = false;
  53. }
  54. public boolean setPosition(int position) {
  55. if (position != 1) {
  56. return false;
  57. }
  58. super.setPosition(position);
  59. if (!startedSet) {
  60. startedSet = true;
  61. nodePointer = (NodePointer) parentContext.getCurrentNodePointer();
  62. }
  63. if (nodePointer == null) {
  64. return false;
  65. }
  66. return nodeTest == null || nodePointer.testNode(nodeTest);
  67. }
  68. }