Permalink
Browse files

Added support for version 2 of the translate API.

  • Loading branch information...
richmidwinter committed Nov 5, 2011
1 parent 7e943f2 commit 7ab4f8371882379cb77d24b16f11666314cf0afe
View
@@ -1,9 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java"/>
- <classpathentry kind="src" path="src/main/test"/>
- <classpathentry kind="src" path="src/main/resources"/>
- <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/>
+ <classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources"/>
+ <classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
+ <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"/>
<classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="output" path="target/classes"/>
View
@@ -1,2 +1,3 @@
+*.sh
.settings/
target/
View
@@ -15,8 +15,14 @@
<arguments>
</arguments>
</buildCommand>
+ <buildCommand>
+ <name>org.eclipse.m2e.core.maven2Builder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
</buildSpec>
<natures>
+ <nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.maven.ide.eclipse.maven2Nature</nature>
</natures>
View
52 pom.xml
@@ -3,25 +3,49 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.google.api</groupId>
<artifactId>google-api-translate-java</artifactId>
- <version>0.95-SNAPSHOT</version>
+ <version>0.96-SNAPSHOT</version>
<name>google-api-translate-java</name>
<description>A Java wrapper for the Google Translate service</description>
+ <packaging>jar</packaging>
+
+ <properties>
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+ </properties>
+
+ <distributionManagement>
+ <repository>
+ <id>repo</id>
+ <url>https://github.com/richmidwinter/maven/raw/master/releases</url>
+ </repository>
+ <snapshotRepository>
+ <id>snapshot-repo</id>
+ <url>https://github.com/richmidwinter/maven/raw/master/snapshots</url>
+ </snapshotRepository>
+ </distributionManagement>
+
<dependencies>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20090211</version>
</dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>4.10</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
+
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
- <source>1.5</source>
- <target>1.5</target>
+ <source>1.6</source>
+ <target>1.6</target>
</configuration>
</plugin>
<plugin>
@@ -37,6 +61,28 @@
</archive>
</configuration>
</plugin>
+ <plugin>
+ <artifactId>maven-assembly-plugin</artifactId>
+ <configuration>
+ <descriptorRefs>
+ <descriptorRef>jar-with-dependencies</descriptorRef>
+ </descriptorRefs>
+ <archive>
+ <manifest>
+ <mainClass>com.google.api.translate.TranslatorFrame</mainClass>
+ </manifest>
+ </archive>
+ </configuration>
+ <executions>
+ <execution>
+ <id>make-assembly</id>
+ <phase>package</phase>
+ <goals>
+ <goal>assembly</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
</plugins>
</build>
</project>
@@ -36,9 +36,12 @@
* @author Kramar Tomas
*/
public abstract class GoogleAPI {
-
+
+ /**
+ * Default encoding to use.
+ */
protected static final String ENCODING = "UTF-8";
-
+
/**
* The HTTP referrer and API key attribute.
*
@@ -0,0 +1,23 @@
+/**
+ *
+ */
+package com.google.api;
+
+/**
+ * @author Richard Midwinter
+ */
+public class GoogleAPIException extends Exception {
+
+ /**
+ * Serial version UID.
+ */
+ private static final long serialVersionUID = 1904924954995479356L;
+
+ public GoogleAPIException(final String message) {
+ super(message);
+ }
+
+ public GoogleAPIException(final Exception e) {
+ super(e);
+ }
+}
@@ -20,35 +20,17 @@
*/
package com.google.api.translate;
-import java.net.URL;
-import java.net.URLEncoder;
-
-import org.json.JSONArray;
-import org.json.JSONObject;
-
-import com.google.api.GoogleAPI;
-import com.tecnick.htmlutils.htmlentities.HTMLEntities;
+import com.google.api.GoogleAPIException;
/**
- * Makes the Google Translate API available to Java applications.
- *
* @author Richard Midwinter
- * @author Mike Nereson
- * @author Emeric Vernat
- * @author Juan B Cabral
- * @author Kramar Tomas
*/
-public final class Translate extends GoogleAPI {
+public interface Translate {
/**
- * Constants.
+ * Default instance of the Translate API.
*/
- private static final String
- LANG_PARAM = "&langpair=",
- TEXT_PARAM = "&q=",
- PIPE_PARAM = "%7C",
- URL = "http://ajax.googleapis.com/ajax/services/language/translate",
- PARAMETERS = "v=2.0&langpair=#FROM#%7C#TO#&q=";
+ Translate DEFAULT = new TranslateV2();
/**
* Translates text from a given Language to another given Language using Google Translate.
@@ -57,138 +39,40 @@
* @param from The language code to translate from.
* @param to The language code to translate to.
* @return The translated String.
- * @throws Exception on error.
+ * @throws GoogleAPIException on error.
*/
- public static String execute(final String text, final Language from, final Language to) throws Exception {
- validateReferrer();
-
- final URL url = new URL(URL);
- final String parameters = PARAMETERS.replaceAll("#FROM#", from.toString()).replaceAll("#TO#", to.toString())
- +URLEncoder.encode(text, ENCODING) +(key != null ? "&key=" +key : "");
-
- final JSONObject json = retrieveJSON(url, parameters);
-
- return getJSONResponse(json);
- }
-
+ String execute(String text, Language from, Language to) throws GoogleAPIException;
+
/**
* Translates an array of text Strings from a given Language to another given Language using Google Translate.
*
* @param text The array of Strings to translate.
* @param from The language code to translate from.
* @param to The language code to translate to.
* @return The translated array of String results.
- * @throws Exception on error.
+ * @throws GoogleAPIException on error.
*/
- public static String[] execute(final String[] text, final Language from, final Language to) throws Exception {
- validateReferrer();
-
- final Language[] fromArgs = new Language[text.length];
- final Language[] toArgs = new Language[text.length];
-
- for (int i = 0; i<text.length; i++) {
- fromArgs[i] = from;
- toArgs[i] = to;
- }
-
- return execute(text, fromArgs, toArgs);
- }
-
+ String[] execute(String[] text, Language from, Language to) throws GoogleAPIException;
+
/**
* Translates a String from a given Language to an Array of Languages using Google Translate.
*
* @param text The String to translate.
* @param from The language code to translate from.
* @param to The array of Languages to translate to.
* @return The translated array of String results.
- * @throws Exception on error.
+ * @throws GoogleAPIException on error.
*/
- public static String[] execute(final String text, final Language from, final Language[] to) throws Exception {
- validateReferrer();
-
- final String[] textArgs = new String[to.length];
- final Language[] fromArgs = new Language[to.length];
-
- for (int i = 0; i<to.length; i++) {
- textArgs[i] = text;
- fromArgs[i] = from;
- }
-
- return execute(textArgs, fromArgs, to);
- }
-
+ String[] execute(String text, Language from, Language[] to) throws GoogleAPIException;
+
/**
* Translates text from a given Language to another given Language using Google Translate.
*
* @param text The array of Strings to translate.
* @param from The array of Language codes to translate from.
* @param to The array of Language codes to translate to.
* @return The translated array of String results.
- * @throws Exception on error.
- */
- public static String[] execute(final String[] text, final Language from[], final Language[] to) throws Exception {
- validateReferrer();
-
- if (text.length != from.length || from.length != to.length) {
- throw new Exception(
- "[google-api-translate-java] The same number of texts, from and to languages must be supplied.");
- }
-
- if (text.length == 1) {
- return new String[] { execute(text[0], from[0], to[0]) };
- }
-
- final String[] responses = new String[text.length];
-
- final StringBuilder parametersBuilder = new StringBuilder();
-
- parametersBuilder.append(PARAMETERS.replaceAll("#FROM#", from[0].toString()).replaceAll("#TO#", to[0].toString()) +(key != null ? "&key=" +key : ""));
- parametersBuilder.append(URLEncoder.encode(text[0], ENCODING));
-
- for (int i = 1; i<text.length; i++) {
- parametersBuilder.append(LANG_PARAM);
- parametersBuilder.append(from[i].toString());
- parametersBuilder.append(PIPE_PARAM);
- parametersBuilder.append(to[i].toString());
- parametersBuilder.append(TEXT_PARAM);
- parametersBuilder.append(URLEncoder.encode(text[i].toString(), ENCODING));
- }
-
- final URL url = new URL(URL);
-
- final JSONArray json = retrieveJSON(url, parametersBuilder.toString()).getJSONArray("responseData");
-
- for (int i = 0; i<json.length(); i++) {
- final JSONObject obj = json.getJSONObject(i);
-
- responses[i] = getJSONResponse(obj);
- }
-
- return responses;
- }
-
- /**
- * Returns the JSON response data as a String. Throws an exception if the status is not a 200 OK.
- *
- * @param json The JSON object to retrieve the response data from.
- * @return The responseData from the JSONObject.
- * @throws Exception If the responseStatus is not 200 OK.
- */
- private static String getJSONResponse(final JSONObject json) throws Exception {
- if (json.getString("responseStatus").equals("200")) {
- final String translatedText = json.getJSONObject("responseData").getString("translatedText");
- return HTMLEntities.unhtmlentities(translatedText);
- } else {
- throw new Exception("Google returned the following error: [" +json.getString("responseStatus") +"] "
- +json.getString("responseDetails"));
- }
- }
-
- /**
- * @deprecated Replaced by {@link execute()}.
+ * @throws GoogleAPIException on error.
*/
- @Deprecated
- public static String translate(final String text, final Language from, final Language to) throws Exception {
- return execute(text, from, to);
- }
-}
+ String[] execute(String[] text, Language from[], Language[] to) throws GoogleAPIException;
+}
Oops, something went wrong.

0 comments on commit 7ab4f83

Please sign in to comment.