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 org.apache.commons.lang.StringEscapeUtils;
19  
20  /**
21   * Creates instances of {@link StreamPropertySink} by providing a template to
22   * generate a CSV document.
23   */
24  public final class CsvFactory extends Factory
25  {
26    // ********************************* Fields *********************************
27  
28    // --- constants ------------------------------------------------------------
29  
30    // --- members --------------------------------------------------------------
31  
32    /**
33     * The flag controls whether (<code>true</code>) or not (<code>false</code>)
34     * write a CSV header for columns.
35     */
36    private boolean writeHeader;
37  
38    // ****************************** Initializer *******************************
39  
40    // ****************************** Constructors ******************************
41  
42    /**
43     * Default constructor.
44     */
45    public CsvFactory()
46    {
47      setEscaper(new ValueEscaper()
48      {
49        @Override
50        public String escape(final String value)
51        {
52          return StringEscapeUtils.escapeCsv(value);
53        }
54      });
55  
56      final String template = "${configKey},${name},${value}${newline}";
57      setTemplate(template);
58    }
59  
60    // ****************************** Inner Classes *****************************
61  
62    // ********************************* Methods ********************************
63  
64    // --- init -----------------------------------------------------------------
65  
66    // --- get&set --------------------------------------------------------------
67  
68    /**
69     * Sets the flag controls whether (<code>true</code>) or not (
70     * <code>false</code>) write a CSV header for columns.
71     *
72     * @param writeHeader the flag controls whether (<code>true</code>) or not (
73     *          <code>false</code>) write a CSV header for columns.
74     */
75    public void setWriteHeader(final boolean writeHeader)
76    {
77      this.writeHeader = writeHeader;
78    }
79  
80    @Override
81    public StreamPropertySink create() throws IllegalStateException
82    {
83      if (writeHeader)
84      {
85        setDocumentIntro("config,name,value${newline}");
86      }
87  
88      return super.create();
89    }
90  
91    // --- business -------------------------------------------------------------
92  
93    // --- object basics --------------------------------------------------------
94  
95  }