Frames | No Frames |
1: /* ======================================================================== 2: * JCommon : a free general purpose class library for the Java(tm) platform 3: * ======================================================================== 4: * 5: * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors. 6: * 7: * Project Info: http://www.jfree.org/jcommon/index.html 8: * 9: * This library is free software; you can redistribute it and/or modify it 10: * under the terms of the GNU Lesser General Public License as published by 11: * the Free Software Foundation; either version 2.1 of the License, or 12: * (at your option) any later version. 13: * 14: * This library is distributed in the hope that it will be useful, but 15: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 16: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 17: * License for more details. 18: * 19: * You should have received a copy of the GNU Lesser General Public 20: * License along with this library; if not, write to the Free Software 21: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 22: * USA. 23: * 24: * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 25: * in the United States and other countries.] 26: * 27: * ---------------------- 28: * LineBreakIterator.java 29: * ---------------------- 30: * (C)opyright 2003, by Thomas Morgner and Contributors. 31: * 32: * Original Author: Thomas Morgner; 33: * Contributor(s): David Gilbert (for Object Refinery Limited); 34: * 35: * $Id: LineBreakIterator.java,v 1.4 2005/11/03 09:55:26 mungady Exp $ 36: * 37: * Changes 38: * ------- 39: * 13-03-2003 : Initial version 40: */ 41: package org.jfree.util; 42: 43: import java.util.Iterator; 44: 45: /** 46: * An iterator that breaks text into lines. 47: * The result is equal to BufferedReader.readLine(). 48: * 49: * @author Thomas Morgner 50: */ 51: public class LineBreakIterator implements Iterator 52: { 53: /** A useful constant. */ 54: public static final int DONE = -1; 55: 56: /** Storage for the text. */ 57: private char[] text; 58: 59: /** The current position. */ 60: private int position; 61: 62: /** 63: * Default constructor. 64: */ 65: public LineBreakIterator() 66: { 67: setText(""); 68: } 69: 70: /** 71: * Creates a new line break iterator. 72: * 73: * @param text the text to be broken up. 74: */ 75: public LineBreakIterator(final String text) 76: { 77: setText(text); 78: } 79: 80: /** 81: * Returns the position of the next break. 82: * 83: * @return A position. 84: */ 85: public synchronized int nextPosition() 86: { 87: if (this.text == null) 88: { 89: return DONE; 90: } 91: if (this.position == DONE) 92: { 93: return DONE; 94: } 95: 96: // recognize \n, \r, \r\n 97: 98: final int nChars = this.text.length; 99: int nextChar = this.position; 100: 101: for (;;) 102: { 103: if (nextChar >= nChars) 104: { 105: /* End of text reached */ 106: this.position = DONE; 107: return DONE; 108: } 109: 110: boolean eol = false; 111: char c = 0; 112: int i; 113: 114: // search the next line break, either \n or \r 115: for (i = nextChar; i < nChars; i++) 116: { 117: c = this.text[i]; 118: if ((c == '\n') || (c == '\r')) 119: { 120: eol = true; 121: break; 122: } 123: } 124: 125: nextChar = i; 126: if (eol) 127: { 128: nextChar++; 129: if (c == '\r') 130: { 131: if ((nextChar < nChars) && (this.text[nextChar] == '\n')) 132: { 133: nextChar++; 134: } 135: } 136: this.position = nextChar; 137: return (this.position); 138: } 139: } 140: } 141: 142: /** 143: * Same like next(), but returns the End-Of-Text as 144: * if there was a linebreak added (Reader.readLine() compatible) 145: * 146: * @return The next position. 147: */ 148: public int nextWithEnd() 149: { 150: final int pos = this.position; 151: if (pos == DONE) 152: { 153: return DONE; 154: } 155: if (pos == this.text.length) 156: { 157: this.position = DONE; 158: return DONE; 159: } 160: final int retval = nextPosition(); 161: if (retval == DONE) 162: { 163: return this.text.length; 164: } 165: return retval; 166: } 167: 168: /** 169: * Returns the text to be broken up. 170: * 171: * @return The text. 172: */ 173: public String getText() 174: { 175: return new String(this.text); 176: } 177: 178: /** 179: * Sets the text to be broken up. 180: * 181: * @param text the text. 182: */ 183: public void setText(final String text) 184: { 185: this.position = 0; 186: this.text = text.toCharArray(); 187: } 188: 189: /** 190: * Returns <tt>true</tt> if the iteration has more elements. (In other 191: * words, returns <tt>true</tt> if <tt>next</tt> would return an element 192: * rather than throwing an exception.) 193: * 194: * @return <tt>true</tt> if the iterator has more elements. 195: */ 196: public boolean hasNext() 197: { 198: return (this.position != DONE); 199: } 200: 201: /** 202: * Returns the next element in the iteration. 203: * 204: * @return the next element in the iteration. 205: */ 206: public Object next() 207: { 208: if (this.position == DONE) 209: { 210: // allready at the end ... 211: return null; 212: } 213: 214: final int lastFound = this.position; 215: int pos = nextWithEnd(); 216: if (pos == DONE) 217: { 218: // the end of the text has been reached ... 219: return new String(this.text, lastFound, this.text.length - lastFound); 220: } 221: 222: // step one char back 223: if (pos > 0) 224: { 225: final int end = lastFound; 226: for (; ((pos) > end) && ((this.text[pos - 1] == '\n') || this.text[pos - 1] == '\r'); pos--) 227: { 228: // search the end of the current linebreak sequence .. 229: } 230: } 231: //System.out.println ("text: " + new String (text)); 232: //System.out.println ("pos: " + pos + " lastFound: " + lastFound); 233: return new String(this.text, lastFound, pos - lastFound); 234: } 235: 236: /** 237: * 238: * Removes from the underlying collection the last element returned by the 239: * iterator (optional operation). This method can be called only once per 240: * call to <tt>next</tt>. The behavior of an iterator is unspecified if 241: * the underlying collection is modified while the iteration is in 242: * progress in any way other than by calling this method. 243: * 244: * @exception UnsupportedOperationException if the <tt>remove</tt> 245: * operation is not supported by this Iterator. 246: * @exception IllegalStateException if the <tt>next</tt> method has not 247: * yet been called, or the <tt>remove</tt> method has already 248: * been called after the last call to the <tt>next</tt> 249: * method. 250: */ 251: public void remove() 252: { 253: throw new UnsupportedOperationException("This iterator is read-only."); 254: } 255: }