1
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
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 }