001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
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    /**
029     * @author Brian Wing Shun Chan
030     */
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://babelfish.yahoo.com/translate_txt?");
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    }