1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.news.util;
24  
25  import com.liferay.portal.kernel.util.HttpUtil;
26  import com.liferay.portal.kernel.util.Time;
27  import com.liferay.portal.kernel.webcache.WebCacheException;
28  import com.liferay.portal.kernel.webcache.WebCacheItem;
29  import com.liferay.portlet.news.model.Article;
30  import com.liferay.portlet.news.model.News;
31  
32  import java.io.ByteArrayInputStream;
33  
34  import java.text.DateFormat;
35  import java.text.SimpleDateFormat;
36  
37  import java.util.ArrayList;
38  import java.util.Iterator;
39  import java.util.List;
40  
41  import org.dom4j.Document;
42  import org.dom4j.Element;
43  import org.dom4j.io.SAXReader;
44  
45  /**
46   * <a href="NewsWebCacheItem.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Brian Wing Shun Chan
49   *
50   */
51  public class NewsWebCacheItem implements WebCacheItem {
52  
53      public Object convert(String text) throws WebCacheException {
54          try {
55              int pos = text.indexOf(";");
56  
57              String categoryName = text.substring(0, pos);
58              String feedURL = text.substring(pos + 1, text.length());
59              String xml = HttpUtil.URLtoString(
60                  "http://p.moreover.com/cgi-local/page?" + feedURL + "&o=xml");
61  
62              ByteArrayInputStream bais =
63                  new ByteArrayInputStream(xml.getBytes());
64  
65              Document doc = new SAXReader().read(bais);
66  
67              List<Article> articles = new ArrayList<Article>();
68  
69              DateFormat df = new SimpleDateFormat("MMM d yyyy K:mma z");
70  
71              Element root = doc.getRootElement();
72  
73              Iterator<Element> itr = root.elements("article").iterator();
74  
75              while (itr.hasNext()) {
76                  Element articleEl = itr.next();
77  
78                  String harvestTime =
79                      articleEl.element("harvest_time").getTextTrim() + " GMT";
80  
81                  Article article = new Article(
82                      articleEl.element("headline_text").getTextTrim(),
83                      articleEl.element("url").getTextTrim(),
84                      articleEl.element("source").getTextTrim(),
85                      articleEl.element("document_url").getTextTrim(),
86                      articleEl.element("access_status").getTextTrim(),
87                      articleEl.element("access_registration").getTextTrim(),
88                      df.parse(harvestTime));
89  
90                  articles.add(article);
91              }
92  
93              return new News(feedURL, categoryName, articles);
94          }
95          catch (Exception e) {
96              throw new WebCacheException(e);
97          }
98      }
99  
100     public long getRefreshTime() {
101         return _REFRESH_TIME;
102     }
103 
104     private static final long _REFRESH_TIME = Time.MINUTE * 20;
105 
106 }