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.QName;
  19. import org.apache.commons.jxpath.ri.compiler.NodeNameTest;
  20. import org.apache.commons.jxpath.ri.compiler.NodeTest;
  21. import org.apache.commons.jxpath.ri.model.NodeIterator;
  22. import org.apache.commons.jxpath.ri.model.NodePointer;
  23. /**
  24. * EvalContext that walks the "attribute::" axis.
  25. *
  26. * @author Dmitri Plotnikov
  27. * @version $Revision: 1.10 $ $Date: 2004/02/29 14:17:38 $
  28. */
  29. public class AttributeContext extends EvalContext {
  30. private NodeTest nodeTest;
  31. private boolean setStarted = false;
  32. private NodeIterator iterator;
  33. private NodePointer currentNodePointer;
  34. /**
  35. * @param parentContext represents the previous step on the path
  36. * @param nameTest is the name of the attribute we are looking for
  37. */
  38. public AttributeContext(EvalContext parentContext, NodeTest nodeTest) {
  39. super(parentContext);
  40. this.nodeTest = nodeTest;
  41. }
  42. public NodePointer getCurrentNodePointer() {
  43. return currentNodePointer;
  44. }
  45. public void reset() {
  46. setStarted = false;
  47. iterator = null;
  48. super.reset();
  49. }
  50. public boolean setPosition(int position) {
  51. if (position < getCurrentPosition()) {
  52. reset();
  53. }
  54. while (getCurrentPosition() < position) {
  55. if (!nextNode()) {
  56. return false;
  57. }
  58. }
  59. return true;
  60. }
  61. public boolean nextNode() {
  62. super.setPosition(getCurrentPosition() + 1);
  63. if (!setStarted) {
  64. setStarted = true;
  65. if (!(nodeTest instanceof NodeNameTest)) {
  66. return false;
  67. }
  68. QName name = ((NodeNameTest) nodeTest).getNodeName();
  69. iterator =
  70. parentContext.getCurrentNodePointer().attributeIterator(name);
  71. }
  72. if (iterator == null) {
  73. return false;
  74. }
  75. if (!iterator.setPosition(iterator.getPosition() + 1)) {
  76. return false;
  77. }
  78. currentNodePointer = iterator.getNodePointer();
  79. return true;
  80. }
  81. }