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 StringExaminer
028: // AUTHOR : Manfred Duchrow
029: // VERSION : 1.0 - 29/09/2002
030: // HISTORY :
031: // 29/09/2002 duma CREATED
032: //
033: // Copyright (c) 2002, by Manfred Duchrow. All rights reserved.
034: // ===========================================================================
035: package com.sshtools.daemon.util;
036:
037: // ===========================================================================
038: // IMPORTS
039: // ===========================================================================
040:
041: /**
042: * As a subclass of StringScanner this class allows more advanced navigation
043: * over the underlying string. <br>
044: * That includes moving to positions of specific substrings etc.
045: *
046: * @author Manfred Duchrow
047: * @version $Id: StringExaminer.java,v 1.7 2003/09/11 15:37:07 martianx Exp $
048: */
049: public class StringExaminer extends StringScanner {
050: // =========================================================================
051: // CONSTANTS
052: // =========================================================================
053: // =========================================================================
054: // INSTANCE VARIABLES
055: // =========================================================================
056: private boolean ignoreCase = false;
057:
058: // =========================================================================
059: // CLASS METHODS
060: // =========================================================================
061: // =========================================================================
062: // CONSTRUCTORS
063: // =========================================================================
064:
065: /**
066: * Initialize the new instance with the string to examine. <br>
067: * The string will be treated case-sensitive.
068: *
069: * @param stringToExamine The string that should be examined
070: */
071: public StringExaminer(String stringToExamine) {
072: this (stringToExamine, false);
073: }
074:
075: // StringExaminer()
076: // -------------------------------------------------------------------------
077:
078: /**
079: * Initialize the new instance with the string to examine.
080: *
081: * @param stringToExamine The string that should be examined
082: * @param ignoreCase Specified whether or not treating the string case
083: * insensitive
084: */
085: public StringExaminer(String stringToExamine, boolean ignoreCase) {
086: super (stringToExamine);
087: this .ignoreCase(ignoreCase);
088: }
089:
090: // StringExaminer()
091:
092: /**
093: *
094: *
095: * @return
096: */
097: protected boolean ignoreCase() {
098: return ignoreCase;
099: }
100:
101: /**
102: *
103: *
104: * @param newValue
105: */
106: protected void ignoreCase(boolean newValue) {
107: ignoreCase = newValue;
108: }
109:
110: // -------------------------------------------------------------------------
111: // =========================================================================
112: // PUBLIC INSTANCE METHODS
113: // =========================================================================
114:
115: /**
116: * Increments the position pointer up to the last character that matched
117: * the character sequence in the given matchString. Returns true, if the
118: * matchString was found, otherwise false.
119: *
120: * <p>
121: * If the matchString was found, the next invocation of method nextChar()
122: * returns the first character after that matchString.
123: * </p>
124: *
125: * @param matchString The string to look up
126: *
127: * @return
128: */
129: public boolean skipAfter(String matchString) {
130: char ch = '-';
131: char matchChar = ' ';
132: boolean found = false;
133: int index = 0;
134:
135: if ((matchString == null) || (matchString.length() == 0)) {
136: return false;
137: }
138:
139: ch = this .nextChar();
140:
141: while ((endNotReached(ch)) && (!found)) {
142: matchChar = matchString.charAt(index);
143:
144: if (this .charsAreEqual(ch, matchChar)) {
145: index++;
146:
147: if (index >= matchString.length()) { // whole matchString checked ?
148: found = true;
149: } else {
150: ch = this .nextChar();
151: }
152: } else {
153: if (index == 0) {
154: ch = this .nextChar();
155: } else {
156: index = 0;
157: }
158: }
159: }
160:
161: return found;
162: }
163:
164: // skipAfter()
165: // -------------------------------------------------------------------------
166:
167: /**
168: * Increments the position pointer up to the first character before the
169: * character sequence in the given matchString. Returns true, if the
170: * matchString was found, otherwise false.
171: *
172: * <p>
173: * If the matchString was found, the next invocation of method nextChar()
174: * returns the first character of that matchString from the position where
175: * it was found inside the examined string.
176: * </p>
177: *
178: * @param matchString The string to look up
179: *
180: * @return
181: */
182: public boolean skipBefore(String matchString) {
183: boolean found;
184: found = this .skipAfter(matchString);
185:
186: if (found) {
187: this .skip(0 - matchString.length());
188: }
189:
190: return found;
191: }
192:
193: // skipBefore()
194: // -------------------------------------------------------------------------
195:
196: /**
197: * Returns the a string containing all characters from the current position
198: * up to the end of the examined string. <br>
199: * The character position of the examiner is not changed by this method.
200: *
201: * @return
202: */
203: public String peekUpToEnd() {
204: return this .upToEnd(true);
205: }
206:
207: // peekUpToEnd()
208: // -------------------------------------------------------------------------
209:
210: /**
211: * Returns the a string containing all characters from the current position
212: * up to the end of the examined string. <br>
213: * The character position is put to the end by this method. That means the
214: * next invocation of nextChar() returns END_REACHED.
215: *
216: * @return
217: */
218: public String upToEnd() {
219: return this .upToEnd(false);
220: }
221:
222: // upToEnd()
223:
224: /**
225: *
226: *
227: * @param char1
228: * @param char2
229: *
230: * @return
231: */
232: protected boolean charsAreEqual(char char1, char char2) {
233: return (this .ignoreCase()) ? (Character.toUpperCase(char1) == Character
234: .toUpperCase(char2))
235: : (char1 == char2);
236: }
237:
238: // charsAreEqual()
239: // -------------------------------------------------------------------------
240:
241: /**
242: * Returns the a string containing all characters from the current position
243: * up to the end of the examined string. <br>
244: * Depending on the peek flag the character position of the examiner is
245: * unchanged (true) after calling this method or points behind the strings
246: * last character.
247: *
248: * @param peek
249: *
250: * @return
251: */
252: protected String upToEnd(boolean peek) {
253: char result = '-';
254: int lastPosition = 0;
255: StringBuffer buffer = new StringBuffer(100);
256: lastPosition = this .getPosition();
257: result = this .nextChar();
258:
259: while (endNotReached(result)) {
260: buffer.append(result);
261: result = this .nextChar();
262: }
263:
264: if (peek) {
265: this .setPosition(lastPosition);
266: }
267:
268: return buffer.toString();
269: }
270:
271: // upToEnd()
272: // -------------------------------------------------------------------------
273: }
274:
275: // class StringExaminer
|