001
014
015 package com.liferay.portlet.translator.util;
016
017 import com.liferay.portal.kernel.util.HttpUtil;
018 import com.liferay.portal.kernel.util.StringBundler;
019 import com.liferay.portal.kernel.util.StringUtil;
020 import com.liferay.portal.kernel.util.Time;
021 import com.liferay.portal.kernel.webcache.WebCacheException;
022 import com.liferay.portal.kernel.webcache.WebCacheItem;
023 import com.liferay.portlet.translator.model.Translation;
024
025 import java.net.URL;
026
027
030 public class TranslationWebCacheItem implements WebCacheItem {
031
032 public TranslationWebCacheItem(String translationId, String fromText) {
033 _translationId = translationId;
034 _fromText = fromText;
035 }
036
037 public Object convert(String key) throws WebCacheException {
038 Translation translation = new Translation(_translationId, _fromText);
039
040 try {
041 StringBundler sb = new StringBundler(6);
042
043 sb.append("http:
044 sb.append("ei=UTF-8&doit=done&fr=bf-res&intl=1&tt=urltext");
045 sb.append("&trtext=");
046 sb.append(HttpUtil.encodeURL(_fromText));
047 sb.append("&lp=");
048 sb.append(_translationId);
049
050 String text = HttpUtil.URLtoString(new URL(sb.toString()));
051
052 int x = text.indexOf("<div id=\"result\">");
053
054 x = text.indexOf(">", x) + 1;
055 x = text.indexOf(">", x) + 1;
056
057 int y = text.indexOf("</div>", x);
058
059 String toText = text.substring(x, y).trim();
060
061 toText = StringUtil.replace(toText, "\n", " ");
062
063 translation.setToText(toText);
064 }
065 catch (Exception e) {
066 throw new WebCacheException(e);
067 }
068
069 return translation;
070 }
071
072 public long getRefreshTime() {
073 return _REFRESH_TIME;
074 }
075
076 private static final long _REFRESH_TIME = Time.DAY * 90;
077
078 private String _translationId;
079 private String _fromText;
080
081 }