1 37 package net.thauvin.google; 38 39 import com.google.soap.search.GoogleSearch; 40 import com.google.soap.search.GoogleSearchFault; 41 import com.google.soap.search.GoogleSearchResult; 42 import com.google.soap.search.GoogleSearchResultElement; 43 44 import java.net.URL ; 45 import java.net.URLEncoder ; 46 47 48 56 public class GoogleSearchBean 57 { 58 61 public static final boolean DEFAULT_CACHE = true; 62 63 66 public static final boolean DEFAULT_FILTER = true; 67 68 71 public static final String DEFAULT_LR = ""; 72 73 76 public static final int DEFAULT_MAX_RESULTS = 10; 77 78 81 public static final String DEFAULT_RESTRICT = ""; 82 83 86 public static final boolean DEFAULT_SAFE_SEARCH = false; 87 88 91 public static final String DEFAULT_SITE = ""; 92 93 96 public static final int DEFAULT_START = 0; 97 98 103 public static final String DEFAULT_TYPE = ""; 104 105 108 public static final String NEXT_KEYWORD = "next"; 109 110 113 public static final String PREVIOUS_KEYWORD = "previous"; 114 115 private static final String INVALID_KEY_ERROR = 117 "The authorization key has not been specified."; 118 private GoogleSearch service = null; 119 private GoogleSearchResult result = null; 120 private String keywords = null; 121 private GoogleSearchResultElement elements[] = null; 122 private boolean keySet = false; 123 private int maxResults = 10; 124 125 128 public GoogleSearchBean() 129 { 130 service = new GoogleSearch(); 131 } 132 133 139 public GoogleSearchBean(String key) 140 { 141 this(); 142 setKey(key); 143 } 144 145 150 public final void setKey(String key) 151 { 152 service.setKey(key); 153 154 if (isValidString(key)) 155 { 156 keySet = true; 157 } 158 else 159 { 160 keySet = false; 161 } 162 } 163 164 169 public final boolean isKeySet() 170 { 171 return keySet; 172 } 173 174 181 public final void setKeywords(String keywords) 182 { 183 this.keywords = keywords; 184 } 185 186 193 public final String getKeywords() 194 { 195 return this.keywords; 196 } 197 198 206 public void setProxyServer(String proxyHost, String proxyPort, 207 String proxyUserName, String proxyPassword) 208 { 209 int port = -1; 210 211 if (isValidString(proxyPort)) 212 { 213 try 214 { 215 port = Integer.valueOf(proxyPort).intValue(); 216 } 217 catch (NumberFormatException e) 218 { 219 ; } 221 } 222 223 setProxyServer(proxyHost, port, proxyUserName, proxyPassword); 224 } 225 226 234 public void setProxyServer(String proxyHost, int proxyPort, 235 String proxyUserName, String proxyPassword) 236 { 237 if (isValidString(proxyHost)) 238 { 239 service.setProxyHost(proxyHost); 240 241 if (proxyPort > 0) 242 { 243 service.setProxyPort(proxyPort); 244 } 245 246 if (isValidString(proxyUserName)) 247 { 248 service.setProxyUserName(proxyUserName); 249 } 250 251 if (isValidString(proxyPassword)) 252 { 253 service.setProxyPassword(proxyPassword); 254 } 255 } 256 } 257 258 263 public final GoogleSearchResult getResult() 264 { 265 return result; 266 } 267 268 274 public final GoogleSearchResultElement[] getResultElements() 275 { 276 return elements; 277 } 278 279 285 public final int getResultElementsCount() 286 { 287 if (elements != null) 288 { 289 return elements.length; 290 } 291 292 return 0; 293 } 294 295 301 public final boolean isValidResult() 302 { 303 if (result != null) 304 { 305 return true; 306 } 307 308 return false; 309 } 310 311 318 public String getCachedPage(String url) 319 throws GoogleSearchFault 320 { 321 if (isKeySet()) 322 { 323 reset(); 324 325 return new String (service.doGetCachedPage(url)); 326 } 327 328 throw new GoogleSearchFault(INVALID_KEY_ERROR); 329 } 330 331 344 public GoogleSearchResult getGoogleSearch(String q, int start, 345 int maxResults, boolean filter, 346 String restrict, 347 boolean safeSearch, String lr) 348 throws GoogleSearchFault 349 { 350 if (isKeySet()) 351 { 352 reset(); 353 354 service.setQueryString(q); 355 service.setStartResult(start); 356 357 this.maxResults = maxResults; 358 service.setMaxResults(maxResults); 359 360 service.setFilter(filter); 361 service.setRestrict(restrict); 362 service.setSafeSearch(safeSearch); 363 service.setLanguageRestricts(lr); 364 365 result = service.doSearch(); 366 367 if (result != null) 368 { 369 elements = result.getResultElements(); 370 } 371 372 return result; 373 } 374 375 throw new GoogleSearchFault(INVALID_KEY_ERROR); 376 } 377 378 393 public GoogleSearchResult getGoogleSearch(String key, String q, int start, 394 int maxResults, boolean filter, 395 String restrict, 396 boolean safeSearch, String lr) 397 throws GoogleSearchFault 398 { 399 setKey(key); 400 401 return getGoogleSearch(q, start, maxResults, filter, restrict, 402 safeSearch, lr); 403 } 404 405 413 public GoogleSearchResult getGoogleSearch(String q) 414 throws GoogleSearchFault 415 { 416 return getGoogleSearch(q, DEFAULT_START, DEFAULT_MAX_RESULTS, 417 DEFAULT_FILTER, DEFAULT_RESTRICT, 418 DEFAULT_SAFE_SEARCH, DEFAULT_LR); 419 } 420 421 463 public String getResultElementProperty(int index, String property) 464 { 465 if (elements != null) 466 { 467 if ((index >= 0) && (index < elements.length)) 468 { 469 if (property.equalsIgnoreCase("url")) 470 { 471 return elements[index].getURL(); 472 } 473 else if (property.equalsIgnoreCase("summary")) 474 { 475 return elements[index].getSummary(); 476 } 477 else if (property.equalsIgnoreCase("snippet")) 478 { 479 return elements[index].getSnippet(); 480 } 481 else if (property.equalsIgnoreCase("title")) 482 { 483 return elements[index].getTitle(); 484 } 485 else if (property.equalsIgnoreCase("cachedSize")) 486 { 487 return elements[index].getCachedSize(); 488 } 489 else if (property.equalsIgnoreCase("hostName")) 490 { 491 return elements[index].getHostName(); 492 } 493 else if (property.equalsIgnoreCase("relatedInformationPresent")) 494 { 495 return String.valueOf(elements[index] 496 .getRelatedInformationPresent()); 497 } 498 else if (property.equalsIgnoreCase("directoryTitle")) 499 { 500 return elements[index].getDirectoryTitle(); 501 } 502 else if (property.equalsIgnoreCase("directoryCategoryName")) 503 { 504 return elements[index].getDirectoryCategory() 505 .getFullViewableName(); 506 } 507 else if (property.equalsIgnoreCase("directoryCategoryEncoding")) 508 { 509 return elements[index].getDirectoryCategory() 510 .getSpecialEncoding(); 511 } 512 else if (property.toLowerCase().endsWith("query")) 513 { 514 try 515 { 516 URL url = new URL (elements[index].getURL()); 517 String urlString = url.toString(); 518 String staticQuery = 519 urlString.substring(urlString.indexOf(url.getHost())); 520 521 if (property.equalsIgnoreCase("relatedQuery")) 522 { 523 return ("related:" + URLEncoder.encode(staticQuery)); 524 } 525 else if (property.equalsIgnoreCase("cachedQuery")) 526 { 527 return (URLEncoder.encode(staticQuery)); 528 } 529 else if (property.equalsIgnoreCase("staticQuery")) 530 { 531 return staticQuery; 532 } 533 } 534 catch (Exception e) 535 { 536 ; } 538 } 539 } 540 } 541 542 return ""; 543 } 544 545 576 public String getResultProperty(String property) 577 { 578 if (result != null) 579 { 580 if (property.equalsIgnoreCase("estimatedTotalResultsCount")) 581 { 582 return String.valueOf(result.getEstimatedTotalResultsCount()); 583 } 584 else if (property.equalsIgnoreCase("startIndex")) 585 { 586 return String.valueOf(result.getStartIndex()); 587 } 588 else if (property.equalsIgnoreCase("endIndex")) 589 { 590 return String.valueOf(result.getEndIndex()); 591 } 592 else if (property.equalsIgnoreCase("searchTime")) 593 { 594 return String.valueOf(result.getSearchTime()); 595 } 596 else if (property.equalsIgnoreCase("searchTips")) 597 { 598 return result.getSearchTips(); 599 } 600 else if (property.equalsIgnoreCase("searchComments")) 601 { 602 return result.getSearchComments(); 603 } 604 else if (property.equalsIgnoreCase("documentFiltering")) 605 { 606 return String.valueOf(result.getDocumentFiltering()); 607 } 608 else if (property.equalsIgnoreCase("searchKeywords")) 609 { 610 return getKeywords(); 611 } 612 else if (property.equalsIgnoreCase("searchQuery")) 613 { 614 return result.getSearchQuery(); 615 } 616 else if (property.equalsIgnoreCase(NEXT_KEYWORD)) 617 { 618 if (result.getEndIndex() < result.getEstimatedTotalResultsCount()) 619 { 620 if (maxResults == (result.getEndIndex() 621 - result.getStartIndex() + 1)) 622 { 623 return String.valueOf(result.getEndIndex()); 624 } 625 } 626 } 627 else if (property.equalsIgnoreCase(PREVIOUS_KEYWORD)) 628 { 629 if (result.getStartIndex() > 1) 630 { 631 return String.valueOf(result.getStartIndex() - maxResults 632 - 1); 633 } 634 } 635 } 636 637 return ""; 638 } 639 640 647 public String getSpellingSuggestion(String phrase) 648 throws GoogleSearchFault 649 { 650 if (isKeySet()) 651 { 652 reset(); 653 654 return service.doSpellingSuggestion(phrase); 655 } 656 657 throw new GoogleSearchFault(INVALID_KEY_ERROR); 658 } 659 660 668 public static final void main(String args[]) 669 { 670 GoogleSearchBean bean = new GoogleSearchBean(); 671 672 if (args.length == 3) 673 { 674 String action = args[1]; 675 676 try 677 { 678 bean.setKey(args[0]); 679 680 if (action.equalsIgnoreCase("search")) 681 { 682 bean.getGoogleSearch(args[2]); 683 684 for (int i = 0; i < bean.getResultElementsCount(); i++) 685 { 686 System.out.println(bean.getResultElementProperty(i, 687 "title") 688 + " (" 689 + bean.getResultElementProperty(i, 690 "url") 691 + ')'); 692 } 693 } 694 else if (action.equalsIgnoreCase("spell")) 695 { 696 System.out.println(bean.getSpellingSuggestion(args[2])); 697 } 698 else if (action.equalsIgnoreCase("cached")) 699 { 700 System.out.println(bean.getCachedPage(args[2])); 701 } 702 else 703 { 704 usage(); 705 } 706 } 707 catch (GoogleSearchFault googleSearchFault) 708 { 709 googleSearchFault.printStackTrace(); 710 } 711 } 712 else 713 { 714 usage(); 715 } 716 } 717 718 721 public void reset() 722 { 723 result = null; 725 elements = null; 726 } 727 728 734 private boolean isValidString(String stringValue) 735 { 736 if ((stringValue != null) && (stringValue.trim().length() > 0)) 737 { 738 return true; 739 } 740 741 return false; 742 } 743 744 747 private static void usage() 748 { 749 System.err.println("Usage: java " + GoogleSearchBean.class.getName() 750 + " <client-key> (search <query> | cached <URL> | spell <phrase>)"); 751 System.exit(1); 752 } 753 } 754 | Popular Tags |