/Users/lyon/j4p/src/net/web/MetaSearch.java

1    package net.web; 
2     
3    import net.mail.Smtp; 
4    import net.proxy.Proxy; 
5    import net.stocks.Quote; 
6     
7    import java.io.UnsupportedEncodingException; 
8    import java.net.URLEncoder; 
9    import java.util.Vector; 
10    
11   /** 
12    * For homework, add 6 search engines to the meta search 
13    * facility so that a search string with multiple elements 
14    * is used to create a single result that can be contantated 
15    * into one massive search using all 6 the search engines. 
16    * You may select your own search engines. 
17    * For example 
18    * MetaSearch ms =  new MetaSearch( 
19    * "java server pages"); 
20    * System.out.println(ms.getSearchResults()); 
21    */ 
22   public class MetaSearch 
23           implements Runnable { 
24       String ss; 
25       String email; 
26       // 
27       //request.getParameter ("searchString")); 
28    
29       Thread thread = new Thread(this); 
30       Vector sv = new Vector(); 
31    
32       //For google, use: 
33       //http://www.google.com/search?hl=en&lr=&ie=UTF-8&q=searchItem1+SearchItem2 
34       //For homework 
35       // extend the search engine so that it uses 
36       // http://search.excite.com/ 
37       // http://search.yahoo.com/search?ei=UTF-8&fr=sfp&p=java+cobal 
38       // http://sjc-search.sjc.lycos.com/default.asp?lpv=1&loc=lycoshp&tab=web&query=java+cobal 
39       private void initSearchVector() { 
40           //sv.addElement("http://www.google.com/search?q=" 
41           //        + ss); 
42           sv.addElement("http://search.yahoo.com/search?ei=UTF-8&fr=sfp&p=" + ss); 
43           sv.addElement("http://citeseer.ist.psu.edu/cis?q=" + ss + "&submit=Search+Documents&cs=1"); 
44           //sv.addElement("http://www.google.com/search?hl=en&lr=&ie=UTF-8&q=java+cobol"); 
45       } 
46    
47       public MetaSearch(String searchString) { 
48           // for jdk1.4.x, uncomment the try,catch 
49           try { 
50               // used to be that the encoding 
51               // was not passed as an argument. 
52               // however, this was deprecated. 
53               ss = URLEncoder.encode(searchString, "UTF-8"); 
54               System.out.println("searchString=" + ss); 
55               // ss = URLEncoder.encode(searchString); 
56           } catch (UnsupportedEncodingException e) { 
57               e.printStackTrace(); 
58           } 
59           initSearchVector(); 
60       } 
61    
62       public MetaSearch(String searchString, String _email) { 
63           // for jdk1.4.x, uncomment the try,catch 
64           try { 
65               // used to be that the encoding 
66               // was not passed as an argument. 
67               // however, this was deprecated. 
68               ss = URLEncoder.encode(searchString, "UTF-8"); 
69               // ss = URLEncoder.encode(searchString); 
70           } catch (UnsupportedEncodingException e) { 
71               e.printStackTrace(); 
72           } 
73           email = _email; 
74           initSearchVector(); 
75           thread.start(); 
76       } 
77    
78       public void run() { 
79           String message = getSearchResults(); 
80           Smtp sm = new Smtp(); 
81           sm.setMailServerHostName("192.168.1.95"); 
82           sm.setRecipientEmail(email); 
83           sm.setSenderEmail("lyon@docjava.com"); 
84           sm.setMessage(message); 
85           sm.start(); 
86       } 
87    
88       public String getSearchResults() { 
89           // this may not be needed when you get home. 
90    
91           Proxy.setSoeProxy(); 
92           String searchResults = 
93                   "Search for " + ss + " yields:<hr><p>"; 
94           for (int i = 0; i < sv.size(); i++) { 
95               String url = (String) sv.elementAt(i); 
96               System.out.println("url=" + url); 
97               String s = "<hr><p>" + url 
98                       + " yields:<p>" 
99                       + //net.web.Browser.toString(url); 
100                      Quote.getUrl(url); 
101              searchResults = 
102                      searchResults + s; 
103          } 
104          return searchResults; 
105      } 
106   
107      public static void main(String args[]) { 
108          MetaSearch ms = new MetaSearch("java server pages"); 
109          String searchResults = ms.getSearchResults(); 
110          System.out.println(searchResults); 
111          System.out.println("done!"); 
112      } 
113  }