Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
XmlPropertyReport |
|
|
2.0;2 |
1 | /* |
|
2 | * Copyright 2012-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.properties.reports; |
|
17 | ||
18 | import java.io.IOException; |
|
19 | import java.io.OutputStream; |
|
20 | ||
21 | import org.apache.commons.io.IOUtils; |
|
22 | ||
23 | import de.smartics.properties.report.app.ReportException; |
|
24 | import de.smartics.properties.report.data.PropertyReportItem; |
|
25 | import de.smartics.properties.report.data.PropertyReportSet; |
|
26 | import de.smartics.util.io.StreamHandler; |
|
27 | import de.smartics.util.lang.Arg; |
|
28 | ||
29 | /** |
|
30 | * Writes each report to a file. |
|
31 | */ |
|
32 | public final class XmlPropertyReport extends AbstractPropertyReport |
|
33 | { |
|
34 | // ********************************* Fields ********************************* |
|
35 | ||
36 | // --- constants ------------------------------------------------------------ |
|
37 | ||
38 | // --- members -------------------------------------------------------------- |
|
39 | ||
40 | /** |
|
41 | * The encoding used to write to the stream. |
|
42 | */ |
|
43 | private final String encoding; |
|
44 | ||
45 | /** |
|
46 | * The location for property set reports being written to. |
|
47 | */ |
|
48 | private final StreamHandler propertySetReportsRootDir; |
|
49 | ||
50 | /** |
|
51 | * The location for property reports being written to. |
|
52 | */ |
|
53 | private final StreamHandler propertyReportsRootDir; |
|
54 | ||
55 | // ****************************** Initializer ******************************* |
|
56 | ||
57 | // ****************************** Constructors ****************************** |
|
58 | ||
59 | /** |
|
60 | * Constructor using UTF-8 as encoding. |
|
61 | * |
|
62 | * @param propertySetReportsRootDir the location for property set reports |
|
63 | * being written to. |
|
64 | * @param propertyReportsRootDir the location for property reports being |
|
65 | * written to. |
|
66 | * @throws NullPointerException if {@code propertySetReportsRootDir} or |
|
67 | * {@code propertyReportsRootDir} is <code>null</code>. |
|
68 | */ |
|
69 | public XmlPropertyReport(final StreamHandler propertySetReportsRootDir, |
|
70 | final StreamHandler propertyReportsRootDir) throws NullPointerException |
|
71 | { |
|
72 | 0 | this("UTF-8", propertySetReportsRootDir, propertyReportsRootDir); |
73 | 0 | } |
74 | ||
75 | /** |
|
76 | * Default constructor. |
|
77 | * |
|
78 | * @param encoding the encoding used to write to the stream. |
|
79 | * @param propertySetReportsRootDir the location for property set reports |
|
80 | * being written to. |
|
81 | * @param propertyReportsRootDir the location for property reports being |
|
82 | * written to. |
|
83 | * @throws NullPointerException if {@code encoding}, |
|
84 | * {@code propertySetReportsRootDir} or |
|
85 | * {@code propertyReportsRootDir} is <code>null</code>. |
|
86 | * @throws IllegalArgumentException if {@code encoding} is blank. |
|
87 | */ |
|
88 | public XmlPropertyReport(final String encoding, |
|
89 | final StreamHandler propertySetReportsRootDir, |
|
90 | final StreamHandler propertyReportsRootDir) throws NullPointerException, |
|
91 | IllegalArgumentException |
|
92 | 0 | { |
93 | 0 | this.encoding = Arg.checkNotBlank("encoding", encoding); |
94 | 0 | this.propertySetReportsRootDir = |
95 | Arg.checkNotNull("propertySetReportsRootDir", propertySetReportsRootDir); |
|
96 | 0 | this.propertyReportsRootDir = |
97 | Arg.checkNotNull("propertyReportsRootDir", propertyReportsRootDir); |
|
98 | 0 | } |
99 | ||
100 | // ****************************** Inner Classes ***************************** |
|
101 | ||
102 | // ********************************* Methods ******************************** |
|
103 | ||
104 | // --- init ----------------------------------------------------------------- |
|
105 | ||
106 | // --- get&set -------------------------------------------------------------- |
|
107 | ||
108 | // --- business ------------------------------------------------------------- |
|
109 | ||
110 | @Override |
|
111 | public void handle(final PropertyReportSet reportSet) throws ReportException |
|
112 | { |
|
113 | 0 | final String resourceId = reportSet.getName() + ".xml"; |
114 | 0 | OutputStream out = null; |
115 | try |
|
116 | { |
|
117 | 0 | out = propertySetReportsRootDir.openToWrite(resourceId); |
118 | 0 | final XmlPropertySetReporter reporter = |
119 | new XmlPropertySetReporter(encoding, reportSet); |
|
120 | 0 | reporter.write(out); |
121 | } |
|
122 | 0 | catch (final IOException e) |
123 | { |
|
124 | 0 | throw new ReportException("Cannot write report '" + resourceId + "'.", e); |
125 | } |
|
126 | finally |
|
127 | { |
|
128 | 0 | IOUtils.closeQuietly(out); |
129 | 0 | } |
130 | 0 | } |
131 | ||
132 | @Override |
|
133 | public void handle(final PropertyReportItem item) throws ReportException |
|
134 | { |
|
135 | 0 | super.handle(item); |
136 | ||
137 | 0 | final String resourceId = item.getName() + ".xml"; |
138 | 0 | OutputStream out = null; |
139 | try |
|
140 | { |
|
141 | 0 | out = propertyReportsRootDir.openToWrite(resourceId); |
142 | 0 | final XmlPropertyReporter reporter = |
143 | new XmlPropertyReporter(encoding, item); |
|
144 | 0 | reporter.write(out); |
145 | } |
|
146 | 0 | catch (final IOException e) |
147 | { |
|
148 | 0 | throw new ReportException("Cannot write report '" + resourceId + "'.", e); |
149 | } |
|
150 | finally |
|
151 | { |
|
152 | 0 | IOUtils.closeQuietly(out); |
153 | 0 | } |
154 | 0 | } |
155 | ||
156 | // --- object basics -------------------------------------------------------- |
|
157 | ||
158 | } |