1   /**
2    * Copyright (c) 2000-2004 Liferay, LLC. 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   * @version $Revision: 1.9 $
50   *
51   */
52  public class NewsConverter implements WebCacheable {
53  
54      public Object convert(String text) throws ConverterException {
55          try {
56              int pos = text.indexOf(";");
57  
58              String categoryName = text.substring(0, pos);
59              String feedURL = text.substring(pos + 1, text.length());
60              String xml = Http.URLtoString(
61                  "http://p.moreover.com/cgi-local/page?" + feedURL + "&o=xml");
62  
63              ByteArrayInputStream bais =
64                  new ByteArrayInputStream(xml.getBytes());
65  
66              Document doc = new SAXReader().read(bais);
67  
68              ArrayList list = new ArrayList();
69  
70              DateFormat df = new SimpleDateFormat("MMM d yyyy K:mma z");
71  
72              Element root = doc.getRootElement();
73  
74              List articles = root.elements("article");
75              Iterator i = articles.iterator();
76  
77              while (i.hasNext()) {
78                  Element article = (Element)i.next();
79  
80                  String harvestTime =
81                      article.element("harvest_time").getTextTrim() + " GMT";
82  
83                  list.add(new Article(
84                      article.element("headline_text").getTextTrim(),
85                      article.element("url").getTextTrim(),
86                      article.element("source").getTextTrim(),
87                      article.element("document_url").getTextTrim(),
88                      article.element("access_status").getTextTrim(),
89                      article.element("access_registration").getTextTrim(),
90                      df.parse(harvestTime)));
91              }
92  
93              return new News(feedURL, categoryName, list);
94          }
95          catch (Exception e) {
96              throw new ConverterException(e);
97          }
98      }
99  
100     public long getRefreshTime() {
101         return _refreshTime;
102     }
103 
104     private long _refreshTime = Time.MINUTE * 20;
105 
106 }