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.NodeSet;
  18. import org.apache.commons.jxpath.ri.EvalContext;
  19. import org.apache.commons.jxpath.ri.model.NodePointer;
  20. /**
  21. * A simple context that is based on a NodeSet.
  22. *
  23. * @author Dmitri Plotnikov
  24. * @version $Revision: 1.3 $ $Date: 2004/02/29 14:17:37 $
  25. */
  26. public class NodeSetContext extends EvalContext {
  27. private boolean startedSet = false;
  28. private NodeSet nodeSet;
  29. public NodeSetContext(EvalContext parentContext, NodeSet nodeSet) {
  30. super(parentContext);
  31. this.nodeSet = nodeSet;
  32. }
  33. public NodeSet getNodeSet() {
  34. return nodeSet;
  35. }
  36. public NodePointer getCurrentNodePointer() {
  37. if (position == 0) {
  38. if (!setPosition(1)) {
  39. return null;
  40. }
  41. }
  42. return (NodePointer) nodeSet.getPointers().get(position - 1);
  43. }
  44. public boolean setPosition(int position) {
  45. super.setPosition(position);
  46. return position >= 1 && position <= nodeSet.getPointers().size();
  47. }
  48. public boolean nextSet() {
  49. if (startedSet) {
  50. return false;
  51. }
  52. startedSet = true;
  53. return true;
  54. }
  55. public boolean nextNode() {
  56. return setPosition(position + 1);
  57. }
  58. }