1   /**
2    * Copyright (c) 2000-2007 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.util.WebCacheable;
26  import com.liferay.portlet.news.model.Article;
27  import com.liferay.portlet.news.model.News;
28  import com.liferay.util.ConverterException;
29  import com.liferay.util.Http;
30  import com.liferay.util.Time;
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="NewsConverter.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Brian Wing Shun Chan
49   *
50   */
51  public class NewsConverter implements WebCacheable {
52  
53      public Object convert(String text) throws ConverterException {
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 = Http.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              ArrayList list = new ArrayList();
68  
69              DateFormat df = new SimpleDateFormat("MMM d yyyy K:mma z");
70  
71              Element root = doc.getRootElement();
72  
73              List articles = root.elements("article");
74              Iterator i = articles.iterator();
75  
76              while (i.hasNext()) {
77                  Element article = (Element)i.next();
78  
79                  String harvestTime =
80                      article.element("harvest_time").getTextTrim() + " GMT";
81  
82                  list.add(new Article(
83                      article.element("headline_text").getTextTrim(),
84                      article.element("url").getTextTrim(),
85                      article.element("source").getTextTrim(),
86                      article.element("document_url").getTextTrim(),
87                      article.element("access_status").getTextTrim(),
88                      article.element("access_registration").getTextTrim(),
89                      df.parse(harvestTime)));
90              }
91  
92              return new News(feedURL, categoryName, list);
93          }
94          catch (Exception e) {
95              throw new ConverterException(e);
96          }
97      }
98  
99      public long getRefreshTime() {
100         return _REFRESH_TIME;
101     }
102 
103     private static final long _REFRESH_TIME = Time.MINUTE * 20;
104 
105 }