com.memetix.mst
Class MicrosoftTranslatorAPI

java.lang.Object
  extended by com.memetix.mst.MicrosoftTranslatorAPI
Direct Known Subclasses:
BreakSentences, Detect, Speak, Translate

public abstract class MicrosoftTranslatorAPI
extends Object

MicrosoftAPI Makes the generic Microsoft Translator API calls. Different service classes then extend this to make the specific service calls. Uses the AJAX Interface V2 - see: http://msdn.microsoft.com/en-us/library/ff512404.aspx

Author:
Jonathan Griggs

Field Summary
protected static String apiKey
           
protected static String ENCODING
           
protected static String PARAM_APP_ID
           
protected static String PARAM_FROM_LANG
           
protected static String PARAM_LANGUAGE_CODES
           
protected static String PARAM_LOCALE
           
protected static String PARAM_SENTENCES_LANGUAGE
           
protected static String PARAM_SPOKEN_LANGUAGE
           
protected static String PARAM_TEXT_ARRAY
           
protected static String PARAM_TEXT_SINGLE
           
protected static String PARAM_TO_LANG
           
 
Constructor Summary
MicrosoftTranslatorAPI()
           
 
Method Summary
protected static String buildStringArrayParam(Object[] values)
           
static String getToken(String clientId, String clientSecret)
          Gets the OAuth access token.
protected static Integer[] retrieveIntArray(URL url)
          Fetches the JSON response, parses the JSON Response, returns the result of the request as an array of integers.
protected static String retrieveString(URL url)
          Fetches the JSON response, parses the JSON Response, returns the result of the request as a String.
protected static String[] retrieveStringArr(URL url)
          Fetches the JSON response, parses the JSON Response as an array of Strings and returns the result of the request as a String Array.
protected static String[] retrieveStringArr(URL url, String jsonProperty)
          Fetches the JSON response, parses the JSON Response as an Array of JSONObjects, retrieves the String value of the specified JSON Property, and returns the result of the request as a String Array.
static void setClientId(String pClientId)
          Sets the Client ID.
static void setClientSecret(String pClientSecret)
          Sets the Client Secret.
static void setContentType(String pKey)
          Sets the API key.
static void setHttpReferrer(String pReferrer)
          Sets the Http Referrer.
static void setKey(String pKey)
          Sets the API key.
protected static void validateServiceState()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ENCODING

protected static final String ENCODING
See Also:
Constant Field Values

apiKey

protected static String apiKey

PARAM_APP_ID

protected static final String PARAM_APP_ID
See Also:
Constant Field Values

PARAM_TO_LANG

protected static final String PARAM_TO_LANG
See Also:
Constant Field Values

PARAM_FROM_LANG

protected static final String PARAM_FROM_LANG
See Also:
Constant Field Values

PARAM_TEXT_SINGLE

protected static final String PARAM_TEXT_SINGLE
See Also:
Constant Field Values

PARAM_TEXT_ARRAY

protected static final String PARAM_TEXT_ARRAY
See Also:
Constant Field Values

PARAM_SPOKEN_LANGUAGE

protected static final String PARAM_SPOKEN_LANGUAGE
See Also:
Constant Field Values

PARAM_SENTENCES_LANGUAGE

protected static final String PARAM_SENTENCES_LANGUAGE
See Also:
Constant Field Values

PARAM_LOCALE

protected static final String PARAM_LOCALE
See Also:
Constant Field Values

PARAM_LANGUAGE_CODES

protected static final String PARAM_LANGUAGE_CODES
See Also:
Constant Field Values
Constructor Detail

MicrosoftTranslatorAPI

public MicrosoftTranslatorAPI()
Method Detail

setKey

public static void setKey(String pKey)
Sets the API key. Note: Should ONLY be used with API Keys generated prior to March 31, 2012. All new applications should obtain a ClientId and Client Secret by following the guide at: http://msdn.microsoft.com/en-us/library/hh454950.aspx

Parameters:
pKey - The API key.

setContentType

public static void setContentType(String pKey)
Sets the API key. Note: Should ONLY be used with API Keys generated prior to March 31, 2012. All new applications should obtain a ClientId and Client Secret by following the guide at: http://msdn.microsoft.com/en-us/library/hh454950.aspx

Parameters:
pKey - The API key.

setClientId

public static void setClientId(String pClientId)
Sets the Client ID. All new applications should obtain a ClientId and Client Secret by following the guide at: http://msdn.microsoft.com/en-us/library/hh454950.aspx

Parameters:
pKey - The Client Id.

setClientSecret

public static void setClientSecret(String pClientSecret)
Sets the Client Secret. All new applications should obtain a ClientId and Client Secret by following the guide at: http://msdn.microsoft.com/en-us/library/hh454950.aspx

Parameters:
pKey - The Client Secret.

setHttpReferrer

public static void setHttpReferrer(String pReferrer)
Sets the Http Referrer.

Parameters:
pReferrer - The HTTP client referrer.

getToken

public static String getToken(String clientId,
                              String clientSecret)
                       throws Exception
Gets the OAuth access token.

Parameters:
clientId - The Client key.
clientSecret - The Client Secret
Throws:
Exception

retrieveString

protected static String retrieveString(URL url)
                                throws Exception
Fetches the JSON response, parses the JSON Response, returns the result of the request as a String.

Parameters:
url - The URL to query for a String response.
Returns:
The translated String.
Throws:
Exception - on error.

retrieveStringArr

protected static String[] retrieveStringArr(URL url,
                                            String jsonProperty)
                                     throws Exception
Fetches the JSON response, parses the JSON Response as an Array of JSONObjects, retrieves the String value of the specified JSON Property, and returns the result of the request as a String Array.

Parameters:
url - The URL to query for a String response.
Returns:
The translated String[].
Throws:
Exception - on error.

retrieveStringArr

protected static String[] retrieveStringArr(URL url)
                                     throws Exception
Fetches the JSON response, parses the JSON Response as an array of Strings and returns the result of the request as a String Array. Overloaded to pass null as the JSON Property (assume only Strings instead of JSONObjects)

Parameters:
url - The URL to query for a String response.
Returns:
The translated String[].
Throws:
Exception - on error.

retrieveIntArray

protected static Integer[] retrieveIntArray(URL url)
                                     throws Exception
Fetches the JSON response, parses the JSON Response, returns the result of the request as an array of integers.

Parameters:
url - The URL to query for a String response.
Returns:
The translated String.
Throws:
Exception - on error.

validateServiceState

protected static void validateServiceState()
                                    throws Exception
Throws:
Exception

buildStringArrayParam

protected static String buildStringArrayParam(Object[] values)


Copyright © 2012. All Rights Reserved.