View Javadoc

1   /*
2    * Copyright 2007-2013 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.exceptions.report.generator;
17  
18  import java.util.List;
19  import java.util.ResourceBundle;
20  
21  import javax.xml.stream.XMLStreamException;
22  import javax.xml.stream.XMLStreamWriter;
23  
24  import org.apache.commons.lang.StringUtils;
25  
26  import de.smartics.exceptions.core.Code;
27  import de.smartics.exceptions.report.app.ReportException;
28  import de.smartics.exceptions.report.data.ExceptionCodeReportItem;
29  import de.smartics.exceptions.report.data.ProjectConfiguration;
30  import de.smartics.exceptions.report.data.StoredExceptionCodesReport;
31  import de.smartics.exceptions.report.renderer.JavadocRenderer;
32  import de.smartics.exceptions.report.renderer.html.HtmlRendererFactory;
33  import de.smartics.exceptions.report.utils.CopyReader;
34  
35  /**
36   * Simple generator that generates a HTML file for exception codes.
37   */
38  public class HtmlReportGenerator extends AbstractHtmlReportGenerator
39  {
40    // ********************************* Fields *********************************
41  
42    // --- constants ------------------------------------------------------------
43  
44    // --- members --------------------------------------------------------------
45  
46    // ****************************** Initializer *******************************
47  
48    // ****************************** Constructors ******************************
49  
50    /**
51     * Default constructor.
52     *
53     * @see AbstractHtmlReportGenerator#AbstractHtmlReportGenerator()
54     */
55    public HtmlReportGenerator()
56    {
57      super(new JavadocRenderer(new HtmlRendererFactory()));
58    }
59  
60    // ****************************** Inner Classes *****************************
61  
62    // ********************************* Methods ********************************
63  
64    // --- init -----------------------------------------------------------------
65  
66    // --- get&set --------------------------------------------------------------
67  
68    // --- business -------------------------------------------------------------
69  
70    /**
71     * {@inheritDoc}
72     */
73    @Override
74    protected void writeTableHeader(final XMLStreamWriter xmlWriter,
75        final ProjectConfiguration<XMLStreamWriter> config)
76      throws XMLStreamException
77    {
78      xmlWriter.writeStartElement("table");
79      xmlWriter.writeStartElement("tr");
80  
81      final ResourceBundle bundle = config.getBundle();
82      final String codeLabel =
83          StringFunction.getMessage(bundle,
84              "report.exceptioncode.item.reportElement", "Code");
85      xmlWriter.writeStartElement("th");
86      xmlWriter.writeCharacters(codeLabel);
87      xmlWriter.writeEndElement();
88  
89      final String propertyNameLabel =
90          StringFunction.getMessage(bundle,
91              "report.exceptioncode.item.propertyName", "Property Name");
92      xmlWriter.writeStartElement("th");
93      xmlWriter.writeCharacters(propertyNameLabel);
94      xmlWriter.writeEndElement();
95  
96      final String descriptionLabel =
97          StringFunction.getMessage(bundle,
98              "report.exceptioncode.item.description", "Description");
99      xmlWriter.writeStartElement("th");
100     xmlWriter.writeCharacters(descriptionLabel);
101     xmlWriter.writeEndElement();
102 
103     xmlWriter.writeEndElement();
104   }
105 
106   /**
107    * {@inheritDoc}
108    */
109   @Override
110   protected void writeReportElementInfo(final XMLStreamWriter xmlWriter,
111       final ProjectConfiguration<XMLStreamWriter> config,
112       final ExceptionCodeReportItem item) throws Exception
113   {
114     xmlWriter.writeStartElement("tr");
115 
116     final Code instance = item.getCode();
117 
118     xmlWriter.writeStartElement("td");
119     xmlWriter.writeAttribute("style", "white-space: nowrap;");
120     xmlWriter.writeCharacters(String.valueOf(instance));
121     xmlWriter.writeEndElement();
122 
123     xmlWriter.writeStartElement("td");
124     xmlWriter.writeAttribute("style", "white-space: nowrap;");
125     xmlWriter.writeCharacters(item.getName());
126     xmlWriter.writeEndElement();
127 
128     xmlWriter.writeStartElement("td");
129     final String javadoc = this.renderer.render(item.getJavadoc());
130     if (StringUtils.isNotBlank(javadoc))
131     {
132       // FIXME: This breaks if the content read is not valid XHTML...
133       final CopyReader copy = new CopyReader(xmlWriter);
134       copy.copy(javadoc);
135     }
136     xmlWriter.writeEndElement();
137     xmlWriter.writeEndElement();
138   }
139 
140   @Override
141   public void writeReport(final ProjectConfiguration<XMLStreamWriter> config,
142       final StoredExceptionCodesReport report) throws ReportException
143   {
144     // Do nothing
145   }
146 
147   @Override
148   protected List<ExceptionCodeReportItem> getItems(
149       final StoredExceptionCodesReport report)
150   {
151     return report.getItems();
152   }
153 
154   @Override
155   protected boolean hasSectionChanged(final String currentSection,
156       final ExceptionCodeReportItem item)
157   {
158     return !item.getDeclaringTypeName().equals(currentSection);
159   }
160 
161   @Override
162   protected String getCurrentSelection(final ExceptionCodeReportItem item)
163   {
164     return item.getDeclaringTypeName();
165   }
166 
167   @Override
168   protected String getCodeTitle(final String headLine,
169       final ExceptionCodeReportItem item)
170   {
171     final String title;
172     if (headLine != null)
173     {
174       title = headLine;
175     }
176     else
177     {
178       final String componentId = item.getCode().getComponentId();
179       if (StringUtils.isNotBlank(componentId))
180       {
181         title = componentId;
182       }
183       else
184       {
185         final String className = item.getDeclaringTypeName();
186         title = className;
187       }
188     }
189 
190     return title;
191   }
192 
193   // --- object basics --------------------------------------------------------
194 
195 }