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 javax.xml.stream.XMLStreamException;
19  import javax.xml.stream.XMLStreamWriter;
20  
21  import de.smartics.exceptions.report.data.ExceptionCodeReportItem;
22  import de.smartics.exceptions.report.data.ProjectConfiguration;
23  import de.smartics.exceptions.report.data.StoredExceptionCodesReport;
24  import de.smartics.exceptions.report.renderer.JavadocRenderer;
25  import de.smartics.exceptions.report.utils.CopyReader;
26  
27  /**
28   * Simple generator that generates a HTML file.
29   */
30  public abstract class AbstractHtmlReportGenerator extends
31      AbstractXmlReportGenerator
32  {
33    // ********************************* Fields *********************************
34  
35    // --- constants ------------------------------------------------------------
36  
37    // --- members --------------------------------------------------------------
38  
39    // ****************************** Initializer *******************************
40  
41    // ****************************** Constructors ******************************
42  
43    /**
44     * Default constructor.
45     *
46     * @param renderer the renderer used to map Javadoc comments to the report's
47     *          output format.
48     */
49    protected AbstractHtmlReportGenerator(final JavadocRenderer renderer)
50    {
51      super(renderer);
52    }
53  
54    // ****************************** Inner Classes *****************************
55  
56    // ********************************* Methods ********************************
57  
58    // --- init -----------------------------------------------------------------
59  
60    // --- get&set --------------------------------------------------------------
61  
62    // --- business -------------------------------------------------------------
63  
64    /**
65     * {@inheritDoc}
66     */
67    @Override
68    protected void writeContent(final XMLStreamWriter xmlWriter,
69        final ProjectConfiguration<XMLStreamWriter> config,
70        final StoredExceptionCodesReport report) throws Exception // NOPMD
71    {
72      xmlWriter.writeStartElement("html");
73      writeHead(xmlWriter, config);
74      xmlWriter.writeStartElement("body");
75  
76      xmlWriter.writeStartElement("h1");
77      xmlWriter.writeCharacters(config.getProjectName());
78      xmlWriter.writeEndElement();
79  
80      write(xmlWriter, config, report);
81      xmlWriter.writeEndElement();
82      xmlWriter.writeEndElement();
83    }
84  
85    /**
86     * Writes the HTML head with the style sheet information.
87     *
88     * @param xmlWriter the writer to use to write the head element.
89     * @param config the configuration to access the style sheet information.
90     * @throws XMLStreamException if the XML writer encountered a problem.
91     */
92    private void writeHead(final XMLStreamWriter xmlWriter,
93        final ProjectConfiguration<XMLStreamWriter> config)
94      throws XMLStreamException
95    {
96      final String styleSheet = config.getStyleSheet();
97      if (styleSheet != null)
98      {
99        xmlWriter.writeStartElement("head");
100 
101       xmlWriter.writeStartElement("title");
102       xmlWriter.writeCharacters(config.getProjectName());
103       xmlWriter.writeEndElement();
104 
105       xmlWriter.writeStartElement("link");
106       xmlWriter.writeAttribute("rel", "stylesheet");
107       xmlWriter.writeAttribute("type", "text/css");
108       xmlWriter.writeAttribute("href", styleSheet);
109       xmlWriter.writeEndElement();
110 
111       xmlWriter.writeEndElement();
112     }
113   }
114 
115   /**
116    * {@inheritDoc}
117    */
118   @Override
119   protected void writeInfoHeader(final XMLStreamWriter xmlWriter,
120       final ProjectConfiguration<XMLStreamWriter> config,
121       final String headLine, final ExceptionCodeReportItem item) throws Exception // NOPMD
122   {
123     xmlWriter.writeStartElement("h2");
124     if (headLine != null)
125     {
126       xmlWriter.writeCharacters(headLine);
127     }
128 
129     if (headLine != null)
130     {
131       xmlWriter.writeCharacters(": ");
132     }
133     final String className = item.getDeclaringTypeName();
134     xmlWriter.writeCharacters(className);
135     xmlWriter.writeEndElement();
136 
137     final String classComment = item.getTypeComment();
138     if (classComment != null)
139     {
140       // FIXME: This breaks if the content read is not valid XHTML...
141       final CopyReader copy = new CopyReader(xmlWriter);
142       xmlWriter.writeStartElement("div");
143       copy.copy(classComment);
144       xmlWriter.writeEndElement();
145     }
146   }
147 
148   /**
149    * {@inheritDoc}
150    */
151   @Override
152   protected void writeInfoFooter(final XMLStreamWriter xmlWriter,
153       final ProjectConfiguration<XMLStreamWriter> config) throws Exception // NOPMD
154   {
155     xmlWriter.writeEndElement();
156   }
157 
158   // --- object basics --------------------------------------------------------
159 
160 }