Home   Info   DevZone   Wiki  
UsersWeb  |  MainWeb  |  InfoWeb  |  DevZoneWeb  |  SupportWeb
FindTextBean ] [ not logged in ] [ Web: Imported ] goto:  options
[ get info on or edit ] login or new user ] [ list of topics, hubs & nodes, or recent changes ]

__Objective:__ to produce a searching text engine in the form of a JavaBean. Will be pluggable with multiple GUIs and TUIs.

This bean should contain the following features:

Inputs:

Outputs: Keep in mind - Maximum Code Reusability
Started: StefanBorg, 6 Nov 1997 Edited: StefanBorg, 13 Nov 1997

Return to ApplicationsGroup


Code:
package jos.utils.find;
/** FindTextBean
 * This is the 'underlying architecture' or 'workhorse'.
 * Please refer to JOS Collaborative Effort for more info.
 * 
 * Author:	Stefan Borg
 * Started Date:	6 Nov 1997
 * Edited Date:	11 Nov 1997
 * Revise:	0.12
 */

import java.beans.Beans;
import java.io.*;

public class FindTextBean implements Serializable {

/** Instance variables.
 */
	private boolean debug = false;
	private Reader reader = null;
	private char[] searchBuffer = null;
	private long offset = 0;
	private boolean ignoreCase = false;
	private boolean wholeWords = false;

/** Internal variables.
 */
	private char[] buffer = null;
	private int searchLength = 0;
	private int bufferOffset = 0;

/** Enables/disables system console messages for debuggin purposes.
 */
	public void setDebug(boolean debug) { this.debug = debug; }
	public boolean getDebug() {	return debug;	}

/** Set/gets source text for searching through.
*/
	public void setReader(Reader reader) { this.reader = reader; }
	public Reader getReader() {	return reader;	}

/** Set/gets search text for searching for.
 */
	public void setSearchText(String searchText) { 
		searchBuffer = searchText.toCharArray();
		searchLength = searchBuffer.length;
	}
	public String getSearchText() {	return new String(searchBuffer);	}

/** Set/gets option for ignoring case.
 */
	public void setIgnoreCase(boolean ignoreCase) { this.ignoreCase = ignoreCase; }
	public boolean getIgnoreCase() {	return ignoreCase;	}

/** Set/gets option for whole words only.
 */
	public void setWholeWords(boolean wholeWords) { this.wholeWords = wholeWords; }
	public boolean getWholeWords() {	return wholeWords;	}

/** Set/gets offset value to begin searching from. Later to become a protected method.
 */
	public void setOffset(long offset) { this.offset = offset; }
	public long getOffset() {	return offset;	}

/** Returns index value of found text. Maybe later to become a protected method.
 * Queries:
 *	 should reading depend on Reader.ready()??
 */
	public long getIndex() throws InvalidSourceException {

// Checks all inputs are available
		check();

		long index = -1;
		Reader tempReader = reader;
		buffer = new char[searchLength];
		long currentIndex = offset;
		bufferOffset = 0;

	out:
		try {

// Skips offset value
			tempReader.skip(currentIndex);

			do {

// Reads stream into buffer
				if (tempReader.read(buffer, bufferOffset, 
						(searchLength - bufferOffset)) == -1)
					break out;

				currentIndex += searchLength - bufferOffset;

// Until buffer matches search text
			} while (!compareBuffer());

			index = currentIndex - searchLength;
		} catch (IOException io) {
			io.getLocalizedMessage();
			io.printStackTrace();
		}

		if (debug)
			System.err.println("getIndex = " + index);

		return index;
	}

/** Returns true if arrays are equal.
 */
	private boolean compareBuffer() {
		boolean result = true;

		try {
			for (int i = 0; i < searchLength; i++)
				result = compareCharacter(buffer[i], i);
		} catch(NullPointerException npe) {
			result = false;
		}

// if unequal, check that start of search text is not in the buffer
		if (!result) {
			bufferOffset = checkBuffer();
		}

		return result;
	}

/** Returns index of first character equal to first
 * character of search text. Returns -1 if none found.
 */
	private int checkBuffer() {
		for (int i = 1; i < searchLength; i++) {
			if (compareCharacter(buffer[i], 0)) {
				if (debug)
					System.err.println("found @:"+i);
				return shiftBuffer(i);
			}
		}
		return 0;
	}

/** Shifts array into new position. Returns last index.
 */
	private int shiftBuffer(int first) {
		char[] tmpBuffer = buffer;
		int i = 0;
		for (; i < (searchLength - first); i++) {
			buffer[i] = tmpBuffer[first + i];
		}

		if (debug)
			System.err.println("shifted:"+buffer);

		return i;
	}

/** Returns true if comparison of characters at a position in the
 * search text are equal.
 */
	private boolean compareCharacter(char bufferChar, int pos) {
		boolean result = true;
		char searchChar = searchBuffer[pos];

		if (ignoreCase) {
			bufferChar = Character.toLowerCase(bufferChar);
			searchChar = Character.toLowerCase(searchChar);
		}

		if (bufferChar != searchChar) {
			result = false;
		}
		return result;
	}

/** Returns true if Reader stream and search text are valid.
 */
	private void check() throws InvalidSourceException {
		if (reader == null) {
			throw new InvalidSourceException();
		}
	}

/** InvalidSourceException class.
 */
	class InvalidSourceException extends Exception {
		InvalidSourceException() {
			super("Reader source does not exist.");
		}
	};

/** For testing.
 */
	public static void main(String args[]) {
		try {
			FindTextBean ftb = (FindTextBean)Beans.instantiate(null, "jos.utils.find.FindTextBean");
			ftb.setDebug(true);
			ftb.setReader(new FileReader("META-INF/MANIFEST.MF"));
			ftb.setSearchText("bean");
			ftb.setOffset(21);
			ftb.setIgnoreCase(true);

			System.out.println(ftb.getIndex());
		} catch (Exception e) {
			e.getLocalizedMessage();
			e.printStackTrace();
		}
	}
}



Content of these pages are owned and copyrighted by the poster.
Hosted by: