import com.google.soap.search.GoogleSearch; import com.google.soap.search.GoogleSearchResult; import com.google.soap.search.GoogleSearchFault; public class DemoGoogleAgent implements GoogleAgent { private GoogleSearch delegate; private String errorMessage; public DemoGoogleAgent( String googleKey ) { delegate = new GoogleSearch(); delegate.setKey( googleKey ); errorMessage = "None."; } public GoogleSearchResult search( String keywords ) { GoogleSearchResult result = null; try { delegate.setQueryString( keywords ); result = delegate.doSearch(); } catch (GoogleSearchFault f) { errorMessage = "*** The search for [" + keywords + "] failed.\n" + f.toString(); } return result; } public String errorMessage() { String answer = errorMessage; errorMessage = "None."; return answer; } public String getCachedPage( String url ) { String result = null; try { byte[] resultAsByteArray = delegate.doGetCachedPage( url ); result = new String( resultAsByteArray ); } catch (GoogleSearchFault f) { result = "*** The attempt to retrieve <" + url + "> failed.\n" + f.toString(); } return result; } public String suggestSpelling( String sourceString ) { String result = null; try { result = delegate.doSpellingSuggestion( sourceString ); } catch (GoogleSearchFault f) { result = "*** The attempt to spell-check [" + sourceString + "] failed.\n" + f.toString(); } return result; } public void setSearchSize( int desiredSize ) { delegate.setMaxResults( desiredSize ); } public void setStartResult( int firstIndex ) { delegate.setStartResult( firstIndex ); } }