1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.maven.plugin.buildmetadata.io;
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.IOException;
23 import java.io.OutputStream;
24 import java.util.List;
25 import java.util.Properties;
26
27 import javax.xml.parsers.DocumentBuilder;
28 import javax.xml.parsers.DocumentBuilderFactory;
29 import javax.xml.parsers.ParserConfigurationException;
30 import javax.xml.transform.TransformerException;
31
32 import org.apache.commons.io.IOUtils;
33 import org.apache.maven.plugin.MojoExecutionException;
34 import org.apache.maven.plugin.logging.Log;
35 import org.w3c.dom.Document;
36
37 import de.smartics.maven.io.MojoIoUtils;
38 import de.smartics.maven.plugin.buildmetadata.common.MojoUtils;
39 import de.smartics.maven.plugin.buildmetadata.common.Property;
40
41
42
43
44
45
46
47 public final class BuildXmlFileHelper
48 {
49
50
51
52
53
54
55
56
57
58 private final Log log;
59
60
61
62
63 private final File xmlOutputFile;
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85 private final List<Property> selectedProperties;
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100 public BuildXmlFileHelper(final Log log, final File xmlOutputFile,
101 final List<Property> selectedProperties)
102 {
103 this.log = log;
104 this.xmlOutputFile = xmlOutputFile;
105 this.selectedProperties = selectedProperties;
106 }
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126 public File writeXmlFile(final Properties buildMetaDataProperties)
127 throws MojoExecutionException
128 {
129 final File buildMetaDataFile = createBuildMetaDataFile(xmlOutputFile);
130 if (log.isInfoEnabled())
131 {
132 log.info("Writing XML report '" + buildMetaDataFile.getAbsolutePath()
133 + "'...");
134 }
135
136 writeContent(buildMetaDataProperties, buildMetaDataFile);
137
138 return buildMetaDataFile;
139 }
140
141 private void writeContent(final Properties buildMetaDataProperties,
142 final File buildMetaDataFile) throws MojoExecutionException
143 {
144 OutputStream out = null;
145 try
146 {
147 out = new BufferedOutputStream(new FileOutputStream(buildMetaDataFile));
148 serializeDocument(buildMetaDataProperties, out);
149 }
150 catch (final FileNotFoundException e)
151 {
152 final String message =
153 "Cannot find file '" + buildMetaDataFile
154 + "' to write XML report to.";
155 throw MojoUtils.createException(log, e, message);
156 }
157 catch (final IOException e)
158 {
159 final String message =
160 "Cannot write XML report to file '" + buildMetaDataFile + "'.";
161 throw MojoUtils.createException(log, e, message);
162 }
163 catch (final ParserConfigurationException e)
164 {
165 final String message =
166 "Cannot create XML report to write to file '" + buildMetaDataFile
167 + "'.";
168 throw MojoUtils.createException(log, e, message);
169 }
170 catch (final TransformerException e)
171 {
172 final String message =
173 "Cannot transform build meta data to XML to write to file '"
174 + buildMetaDataFile + "'.";
175 throw MojoUtils.createException(log, e, message);
176 }
177 finally
178 {
179 IOUtils.closeQuietly(out);
180 }
181 }
182
183 private void serializeDocument(final Properties buildMetaDataProperties,
184 final OutputStream out) throws ParserConfigurationException, IOException,
185 TransformerException
186 {
187 final Document document = createDocument();
188 final SdocBuilder builder =
189 new SdocBuilder(document, buildMetaDataProperties, selectedProperties);
190 builder.writeDocumentContent();
191 MojoIoUtils.serialize(document, out);
192 }
193
194
195
196
197
198
199
200
201
202 private File createBuildMetaDataFile(final File propertiesOutputFile)
203 throws MojoExecutionException
204 {
205 final File outputDirectory = propertiesOutputFile.getParentFile();
206 if (!outputDirectory.exists())
207 {
208 final boolean created = outputDirectory.mkdirs();
209 if (!created)
210 {
211 throw new MojoExecutionException("Cannot create output directory '"
212 + outputDirectory + "'.");
213 }
214 }
215 return propertiesOutputFile;
216 }
217
218 private Document createDocument() throws ParserConfigurationException
219 {
220 final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
221 final DocumentBuilder builder = factory.newDocumentBuilder();
222 final Document document = builder.newDocument();
223 return document;
224 }
225
226
227
228 }