001: /* ====================================================================
002: * Tea - Copyright (c) 1997-2000 Walt Disney Internet Group
003: * ====================================================================
004: * The Tea Software License, Version 1.1
005: *
006: * Copyright (c) 2000 Walt Disney Internet Group. All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * 1. Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * 2. Redistributions in binary form must reproduce the above copyright
016: * notice, this list of conditions and the following disclaimer in
017: * the documentation and/or other materials provided with the
018: * distribution.
019: *
020: * 3. The end-user documentation included with the redistribution,
021: * if any, must include the following acknowledgment:
022: * "This product includes software developed by the
023: * Walt Disney Internet Group (http://opensource.go.com/)."
024: * Alternately, this acknowledgment may appear in the software itself,
025: * if and wherever such third-party acknowledgments normally appear.
026: *
027: * 4. The names "Tea", "TeaServlet", "Kettle", "Trove" and "BeanDoc" must
028: * not be used to endorse or promote products derived from this
029: * software without prior written permission. For written
030: * permission, please contact opensource@dig.com.
031: *
032: * 5. Products derived from this software may not be called "Tea",
033: * "TeaServlet", "Kettle" or "Trove", nor may "Tea", "TeaServlet",
034: * "Kettle", "Trove" or "BeanDoc" appear in their name, without prior
035: * written permission of the Walt Disney Internet Group.
036: *
037: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
038: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
039: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
040: * DISCLAIMED. IN NO EVENT SHALL THE WALT DISNEY INTERNET GROUP OR ITS
041: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
042: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
043: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
044: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
045: * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
046: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
047: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
048: * ====================================================================
049: *
050: * For more information about Tea, please see http://opensource.go.com/.
051: */
052:
053: package com.go.tea.io;
054:
055: import java.io.*;
056:
057: /******************************************************************************
058: * LinePositionReader aids in printing line numbers for error reporting.
059: *
060: * @author Brian S O'Neill
061: * @version
062: * <!--$$Revision:--> 16 <!-- $-->, <!--$$JustDate:--> 12/11/00 <!-- $-->
063: * @deprecated Moved to com.go.trove.io package.
064: */
065: public class LinePositionReader extends PositionReader {
066: private int mLineNumber = 1;
067: private int mPushback = -1;
068:
069: public LinePositionReader(Reader reader) {
070: super (reader);
071: }
072:
073: public int read() throws IOException {
074: int c;
075:
076: if (mPushback >= 0) {
077: c = mPushback;
078: mPushback = -1;
079: } else {
080: c = super .read();
081: }
082:
083: if (c == '\n') {
084: mLineNumber++;
085: } else if (c == '\r') {
086: int peek = super .read();
087: if (peek != '\n') {
088: mLineNumber++;
089: }
090: mPushback = peek;
091: }
092:
093: return c;
094: }
095:
096: /**
097: * After calling readLine, calling getLineNumber returns the next line
098: * number.
099: */
100: public String readLine() throws IOException {
101: StringBuffer buf = new StringBuffer(80);
102: int line = mLineNumber;
103: int c;
104: while (line == mLineNumber && (c = read()) >= 0) {
105: buf.append((char) c);
106: }
107: return buf.toString();
108: }
109:
110: /**
111: * Skips forward into the stream to the line number specified. The line
112: * can then be read by calling readLine. Calling getPosition
113: * returns the position that the line begins.
114: *
115: * @return the line number reached
116: */
117: public int skipForwardToLine(int line) throws IOException {
118: while (mLineNumber < line && read() >= 0) {
119: }
120: return mLineNumber;
121: }
122:
123: /**
124: * @return the number of the line currently being read or the next one
125: * available.
126: */
127: public int getLineNumber() {
128: return mLineNumber;
129: }
130:
131: /**
132: * Converts all whitespace characters in a String to space characters
133: * (\u0020).
134: */
135: public static String cleanWhitespace(String str) {
136: int length = str.length();
137:
138: StringBuffer buf = new StringBuffer(length);
139: for (int i = 0; i < length; i++) {
140: char c = str.charAt(i);
141: if (Character.isWhitespace(c)) {
142: buf.append(' ');
143: } else {
144: buf.append(c);
145: }
146: }
147:
148: return buf.toString();
149: }
150:
151: /**
152: * Creates and returns a String containing a sequence of the specified
153: * length, repeating the given character.
154: */
155: public static String createSequence(char c, int length) {
156: if (length < 0)
157: length = 1;
158:
159: StringBuffer buf = new StringBuffer(length);
160: for (; length > 0; length--) {
161: buf.append(c);
162: }
163: return buf.toString();
164: }
165: }
|