1 package org.apache.velocity.site.news.plugin;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.StringReader;
23 import java.text.DateFormat;
24 import java.text.SimpleDateFormat;
25 import java.util.Iterator;
26 import java.util.Locale;
27
28 import org.apache.maven.doxia.parser.ParseException;
29 import org.apache.maven.doxia.sink.Sink;
30 import org.apache.velocity.site.news.NewsReport;
31 import org.apache.velocity.site.news.VelocityNewsException;
32 import org.apache.velocity.site.news.VelocityNewsUtils;
33 import org.apache.velocity.site.news.model.Item;
34
35
36
37
38
39
40
41
42 public class NewsPageGenerator
43 {
44
45 private final NewsReport newsReport;
46
47 private final Sink sink;
48
49 private final Locale locale;
50
51 private final DateFormat localeDateFormat;
52
53 public NewsPageGenerator(final NewsReport newsReport, final Locale locale)
54 {
55 this.newsReport = newsReport;
56 this.locale = locale;
57 this.sink = newsReport.getSink();
58
59 localeDateFormat = new SimpleDateFormat(newsReport.getBundle(locale).getString("report.news.page.dateFormat"));
60 }
61
62 public void openPage()
63 {
64 sink.head();
65 sink.title();
66 sink.text(getLocaleTitle());
67 sink.title_();
68 sink.head_();
69
70 sink.body();
71 }
72
73 public void closePage()
74 {
75 sink.body_();
76 sink.flush();
77 }
78
79 public void displayHeader()
80 {
81 sink.section1();
82 sink.sectionTitle1();
83 sink.text(getLocaleTitle());
84 sink.sectionTitle1_();
85 sink.section1_();
86
87 }
88
89 public void displayItem(final Item item) throws VelocityNewsException
90 {
91
92 sink.section2();
93
94 sink.sectionTitle2();
95 sink.anchor(item.getId());
96 sink.text(item.getHeadline());
97 sink.anchor_();
98 sink.sectionTitle2_();
99
100 if (item.getDate() != null)
101 {
102 sink.paragraph();
103 sink.bold();
104 sink.text(localeDateFormat.format(VelocityNewsUtils.parseItemDate(item.getDate())));
105 sink.bold_();
106 sink.paragraph_();
107 }
108
109 addAptParagraph(item.getText());
110
111 if (item.getCategories() != null)
112 {
113 sink.paragraph();
114
115 sink.text(getLocaleCategories());
116
117 for (Iterator it = item.getCategories().iterator(); it.hasNext();)
118 {
119 String category = (String) it.next();
120
121 sink.italic();
122 sink.text(category);
123 sink.italic_();
124
125 if (it.hasNext())
126 {
127 sink.text(", ");
128 }
129 }
130 sink.paragraph_();
131 }
132
133 sink.section2_();
134 }
135
136 private void addAptParagraph(final String text) throws VelocityNewsException
137 {
138
139 String aptText = " " + text;
140 try
141 {
142 newsReport.getAptParser().parse(new StringReader(aptText), sink);
143 }
144 catch (ParseException pe)
145 {
146 throw new VelocityNewsException("While parsing Apt: ", pe);
147 }
148 }
149
150 private String getLocaleTitle()
151 {
152 return newsReport.getBundle(locale).getString("report.news.page.title");
153 }
154
155 private String getLocaleCategories()
156 {
157 return newsReport.getBundle(locale).getString("report.news.page.categories") + " ";
158 }
159
160 }