net.spy.translate
Class GoogleTranslateScraper

java.lang.Object
  extended by net.spy.translate.GoogleTranslateScraper

public class GoogleTranslateScraper
extends java.lang.Object

This class is the translation driver for using the Google Translate Scraper. typical usage might look like this :

GoogleTranslateScraper spanishTranslator = new GoogleTranslateScraper();
spanishTranslator.setTranslateMode(TranslateMode.SPANISH_TO_ENGLISH);

String spanishText = "gracias";
String englishText = spanishTranslator.doTranslate(spanishText);

Author:
- Jason Hudgins (jasonlee@spy.net)

Constructor Summary
GoogleTranslateScraper()
          Simple no argument constructor.
GoogleTranslateScraper(TranslateMode mode)
          Overloaded constructor that takes a translation mode as an argument.
 
Method Summary
protected  org.apache.commons.httpclient.methods.PostMethod composePost(java.lang.String sourceString)
          This method is used for composing the PostMethod needed for submitting the data to the google translate website.
 java.lang.String doTranslate(java.lang.String sourceString)
          This method represents the soul of the translate scraper.
protected  java.lang.String parseResults(java.io.InputStream inputStream)
          This method sets up the tagsoup SAX compliant parser, the result handler, parses the results and tries to find the result string.
 void setTranslateMode(TranslateMode newMode)
          Set/Change the translation mode.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

GoogleTranslateScraper

public GoogleTranslateScraper()
Simple no argument constructor.


GoogleTranslateScraper

public GoogleTranslateScraper(TranslateMode mode)
Overloaded constructor that takes a translation mode as an argument. The translation mode is how you specify what type of operation you want. Chinese to English, for example

Parameters:
mode - The translation mode to use.
See Also:
TranslateMode
Method Detail

setTranslateMode

public void setTranslateMode(TranslateMode newMode)
Set/Change the translation mode. The legal options are in the enum net.spy.translate.TranslateMode

Parameters:
newMode - The translation mode to use.
See Also:
TranslateMode

composePost

protected org.apache.commons.httpclient.methods.PostMethod composePost(java.lang.String sourceString)
This method is used for composing the PostMethod needed for submitting the data to the google translate website.

Parameters:
sourceString - The text to be translated.
Returns:
post the initialized PostMethod, ready to be executed.
See Also:
PostMethod

doTranslate

public java.lang.String doTranslate(java.lang.String sourceString)
                             throws TranslationException
This method represents the soul of the translate scraper. You send it a string in some language, and it will return to you the string translated to a different language. Make sure you set the translation mode first, either in the constructor or with the setter method. If you do not set the translation mode, it will default to German to English.

Parameters:
sourceString - the text to be translated.
Throws:
TranslationException

parseResults

protected java.lang.String parseResults(java.io.InputStream inputStream)
                                 throws org.xml.sax.SAXException,
                                        java.io.IOException
This method sets up the tagsoup SAX compliant parser, the result handler, parses the results and tries to find the result string. It's all very SAXy stuff, and hopefully you won't have to know anything about it to use this lib.

Parameters:
inputStream - The stream of html tags to be parsed.
Returns:
result string The translated text.
Throws:
org.xml.sax.SAXException - if a SAX parsing exception occured.
java.io.IOException


Copyright © 2008. All Rights Reserved.