View Javadoc

1   /*
2    * Copyright 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.config.transfer.templatestream;
17  
18  import java.io.BufferedOutputStream;
19  import java.io.File;
20  import java.io.IOException;
21  import java.io.OutputStreamWriter;
22  import java.io.PrintWriter;
23  
24  import org.apache.commons.io.FileUtils;
25  
26  import de.smartics.properties.api.config.transfer.PropertySinkFactory;
27  import de.smartics.properties.spi.config.transfer.TransferException;
28  
29  /**
30   * Creates instances of {@link StreamPropertySink}.
31   */
32  public class Factory implements PropertySinkFactory<StreamPropertySink>
33  {
34    // ********************************* Fields *********************************
35  
36    // --- constants ------------------------------------------------------------
37  
38    // --- members --------------------------------------------------------------
39  
40    /**
41     * The wrapper for the whole document to be written to the stream.
42     */
43    private final Wrapper documentWrapper = new Wrapper();
44  
45    /**
46     * The wrapper for property provider output.
47     */
48    private final Wrapper lineWrapper = new Wrapper();
49  
50    /**
51     * The template to apply on each property.
52     */
53    private String template;
54  
55    /**
56     * The descriptor for table and column names to store property values.
57     */
58    private final TableDescriptor.Builder builder = new TableDescriptor.Builder();
59  
60    /**
61     * The stream to write to.
62     */
63    private PrintWriter stream;
64  
65    /**
66     * The file to write to. If the file is given, the stream is not used.
67     */
68    private File file;
69  
70    /**
71     * The encoding to use to write to the file. Defaults to UTF-8.
72     */
73    private String encoding = "UTF-8";
74  
75    /**
76     * The escaper for config key, property name and value.
77     */
78    private ValueEscaper escaper;
79  
80    // ****************************** Initializer *******************************
81  
82    // ****************************** Constructors ******************************
83  
84    /**
85     * Default constructor.
86     */
87    public Factory()
88    {
89    }
90  
91    // ****************************** Inner Classes *****************************
92  
93    // ********************************* Methods ********************************
94  
95    // --- init -----------------------------------------------------------------
96  
97    // --- get&set --------------------------------------------------------------
98  
99    /**
100    * Sets the introduction to write to the stream at the start.
101    *
102    * @param intro the introduction to write to the stream at the start.
103    */
104   public final void setDocumentIntro(final String intro)
105   {
106     this.documentWrapper.setIntro(intro);
107   }
108 
109   /**
110    * Sets the text to write to the stream at the end before closing the stream.
111    *
112    * @param extro the text to write to the stream at the end before closing the
113    *          stream.
114    */
115   public final void setDocumentExtro(final String extro)
116   {
117     this.documentWrapper.setExtro(extro);
118   }
119 
120   /**
121    * Sets the introduction to write before each property provider.
122    *
123    * @param intro the introduction to write before each property provider.
124    */
125   public final void setLineIntro(final String intro)
126   {
127     this.lineWrapper.setIntro(intro);
128   }
129 
130   /**
131    * Sets the text to write after each property provider.
132    *
133    * @param extro the text to write after each property provider.
134    */
135   public final void setLineExtro(final String extro)
136   {
137     this.lineWrapper.setExtro(extro);
138   }
139 
140   /**
141    * Sets the template to apply on each property.
142    *
143    * @param template the template to apply on each property.
144    */
145   public final void setTemplate(final String template)
146   {
147     this.template = template;
148   }
149 
150   /**
151    * Sets the name of the configuration table.
152    *
153    * @param table the name of the configuration table.
154    * @throws NullPointerException if {@code table} is <code>null</code>.
155    * @throws IllegalArgumentException if {@code table} is blank.
156    */
157   public final void setTable(final String table) throws NullPointerException,
158     IllegalArgumentException
159   {
160     this.builder.setTable(table);
161   }
162 
163   /**
164    * Sets the name of the optional configuration key column.
165    *
166    * @param configColumn the name of the optional configuration key column.
167    * @throws NullPointerException if {@code configColumn} is <code>null</code> .
168    * @throws IllegalArgumentException if {@code configColumn} is blank.
169    */
170   public final void setConfigColumn(final String configColumn)
171     throws NullPointerException, IllegalArgumentException
172   {
173     this.builder.setConfigColumn(configColumn);
174   }
175 
176   /**
177    * Sets the name of the property name column.
178    *
179    * @param nameColumn the name of the property name column.
180    * @throws NullPointerException if {@code nameColumn} is <code>null</code>.
181    * @throws IllegalArgumentException if {@code nameColumn} is blank.
182    */
183   public final void setNameColumn(final String nameColumn)
184     throws NullPointerException, IllegalArgumentException
185   {
186     this.builder.setNameColumn(nameColumn);
187   }
188 
189   /**
190    * Sets the name of the property value column.
191    *
192    * @param valueColumn the name of the property value column.
193    * @throws NullPointerException if {@code valueColumn} is <code>null</code>.
194    * @throws IllegalArgumentException if {@code valueColumn} is blank.
195    */
196   public final void setValueColumn(final String valueColumn)
197     throws NullPointerException, IllegalArgumentException
198   {
199     this.builder.setValueColumn(valueColumn);
200   }
201 
202   /**
203    * Sets the stream to write to.
204    *
205    * @param stream the stream to write to.
206    */
207   public final void setStream(final PrintWriter stream)
208   {
209     this.stream = stream;
210   }
211 
212   /**
213    * Sets the file to write to.
214    *
215    * @param fileName the name of the file to write to.
216    */
217   public final void setFile(final String fileName)
218   {
219     final File file = new File(fileName);
220     this.file = file;
221   }
222 
223   /**
224    * Sets the encoding to use to write to the file. Defaults to UTF-8.
225    *
226    * @param encoding the encoding to use to write to the file.
227    */
228   public final void setEncoding(final String encoding)
229   {
230     this.encoding = encoding;
231   }
232 
233   /**
234    * Returns the encoding to use to write to the file. Defaults to UTF-8.
235    *
236    * @return the encoding to use to write to the file.
237    */
238   public final String getEncoding()
239   {
240     return encoding;
241   }
242 
243   /**
244    * Sets the escaper for config key, property name and value.
245    *
246    * @param escaper the escaper for config key, property name and value.
247    */
248   protected final void setEscaper(final ValueEscaper escaper)
249   {
250     this.escaper = escaper;
251   }
252 
253   /**
254    * Sets the type of the escaper for config key, property name and value.
255    *
256    * @param type the type of the escaper for config key, property name and
257    *          value.
258    * @throws TransferException if the type cannot be instantiated.
259    */
260   @SuppressWarnings("unchecked")
261   public final void setEscaperType(final String type) throws TransferException
262   {
263     try
264     {
265       final Class<? extends ValueEscaper> escaperType =
266           (Class<? extends ValueEscaper>) Class.forName(type);
267       final ValueEscaper escaper = escaperType.newInstance();
268       setEscaper(escaper);
269     }
270     catch (final Exception e)
271     {
272       throw new TransferException(
273           new InvalidEscaperTransferMessageBean(e, type));
274     }
275   }
276 
277   // --- business -------------------------------------------------------------
278 
279   /**
280    * {@inheritDoc}
281    *
282    * @throws TransferException on any problem opening the stream.
283    */
284   @Override
285   public StreamPropertySink create() throws TransferException
286   {
287     PrintWriter stream = this.stream;
288     if (file != null)
289     {
290       try
291       {
292         stream =
293             new PrintWriter(new OutputStreamWriter(new BufferedOutputStream(
294                 FileUtils.openOutputStream(file)), encoding));
295       }
296       catch (final IOException e)
297       {
298         throw new TransferException(new CannotWriteToStreamTransferMessageBean(
299             e, file));
300       }
301     }
302 
303     final TableDescriptor descriptor = builder.build();
304     return new StreamPropertySink(descriptor, documentWrapper, lineWrapper,
305         template, stream, escaper);
306   }
307 
308   // --- object basics --------------------------------------------------------
309 
310 }