Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
PropertiesPropertyReport |
|
|
2.2;2,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.OutputStream; |
|
19 | ||
20 | import org.apache.commons.configuration.ConfigurationException; |
|
21 | import org.apache.commons.configuration.PropertiesConfiguration; |
|
22 | ||
23 | import de.smartics.properties.api.core.domain.PropertyDescriptor; |
|
24 | import de.smartics.properties.api.core.domain.PropertyExpression; |
|
25 | import de.smartics.properties.report.app.ReportException; |
|
26 | import de.smartics.properties.report.data.PropertyReportItem; |
|
27 | import de.smartics.properties.report.data.PropertyReportSet; |
|
28 | import de.smartics.util.lang.Arguments; |
|
29 | ||
30 | /** |
|
31 | * Stores every report item in a property set. |
|
32 | */ |
|
33 | public final class PropertiesPropertyReport extends AbstractPropertyReport |
|
34 | { |
|
35 | // ********************************* Fields ********************************* |
|
36 | ||
37 | // --- constants ------------------------------------------------------------ |
|
38 | ||
39 | // --- members -------------------------------------------------------------- |
|
40 | ||
41 | /** |
|
42 | * The current comment to prepend to the collected properties. |
|
43 | */ |
|
44 | private String currentComment; |
|
45 | ||
46 | /** |
|
47 | * The currently collected properties. |
|
48 | */ |
|
49 | 0 | private final PropertiesConfiguration currentProperties = |
50 | new PropertiesConfiguration(); |
|
51 | ||
52 | /** |
|
53 | * The stream to write to. |
|
54 | */ |
|
55 | private final OutputStream output; |
|
56 | ||
57 | // ****************************** Initializer ******************************* |
|
58 | ||
59 | // ****************************** Constructors ****************************** |
|
60 | ||
61 | /** |
|
62 | * Constructor. |
|
63 | * |
|
64 | * @param output the stream to write to. |
|
65 | * @see de.smartics.properties.reports.AbstractPropertyReport#AbstractPropertyReport() |
|
66 | */ |
|
67 | public PropertiesPropertyReport(final OutputStream output) |
|
68 | 0 | { |
69 | 0 | Arguments.checkNotNull("output", output); |
70 | 0 | this.output = output; |
71 | 0 | } |
72 | ||
73 | // ****************************** Inner Classes ***************************** |
|
74 | ||
75 | // ********************************* Methods ******************************** |
|
76 | ||
77 | // --- init ----------------------------------------------------------------- |
|
78 | ||
79 | // --- get&set -------------------------------------------------------------- |
|
80 | ||
81 | // --- business ------------------------------------------------------------- |
|
82 | ||
83 | /** |
|
84 | * {@inheritDoc} |
|
85 | * |
|
86 | * @see de.smartics.properties.report.data.PropertyReport#handle(de.smartics.properties.report.data.PropertyReportSet) |
|
87 | */ |
|
88 | @Override |
|
89 | public void handle(final PropertyReportSet reportSet) throws ReportException |
|
90 | { |
|
91 | 0 | flush(reportSet.getName()); |
92 | ||
93 | 0 | final StringBuilder buffer = new StringBuilder(256); |
94 | 0 | buffer.append(reportSet.getName()).append('\n') |
95 | .append(reportSet.getComment()); |
|
96 | 0 | currentComment = buffer.toString(); |
97 | 0 | } |
98 | ||
99 | private void flush(final String name) throws ReportException |
|
100 | { |
|
101 | 0 | if (!currentProperties.isEmpty()) |
102 | { |
|
103 | try |
|
104 | { |
|
105 | 0 | currentProperties.setHeader(currentComment); |
106 | 0 | currentProperties.save(output, "ISO8859-1"); |
107 | } |
|
108 | 0 | catch (final ConfigurationException e) |
109 | { |
|
110 | 0 | final String message = |
111 | name != null ? "Cannot write report set '" + name + "'." |
|
112 | : "Cannot flush."; |
|
113 | 0 | throw new ReportException(message, e); |
114 | 0 | } |
115 | ||
116 | 0 | currentProperties.clear(); |
117 | } |
|
118 | 0 | } |
119 | ||
120 | /** |
|
121 | * {@inheritDoc} |
|
122 | * |
|
123 | * @see de.smartics.properties.report.data.PropertyReport#handle(de.smartics.properties.report.data.PropertyReportItem) |
|
124 | */ |
|
125 | @Override |
|
126 | public void handle(final PropertyReportItem item) throws ReportException |
|
127 | { |
|
128 | 0 | super.handle(item); |
129 | ||
130 | 0 | final PropertyDescriptor descriptor = item.getDescriptor(); |
131 | 0 | final String key = descriptor.getKey().toString(); |
132 | ||
133 | 0 | final PropertyExpression expression = descriptor.getDefaultExpression(); |
134 | 0 | final String value = |
135 | expression != null && expression.getExpression() != null ? expression |
|
136 | .getExpression() : ""; |
|
137 | ||
138 | 0 | currentProperties.setProperty(key, value); |
139 | 0 | } |
140 | ||
141 | /** |
|
142 | * Flushes the latest report set and items. |
|
143 | */ |
|
144 | public void flush() |
|
145 | { |
|
146 | 0 | flush(null); |
147 | 0 | } |
148 | ||
149 | // --- object basics -------------------------------------------------------- |
|
150 | ||
151 | } |