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: * WordBreakIterator.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.util;
030:
031: /**
032: * Behaves similiar to BreakIterator.getWordInstance() but handles line break delimeters
033: * as simple whitespaces.
034: * <p/>
035: * This class is not synchronized.
036: *
037: * @author Thomas Morgner.
038: * @deprecated This functionality is now provided by LibFonts.
039: */
040: public class WordBreakIterator {
041: /**
042: * A useful constant.
043: */
044: public static final int DONE = -1;
045:
046: /**
047: * The current position.
048: */
049: private int position;
050:
051: /**
052: * Storage for characters.
053: */
054: private char[] text;
055:
056: /**
057: * Creates a new iterator.
058: *
059: * @param text the text to break.
060: */
061: public WordBreakIterator(final String text) {
062: setText(text);
063: }
064:
065: /**
066: * Returns the next word boundary.
067: *
068: * @return The index of the next word boundary.
069: */
070: public int next() {
071: if (position == DONE) {
072: return DONE;
073: }
074: if (text == null) {
075: return DONE;
076: }
077: if (position == text.length) {
078: return DONE;
079: }
080:
081: //lastFound = position;
082:
083: if (Character.isWhitespace(text[position])) {
084: // search the first non whitespace character ..., this is the beginning of the word
085: while ((position < text.length)
086: && (Character.isWhitespace(text[position]))) {
087: position++;
088: }
089: return position;
090: } else {
091: // now search the first whitespace character ..., this is the end of the word
092: while ((position < text.length)
093: && (Character.isWhitespace(text[position]) == false)) {
094: position++;
095: }
096: return position;
097: }
098: }
099:
100: /**
101: * Same like next(), but returns the End-Of-Text as if there was a linebreak added
102: * (Reader.readLine() compatible)
103: *
104: * @return The next position.
105: */
106: public int nextWithEnd() {
107: final int pos = position;
108: if (pos == DONE || pos == text.length) {
109: return DONE;
110: }
111: final int retval = next();
112: if (retval == DONE) {
113: return text.length;
114: }
115: return retval;
116: }
117:
118: /**
119: * Returns the position of the previous break.
120: *
121: * @return The index.
122: */
123: public int previous() {
124: //return lastFound;
125:
126: if (position == 0) {
127: return 0;
128: }
129: if (text == null) {
130: return DONE;
131: }
132: if (position == DONE) {
133: position = text.length;
134: return position;
135: }
136: //lastFound = position;
137:
138: if (Character.isWhitespace(text[position - 1])) {
139: // search the first non whitespace character ..., this is the beginning of the word
140: while ((position > 0)
141: && (Character.isWhitespace(text[position - 1]))) {
142: position--;
143: }
144: return position;
145: } else {
146: // now search the first whitespace character ..., this is the end of the word
147: while ((position > 0)
148: && (Character.isWhitespace(text[position - 1]) == false)) {
149: position--;
150: }
151: return position;
152: }
153:
154: }
155:
156: /**
157: * Returns the text to be broken up.
158: *
159: * @return the text.
160: */
161: public String getText() {
162: return new String(text);
163: }
164:
165: /**
166: * Sets the text to be broken up.
167: *
168: * @param text the text.
169: */
170: public void setText(final String text) {
171: position = 0;
172: //lastFound = 0;
173: this .text = text.toCharArray();
174: }
175:
176: /**
177: * Returns the current parsing position of this iterator.
178: *
179: * @return returns the current parsing position of this iterator.
180: */
181: public int getPosition() {
182: return position;
183: }
184:
185: /**
186: * Defines the current parse position for the word break iterator. The position must be
187: * positive and within the range of the current text.
188: *
189: * @param position the position.
190: */
191: public void setPosition(final int position) {
192: if (position < 0) {
193: throw new IndexOutOfBoundsException("Position < 0");
194: }
195: if (position > text.length) {
196: throw new IndexOutOfBoundsException(
197: "Position > text.length");
198: }
199: this.position = position;
200: }
201: }
|