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.model.jdom;
  17. import java.util.Collections;
  18. import java.util.List;
  19. import org.apache.commons.jxpath.ri.compiler.NodeTest;
  20. import org.apache.commons.jxpath.ri.model.NodeIterator;
  21. import org.apache.commons.jxpath.ri.model.NodePointer;
  22. import org.jdom.Document;
  23. import org.jdom.Element;
  24. /**
  25. * An iterator of children of a JDOM Node.
  26. *
  27. * @author Dmitri Plotnikov
  28. * @version $Revision: 1.6 $ $Date: 2004/02/29 14:17:40 $
  29. */
  30. public class JDOMNodeIterator implements NodeIterator {
  31. private NodePointer parent;
  32. private NodeTest nodeTest;
  33. private boolean reverse;
  34. private int position = 0;
  35. private int index = 0;
  36. private List children;
  37. private Object child;
  38. public JDOMNodeIterator(
  39. NodePointer parent, NodeTest nodeTest,
  40. boolean reverse, NodePointer startWith)
  41. {
  42. this.parent = parent;
  43. if (startWith != null) {
  44. this.child = startWith.getNode();
  45. }
  46. // TBD: optimize me for different node tests
  47. Object node = parent.getNode();
  48. if (node instanceof Document) {
  49. this.children = ((Document) node).getContent();
  50. }
  51. else if (node instanceof Element) {
  52. this.children = ((Element) node).getContent();
  53. }
  54. else {
  55. this.children = Collections.EMPTY_LIST;
  56. }
  57. this.nodeTest = nodeTest;
  58. this.reverse = reverse;
  59. }
  60. public NodePointer getNodePointer() {
  61. if (child == null) {
  62. if (!setPosition(1)) {
  63. return null;
  64. }
  65. position = 0;
  66. }
  67. return new JDOMNodePointer(parent, child);
  68. }
  69. public int getPosition() {
  70. return position;
  71. }
  72. public boolean setPosition(int position) {
  73. while (this.position < position) {
  74. if (!next()) {
  75. return false;
  76. }
  77. }
  78. while (this.position > position) {
  79. if (!previous()) {
  80. return false;
  81. }
  82. }
  83. return true;
  84. }
  85. /**
  86. * This is actually never invoked during the normal evaluation
  87. * of xpaths - an iterator is always going forward, never backwards.
  88. * So, this is implemented only for completeness and perhaps for
  89. * those who use these iterators outside of XPath evaluation.
  90. */
  91. private boolean previous() {
  92. position--;
  93. if (!reverse) {
  94. while (--index >= 0) {
  95. child = children.get(index);
  96. if (testChild()) {
  97. return true;
  98. }
  99. }
  100. }
  101. else {
  102. for (; index < children.size(); index++) {
  103. child = children.get(index);
  104. if (testChild()) {
  105. return true;
  106. }
  107. }
  108. }
  109. return false;
  110. }
  111. private boolean next() {
  112. position++;
  113. if (!reverse) {
  114. if (position == 1) {
  115. index = 0;
  116. if (child != null) {
  117. index = children.indexOf(child) + 1;
  118. }
  119. }
  120. else {
  121. index++;
  122. }
  123. for (; index < children.size(); index++) {
  124. child = children.get(index);
  125. if (testChild()) {
  126. return true;
  127. }
  128. }
  129. return false;
  130. }
  131. else {
  132. if (position == 1) {
  133. index = children.size() - 1;
  134. if (child != null) {
  135. index = children.indexOf(child) - 1;
  136. }
  137. }
  138. else {
  139. index--;
  140. }
  141. for (; index >= 0; index--) {
  142. child = children.get(index);
  143. if (testChild()) {
  144. return true;
  145. }
  146. }
  147. return false;
  148. }
  149. }
  150. private boolean testChild() {
  151. return JDOMNodePointer.testNode(parent, child, nodeTest);
  152. }
  153. }