/* SearchResultsCollector.java - October 1999 - Scott Parent */ import java.util.*; import java.io.IOException; /** * This class is designed to be packaged with a COM DLL output format. * The class has no standard entry points, other than the constructor. * Public methods will be exposed as methods on the default COM interface. * @com.register ( clsid=3BC0D8AA-8981-11D3-91AE-004095013729, typelib=3BC0D8AB-8981-11D3-91AE-004095013729 ) */ public class SearchResultsCollector extends Thread { // m_MaxIndex property - 11/09/99 - Deric Bertrand private int m_MaxIndex; private SearchEngineFacade m_Engine = null; private Vector m_Collection = new Vector(); private String m_queryText = null; private Exception m_lastException = null; // state management // Changed scope of state constants to class (static) - 11/09/99 - Deric Bertrand public static final int S_New = 0; public static final int S_Ready = 1; public static final int S_Running = 2; public static final int S_Done = 3; public static final int S_Exception = 4; private int m_state = S_New; public SearchResultsCollector() // default constructor { m_Engine = new AltaVistaFacade(); m_MaxIndex = 190; // Initialize property - 11/09/99 - Deric Bertrand } public SearchEngineFacade getEngine() { return m_Engine; } public Exception getLastException() { return m_lastException; } public SearchResult getSearchResult( int index) { if( index < m_Collection.size()) return (SearchResult)m_Collection.elementAt( index); else return null; } public int getCount() { return m_Collection.size();} public int getState() { return m_state;} public String getQueryText() { return m_queryText;} public void setEngine(SearchEngineFacade anEngine) { m_Engine = anEngine; } // Method to set max index property - 11/09/99 - Deric Bertrand public void setMaxIndex(int newMaxIndex) { m_MaxIndex = newMaxIndex; } public void setQueryText( String newText) throws IllegalStateException { if( getState() < S_Running) { m_queryText = newText; m_state = S_Ready; } else throw new IllegalStateException( "Query text cannot be changed once result collection is started" ); } public void run() { if( getState() == S_Ready) { int listingIndex = 0; // Bug fix - 11/09/99 - Deric Bertrand int resultCount = 0; for( m_state = S_Running; m_state == S_Running; listingIndex += resultCount) { try { resultCount = m_Engine.SubmitQuery( m_queryText, listingIndex, m_Collection); // Bounds checking - 11/09/99 - Deric Bertrand // Bug Fix - 11/11/99 - Deric Bertrand if( resultCount == 0 || (resultCount + listingIndex) > m_MaxIndex) m_state = S_Done; } catch( IOException x) { m_state = S_Exception; m_lastException = x; } // Exception handling - 11/09/99 - Deric Bertrand catch( java.text.ParseException x) { m_state = S_Exception; m_lastException = x; } // Exception handling - 11/11/99 - Deric Bertrand catch( Exception x) { m_state = S_Exception; m_lastException = x; } } } } }