001: /*
002: * SSHTools - Java SSH2 API
003: *
004: * Copyright (C) 2002-2003 Lee David Painter and Contributors.
005: *
006: * Contributions made by:
007: *
008: * Brett Smith
009: * Richard Pernavas
010: * Erwin Bolwidt
011: *
012: * This program is free software; you can redistribute it and/or
013: * modify it under the terms of the GNU General Public License
014: * as published by the Free Software Foundation; either version 2
015: * of the License, or (at your option) any later version.
016: *
017: * This program is distributed in the hope that it will be useful,
018: * but WITHOUT ANY WARRANTY; without even the implied warranty of
019: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
020: * GNU General Public License for more details.
021: *
022: * You should have received a copy of the GNU General Public License
023: * along with this program; if not, write to the Free Software
024: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
025: */
026: // ===========================================================================
027: // CONTENT : CLASS StringScanner
028: // AUTHOR : Manfred Duchrow
029: // VERSION : 1.1 - 29/09/2002
030: // HISTORY :
031: // 11/07/2001 duma CREATED
032: // 29/09/2002 duma added -> endReached(), endNotReached()
033: //
034: // Copyright (c) 2001-2002, by Manfred Duchrow. All rights reserved.
035: // ===========================================================================
036: package com.sshtools.daemon.util;
037:
038: // ===========================================================================
039: // IMPORTS
040: // ===========================================================================
041:
042: /**
043: * Simple scanner that allows to navigate over the characters of a string.
044: *
045: * @author Manfred Duchrow
046: * @version 1.1
047: */
048: public class StringScanner {
049: // =========================================================================
050: // CONSTANTS
051: // =========================================================================
052:
053: /** */
054: public static final char END_REACHED = (char) -1;
055:
056: // =========================================================================
057: // INSTANCE VARIABLES
058: // =========================================================================
059:
060: /** */
061: protected int length = 0;
062:
063: /** */
064: protected int position = 0;
065:
066: /** */
067: protected int pos_marker = 0;
068:
069: /** */
070: protected char[] buffer = null;
071:
072: // -------------------------------------------------------------------------
073: // =========================================================================
074: // CONSTRUCTORS
075: // =========================================================================
076:
077: /**
078: * Initialize the new instance with the string that should be scanned.
079: *
080: * @param stringToScan
081: */
082: public StringScanner(String stringToScan) {
083: super ();
084: length = stringToScan.length();
085: buffer = new char[length];
086: stringToScan.getChars(0, length, buffer, 0);
087: }
088:
089: // StringScanner()
090: // =========================================================================
091: // PUBLIC CLASS METHODS
092: // =========================================================================
093:
094: /**
095: * Returns true, if the given character indicates that the end of the
096: * scanned string is reached.
097: *
098: * @param character
099: *
100: * @return
101: */
102: public boolean endReached(char character) {
103: return (character == END_REACHED);
104: }
105:
106: // endReached()
107: // -------------------------------------------------------------------------
108:
109: /**
110: * Returns true, if the given character does <b>not</b> indicate that the
111: * end of the scanned string si reached.
112: *
113: * @param character
114: *
115: * @return
116: */
117: public boolean endNotReached(char character) {
118: return (!endReached(character));
119: }
120:
121: // endNotReached()
122: // =========================================================================
123: // PUBLIC INSTANCE METHODS
124: // =========================================================================
125:
126: /**
127: * Returns the string the scanner was initialized with
128: *
129: * @return
130: */
131: public String toString() {
132: return new String(buffer);
133: }
134:
135: // toString()
136: // -------------------------------------------------------------------------
137:
138: /**
139: * Moves the position pointer count characters. positive values move
140: * forwards, negative backwards. The position never becomes negative !
141: *
142: * @param count
143: */
144: public void skip(int count) {
145: position += count;
146:
147: if (position < 0) {
148: position = 0;
149: }
150: }
151:
152: // skip()
153: // -------------------------------------------------------------------------
154:
155: /**
156: * Returns the character at the current position without changing the
157: * position, that is subsequent calls to this method return always the
158: * same character.
159: *
160: * @return
161: */
162: public char peek() {
163: return ((position < length()) ? buffer[position] : END_REACHED);
164: }
165:
166: // skip()
167: // -------------------------------------------------------------------------
168:
169: /**
170: * Returns the character at the current position and increments the
171: * position afterwards by 1.
172: *
173: * @return
174: */
175: public char nextChar() {
176: char next = this .peek();
177:
178: if (endNotReached(next)) {
179: this .skip(1);
180: }
181:
182: return next;
183: }
184:
185: // nextChar()
186: // -------------------------------------------------------------------------
187:
188: /**
189: * Returns true, if the scanner has reached the end and a further
190: * invocation of nextChar() would return the END_REACHED character.
191: *
192: * @return
193: */
194: public boolean atEnd() {
195: return (endReached(this .peek()));
196: }
197:
198: // atEnd()
199: // -------------------------------------------------------------------------
200:
201: /**
202: * Returns true, if the scanner has not yet reached the end.
203: *
204: * @return
205: */
206: public boolean hasNext() {
207: return !this .atEnd();
208: }
209:
210: // hasNext()
211: // -------------------------------------------------------------------------
212:
213: /**
214: * Returns the next character that is no whitespace and leaves the position
215: * pointer one character after the returned one.
216: *
217: * @return
218: */
219: public char nextNoneWhitespaceChar() {
220: char next = this .nextChar();
221:
222: while ((endNotReached(next)) && (Character.isWhitespace(next))) {
223: next = this .nextChar();
224: }
225:
226: return next;
227: }
228:
229: // nextNoneWhitespaceChar()
230: // -------------------------------------------------------------------------
231:
232: /**
233: * Returns the current position in the string
234: *
235: * @return
236: */
237: public int getPosition() {
238: return position;
239: }
240:
241: // getPosition()
242: // -------------------------------------------------------------------------
243:
244: /**
245: * Remembers the current position for later use with restorePosition()
246: */
247: public void markPosition() {
248: pos_marker = position;
249: }
250:
251: // markPosition()
252: // -------------------------------------------------------------------------
253:
254: /**
255: * Restores the position to the value of the latest markPosition() call
256: */
257: public void restorePosition() {
258: this .setPosition(pos_marker);
259: }
260:
261: // restorePosition()
262:
263: /**
264: *
265: *
266: * @return
267: */
268: protected int length() {
269: return length;
270: }
271:
272: // length()
273: // -------------------------------------------------------------------------
274: protected void setPosition(int pos) {
275: if ((pos >= 0) && (pos <= this .length())) {
276: position = pos;
277: }
278: }
279:
280: // setPosition()
281: // -------------------------------------------------------------------------
282: }
283:
284: // class StringScanner
|