1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.translator.util;
24  
25  import com.liferay.portal.kernel.util.HttpUtil;
26  import com.liferay.portal.kernel.util.StringUtil;
27  import com.liferay.portal.kernel.util.Time;
28  import com.liferay.portal.kernel.webcache.WebCacheException;
29  import com.liferay.portal.kernel.webcache.WebCacheItem;
30  import com.liferay.portlet.translator.model.Translation;
31  
32  import java.net.URL;
33  
34  /**
35   * <a href="TranslationWebCacheItem.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Brian Wing Shun Chan
38   */
39  public class TranslationWebCacheItem implements WebCacheItem {
40  
41      public TranslationWebCacheItem(String translationId, String fromText) {
42          _translationId = translationId;
43          _fromText = fromText;
44      }
45  
46      public Object convert(String key) throws WebCacheException {
47          Translation translation = new Translation(_translationId, _fromText);
48  
49          try {
50              StringBuilder url = new StringBuilder();
51  
52              url.append("http://babelfish.yahoo.com/translate_txt?");
53              url.append("ei=UTF-8&doit=done&fr=bf-res&intl=1&tt=urltext");
54              url.append("&trtext=").append(HttpUtil.encodeURL(_fromText));
55              url.append("&lp=").append(_translationId);
56  
57              String text = HttpUtil.URLtoString(new URL(url.toString()));
58  
59              int x = text.indexOf("<div id=\"result\">");
60  
61              x = text.indexOf(">", x) + 1;
62              x = text.indexOf(">", x) + 1;
63  
64              int y = text.indexOf("</div>", x);
65  
66              String toText = text.substring(x, y).trim();
67  
68              toText = StringUtil.replace(toText, "\n", " ");
69  
70              translation.setToText(toText);
71          }
72          catch (Exception e) {
73              throw new WebCacheException(e);
74          }
75  
76          return translation;
77      }
78  
79      public long getRefreshTime() {
80          return _REFRESH_TIME;
81      }
82  
83      private static final long _REFRESH_TIME = Time.DAY * 90;
84  
85      private String _translationId;
86      private String _fromText;
87  
88  }