01: /*
02: * Created on Dec 14, 2005
03: *
04: * TODO To change the template for this generated file go to
05: * Window - Preferences - Java - Code Style - Code Templates
06: */
07: package org.enhydra.snapper.google.search.wrapper;
08:
09: import org.enhydra.snapper.api.google.GoogleSearcher;
10: import org.enhydra.snapper.api.google.GoogleSearchResult;
11: import org.enhydra.snapper.google.search.GoogleSearchPort;
12: import org.enhydra.snapper.google.search.GoogleSearchService;
13: import org.enhydra.snapper.google.search.GoogleSearchServiceLocator;
14:
15: /**
16: * @author lola
17: *
18: * TODO To change the template for this generated type comment go to
19: * Window - Preferences - Java - Code Style - Code Templates
20: */
21: public class SnapperGoogleSearcher implements GoogleSearcher {
22:
23: GoogleSearchService loc;
24: GoogleSearchPort service;
25:
26: private String url;
27: private String key;
28:
29: public SnapperGoogleSearcher(String url, String key) {
30: this .url = url;
31: this .key = key;
32: try {
33: loc = new GoogleSearchServiceLocator();
34: service = loc.getGoogleSearchPort(new java.net.URL(url));
35:
36: } catch (Exception e) {
37: SnapperGoogleSearcherFactory.logger.error(e.getMessage());
38: }
39: }
40:
41: public GoogleSearchResult doGoogleSearch(java.lang.String q,
42: int start, int maxResults, boolean filter,
43: java.lang.String restrict, boolean safeSearch,
44: java.lang.String lr, java.lang.String ie,
45: java.lang.String oe) throws java.rmi.RemoteException {
46: org.enhydra.snapper.google.search.GoogleSearchResult googleResult = service
47: .doGoogleSearch(key, q, start, maxResults, filter,
48: restrict, safeSearch, lr, ie, oe);
49: GoogleSearchResultImpl googleSearchResult = new GoogleSearchResultImpl();
50:
51: googleSearchResult.setBeginIndex((new Integer(googleResult
52: .getStartIndex()).toString()));
53: googleSearchResult.setDocsNumber((new Integer(googleResult
54: .getEstimatedTotalResultsCount()).toString()));
55: googleSearchResult.setEndIndex((new Integer(googleResult
56: .getEndIndex()).toString()));
57: googleSearchResult.setSearchedTerm(googleResult
58: .getSearchQuery());
59: googleSearchResult.setSearchedTime((new Double(googleResult
60: .getSearchTime()).toString()));
61:
62: org.enhydra.snapper.google.search.ResultElement resultElements[] = googleResult
63: .getResultElements();
64:
65: GoogleResultElementImpl result[] = new GoogleResultElementImpl[resultElements.length];
66:
67: for (int i = 0; i < resultElements.length; i++) {
68: result[i] = new GoogleResultElementImpl();
69: result[i].setContent(resultElements[i].getSnippet());
70: result[i].setTitle(resultElements[i].getTitle());
71: result[i].setURL(resultElements[i].getURL());
72: }
73:
74: googleSearchResult.setResultElements(result);
75:
76: return googleSearchResult;
77: }
78:
79: }
|