You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-dev@axis.apache.org by aj...@apache.org on 2005/06/03 11:19:29 UTC
svn commit: r179755 - in
/webservices/axis/trunk/java/modules/samples/src/sample/google/spellcheck:
./ AsyncPanel.java FormModel.java Observer.java SuggestionForm.java
SyncPanel.java
Author: ajith
Date: Fri Jun 3 02:19:28 2005
New Revision: 179755
URL: http://svn.apache.org/viewcvs?rev=179755&view=rev
Log:
Adding the google spellcheck sample. Still needs some work to get it properly running
Added:
webservices/axis/trunk/java/modules/samples/src/sample/google/spellcheck/
webservices/axis/trunk/java/modules/samples/src/sample/google/spellcheck/AsyncPanel.java
webservices/axis/trunk/java/modules/samples/src/sample/google/spellcheck/FormModel.java
webservices/axis/trunk/java/modules/samples/src/sample/google/spellcheck/Observer.java
webservices/axis/trunk/java/modules/samples/src/sample/google/spellcheck/SuggestionForm.java
webservices/axis/trunk/java/modules/samples/src/sample/google/spellcheck/SyncPanel.java
Added: webservices/axis/trunk/java/modules/samples/src/sample/google/spellcheck/AsyncPanel.java
URL: http://svn.apache.org/viewcvs/webservices/axis/trunk/java/modules/samples/src/sample/google/spellcheck/AsyncPanel.java?rev=179755&view=auto
==============================================================================
--- webservices/axis/trunk/java/modules/samples/src/sample/google/spellcheck/AsyncPanel.java (added)
+++ webservices/axis/trunk/java/modules/samples/src/sample/google/spellcheck/AsyncPanel.java Fri Jun 3 02:19:28 2005
@@ -0,0 +1,72 @@
+package sample.google.spellcheck;
+
+import org.apache.axis.clientapi.Call;
+import org.apache.axis.engine.AxisFault;
+import org.apache.axis.addressing.EndpointReference;
+import org.apache.axis.addressing.AddressingConstants;
+import org.apache.axis.soap.SOAPEnvelope;
+
+import javax.xml.stream.XMLOutputFactory;
+import javax.xml.stream.XMLStreamWriter;
+import javax.xml.stream.XMLStreamException;
+import javax.swing.*;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.awt.event.KeyListener;
+import java.awt.event.KeyEvent;
+import java.awt.*;
+
+/**
+ * class sample.google.spellcheck.AsyncPanel
+ * This Impements its own GUI of the Asynchronous Client and it updates the string after getting the response to textarea
+ * @author Nadana Gunarathna
+ *
+ */
+public class AsyncPanel extends javax.swing.JPanel implements Observer,KeyListener{
+ FormModel formModel;
+ javax.swing.JTextArea textAreaget;
+ javax.swing.JTextArea textAreaset;
+ public AsyncPanel()
+ {
+ formModel = new FormModel(this);
+ textAreaget = new javax.swing.JTextArea(15,10);
+ textAreaset = new javax.swing.JTextArea(15,10);
+ JScrollPane scrollPaneGet = new JScrollPane(textAreaget);
+ JScrollPane scrollPaneSet = new JScrollPane(textAreaset);
+ setPreferredSize(new Dimension(450, 460));
+
+ textAreaget.setText("Enter a String");
+ textAreaget.addKeyListener(this);
+
+
+ this.add(scrollPaneGet,BorderLayout.NORTH);
+ this.add(scrollPaneSet,BorderLayout.SOUTH);
+
+
+ }
+ public void update(String suggestion)
+ {
+ textAreaset.setText(suggestion);
+
+ // put the suggestion string in the reaponse box along with the misspelt word
+ }
+
+
+ public void keyPressed(KeyEvent e) {
+ //To change body of implemented methods use File | Settings | File Templates.
+ int key=e.getKeyChar();
+ if((key==32)||(key==10)){
+ String suggesion=textAreaget.getText().trim();
+ //suggesion.trim();
+ formModel.doAsyncSpellingSuggestion(suggesion);
+ }
+ }
+
+ public void keyReleased(KeyEvent e) {
+ //To change body of implemented methods use File | Settings | File Templates.
+ }
+
+ public void keyTyped(KeyEvent e) {
+ //To change body of implemented methods use File | Settings | File Templates.
+ }
+}
Added: webservices/axis/trunk/java/modules/samples/src/sample/google/spellcheck/FormModel.java
URL: http://svn.apache.org/viewcvs/webservices/axis/trunk/java/modules/samples/src/sample/google/spellcheck/FormModel.java?rev=179755&view=auto
==============================================================================
--- webservices/axis/trunk/java/modules/samples/src/sample/google/spellcheck/FormModel.java (added)
+++ webservices/axis/trunk/java/modules/samples/src/sample/google/spellcheck/FormModel.java Fri Jun 3 02:19:28 2005
@@ -0,0 +1,171 @@
+package sample.google.spellcheck;
+
+import org.apache.axis.soap.SOAPFactory;
+import org.apache.axis.soap.SOAPEnvelope;
+import org.apache.axis.om.OMAbstractFactory;
+import org.apache.axis.om.OMNamespace;
+import org.apache.axis.om.OMElement;
+import org.apache.axis.clientapi.AsyncResult;
+import org.apache.axis.clientapi.Call;
+import org.apache.axis.clientapi.Callback;
+import org.apache.axis.engine.AxisFault;
+import org.apache.axis.addressing.EndpointReference;
+import org.apache.axis.addressing.AddressingConstants;
+
+import javax.xml.stream.XMLStreamWriter;
+import javax.xml.stream.XMLOutputFactory;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.namespace.QName;
+import java.net.URL;
+import java.net.MalformedURLException;
+
+/**
+ * class sample.google.spellcheck.FormModel
+ * This is the Impementation of the Asynchronous Client
+ * @author Nadana Gunarathna
+ *
+ */
+public class FormModel extends Callback {
+
+ Observer observer;
+ public FormModel(Observer observer)
+ {
+ this.observer = observer;
+ }
+
+ private OMElement getElement(String word){
+ SOAPFactory omfactory=OMAbstractFactory.getSOAP11Factory();
+ OMNamespace opN = omfactory.createOMNamespace("urn:GoogleSearch","ns1");
+ OMNamespace emptyNs=omfactory.createOMNamespace("", null);
+
+ OMElement method = omfactory.createOMElement("doSpellingSuggestion", opN);
+ //reqEnv.getBody().addChild(method);
+ method.addAttribute("soapenv:encodingStyle","http://schemas.xmlsoap.org/soap/encoding/",null);
+ OMElement value1 = omfactory.createOMElement("key",emptyNs);
+ OMElement value2=omfactory.createOMElement("phrase",emptyNs);
+ value1.addAttribute("xsi:type","xsd:string",null);
+ value2.addAttribute("xsi:type","xsd:string",null);
+ value1.addChild(omfactory.createText(value1, "wzdxGcZQFHJ71w7IgCj5ddQGLmODsP9g"));
+ value2.addChild(omfactory.createText(value2,word));
+ method.addChild(value2);
+ method.addChild(value1);
+ return method;
+ }
+
+ private SOAPEnvelope getEnvelope(String word)
+ {
+ SOAPFactory omfactory=OMAbstractFactory.getSOAP11Factory();
+ SOAPEnvelope reqEnv=omfactory.getDefaultEnvelope();
+ OMNamespace emptyNs=omfactory.createOMNamespace("", null);
+ OMNamespace xsi = reqEnv.declareNamespace("http://www.w3.org/1999/XMLSchema-instance","xsi");
+ OMNamespace xsd = reqEnv.declareNamespace("http://www.w3.org/1999/XMLSchema","xsd");
+ OMNamespace opN = reqEnv.declareNamespace("urn:GoogleSearch","ns1");
+ OMElement method = omfactory.createOMElement("doSpellingSuggestion", opN);
+ reqEnv.getBody().addChild(method);
+ method.addAttribute("soapenv:encodingStyle","http://schemas.xmlsoap.org/soap/encoding/",null);
+ OMElement value1 = omfactory.createOMElement("key",emptyNs);
+ OMElement value2=omfactory.createOMElement("phrase",emptyNs);
+ value1.addAttribute("xsi:type","xsd:string",null);
+ value2.addAttribute("xsi:type","xsd:string",null);
+ value1.addChild(omfactory.createText(value1, "wzdxGcZQFHJ71w7IgCj5ddQGLmODsP9g"));
+ value2.addChild(omfactory.createText(value2,word));
+ method.addChild(value2);
+ method.addChild(value1);
+ return reqEnv;
+
+ }
+ public void doAsyncSpellingSuggestion(String word)
+ {
+ OMElement requestElement = getElement(word);
+ System.out.println("Initializing the Client Call....");
+ Call call = null;
+ try {
+ call = new Call();
+ } catch (AxisFault axisFault) {
+ axisFault.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
+ }
+ System.out.println("Setting the Endpointreference ");
+ URL url = null;
+ try {
+// url = new URL("http","127.0.0.1",8080,"/search/beta2");
+ url = new URL("http","api.google.com","/search/beta2");
+ } catch (MalformedURLException e) {
+
+ e.printStackTrace();
+ System.exit(0);
+ }
+
+ call.setTo(new EndpointReference(AddressingConstants.WSA_TO, url.toString()));
+ //call.invokeNonBlocking("doGoogleSpellingSugg",requestEnvelop, new ClientCallbackHandler(parent));
+ try {
+ call.invokeNonBlocking("doGoogleSpellingSugg",requestElement,this);
+ } catch (AxisFault axisFault) {
+ axisFault.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
+ }
+
+ }
+ public void doSyncSpellingSuggestion(String word)
+ {
+ SOAPEnvelope response=null;
+ SOAPEnvelope requestEnvelope = getEnvelope(word);
+ System.out.println("Initializing the Client Call....");
+ Call call = null;
+ try {
+ call = new Call();
+ } catch (AxisFault axisFault) {
+ axisFault.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
+ }
+ System.out.println("Setting the Endpointreference ");
+ URL url = null;
+ try {
+ url = new URL("http","api.google.com","/search/beta2");
+ } catch (MalformedURLException e) {
+
+ e.printStackTrace();
+ System.exit(0);
+ }
+
+ call.setTo(new EndpointReference(AddressingConstants.WSA_TO, url.toString()));
+ try {
+ response=(SOAPEnvelope)call.invokeBlocking("doGoogleSpellingSugg",requestEnvelope);
+ } catch (AxisFault axisFault) {
+ axisFault.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
+ }
+
+
+ this.getResponse(response);
+ }
+ public String getResponse(SOAPEnvelope responseEnvelope){
+ ////////////////////////////////////////////
+ try {
+ XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(System.out);
+ responseEnvelope.serialize(writer);
+ writer.flush();
+ } catch (XMLStreamException e) {
+ e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
+ }
+
+ ////////////////////////////////////////////
+ QName qName1 = new QName("urn:GoogleSearch", "doSpellingSuggestionResponse");
+ QName qName2 = new QName("urn:GoogleSearch", "return");
+ OMElement returnvalue1 = responseEnvelope.getBody().getFirstChildWithName(qName1);
+ OMElement val = returnvalue1.getFirstChildWithName(qName2);
+ org.apache.axis.om.OMNode omtext = val.getFirstChild();
+ String sugession = null;
+ sugession = ((org.apache.axis.om.OMText) omtext).getText();
+ this.observer.update(sugession);
+
+ return sugession;
+ }
+
+ public void onComplete(AsyncResult asyncResult) {
+ String sugession = getResponse(asyncResult.getResponseEnvelope());
+ this.observer.update(sugession);
+ //To change body of implemented methods use File | Settings | File Templates.
+
+ }
+
+ public void reportError(Exception e) {
+ //To change body of implemented methods use File | Settings | File Templates.
+ }
+}
Added: webservices/axis/trunk/java/modules/samples/src/sample/google/spellcheck/Observer.java
URL: http://svn.apache.org/viewcvs/webservices/axis/trunk/java/modules/samples/src/sample/google/spellcheck/Observer.java?rev=179755&view=auto
==============================================================================
--- webservices/axis/trunk/java/modules/samples/src/sample/google/spellcheck/Observer.java (added)
+++ webservices/axis/trunk/java/modules/samples/src/sample/google/spellcheck/Observer.java Fri Jun 3 02:19:28 2005
@@ -0,0 +1,14 @@
+package sample.google.spellcheck;
+
+import javax.swing.*;
+import java.awt.*;
+import java.awt.event.ActionEvent;
+
+/**
+ * interface sample.google.spellcheck.Observer
+ * @author Nadana Gunarathna
+ *
+ */
+public interface Observer {
+ public void update(String suggestion);
+ }
Added: webservices/axis/trunk/java/modules/samples/src/sample/google/spellcheck/SuggestionForm.java
URL: http://svn.apache.org/viewcvs/webservices/axis/trunk/java/modules/samples/src/sample/google/spellcheck/SuggestionForm.java?rev=179755&view=auto
==============================================================================
--- webservices/axis/trunk/java/modules/samples/src/sample/google/spellcheck/SuggestionForm.java (added)
+++ webservices/axis/trunk/java/modules/samples/src/sample/google/spellcheck/SuggestionForm.java Fri Jun 3 02:19:28 2005
@@ -0,0 +1,56 @@
+package sample.google.spellcheck;
+
+import sample.google.spellcheck.AsyncPanel;
+
+import javax.swing.*;
+import java.awt.*;
+import java.awt.event.KeyEvent;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+
+/**
+ * class sample.google.spellcheck.SuggestionForm
+ * This is the implementation of the GUI
+ * @author Nadana Gunarathna
+ *
+ */
+public class SuggestionForm extends javax.swing.JFrame implements ActionListener {
+ AsyncPanel asyncPanel;
+ SyncPanel syncPanel;
+
+ public SuggestionForm() throws HeadlessException {
+ asyncPanel = new AsyncPanel();
+ syncPanel = new SyncPanel();
+ JMenuBar menuBar;
+ //Create the menu bar.
+ menuBar = new JMenuBar();
+ JMenu mainMenu = new JMenu("Select");
+
+ mainMenu.setMnemonic(KeyEvent.VK_A);
+
+ JMenuItem syncMenuItem = new JMenuItem("Sync Call", KeyEvent.VK_T);
+ syncMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1, ActionEvent.ALT_MASK));
+
+ syncMenuItem.addActionListener(this);
+ mainMenu.add(syncMenuItem);
+
+ this.setJMenuBar(menuBar);
+ menuBar.add(mainMenu);
+ //this.getContentPane().add(menuBar);
+
+ //this.getContentPane().add(syncPanel);
+ this.getContentPane().add(asyncPanel);
+ }
+
+ public static void main(String[] args) {
+ SuggestionForm form = new SuggestionForm();
+ form.setSize(800, 800);
+ form.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
+ form.pack();
+ form.show();
+ }
+
+ public void actionPerformed(ActionEvent e) {
+ //To change body of implemented methods use File | Settings | File Templates.
+ }
+}
Added: webservices/axis/trunk/java/modules/samples/src/sample/google/spellcheck/SyncPanel.java
URL: http://svn.apache.org/viewcvs/webservices/axis/trunk/java/modules/samples/src/sample/google/spellcheck/SyncPanel.java?rev=179755&view=auto
==============================================================================
--- webservices/axis/trunk/java/modules/samples/src/sample/google/spellcheck/SyncPanel.java (added)
+++ webservices/axis/trunk/java/modules/samples/src/sample/google/spellcheck/SyncPanel.java Fri Jun 3 02:19:28 2005
@@ -0,0 +1,61 @@
+package sample.google.spellcheck;
+
+import sample.google.spellcheck.FormModel;
+import sample.google.spellcheck.Observer;
+
+import javax.swing.*;
+import java.awt.*;
+import java.awt.event.KeyEvent;
+import java.awt.event.ActionListener;
+import java.awt.event.ActionEvent;
+
+/**
+ * class sample.google.spellcheck.SyncPanel
+ * This Impements its own GUI of the Synchronous Client and it send the SOAP request after the mouse event.
+ * @author Nadana Gunarathna
+ *
+ */
+public class SyncPanel extends javax.swing.JPanel implements Observer, ActionListener{
+ FormModel formModel;
+ javax.swing.JTextField tFieldget;
+ javax.swing.JTextField tFieldset;
+ javax.swing.JButton button;
+ public SyncPanel()
+ {
+ formModel = new FormModel(this);
+ tFieldget = new javax.swing.JTextField(10);
+ tFieldset = new javax.swing.JTextField(10);
+ button=new javax.swing.JButton("Send");
+ JScrollPane scrollPaneget = new JScrollPane(tFieldget);
+ JScrollPane scrollPaneset = new JScrollPane(tFieldset);
+
+ setPreferredSize(new Dimension(150, 150));
+
+ tFieldget.setText("Enter a String");
+
+ this.add(tFieldget,BorderLayout.CENTER );
+ this.add(scrollPaneget,BorderLayout.CENTER);
+ this.add(button);
+ this.add(tFieldset ,BorderLayout.CENTER);
+ this.add(scrollPaneset,BorderLayout.CENTER);
+ //this.setLayout(new GridLayout(0,1));
+ button.addActionListener(this);
+
+ }
+ public void update(String suggestion)
+ {
+ tFieldset.setText(suggestion); // put the suggestion string in the reaponse box along with the misspelt word
+ }
+
+
+ public void actionPerformed(ActionEvent e) {
+ String str=tFieldget.getText().trim();
+ formModel.doSyncSpellingSuggestion(str);
+ //formModel.getResponse();
+ }
+
+
+
+}
+
+