You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RecursiveCharIterator.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.fo;
  19. import java.util.Iterator;
  20. import java.util.NoSuchElementException;
  21. /**
  22. * Kind of a super-iterator that iterates through child nodes of an FONode,
  23. * in turn managing character iterators for each of them. Caveat: Because this
  24. * class is itself a CharIterator, and manages a collection of CharIterators, it
  25. * is easy to get confused.
  26. */
  27. public class RecursiveCharIterator extends CharIterator {
  28. /** parent node for whose child nodes this iterator iterates */
  29. private FONode fobj;
  30. /** iterator for the child nodes */
  31. private Iterator childIter;
  32. /** current child object that is being managed by childIter*/
  33. private FONode curChild;
  34. /** CharIterator for curChild's characters */
  35. private CharIterator curCharIter;
  36. /**
  37. * Constructor which creates an iterator for all child nodes
  38. * @param fobj FONode for which an iterator should be created
  39. */
  40. public RecursiveCharIterator(FObj fobj) {
  41. // Set up first child iterator
  42. this.fobj = fobj;
  43. this.childIter = fobj.getChildNodes();
  44. getNextCharIter();
  45. }
  46. /**
  47. * Constructor which creates an iterator for only some child nodes
  48. * @param fobj FObj for which an iterator should be created
  49. * @param child FONode of the first child to include in iterator
  50. */
  51. public RecursiveCharIterator(FObj fobj, FONode child) {
  52. // Set up first child iterator
  53. this.fobj = fobj;
  54. this.childIter = fobj.getChildNodes(child);
  55. getNextCharIter();
  56. }
  57. /**
  58. * @return clone of this, cast as a CharIterator
  59. */
  60. public CharIterator mark() {
  61. return (CharIterator) this.clone();
  62. }
  63. /**
  64. * @return a clone of this
  65. */
  66. public Object clone() {
  67. RecursiveCharIterator ci = (RecursiveCharIterator) super.clone();
  68. ci.childIter = fobj.getChildNodes(ci.curChild);
  69. // Need to advance to the next child, else we get the same one!!!
  70. ci.childIter.next();
  71. ci.curCharIter = (CharIterator) curCharIter.clone();
  72. return ci;
  73. }
  74. /**
  75. * Replaces the current character in the CharIterator with a specified
  76. * character
  77. * @param c the character which should be used to replace the current
  78. * character
  79. */
  80. public void replaceChar(char c) {
  81. if (curCharIter != null) {
  82. curCharIter.replaceChar(c);
  83. }
  84. }
  85. /**
  86. * advances curChild to the next child in the collection, and curCharIter
  87. * to the CharIterator for that item, or sets them to null if the iterator
  88. * has no more items
  89. */
  90. private void getNextCharIter() {
  91. if (childIter != null && childIter.hasNext()) {
  92. this.curChild = (FONode) childIter.next();
  93. this.curCharIter = curChild.charIterator();
  94. } else {
  95. curChild = null;
  96. curCharIter = null;
  97. }
  98. }
  99. /**
  100. * @return true if there are more items in the CharIterator
  101. */
  102. public boolean hasNext() {
  103. while (curCharIter != null) {
  104. if (!curCharIter.hasNext()) {
  105. getNextCharIter();
  106. } else {
  107. return true;
  108. }
  109. }
  110. return false;
  111. }
  112. /**
  113. * {@inheritDoc}
  114. */
  115. public char nextChar() throws NoSuchElementException {
  116. if (curCharIter != null) {
  117. return curCharIter.nextChar();
  118. } else {
  119. throw new NoSuchElementException();
  120. }
  121. }
  122. /**
  123. * {@inheritDoc}
  124. */
  125. public void remove() {
  126. if (curCharIter != null) {
  127. curCharIter.remove();
  128. }
  129. }
  130. }