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.model.NodePointer;
  20. /**
  21. * A single-set EvalContext that provides access to the current node of
  22. * the parent context and nothing else. It does not pass the iteration
  23. * on to the parent context.
  24. *
  25. * @author Dmitri Plotnikov
  26. * @version $Revision: 1.15 $ $Date: 2004/07/30 13:51:01 $
  27. */
  28. public class InitialContext extends EvalContext {
  29. private boolean startedSet = false;
  30. private boolean started = false;
  31. private boolean collection;
  32. private NodePointer nodePointer;
  33. public InitialContext(EvalContext parentContext) {
  34. super(parentContext);
  35. nodePointer =
  36. (NodePointer) parentContext.getCurrentNodePointer().clone();
  37. if (nodePointer != null) {
  38. collection =
  39. (nodePointer.getIndex() == NodePointer.WHOLE_COLLECTION);
  40. }
  41. }
  42. public Pointer getSingleNodePointer() {
  43. return nodePointer;
  44. }
  45. public NodePointer getCurrentNodePointer() {
  46. return nodePointer;
  47. }
  48. public Object getValue() {
  49. return nodePointer.getValue();
  50. }
  51. public boolean nextNode() {
  52. return setPosition(position + 1);
  53. }
  54. public boolean setPosition(int position) {
  55. this.position = position;
  56. if (collection) {
  57. if (position >= 1 && position <= nodePointer.getLength()) {
  58. nodePointer.setIndex(position - 1);
  59. return true;
  60. }
  61. return false;
  62. }
  63. else {
  64. return position == 1;
  65. }
  66. }
  67. public boolean nextSet() {
  68. if (started) {
  69. return false;
  70. }
  71. started = true;
  72. return true;
  73. }
  74. }