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.maven.exceptions.sdoc;
17  
18  import java.io.BufferedOutputStream;
19  import java.io.File;
20  import java.io.FileNotFoundException;
21  import java.io.FileOutputStream;
22  import java.io.OutputStream;
23  import java.util.List;
24  
25  import javax.xml.parsers.DocumentBuilder;
26  import javax.xml.parsers.DocumentBuilderFactory;
27  import javax.xml.parsers.ParserConfigurationException;
28  
29  import org.apache.commons.io.IOUtils;
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  import org.apache.maven.plugin.MojoExecutionException;
33  import org.w3c.dom.Document;
34  
35  import de.smartics.exceptions.core.Code;
36  import de.smartics.exceptions.report.data.ExceptionCodeReportItem;
37  import de.smartics.exceptions.report.data.ProjectConfiguration;
38  import de.smartics.exceptions.report.data.StoredExceptionCodesReport;
39  import de.smartics.exceptions.report.generator.AbstractReportGenerator;
40  import de.smartics.exceptions.report.renderer.JavadocRenderer;
41  import de.smartics.exceptions.report.renderer.html.HtmlRendererFactory;
42  import de.smartics.maven.io.MojoIoUtils;
43  
44  /**
45   * Writes individual reports in the projectdoc's code doctype.
46   */
47  public class SdocCodeReportGenerator extends AbstractReportGenerator<File>
48  {
49  
50    // ********************************* Fields *********************************
51  
52    // --- constants ------------------------------------------------------------
53  
54    /**
55     * Reference to the logger for this class.
56     */
57    private static final Log LOG = LogFactory
58        .getLog(SdocCodeReportGenerator.class);
59  
60    // --- members --------------------------------------------------------------
61  
62    /**
63     * The builder for XML documents.
64     */
65    private final DocumentBuilder builder;
66  
67    // ****************************** Initializer *******************************
68  
69    // ****************************** Constructors ******************************
70  
71    /**
72     * Default constructor.
73     *
74     * @param renderer the renderer used to map Javadoc comments to the report's
75     *          output format.
76     * @throws ParserConfigurationException if creating XML infrstructure fails.
77     */
78    public SdocCodeReportGenerator() throws ParserConfigurationException
79    {
80      super(new JavadocRenderer(new HtmlRendererFactory()));
81  
82      final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
83      builder = factory.newDocumentBuilder();
84    }
85  
86    // ****************************** Inner Classes *****************************
87  
88    // ********************************* Methods ********************************
89  
90    // --- init -----------------------------------------------------------------
91  
92    // --- get&set --------------------------------------------------------------
93  
94    // --- business -------------------------------------------------------------
95  
96    @Override
97    protected void writeContent(final File output,
98        final ProjectConfiguration<File> config,
99        final StoredExceptionCodesReport reportData) throws Exception
100   {
101     final List<ExceptionCodeReportItem> items = reportData.getItems();
102     for (final ExceptionCodeReportItem codeContainer : items)
103     {
104       writeReportToFile(output, config, codeContainer);
105     }
106   }
107 
108   private void writeReportToFile(final File output,
109       final ProjectConfiguration<File> config,
110       final ExceptionCodeReportItem codeContainer)
111     throws MojoExecutionException, FileNotFoundException, Exception
112   {
113     final File file = createFile(output, codeContainer);
114 
115     if (LOG.isDebugEnabled())
116     {
117       LOG.debug("Creating report file '" + file.getAbsolutePath() + "'.");
118     }
119     OutputStream out = null;
120     try
121     {
122       out = new BufferedOutputStream(new FileOutputStream(file));
123       writeReportElementInfo(out, config, codeContainer);
124     }
125     finally
126     {
127       IOUtils.closeQuietly(out);
128     }
129   }
130 
131   private static File createFile(final File baseDir,
132       final ExceptionCodeReportItem codeContainer)
133     throws MojoExecutionException
134   {
135     final Code code = codeContainer.getCode();
136     final String pathName =
137         MojoIoUtils.normalizeFileName(code.getComponentId());
138     final String fileName =
139         MojoIoUtils.normalizeFileName(code.getCode()) + ".xml";
140     final File dir = new File(baseDir, pathName);
141     MojoIoUtils.provideMojoDirectory(dir);
142     final File file = new File(dir, fileName);
143     return file;
144   }
145 
146   protected void writeReportElementInfo(final OutputStream reportOut,
147       final ProjectConfiguration<File> config,
148       final ExceptionCodeReportItem codeContainer) throws Exception
149   {
150     final Code codeInstance = codeContainer.getCode();
151     final String codeRepresentation = String.valueOf(codeInstance);
152 
153     final Document document = createDocument();
154     final SdocCodeBuilder builder =
155         new SdocCodeBuilder(this.renderer, config, document, codeRepresentation, codeContainer);
156     builder.writeDocumentContent();
157     MojoIoUtils.serialize(document, reportOut, true);
158   }
159 
160   private Document createDocument()
161   {
162     final Document document = builder.newDocument();
163     return document;
164   }
165 
166   // --- object basics --------------------------------------------------------
167 
168 }