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 {
69 this.feed = new SyndFeedImpl();
70 this.rbundle = ResourceBundle.getBundle( "changes-report", locale, this.getClass().getClassLoader() );
71 }
72
73
74
75
76
77
78 public String getAuthor()
79 {
80 return author;
81 }
82
83
84
85
86
87
88 public void setAuthor( final String author )
89 {
90 this.author = author.trim();
91 }
92
93
94
95
96
97
98 public String getTitle()
99 {
100 return title;
101 }
102
103
104
105
106
107
108 public void setTitle( final String title )
109 {
110 this.title = title.trim();
111 }
112
113
114
115
116
117
118 public DateFormat getDateFormat()
119 {
120 return dateFormat;
121 }
122
123
124
125
126
127
128
129 public void setDateFormat( final DateFormat dateFormat )
130 {
131 this.dateFormat = dateFormat;
132 }
133
134
135
136
137
138
139 public String getLink()
140 {
141 return link;
142 }
143
144
145
146
147
148
149 public void setLink( final String link )
150 {
151 this.link = link.trim();
152 }
153
154
155
156
157
158
159
160
161
162
163 public boolean isSupportedFeedType( final String type )
164 {
165 return getSupportedFeedTypes().contains( type );
166 }
167
168
169
170
171
172
173
174
175 @SuppressWarnings( "unchecked" )
176 public List<String> getSupportedFeedTypes()
177 {
178 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 feed.setFeedType( feedType );
196 feed.setTitle( title );
197 feed.setAuthor( author );
198 feed.setPublishedDate( new Date() );
199 feed.setLink( link );
200 feed.setDescription( rbundle.getString( "report.changes.text.rssfeed.description" ) );
201 feed.setLanguage( rbundle.getLocale().getLanguage() );
202
203
204 feed.setEntries( getEntries( releases ) );
205
206 try
207 {
208 new SyndFeedOutput().output( feed, writer );
209 }
210 catch ( FeedException ex )
211 {
212 IOException ioex = new IOException( ex.getMessage() );
213 ioex.initCause( ex );
214 throw ioex;
215 }
216 }
217
218 private List<SyndEntry> getEntries( final List<Release> releases )
219 {
220 final List<SyndEntry> entries = new ArrayList<SyndEntry>( 1 );
221
222 if ( releases.size() > 0 )
223 {
224 final Release release = releases.get( 0 );
225
226 final SyndEntry entry = new SyndEntryImpl();
227 entry.setTitle( release.getVersion() );
228 entry.setLink( link + "#" + HtmlTools.encodeId( release.getVersion() ) );
229 entry.setDescription( getSyndContent( release ) );
230 entry.setPublishedDate( getDate( release.getDateRelease(), dateFormat ) );
231
232 entries.add( entry );
233 }
234
235 return entries;
236 }
237
238 private static SyndContent getSyndContent( final Release release )
239 {
240 final SyndContent syndContent = new SyndContentImpl();
241 syndContent.setType( "text/html" );
242
243 final StringBuilder sb = new StringBuilder( 512 );
244
245 final String description = release.getDescription();
246
247 if ( description != null && description.trim().length() > 0 )
248 {
249 sb.append( "<p>" ).append( description ).append( "</p>" );
250 }
251
252
253 sb.append( "<p>Version " )
254 .append( release.getVersion() ).append( " is available with " )
255 .append( release.getActions().size() ).append( " fixed issues.</p>" );
256
257 syndContent.setValue( sb.toString() );
258
259 return syndContent;
260 }
261
262 private static Date getDate( final String dateRelease, final DateFormat dateFormat )
263 {
264 if ( dateFormat == null )
265 {
266 return new Date();
267 }
268
269 try
270 {
271 return dateFormat.parse( dateRelease );
272 }
273 catch ( ParseException ex )
274 {
275 return new Date();
276 }
277 }
278 }