1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.properties.config.transfer.templatestream;
17
18 import java.io.PrintWriter;
19
20 import org.apache.commons.lang.StringUtils;
21
22 import de.smartics.properties.api.config.domain.Property;
23 import de.smartics.properties.api.config.domain.PropertyProvider;
24 import de.smartics.properties.api.config.domain.key.ConfigurationKey;
25 import de.smartics.properties.api.config.transfer.PropertySink;
26 import de.smartics.properties.api.config.transfer.TransferException;
27 import de.smartics.util.lang.Arg;
28
29
30
31
32 public final class StreamPropertySink implements PropertySink
33 {
34
35
36
37
38
39
40
41
42
43 private final Wrapper documentWrapper;
44
45
46
47
48 private final Wrapper lineWrapper;
49
50
51
52
53 private final String template;
54
55
56
57
58
59 private final PrintWriter stream;
60
61
62
63
64 private final ValueEscaper escaper;
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83 public StreamPropertySink(final String template, final PrintWriter stream,
84 final ValueEscaper escaper) throws NullPointerException,
85 IllegalArgumentException
86 {
87 this(null, template, stream, escaper);
88 }
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105 public StreamPropertySink(final TableDescriptor descriptor,
106 final String template, final PrintWriter stream,
107 final ValueEscaper escaper) throws NullPointerException,
108 IllegalArgumentException
109 {
110 this(descriptor, null, null, template, stream, escaper);
111 }
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132 public StreamPropertySink(final TableDescriptor descriptor,
133 final Wrapper documentWrapper, final Wrapper lineWrapper,
134 final String template, final PrintWriter stream,
135 final ValueEscaper escaper) throws NullPointerException,
136 IllegalArgumentException
137 {
138 final TableDescriptor desc = createDescriptor(descriptor);
139 this.documentWrapper = createWrapper(documentWrapper);
140 this.lineWrapper = createWrapper(lineWrapper);
141 this.template =
142 applyStandardAndDescriptor(desc,
143 Arg.checkNotBlank("template", template));
144 this.stream = Arg.checkNotNull("stream", stream);
145 this.escaper = Arg.checkNotNull("escaper", escaper);
146 }
147
148
149
150
151
152
153
154 private static Wrapper createWrapper(final Wrapper wrapper)
155 {
156 final Wrapper norm = wrapper == null ? new Wrapper() : wrapper;
157
158 norm.setIntro(applyStandard(norm.getIntro()));
159 norm.setExtro(applyStandard(norm.getExtro()));
160
161 return norm;
162 }
163
164 private static TableDescriptor createDescriptor(
165 final TableDescriptor descriptor)
166 {
167 if (descriptor != null)
168 {
169 return descriptor;
170 }
171 return new TableDescriptor.Builder().build();
172 }
173
174 private static String applyStandard(final String template)
175 {
176 final String applied =
177 StringUtils.replaceEach(template, new String[] { "${newline}" },
178 new String[] { "\n" });
179 return applied;
180 }
181
182 private static String applyStandardAndDescriptor(
183 final TableDescriptor descriptor, final String template)
184 {
185 final String applied =
186 StringUtils.replaceEach(
187 template,
188 new String[] { "${newline}", "${table}", "${configColumn}",
189 "${nameColumn}", "${valueColumn}" },
190 new String[] { "\n", descriptor.getTable(),
191 descriptor.getConfigColumn(),
192 descriptor.getNameColumn(),
193 descriptor.getValueColumn() });
194 return applied;
195 }
196
197
198
199
200
201
202
203
204
205
206
207 @Override
208 public void clear() throws TransferException
209 {
210 }
211
212 @Override
213 public void write(final PropertyProvider provider) throws TransferException
214 {
215 writeDocumentIntro();
216 writeOne(provider);
217 writeDocumentExtro();
218 }
219
220 private void writeDocumentIntro()
221 {
222 final String intro = documentWrapper.getIntro();
223 if (intro != null)
224 {
225 stream.write(intro);
226 }
227 }
228
229 private void writeDocumentExtro()
230 {
231 final String extro = documentWrapper.getExtro();
232 if (extro != null)
233 {
234 stream.write(extro);
235 }
236 }
237
238 private void writeOne(final PropertyProvider provider)
239 {
240 final String intro = lineWrapper.getIntro();
241 if (intro != null)
242 {
243 final String location = String.valueOf(provider.getSourceId());
244 final String line =
245 StringUtils.replaceEach(intro, new String[] { "${location}" },
246 new String[] { location });
247 stream.write(line);
248 }
249
250 final ConfigurationKey<?> configKey = provider.getConfigurationKey();
251 final String key = escaper.escapeConfigKey(configKey.toString());
252
253 for (final Property property : provider.getProperties())
254 {
255 final String line = applyTemplate(key, property);
256 stream.write(line);
257 }
258
259 final String extro = lineWrapper.getExtro();
260 if (extro != null)
261 {
262 stream.write(extro);
263 }
264 }
265
266 private String applyTemplate(final String escapedKey, final Property property)
267 {
268 final String name = escaper.escapeName(property.getName());
269 final String value = escaper.escapeValue(property.getValue());
270 final String line =
271 StringUtils.replaceEach(template,
272 new String[] { "${configKey}", "${name}", "${value}" },
273 new String[] { escapedKey, name, value });
274 return line;
275 }
276
277 @Override
278 public void write(final Iterable<PropertyProvider> providers)
279 throws TransferException
280 {
281 writeDocumentIntro();
282
283 for (final PropertyProvider provider : providers)
284 {
285 writeOne(provider);
286 }
287
288 writeDocumentExtro();
289 }
290
291
292
293
294
295
296
297 @Override
298 public void close() throws TransferException
299 {
300 stream.close();
301 }
302
303
304
305 }