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