01: package com.xoetrope.carousel.langed;
02:
03: import java.io.BufferedReader;
04: import java.io.InputStream;
05: import java.io.InputStreamReader;
06: import java.io.OutputStream;
07: import java.net.URL;
08: import java.net.URLConnection;
09: import java.net.URLEncoder;
10:
11: /**
12: * <p>Language Resource Translation Utility</p>
13: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
14: * the GNU Public License (GPL), please see license.txt for more details. If
15: * you make commercial use of this software you must purchase a commercial
16: * license from Xoetrope.</p>
17: * <p> $Revision: 1.4 $</p>
18: */
19: public class AutoTranslate {
20: public static String translate(String txUrl,
21: String directionPrefix, String direction, String srcPrefix,
22: String srcLangString, String startOfResultTag,
23: String endOfResultTag) throws Exception {
24: try {
25: URLConnection conn;
26: URL url = new URL(txUrl);
27: String parms = directionPrefix
28: + URLEncoder.encode(direction, "UTF-8");
29: parms += srcPrefix
30: + URLEncoder.encode(srcLangString, "UTF-8");
31:
32: conn = url.openConnection();
33: conn.setDefaultUseCaches(false);
34: conn.setDoOutput(true);
35: conn.setIfModifiedSince(0);
36:
37: OutputStream o = conn.getOutputStream();
38: o.write(parms.getBytes());
39: o.flush();
40:
41: conn.connect();
42: InputStream is = conn.getInputStream();
43: InputStreamReader isr = new InputStreamReader(is);
44: BufferedReader br = new BufferedReader(isr);
45: char[] c = new char[4096];
46:
47: int i = 1;
48: String result = new String("");
49: while (i != -1) {
50: c[0] = c[1] = '\0';
51: i = br.read(c, 0, 4096);
52: if (i != -1)
53: result += new String(c, 0, i);
54: }
55: String search = startOfResultTag;
56: String lowerCaseResult = result.toLowerCase();
57: int start = lowerCaseResult.indexOf(search.toLowerCase());
58: if (start >= 0) {
59: int end = lowerCaseResult.indexOf(endOfResultTag
60: .toLowerCase(), start + 1);
61: //System.out.println( result.substring( start + search.length(), end ));
62: return result.substring(start + search.length(), end)
63: .trim();
64: }
65: return "";
66: } catch (Exception e) {
67: e.printStackTrace();
68: throw new Exception();
69: }
70: }
71: }
|