1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
46
47 public class SdocCodeReportGenerator extends AbstractReportGenerator<File>
48 {
49
50
51
52
53
54
55
56
57 private static final Log LOG = LogFactory
58 .getLog(SdocCodeReportGenerator.class);
59
60
61
62
63
64
65 private final DocumentBuilder builder;
66
67
68
69
70
71
72
73
74
75
76
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
87
88
89
90
91
92
93
94
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
167
168 }