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