1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugins.changes;
20
21 import java.io.IOException;
22 import java.io.Writer;
23 import java.text.DateFormat;
24 import java.text.ParseException;
25 import java.util.ArrayList;
26 import java.util.Date;
27 import java.util.List;
28 import java.util.Locale;
29 import java.util.ResourceBundle;
30
31 import com.rometools.rome.feed.synd.SyndContent;
32 import com.rometools.rome.feed.synd.SyndContentImpl;
33 import com.rometools.rome.feed.synd.SyndEntry;
34 import com.rometools.rome.feed.synd.SyndEntryImpl;
35 import com.rometools.rome.feed.synd.SyndFeed;
36 import com.rometools.rome.feed.synd.SyndFeedImpl;
37 import com.rometools.rome.io.FeedException;
38 import com.rometools.rome.io.SyndFeedOutput;
39 import org.apache.maven.doxia.util.DoxiaUtils;
40 import org.apache.maven.plugins.changes.model.Release;
41
42
43
44
45 public class FeedGenerator {
46 private final ResourceBundle rbundle;
47
48 private final SyndFeed feed;
49
50 private String link;
51
52 private String title;
53
54 private String author;
55
56 private DateFormat dateFormat;
57
58
59
60
61
62
63 public FeedGenerator(final Locale locale) {
64 this.feed = new SyndFeedImpl();
65 this.rbundle = ResourceBundle.getBundle(
66 "changes-report", locale, this.getClass().getClassLoader());
67 }
68
69
70
71
72
73
74 public String getAuthor() {
75 return author;
76 }
77
78
79
80
81
82
83 public void setAuthor(final String author) {
84 this.author = author.trim();
85 }
86
87
88
89
90
91
92 public String getTitle() {
93 return title;
94 }
95
96
97
98
99
100
101 public void setTitle(final String title) {
102 this.title = title.trim();
103 }
104
105
106
107
108
109
110 public DateFormat getDateFormat() {
111 return dateFormat;
112 }
113
114
115
116
117
118
119 public void setDateFormat(final DateFormat dateFormat) {
120 this.dateFormat = dateFormat;
121 }
122
123
124
125
126
127
128 public String getLink() {
129 return link;
130 }
131
132
133
134
135
136
137 public void setLink(final String link) {
138 this.link = link.trim();
139 }
140
141
142
143
144
145
146
147
148
149 public boolean isSupportedFeedType(final String type) {
150 return getSupportedFeedTypes().contains(type);
151 }
152
153
154
155
156
157
158
159 public List<String> getSupportedFeedTypes() {
160 return feed.getSupportedFeedTypes();
161 }
162
163
164
165
166
167
168
169
170
171
172 public void export(final List<Release> releases, final String feedType, final Writer writer) throws IOException {
173 feed.setFeedType(feedType);
174 feed.setTitle(title);
175 feed.setAuthor(author);
176 feed.setPublishedDate(new Date());
177 feed.setLink(link);
178 feed.setDescription(rbundle.getString("report.changes.text.rssfeed.description"));
179 feed.setLanguage(rbundle.getLocale().getLanguage());
180 feed.setEntries(getEntries(releases));
181
182 try {
183 new SyndFeedOutput().output(feed, writer);
184 } catch (FeedException ex) {
185 throw new IOException(ex.getMessage(), ex);
186 }
187 }
188
189 private List<SyndEntry> getEntries(final List<Release> releases) {
190 final List<SyndEntry> entries = new ArrayList<>(1);
191
192 if (!releases.isEmpty()) {
193 final Release release = releases.get(0);
194
195 final SyndEntry entry = new SyndEntryImpl();
196 entry.setTitle(release.getVersion());
197 entry.setLink(link + "#" + DoxiaUtils.encodeId(release.getVersion()));
198 entry.setDescription(getSyndContent(release));
199 entry.setPublishedDate(getDate(release.getDateRelease(), dateFormat));
200
201 entries.add(entry);
202 }
203
204 return entries;
205 }
206
207 private static SyndContent getSyndContent(final Release release) {
208 final SyndContent syndContent = new SyndContentImpl();
209 syndContent.setType("text/html");
210
211 final StringBuilder sb = new StringBuilder(512);
212
213 final String description = release.getDescription();
214
215 if (description != null && !description.trim().isEmpty()) {
216 sb.append("<p>").append(description).append("</p>");
217 }
218
219 sb.append("<p>Version ").append(release.getVersion()).append(" is available with ");
220 sb.append(release.getActions().size()).append(" fixed issues.</p>");
221
222 syndContent.setValue(sb.toString());
223
224 return syndContent;
225 }
226
227 private static Date getDate(final String dateRelease, final DateFormat dateFormat) {
228 if (dateFormat == null) {
229 return new Date();
230 }
231
232 try {
233 return dateFormat.parse(dateRelease);
234 } catch (ParseException ex) {
235 return new Date();
236 }
237 }
238 }