001: /**
002: * ===========================================
003: * JFreeReport : a free Java reporting library
004: * ===========================================
005: *
006: * Project Info: http://reporting.pentaho.org/
007: *
008: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
009: *
010: * This library is free software; you can redistribute it and/or modify it under the terms
011: * of the GNU Lesser General Public License as published by the Free Software Foundation;
012: * either version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
015: * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016: * See the GNU Lesser General Public License for more details.
017: *
018: * You should have received a copy of the GNU Lesser General Public License along with this
019: * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: *
022: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
023: * in the United States and other countries.]
024: *
025: * ------------
026: * LineBreakIterator.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.util;
030:
031: import java.util.Iterator;
032:
033: /**
034: * Same as BufferedReader.readLine();
035: *
036: * @author Thomas Morgner
037: * @deprecated The same class is contained in JCommon.
038: */
039: public class LineBreakIterator implements Iterator {
040: /**
041: * A useful constant.
042: */
043: public static final int DONE = -1;
044:
045: /**
046: * Storage for the text.
047: */
048: private char[] text;
049:
050: /**
051: * The current position.
052: */
053: private int position;
054:
055: /**
056: * Default constructor.
057: */
058: public LineBreakIterator() {
059: setText("");
060: }
061:
062: /**
063: * Creates a new line break iterator.
064: *
065: * @param text the text to be broken up.
066: */
067: public LineBreakIterator(final String text) {
068: setText(text);
069: }
070:
071: /**
072: * Returns the position of the next break.
073: *
074: * @return A position.
075: */
076: public synchronized int nextPosition() {
077: if (text == null) {
078: return DONE;
079: }
080: if (position == DONE) {
081: return DONE;
082: }
083:
084: // recognize \n, \r, \r\n
085:
086: final int nChars = text.length;
087: int nextChar = position;
088:
089: while (true) {
090: if (nextChar >= nChars) {
091: /* End of text reached */
092: position = DONE;
093: return DONE;
094: }
095:
096: boolean eol = false;
097: char c = 0;
098: int i;
099:
100: // search the next line break, either \n or \r
101: for (i = nextChar; i < nChars; i++) {
102: c = text[i];
103: if ((c == '\n') || (c == '\r')) {
104: eol = true;
105: break;
106: }
107: }
108:
109: nextChar = i;
110: if (eol) {
111: nextChar++;
112: if (c == '\r') {
113: if ((nextChar < nChars) && (text[nextChar] == '\n')) {
114: nextChar++;
115: }
116: }
117: position = nextChar;
118: return (position);
119: }
120: }
121: }
122:
123: /**
124: * Same like next(), but returns the End-Of-Text as if there was a linebreak added
125: * (Reader.readLine() compatible)
126: *
127: * @return The next position.
128: */
129: public int nextWithEnd() {
130: final int pos = position;
131: if (pos == DONE) {
132: return DONE;
133: }
134: if (pos == text.length) {
135: position = DONE;
136: return DONE;
137: }
138: final int retval = nextPosition();
139: if (retval == DONE) {
140: return text.length;
141: }
142: return retval;
143: }
144:
145: /**
146: * Returns the text to be broken up.
147: *
148: * @return The text.
149: */
150: public String getText() {
151: return new String(text);
152: }
153:
154: /**
155: * Sets the text to be broken up.
156: *
157: * @param text the text.
158: */
159: public void setText(final String text) {
160: position = 0;
161: this .text = text.toCharArray();
162: }
163:
164: /**
165: * Returns <tt>true</tt> if the iteration has more elements. (In other words, returns
166: * <tt>true</tt> if <tt>next</tt> would return an element rather than throwing an
167: * exception.)
168: *
169: * @return <tt>true</tt> if the iterator has more elements.
170: */
171: public boolean hasNext() {
172: return (position != DONE);
173: }
174:
175: /**
176: * Returns the next element in the iteration.
177: *
178: * @return the next element in the iteration.
179: */
180: public Object next() {
181: if (position == DONE) {
182: // allready at the end ...
183: return null;
184: }
185:
186: final int lastFound = position;
187: int pos = nextWithEnd();
188: if (pos == DONE) {
189: // the end of the text has been reached ...
190: return new String(text, lastFound, text.length - lastFound);
191: }
192:
193: // step one char back
194: if (pos > 0) {
195: for (; pos > lastFound
196: && (text[pos - 1] == '\n' || text[pos - 1] == '\r'); pos--) {
197: // search the end of the current linebreak sequence ..
198: }
199: }
200: return new String(text, lastFound, pos - lastFound);
201: }
202:
203: /**
204: * Removes from the underlying collection the last element returned by the iterator
205: * (optional operation). This method can be called only once per call to <tt>next</tt>.
206: * The behavior of an iterator is unspecified if the underlying collection is modified
207: * while the iteration is in progress in any way other than by calling this method.
208: *
209: * @throws UnsupportedOperationException if the <tt>remove</tt> operation is not
210: * supported by this Iterator.
211: * @throws IllegalStateException if the <tt>next</tt> method has not yet been
212: * called, or the <tt>remove</tt> method has
213: * already been called after the last call to the
214: * <tt>next</tt> method.
215: */
216: public void remove() {
217: throw new UnsupportedOperationException();
218: }
219: }
|