001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /* $Id: PositionIterator.java 426576 2006-07-28 15:44:37Z jeremias $ */
019:
020: package org.apache.fop.layoutmgr;
021:
022: import java.util.Iterator;
023: import java.util.NoSuchElementException;
024:
025: public abstract class PositionIterator implements Iterator {
026:
027: private Iterator parentIter;
028: private Object nextObj;
029: private LayoutManager childLM;
030: private boolean bHasNext;
031:
032: protected PositionIterator(Iterator pIter) {
033: parentIter = pIter;
034: lookAhead();
035: //checkNext();
036: }
037:
038: public LayoutManager getNextChildLM() {
039: // Move to next "segment" of iterator, ie: new childLM
040: if (childLM == null && nextObj != null) {
041: childLM = getLM(nextObj);
042: bHasNext = true;
043: }
044: return childLM;
045: }
046:
047: protected abstract LayoutManager getLM(Object nextObj);
048:
049: protected abstract Position getPos(Object nextObj);
050:
051: private void lookAhead() {
052: if (parentIter.hasNext()) {
053: bHasNext = true;
054: nextObj = parentIter.next();
055: } else {
056: endIter();
057: }
058: }
059:
060: protected boolean checkNext() {
061: LayoutManager lm = getLM(nextObj);
062: if (childLM == null) {
063: childLM = lm;
064: } else if (childLM != lm && lm != null) {
065: // End of this sub-sequence with same child LM
066: bHasNext = false;
067: childLM = null;
068: return false;
069: }
070: return true;
071: }
072:
073: protected void endIter() {
074: bHasNext = false;
075: nextObj = null;
076: childLM = null;
077: }
078:
079: public boolean hasNext() {
080: return (bHasNext && checkNext());
081: }
082:
083: public Object next() throws NoSuchElementException {
084: if (bHasNext) {
085: Object retObj = getPos(nextObj);
086: lookAhead();
087: return retObj;
088: } else {
089: throw new NoSuchElementException("PosIter");
090: }
091: }
092:
093: public Object peekNext() {
094: return nextObj;
095: }
096:
097: public void remove() throws UnsupportedOperationException {
098: throw new UnsupportedOperationException(
099: "PositionIterator doesn't support remove");
100: }
101: }
|