KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sample > google > search > ClientCallbackHandler


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package sample.google.search;
17
18
19 import org.apache.axis2.clientapi.AsyncResult;
20 import org.apache.axis2.clientapi.Callback;
21 import org.apache.axis2.om.OMElement;
22 import org.apache.axis2.om.OMNode;
23 import org.apache.axis2.soap.SOAPBody;
24 import org.apache.axis2.soap.SOAPEnvelope;
25
26 import java.util.Iterator JavaDoc;
27
28 /**
29  * This class implements the onComplete method extended by call back
30  * recieves the Response
31  * process the soap with OM to extract the data
32  * Find the <NavigationURL> element and get the text from it
33  *
34  * @author Gayan Asanka (gayan@opensource.lk)
35  */

36 public class ClientCallbackHandler extends Callback {
37
38     /**
39      * HTML Header to desplay snippet text
40      */

41     private String JavaDoc beginHTML = "<HTML><HEAD><TITLE>Wow</TITLE></HEAD><BODY>";
42
43     /**
44      * HTML footer
45      */

46     private String JavaDoc endHTML = "</BODY></HTML>";
47
48     /**
49      * Store the texts read by NavigationURL of soap
50      */

51     private String JavaDoc snippet = beginHTML;
52
53     /**
54      * Store the URLs read by snippet element of soap
55      */

56     private String JavaDoc strURL;
57
58     /**
59      * Store texts temperaly
60      */

61     private String JavaDoc tempStr;
62
63
64     private GUIHandler handler;
65
66     public ClientCallbackHandler(GUIHandler handler) {
67         this.handler = handler;
68     }
69     
70     /**
71          * method onComplete
72          *
73          * @param result
74          */

75
76     public void onComplete(AsyncResult result) {
77         AsyncResult myResult = result;
78         extractDetails(myResult);
79     }
80
81     /**
82      * method extractDetails
83      * Go through the children of soap
84      * stores requres information in variables
85      * Call to desplay the results
86      *
87      * @param result
88      */

89     private void extractDetails(AsyncResult result) {
90         Iterator JavaDoc iterator,iterator2;
91         OMNode node;
92         SOAPBody body;
93         OMElement operation, elem;
94         SOAPEnvelope resEnvelope;
95 // /////////////////////////////////////////
96
// try {
97
// XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(
98
// System.out);
99
// result.getResponseEnvelope().serializeWithCache(writer);
100
// //part.serialize(writer,false);
101
// writer.flush();
102
//
103
// } catch (XMLStreamException e) {
104
// // System.out.println("Error occured after responce is received");
105
// e.printStackTrace();
106
// } catch (FactoryConfigurationError e) {
107
// //System.out.println("Error occured after responce is received");
108
// e.printStackTrace();
109
// }
110
// ////////////////////////////////////////////////
111
resEnvelope = result.getResponseEnvelope();
112         body = resEnvelope.getBody();
113         operation = body.getFirstElement();
114         if (body.hasFault()){
115             snippet =
116                     snippet +
117                     "A Fault message recieved, Check your Licence key. Else you have reached the" +
118                     " daily limit of 1000 requests";
119         } else {
120             OMElement part = operation.getFirstElement();
121
122             iterator = part.getChildren();
123             while (iterator.hasNext()) {
124                 node = (OMNode) iterator.next();
125                 if (node.getType() == OMNode.ELEMENT_NODE) {
126                     elem = (OMElement) node;
127                     String JavaDoc str = elem.getLocalName();
128                     //System.out.println(str);
129
if (str.equals("resultElements")) {
130                         //System.out.println("Got the Result Elements");
131
Iterator JavaDoc iterator0 = elem.getChildren();
132                         while (iterator0.hasNext()) {
133                             node = (OMNode) iterator0.next();
134                             if (node.getType() == OMNode.ELEMENT_NODE) {
135                                 elem = (OMElement) node;
136                                 if (elem.getLocalName().equals("item")) {
137                                     iterator2 = elem.getChildren();
138                                     while (iterator2.hasNext()) {
139                                         node = (OMNode) iterator2.next();
140                                         if (node.getType() == OMNode.ELEMENT_NODE) {
141                                             elem = (OMElement) node;
142                                             String JavaDoc str3 = elem.getLocalName();
143                                             System.out.println(str3);
144                                             if (elem.getLocalName().equals("snippet")) {
145                                                 //System.out.println("Got the snippet");
146
tempStr = elem.getText();
147
148                                                 //System.out.println(tempStr);
149
snippet = snippet + tempStr;
150                                             }
151
152                                             if (elem.getLocalName().equals("URL")) {
153                                                 //System.out.println("Got the URL");
154
strURL = elem.getText();
155                                             }
156                                         }
157                                     }
158                                 }
159                                 snippet = snippet + "<br> URL:-<a HREF=" + strURL + ">" + strURL +
160                                         "</a\n\n> <br><br>";
161                             }
162                         }
163                     }
164                 }
165             }
166         }
167         snippet = snippet + endHTML;
168         this.handler.showResults(snippet);
169     }
170
171     public void reportError(Exception JavaDoc e) {
172         e.printStackTrace();
173     }
174 }
Popular Tags