1 | import java.io.BufferedReader; |
---|
2 | import java.io.BufferedWriter; |
---|
3 | import java.io.FileNotFoundException; |
---|
4 | import java.io.FileReader; |
---|
5 | import java.io.FileWriter; |
---|
6 | import java.io.IOException; |
---|
7 | import java.io.InputStream; |
---|
8 | import java.io.InputStreamReader; |
---|
9 | import java.io.Reader; |
---|
10 | import java.util.regex.Matcher; |
---|
11 | import java.util.regex.Pattern; |
---|
12 | |
---|
13 | import org.apache.commons.httpclient.HttpClient; |
---|
14 | import org.apache.commons.httpclient.HttpException; |
---|
15 | import org.apache.commons.httpclient.NameValuePair; |
---|
16 | import org.apache.commons.httpclient.methods.PostMethod; |
---|
17 | |
---|
18 | |
---|
19 | public class GetTranslation { |
---|
20 | |
---|
21 | private static String translate(String langpair,String text) { |
---|
22 | |
---|
23 | /********************************************************** |
---|
24 | * 要這一行動起來, 你要有一些相依的library才行 |
---|
25 | * commons-logging |
---|
26 | * commons-codec |
---|
27 | */ |
---|
28 | HttpClient client = new HttpClient(); |
---|
29 | |
---|
30 | |
---|
31 | /*********************************************************** |
---|
32 | * 設定連線的站臺位置 |
---|
33 | * 請別在"站臺位置"加了多餘的protocol |
---|
34 | * ex. setHost("http://tinyurl.com", 80, "http"); |
---|
35 | * 不然他就噴UnknownHostException給你:P |
---|
36 | */ |
---|
37 | client.getHostConfiguration().setHost("www.google.com", 80, "http"); |
---|
38 | |
---|
39 | /*********************************************************** |
---|
40 | * 觀察了表單內容是用post方法, 而使用的script name為create.php |
---|
41 | */ |
---|
42 | PostMethod post = new PostMethod("/translate_t"); |
---|
43 | //post.addParameter(new NameValuePair("url", url)); |
---|
44 | post.addParameter("langpair", langpair); |
---|
45 | post.addParameter("text", text); |
---|
46 | |
---|
47 | String s = null; |
---|
48 | try { |
---|
49 | /*********************************************************** |
---|
50 | * 透過http client來執行post/get方法 |
---|
51 | */ |
---|
52 | client.executeMethod(post); |
---|
53 | |
---|
54 | /*********************************************************** |
---|
55 | * 再用您選用的方法傳回response body |
---|
56 | * 這裡偷懶了一下使用了getResponseBodyAsString() |
---|
57 | * log上會發出警告 |
---|
58 | * |
---|
59 | * 2006/5/18 上午 08:13:22 org.apache.commons.httpclient.HttpMethodBase getResponseBody |
---|
60 | * 警告: Going to buffer response body of large or unknown size. |
---|
61 | * Using getResponseBodyAsStream instead is recommended. |
---|
62 | */ |
---|
63 | InputStream in = post.getResponseBodyAsStream(); |
---|
64 | BufferedReader buf = new BufferedReader(new InputStreamReader(in)); |
---|
65 | Pattern p = Pattern.compile(".*id=\"*result_box\"*\\ *dir=\"*ltr\"*\\>"); |
---|
66 | Matcher m = null; |
---|
67 | |
---|
68 | String line; |
---|
69 | while ((line = buf.readLine()) != null) { |
---|
70 | m = p.matcher(line); |
---|
71 | //System.out.println(line); |
---|
72 | if (m.find()) { |
---|
73 | s = line.substring(m.end()); |
---|
74 | line = s; |
---|
75 | p = Pattern.compile("\\<\\ */\\ *div\\ *\\>"); |
---|
76 | m = p.matcher(line); |
---|
77 | if (m.find()) { |
---|
78 | s = line.substring(0, m.start()); |
---|
79 | } |
---|
80 | break; |
---|
81 | } |
---|
82 | } |
---|
83 | |
---|
84 | //s = GetTranslation.instostr(in); |
---|
85 | } catch (HttpException e) { |
---|
86 | e.printStackTrace(); |
---|
87 | } catch (IOException e) { |
---|
88 | e.printStackTrace(); |
---|
89 | } |
---|
90 | return s; |
---|
91 | } |
---|
92 | |
---|
93 | public static String instostr (InputStream in) throws IOException { |
---|
94 | StringBuffer out = new StringBuffer(); |
---|
95 | byte[] b = new byte[4096]; |
---|
96 | for (int n; (n = in.read(b)) != -1;) { |
---|
97 | out.append(new String(b, 0, n)); |
---|
98 | } |
---|
99 | return out.toString(); |
---|
100 | } |
---|
101 | |
---|
102 | /** @param filePath the name of the file to open. Not sure if it can accept URLs or just filenames. Path handling could be better, and buffer sizes are hardcoded |
---|
103 | */ |
---|
104 | public static void translateFile(String langpair, String inPath,String outPath) { |
---|
105 | BufferedReader reader=null; |
---|
106 | BufferedWriter writer=null; |
---|
107 | try { |
---|
108 | reader = new BufferedReader(new FileReader(inPath)); |
---|
109 | writer = new BufferedWriter(new FileWriter(outPath)); |
---|
110 | } catch (FileNotFoundException e) { |
---|
111 | e.printStackTrace(); |
---|
112 | } catch (IOException e) { |
---|
113 | e.printStackTrace(); |
---|
114 | } |
---|
115 | Pattern p = Pattern.compile("="); |
---|
116 | Matcher m = null; |
---|
117 | int count=0; |
---|
118 | String var=null, val=null; |
---|
119 | String line=null, lines="", ret=""; |
---|
120 | try { |
---|
121 | while ((line=reader.readLine())!=null) { |
---|
122 | |
---|
123 | if (line.contains("=")) { |
---|
124 | |
---|
125 | m = p.matcher(line); |
---|
126 | if (m.find()) { |
---|
127 | count++; |
---|
128 | var = line.substring(0,m.start()); |
---|
129 | val = line.substring(m.end()+1,line.length()-1); |
---|
130 | lines+=var+"="+val+"\n"; |
---|
131 | //System.out.println(var+"='"+GetTranslation.translate(langpair,val)+"'"); |
---|
132 | |
---|
133 | } |
---|
134 | } else { |
---|
135 | writer.write(line); |
---|
136 | writer.newLine(); |
---|
137 | } |
---|
138 | if (count==200) { |
---|
139 | ret = GetTranslation.translate(langpair,lines); |
---|
140 | ret = ret.replace("=", "=\""); |
---|
141 | ret = ret.replace("<br>", "\"\n"); |
---|
142 | System.out.println(ret); |
---|
143 | writer.write(ret); |
---|
144 | lines=""; |
---|
145 | count=0; |
---|
146 | Thread.sleep(5000); |
---|
147 | } |
---|
148 | } |
---|
149 | if (count>0) { |
---|
150 | ret=lines; |
---|
151 | ret = GetTranslation.translate(langpair,lines); |
---|
152 | ret = ret.replace("=", "=\""); |
---|
153 | ret = ret.replace("<br>", "\"\n"); |
---|
154 | System.out.println(ret); |
---|
155 | writer.write(ret); |
---|
156 | lines=""; |
---|
157 | Thread.sleep(5000); |
---|
158 | } |
---|
159 | reader.close(); |
---|
160 | writer.close(); |
---|
161 | } catch (IOException e) { |
---|
162 | e.printStackTrace(); |
---|
163 | } catch (InterruptedException e) { |
---|
164 | e.printStackTrace(); |
---|
165 | } |
---|
166 | |
---|
167 | } |
---|
168 | |
---|
169 | public static void main(String[] args) throws HttpException, IOException { |
---|
170 | |
---|
171 | if(null == args || args.length < 3) { |
---|
172 | System.out.println("Usage: java GetTranslation language_pair original_file translated_file"); |
---|
173 | System.out.println("Example: java GetTranslation \"en|zh-TW\" \"en_US\" \"zh_TW\""); |
---|
174 | //String ret = GetTranslation.translate("en|zh-TW","hello"); |
---|
175 | //System.out.println("Return Value: "+ret); |
---|
176 | System.exit(1); |
---|
177 | } |
---|
178 | //System.out.println(GetTranslation.translate("en|zh-TW","hello")); |
---|
179 | //GetTranslation.translateFile("en|zh-TW","en_US","zh_TW"); |
---|
180 | GetTranslation.translateFile(args[0],args[1],args[2]); |
---|
181 | |
---|
182 | //GetTranslation.translateFile("en|zh-TW","en_US","zh_TW"); |
---|
183 | //String ret = GetTranslation.translate("en|zh-TW","hello"); |
---|
184 | //System.out.println("Return Value: "+ret); |
---|
185 | |
---|
186 | } |
---|
187 | } |
---|