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