Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

RecursiveCharIterator.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. /* $Id$ */
  17. package org.apache.fop.fo;
  18. import java.util.Iterator;
  19. import java.util.NoSuchElementException;
  20. /**
  21. * Kind of a super-iterator that iterates through children of an FONode,
  22. * in turn managing character iterators for each of them. Caveat: Because this
  23. * class is itself a CharIterator, and manages a collection of CharIterators, it
  24. * is easy to get confused.
  25. */
  26. public class RecursiveCharIterator extends AbstractCharIterator {
  27. /** parent node for whose children this iterator iterates */
  28. private FONode fobj;
  29. /** iterator for the child nodes */
  30. private Iterator childIter = null;
  31. /** current child object that is being managed by childIter*/
  32. private FONode curChild;
  33. /** CharIterator for curChild's characters */
  34. private CharIterator curCharIter = null;
  35. /**
  36. * Constructor which creates an iterator for all children
  37. * @param fobj FONode for which an iterator should be created
  38. */
  39. public RecursiveCharIterator(FObj fobj) {
  40. // Set up first child iterator
  41. this.fobj = fobj;
  42. this.childIter = fobj.getChildren();
  43. getNextCharIter();
  44. }
  45. /**
  46. * Constructor which creates an iterator for only some children
  47. * @param fobj FObj for which an iterator should be created
  48. * @param child FONode of the first child to include in iterator
  49. */
  50. public RecursiveCharIterator(FObj fobj, FONode child) {
  51. // Set up first child iterator
  52. this.fobj = fobj;
  53. this.childIter = fobj.getChildren(child);
  54. getNextCharIter();
  55. }
  56. /**
  57. * @return clone of this, cast as a CharIterator
  58. */
  59. public CharIterator mark() {
  60. return (CharIterator) this.clone();
  61. }
  62. /**
  63. * @return a clone of this
  64. */
  65. public Object clone() {
  66. RecursiveCharIterator ci = (RecursiveCharIterator) super.clone();
  67. ci.childIter = fobj.getChildren(ci.curChild);
  68. // Need to advance to the next child, else we get the same one!!!
  69. ci.childIter.next();
  70. ci.curCharIter = (CharIterator) curCharIter.clone();
  71. return ci;
  72. }
  73. /**
  74. * Replaces the current character in the CharIterator with a specified
  75. * character
  76. * @param c the character which should be used to replace the current
  77. * character
  78. */
  79. public void replaceChar(char c) {
  80. if (curCharIter != null) {
  81. curCharIter.replaceChar(c);
  82. }
  83. }
  84. /**
  85. * advances curChild to the next child in the collection, and curCharIter
  86. * to the CharIterator for that item, or sets them to null if the iterator
  87. * has no more items
  88. */
  89. private void getNextCharIter() {
  90. if (childIter != null && childIter.hasNext()) {
  91. this.curChild = (FONode) childIter.next();
  92. this.curCharIter = curChild.charIterator();
  93. } else {
  94. curChild = null;
  95. curCharIter = null;
  96. }
  97. }
  98. /**
  99. * @return true if there are more items in the CharIterator
  100. */
  101. public boolean hasNext() {
  102. while (curCharIter != null) {
  103. if (curCharIter.hasNext() == false) {
  104. getNextCharIter();
  105. } else {
  106. return true;
  107. }
  108. }
  109. return false;
  110. }
  111. /**
  112. * @see org.apache.fop.fo.CharIterator#nextChar()
  113. */
  114. public char nextChar() throws NoSuchElementException {
  115. if (curCharIter != null) {
  116. return curCharIter.nextChar();
  117. } else {
  118. throw new NoSuchElementException();
  119. }
  120. }
  121. /**
  122. * @see java.util.Iterator#remove
  123. */
  124. public void remove() {
  125. if (curCharIter != null) {
  126. curCharIter.remove();
  127. }
  128. }
  129. }