View Javadoc

1   package org.apache.velocity.site.news;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.StringReader;
23  import java.text.DateFormat;
24  import java.text.SimpleDateFormat;
25  import java.util.Locale;
26  
27  import org.apache.commons.lang.StringUtils;
28  import org.apache.maven.doxia.parser.ParseException;
29  import org.apache.maven.doxia.sink.Sink;
30  import org.apache.velocity.site.news.model.Item;
31  
32  /**
33   * Render the news teasers for the Macro.
34   *
35   * @author <a href="mailto:henning@apache.org">Henning P. Schmiedehausen</a>
36   * @version $Revision: 526767 $
37   *
38   */
39  public class NewsBlockGenerator
40  {
41  
42      private final NewsReport newsReport;
43  
44      private final Sink sink;
45  
46      private final Locale locale;
47  
48      private final DateFormat localeDateFormat;
49  
50      public NewsBlockGenerator(final NewsReport newsReport, final Locale locale)
51      {
52          this.newsReport = newsReport;
53          this.locale = locale;
54          this.sink = newsReport.getSink();
55  
56          localeDateFormat = new SimpleDateFormat(newsReport.getBundle(this.locale).getString("report.news.page.dateFormat"));
57      }
58  
59      public void displayItem(final Item item) throws VelocityNewsException
60      {
61  
62          sink.section2();
63          sink.sectionTitle2();
64  
65          if (item.getDate() != null)
66          {
67              sink.text(localeDateFormat.format(VelocityNewsUtils.parseItemDate(item.getDate())) + " - ");
68          }
69  
70          sink.text(item.getHeadline());
71          sink.sectionTitle2_();
72  
73          addAptParagraph(item.getText(), item.getId());
74          sink.section2_();
75      }
76  
77      private void addAptParagraph(final String text, final String itemId) throws VelocityNewsException
78      {
79  
80          // The first sentence is our teaser...
81          String[] textFields = StringUtils.splitByWholeSeparator(text, ". ");
82          String aptText = " " + textFields[0] + ". [{{{" + newsReport.getOutputName() + ".html#" + itemId + "}Read more...}}]";
83          try
84          {
85              // Leading space makes sure this gets interpreted as text.
86              newsReport.getAptParser().parse(new StringReader(aptText), sink);
87          }
88          catch (ParseException pe)
89          {
90              throw new VelocityNewsException("While parsing Apt: ", pe);
91          }
92      }
93  }