KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > fo > RecursiveCharIterator


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
18 /* $Id: RecursiveCharIterator.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.fo;
21
22 import java.util.Iterator JavaDoc;
23 import java.util.NoSuchElementException JavaDoc;
24
25 /**
26  * Kind of a super-iterator that iterates through child nodes of an FONode,
27  * in turn managing character iterators for each of them. Caveat: Because this
28  * class is itself a CharIterator, and manages a collection of CharIterators, it
29  * is easy to get confused.
30  */

31 public class RecursiveCharIterator extends CharIterator {
32     /** parent node for whose child nodes this iterator iterates */
33     private FONode fobj;
34     /** iterator for the child nodes */
35     private Iterator JavaDoc childIter = null;
36
37     /** current child object that is being managed by childIter*/
38     private FONode curChild;
39     /** CharIterator for curChild's characters */
40     private CharIterator curCharIter = null;
41
42     /**
43      * Constructor which creates an iterator for all child nodes
44      * @param fobj FONode for which an iterator should be created
45      */

46     public RecursiveCharIterator(FObj fobj) {
47         // Set up first child iterator
48
this.fobj = fobj;
49         this.childIter = fobj.getChildNodes();
50         getNextCharIter();
51     }
52
53     /**
54      * Constructor which creates an iterator for only some child nodes
55      * @param fobj FObj for which an iterator should be created
56      * @param child FONode of the first child to include in iterator
57      */

58     public RecursiveCharIterator(FObj fobj, FONode child) {
59         // Set up first child iterator
60
this.fobj = fobj;
61         this.childIter = fobj.getChildNodes(child);
62         getNextCharIter();
63     }
64
65     /**
66      * @return clone of this, cast as a CharIterator
67      */

68     public CharIterator mark() {
69         return (CharIterator) this.clone();
70     }
71
72     /**
73      * @return a clone of this
74      */

75     public Object JavaDoc clone() {
76         RecursiveCharIterator ci = (RecursiveCharIterator) super.clone();
77         ci.childIter = fobj.getChildNodes(ci.curChild);
78         // Need to advance to the next child, else we get the same one!!!
79
ci.childIter.next();
80         ci.curCharIter = (CharIterator) curCharIter.clone();
81         return ci;
82     }
83
84     /**
85      * Replaces the current character in the CharIterator with a specified
86      * character
87      * @param c the character which should be used to replace the current
88      * character
89      */

90     public void replaceChar(char c) {
91         if (curCharIter != null) {
92             curCharIter.replaceChar(c);
93         }
94     }
95
96     /**
97      * advances curChild to the next child in the collection, and curCharIter
98      * to the CharIterator for that item, or sets them to null if the iterator
99      * has no more items
100      */

101     private void getNextCharIter() {
102         if (childIter != null && childIter.hasNext()) {
103             this.curChild = (FONode) childIter.next();
104             this.curCharIter = curChild.charIterator();
105         } else {
106             curChild = null;
107             curCharIter = null;
108         }
109     }
110
111     /**
112      * @return true if there are more items in the CharIterator
113      */

114     public boolean hasNext() {
115         while (curCharIter != null) {
116             if (curCharIter.hasNext() == false) {
117                 getNextCharIter();
118             } else {
119                 return true;
120             }
121         }
122         return false;
123     }
124
125     /**
126      * @see org.apache.fop.fo.CharIterator#nextChar()
127      */

128     public char nextChar() throws NoSuchElementException JavaDoc {
129         if (curCharIter != null) {
130             return curCharIter.nextChar();
131         } else {
132             throw new NoSuchElementException JavaDoc();
133         }
134     }
135
136     /**
137      * @see java.util.Iterator#remove
138      */

139     public void remove() {
140         if (curCharIter != null) {
141             curCharIter.remove();
142         }
143     }
144 }
145
146
Popular Tags