001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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    /**
028     * @author Brian Wing Shun Chan
029     */
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://babelfish.yahoo.com/translate_txt?");
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    }