001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.news.util;
022:
023: import com.liferay.portal.kernel.webcache.WebCacheException;
024: import com.liferay.portal.kernel.webcache.WebCacheItem;
025: import com.liferay.portlet.news.model.Article;
026: import com.liferay.portlet.news.model.News;
027: import com.liferay.util.Http;
028: import com.liferay.util.Time;
029:
030: import java.io.ByteArrayInputStream;
031:
032: import java.text.DateFormat;
033: import java.text.SimpleDateFormat;
034:
035: import java.util.ArrayList;
036: import java.util.Iterator;
037: import java.util.List;
038:
039: import org.dom4j.Document;
040: import org.dom4j.Element;
041: import org.dom4j.io.SAXReader;
042:
043: /**
044: * <a href="NewsWebCacheItem.java.html"><b><i>View Source</i></b></a>
045: *
046: * @author Brian Wing Shun Chan
047: *
048: */
049: public class NewsWebCacheItem implements WebCacheItem {
050:
051: public Object convert(String text) throws WebCacheException {
052: try {
053: int pos = text.indexOf(";");
054:
055: String categoryName = text.substring(0, pos);
056: String feedURL = text.substring(pos + 1, text.length());
057: String xml = Http
058: .URLtoString("http://p.moreover.com/cgi-local/page?"
059: + feedURL + "&o=xml");
060:
061: ByteArrayInputStream bais = new ByteArrayInputStream(xml
062: .getBytes());
063:
064: Document doc = new SAXReader().read(bais);
065:
066: ArrayList list = new ArrayList();
067:
068: DateFormat df = new SimpleDateFormat("MMM d yyyy K:mma z");
069:
070: Element root = doc.getRootElement();
071:
072: List articles = root.elements("article");
073: Iterator i = articles.iterator();
074:
075: while (i.hasNext()) {
076: Element article = (Element) i.next();
077:
078: String harvestTime = article.element("harvest_time")
079: .getTextTrim()
080: + " GMT";
081:
082: list.add(new Article(article.element("headline_text")
083: .getTextTrim(), article.element("url")
084: .getTextTrim(), article.element("source")
085: .getTextTrim(), article.element("document_url")
086: .getTextTrim(), article
087: .element("access_status").getTextTrim(),
088: article.element("access_registration")
089: .getTextTrim(), df.parse(harvestTime)));
090: }
091:
092: return new News(feedURL, categoryName, list);
093: } catch (Exception e) {
094: throw new WebCacheException(e);
095: }
096: }
097:
098: public long getRefreshTime() {
099: return _REFRESH_TIME;
100: }
101:
102: private static final long _REFRESH_TIME = Time.MINUTE * 20;
103:
104: }
|