View Javadoc

1   /*
2    * Copyright 2007-2011 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.maven.exceptions;
17  
18  import java.util.ResourceBundle;
19  
20  import org.apache.commons.lang.StringUtils;
21  import org.apache.maven.doxia.sink.Sink;
22  
23  import com.sun.javadoc.ClassDoc;
24  import com.sun.javadoc.FieldDoc;
25  
26  import de.smartics.analysis.javadoc.render.JavadocRenderer;
27  import de.smartics.analysis.javadoc.render.html.HtmlRendererFactory;
28  import de.smartics.analysis.javadoc.runtime.RuntimeUtils;
29  import de.smartics.exceptions.core.Code;
30  import de.smartics.exceptions.report.generator.StringFunction;
31  import de.smartics.report.conf.ProjectConfiguration;
32  import de.smartics.report.conf.ReportData;
33  import de.smartics.report.generator.AbstractOutputReportGenerator;
34  
35  /**
36   * Simple generator that generates with a Maven sink.
37   *
38   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
39   * @version $Revision:591 $
40   */
41  public abstract class AbstractSinkReportGenerator extends
42      AbstractOutputReportGenerator
43  {
44    // ********************************* Fields *********************************
45  
46    // --- constants ------------------------------------------------------------
47  
48    // --- members --------------------------------------------------------------
49  
50    // ****************************** Initializer *******************************
51  
52    // ****************************** Constructors ******************************
53  
54    /**
55     * Default constructor.
56     */
57    protected AbstractSinkReportGenerator()
58    {
59      super(new JavadocRenderer(new HtmlRendererFactory()));
60    }
61  
62    // ****************************** Inner Classes *****************************
63  
64    // ********************************* Methods ********************************
65  
66    // --- init -----------------------------------------------------------------
67  
68    // --- get&set --------------------------------------------------------------
69  
70    /**
71     * Makes the cast.
72     *
73     * @param sink the instance to cast.
74     * @return the sink with the concrete type.
75     */
76    protected Sink toSink(final Object sink)
77    {
78      return (Sink) sink;
79    }
80  
81    // --- business -------------------------------------------------------------
82  
83    /**
84     * {@inheritDoc}
85     */
86    protected void writeReportElementInfo(
87        final Object output,
88        final ProjectConfiguration config,
89        final FieldDoc fieldDoc) throws Exception
90    {
91      final Sink sink = toSink(output);
92  
93      sink.tableRow();
94  
95      // TODO: Check to optimize that class is not loaded for every constant but
96      // provided once to be accessed by each constant...
97      final Code instance =
98          (Code) RuntimeUtils.loadInstance(config.getProjectClassLoader(),
99              fieldDoc);
100 
101     sink.tableCell();
102     sink.text(String.valueOf(String.valueOf(instance)));
103     sink.tableCell_();
104 
105     sink.tableCell();
106     sink.text(fieldDoc.name());
107     sink.tableCell_();
108 
109     sink.tableCell();
110 
111     final String javadoc = this.renderer.render(fieldDoc);
112     if (StringUtils.isNotBlank(javadoc))
113     {
114       sink.rawText(javadoc);
115     }
116     else
117     {
118       sink.rawText("&nbsp;");
119     }
120     sink.tableCell_();
121 
122     sink.tableRow_();
123   }
124 
125   /**
126    * {@inheritDoc}
127    */
128   protected void writeContent(
129       final Object output,
130       final ProjectConfiguration config,
131       final ReportData reportData) throws Exception // NOPMD
132   {
133     final Sink sink = toSink(output);
134 
135     final ResourceBundle bundle = config.getBundle();
136     final String title = bundle.getString("report.name");
137     sink.head();
138     sink.title();
139     sink.text(title);
140     sink.title_();
141     sink.head_();
142 
143     sink.body();
144     sink.section1();
145 
146     sink.sectionTitle1();
147     sink.text(title);
148     sink.sectionTitle1_();
149 
150     sink.text(bundle.getString("report.description"));
151     sink.lineBreak();
152 
153     if (!reportData.isEmpty())
154     {
155       write(output, config, reportData);
156     }
157     else
158     {
159       sink.lineBreak();
160       sink.lineBreak();
161       sink.text(bundle.getString("report.noElementsFound"));
162     }
163 
164     sink.section1_();
165     sink.body_();
166     sink.flush();
167     sink.close();
168   }
169 
170   /**
171    * {@inheritDoc}
172    */
173   protected void writeInfoFooter(
174       final Object output,
175       final ProjectConfiguration config) throws Exception // NOPMD
176   {
177     final Sink sink = toSink(output);
178     sink.section2_();
179   }
180 
181   protected void writeInfoSubFooter(
182       final Object output,
183       final ProjectConfiguration config) throws Exception // NOPMD
184   {
185     final Sink sink = toSink(output);
186     sink.table_();
187     sink.section3_();
188   }
189 
190   /**
191    * Writes the header and introduction text on a section 2.
192    * <p>
193    * If no <code>headLine</code> is given, the qualified class name is written
194    * as section title instead.
195    * </p>
196    *
197    * @param output the output to write to.
198    * @param config not used in this implementation.
199    * @param headLine the text for the section header. May be <code>null</code>.
200    * @param classDoc the type information to render if <code>headLine</code> is
201    *          <code>null</code> as section header and its comment to render as
202    *          introduction text.
203    * @throws Exception on any problem encountered.
204    */
205   protected void writeInfoHeader(
206       final Object output,
207       final ProjectConfiguration config,
208       final String headLine,
209       final ClassDoc classDoc) throws Exception // NOPMD
210   {
211     final Sink sink = toSink(output);
212 
213     sink.section2();
214     sink.sectionTitle2();
215     if (headLine != null)
216     {
217       sink.text(headLine);
218     }
219     else
220     {
221       final String className = classDoc.qualifiedName();
222       sink.text(className);
223     }
224 
225     final String classComment = classDoc.commentText();
226     sink.sectionTitle2_();
227 
228     if (classComment != null)
229     {
230       sink.rawText(classComment);
231     }
232   }
233 
234   /**
235    * {@inheritDoc}
236    */
237   protected void writeInfoSubHeader(
238       final Object output,
239       final ProjectConfiguration config,
240       final String subHeader)
241   {
242     final Sink sink = toSink(output);
243     // The section3 increased the space which displays ugly...
244     sink.section3();
245     sink.sectionTitle3();
246     sink.text(subHeader);
247     sink.sectionTitle3_();
248   }
249 
250   /**
251    * Writes the table header.
252    *
253    * @param output the output to write to.
254    * @param config the project configuration to control the output.
255    */
256   protected void writeTableHeader(
257       final Object output,
258       final ProjectConfiguration config)
259   {
260     final Sink sink = toSink(output);
261     sink.table();
262     sink.tableRow();
263 
264     final ResourceBundle bundle = config.getBundle();
265     sink.tableHeaderCell("100");
266     final String codeLabel =
267         StringFunction.getMessage(bundle,
268             "report.exceptioncodes.item.reportElement", "Code");
269     sink.text(codeLabel);
270     sink.tableHeaderCell_();
271 
272     sink.tableHeaderCell("150");
273     final String propertyNameLabel =
274         StringFunction.getMessage(bundle,
275             "report.exceptioncodes.item.propertyName", "Property Name");
276     sink.text(propertyNameLabel);
277     sink.tableHeaderCell_();
278 
279     sink.tableHeaderCell();
280     final String descriptionLabel =
281         StringFunction.getMessage(bundle,
282             "report.exceptioncodes.item.description", "Description");
283     sink.text(descriptionLabel);
284     sink.tableHeaderCell_();
285 
286     sink.tableRow_();
287   }
288 
289   /**
290    * {@inheritDoc}
291    */
292   protected void writeFooter(
293       final Object output,
294       final ProjectConfiguration config)
295   {
296   }
297 
298   // --- object basics --------------------------------------------------------
299 
300 }