Please do not edit here
Submit your comment to jos-apps mailinglist, or the FindReplaceBeanModelCodeComments
package jos.find; /** FindReplaceBean * 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: 9 Nov 1997 * Revise: 0.11 */ import java.beans.*; import java.io.*; public class FindReplaceBean extends Beans 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; if (debug) System.err.println("buffer:" + buffer + " @ " + (currentIndex - searchLength)); // 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++) { char bufferChar = buffer[i]; char searchChar = searchBuffer[i]; if (ignoreCase) { bufferChar = Character.toLowerCase(bufferChar); searchChar = Character.toLowerCase(searchChar); } if (bufferChar != searchChar) result = false; } } 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 (buffer[i] == searchBuffer[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 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[]) { FindReplaceBean frb = new FindReplaceBean(); try { frb.setDebug(true); frb.setReader(new FileReader("META-INF/MANIFEST.MF")); frb.setSearchText("bean"); frb.setOffset(31); frb.setIgnoreCase(true); System.out.println(frb.getIndex()); } catch (Exception e) { e.getLocalizedMessage(); e.printStackTrace(); } } }-- StefanBorg