1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
32
33 public final class PropertiesPropertyReport extends AbstractPropertyReport
34 {
35
36
37
38
39
40
41
42
43
44 private String currentComment;
45
46
47
48
49 private final PropertiesConfiguration currentProperties =
50 new PropertiesConfiguration();
51
52
53
54
55 private final OutputStream output;
56
57
58
59
60
61
62
63
64
65
66
67 public PropertiesPropertyReport(final OutputStream output)
68 {
69 Arguments.checkNotNull("output", output);
70 this.output = output;
71 }
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88 @Override
89 public void handle(final PropertyReportSet reportSet) throws ReportException
90 {
91 flush(reportSet.getName());
92
93 final StringBuilder buffer = new StringBuilder(256);
94 buffer.append(reportSet.getName()).append('\n')
95 .append(reportSet.getComment());
96 currentComment = buffer.toString();
97 }
98
99 private void flush(final String name) throws ReportException
100 {
101 if (!currentProperties.isEmpty())
102 {
103 try
104 {
105 currentProperties.setHeader(currentComment);
106 currentProperties.save(output, "ISO8859-1");
107 }
108 catch (final ConfigurationException e)
109 {
110 final String message =
111 name != null ? "Cannot write report set '" + name + "'."
112 : "Cannot flush.";
113 throw new ReportException(message, e);
114 }
115
116 currentProperties.clear();
117 }
118 }
119
120
121
122
123
124
125 @Override
126 public void handle(final PropertyReportItem item) throws ReportException
127 {
128 super.handle(item);
129
130 final PropertyDescriptor descriptor = item.getDescriptor();
131 final String key = descriptor.getKey().toString();
132
133 final PropertyExpression expression = descriptor.getDefaultExpression();
134 final String value =
135 expression != null && expression.getExpression() != null ? expression
136 .getExpression() : "";
137
138 currentProperties.setProperty(key, value);
139 }
140
141
142
143
144 public void flush()
145 {
146 flush(null);
147 }
148
149
150
151 }