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.ContentUtil;
26  import com.liferay.portal.util.WebCacheable;
27  import com.liferay.portlet.news.model.Feed;
28  import com.liferay.util.CollectionFactory;
29  import com.liferay.util.ConverterException;
30  import com.liferay.util.Http;
31  import com.liferay.util.Time;
32  
33  import java.io.BufferedReader;
34  import java.io.IOException;
35  import java.io.StringReader;
36  
37  import java.util.ArrayList;
38  import java.util.Collections;
39  import java.util.Iterator;
40  import java.util.List;
41  import java.util.Map;
42  import java.util.Set;
43  import java.util.TreeSet;
44  
45  /**
46   * <a href="CategoryConverter.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Brian Wing Shun Chan
49   *
50   */
51  public class CategoryConverter implements WebCacheable {
52  
53      public Object convert(String url) throws ConverterException {
54          try {
55              Map categoryMap = CollectionFactory.getHashMap();
56              Set categorySet = new TreeSet();
57              Map feedMap = CollectionFactory.getHashMap();
58              Set feedSet = new TreeSet();
59  
60              String xml = null;
61  
62              try {
63                  xml = Http.URLtoString(url);
64              }
65              catch (IOException ioe) {
66                  xml = ContentUtil.get(
67                      "com/liferay/portlet/news/dependencies/categories.tsv");
68              }
69  
70              BufferedReader br = new BufferedReader(new StringReader(xml));
71  
72              String s = null;
73  
74              while ((s = br.readLine()) != null) {
75                  String feedURL;
76                  String fullName;
77                  String shortName;
78                  String categoryName;
79  
80                  int pos;
81  
82                  pos = s.indexOf("\t");
83                  feedURL = s.substring(0, pos);
84  
85                  s = s.substring(pos + 1, s.length());
86                  pos = s.indexOf("\t");
87                  fullName = s.substring(0, pos);
88  
89                  s = s.substring(pos + 1, s.length());
90                  pos = s.indexOf("\t");
91                  shortName = s.substring(0, pos);
92  
93                  categoryName = s.substring(pos + 1, s.length());
94  
95                  Feed feed =
96                      new Feed(feedURL, fullName, shortName, categoryName);
97  
98                  categorySet.add(feed.getCategoryName());
99                  feedMap.put(feedURL, feed);
100                 feedSet.add(feed);
101             }
102 
103             categoryMap = CollectionFactory.getHashMap();
104 
105             String temp = "";
106             Set tempSet = null;
107 
108             Iterator i = feedSet.iterator();
109 
110             while (i.hasNext()) {
111                 Feed feed = (Feed)i.next();
112 
113                 if (temp.equals(feed.getCategoryName())) {
114                     tempSet.add(feed);
115                 }
116                 else {
117                     tempSet = new TreeSet();
118                     categoryMap.put(feed.getCategoryName(), tempSet);
119                     tempSet.add(feed);
120                 }
121 
122                 temp = feed.getCategoryName();
123             }
124 
125             categoryMap = Collections.unmodifiableMap(categoryMap);
126             categorySet = Collections.unmodifiableSet(categorySet);
127 
128             feedMap = Collections.unmodifiableMap(feedMap);
129             feedSet = Collections.unmodifiableSet(feedSet);
130 
131             List list = new ArrayList();
132 
133             list.add(categoryMap);
134             list.add(categorySet);
135             list.add(feedMap);
136             list.add(feedSet);
137 
138             return list;
139         }
140         catch (Exception e) {
141             throw new ConverterException(e);
142         }
143     }
144 
145     public long getRefreshTime() {
146         return _REFRESH_TIME;
147     }
148 
149     private static final long _REFRESH_TIME = Time.DAY * 3;
150 
151 }