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.Arg;
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 this.output = Arg.checkNotNull("output", output);
70 }
71
72
73
74
75
76
77
78
79
80
81
82 @Override
83 public void handle(final PropertyReportSet reportSet) throws ReportException
84 {
85 flush(reportSet.getName());
86
87 final StringBuilder buffer = new StringBuilder(256);
88 buffer.append(reportSet.getName()).append('\n')
89 .append(reportSet.getComment());
90 currentComment = buffer.toString();
91 }
92
93 private void flush(final String name) throws ReportException
94 {
95 if (!currentProperties.isEmpty())
96 {
97 try
98 {
99 currentProperties.setHeader(currentComment);
100 currentProperties.save(output, "ISO8859-1");
101 }
102 catch (final ConfigurationException e)
103 {
104 final String message =
105 name != null ? "Cannot write report set '" + name + "'."
106 : "Cannot flush.";
107 throw new ReportException(message, e);
108 }
109
110 currentProperties.clear();
111 }
112 }
113
114 @Override
115 public void handle(final PropertyReportItem item) throws ReportException
116 {
117 super.handle(item);
118
119 final PropertyDescriptor descriptor = item.getDescriptor();
120 final String key = descriptor.getKey().toString();
121
122 final PropertyExpression expression = descriptor.getDefaultExpression();
123 final String value =
124 expression != null && expression.getExpression() != null ? expression
125 .getExpression() : "";
126
127 currentProperties.setProperty(key, value);
128 }
129
130
131
132
133 public void flush()
134 {
135 flush(null);
136 }
137
138
139
140 }