-
Notifications
You must be signed in to change notification settings - Fork 210
/
AbstractTranslator.java
72 lines (61 loc) · 1.86 KB
/
AbstractTranslator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package com.swjtu.trans;
import com.swjtu.http.AbstractHttpAttribute;
import com.swjtu.http.HttpParams;
import com.swjtu.lang.LANG;
import com.swjtu.trans.impl.*;
import java.io.IOException;
/**
* AbstractTranslator is an abstract base class for all translators
* which includes several (abstract) functions. By setting parameters,
* the request is sent to the target server, and then parse the return
* result to achieve the the purpose of translation.
*
* @see BaiduTranslator
* @see GoogleTranslator
* @see YoudaoTranslator
* @see IcibaTranslator
* @see OmiTranslator
* @see SogouTranslator
* @see TencentTranslator
* @see TrycanTranslator
*/
public abstract class AbstractTranslator extends AbstractHttpAttribute implements HttpParams {
public AbstractTranslator(String url) {
super(url);
setLangSupport();
}
@Override
public String run(LANG source, String text) {
return null;
}
@Override
public String run(LANG from, LANG to, String text) {
String result = "";
setFormData(from, to, text);
try {
result = parses(query());
} catch (Exception e) {
e.printStackTrace();
}
close();
return result;
}
/**
* Initialize the supported language mapping.
*/
public abstract void setLangSupport();
@Override
public void setFormData(LANG source, String text){}
@Override
public abstract void setFormData(LANG from, LANG to, String text);
@Override
public abstract String query() throws Exception;
/**
* Parse the string to extract the content of interest.
*
* @param text the string form of the translated result.
* @return translation results after parsing.
* @throws IOException if the parsing fails.
*/
public abstract String parses(String text) throws IOException;
}