View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.core.layout;
18  
19  import java.nio.charset.Charset;
20  
21  import org.apache.commons.csv.CSVFormat;
22  import org.apache.commons.csv.QuoteMode;
23  import org.apache.logging.log4j.core.config.Configuration;
24  
25  /**
26   * A superclass for Comma-Separated Value (CSV) layouts.
27   *
28   * Depends on Apache Commons CSV 1.2.
29   *
30   * @since 2.4
31   */
32  public abstract class AbstractCsvLayout extends AbstractStringLayout {
33  
34      protected static final String DEFAULT_CHARSET = "UTF-8";
35      protected static final String DEFAULT_FORMAT = "Default";
36      private static final String CONTENT_TYPE = "text/csv";
37  
38      protected static CSVFormat createFormat(final String format, final Character delimiter, final Character escape,
39              final Character quote, final QuoteMode quoteMode, final String nullString, final String recordSeparator) {
40          CSVFormat csvFormat = CSVFormat.valueOf(format);
41          if (isNotNul(delimiter)) {
42              csvFormat = csvFormat.withDelimiter(delimiter);
43          }
44          if (isNotNul(escape)) {
45              csvFormat = csvFormat.withEscape(escape);
46          }
47          if (isNotNul(quote)) {
48              csvFormat = csvFormat.withQuote(quote);
49          }
50          if (quoteMode != null) {
51              csvFormat = csvFormat.withQuoteMode(quoteMode);
52          }
53          if (nullString != null) {
54              csvFormat = csvFormat.withNullString(nullString);
55          }
56          if (recordSeparator != null) {
57              csvFormat = csvFormat.withRecordSeparator(recordSeparator);
58          }
59          return csvFormat;
60      }
61  
62      private static boolean isNotNul(final Character character) {
63          return character != null && character.charValue() != 0;
64      }
65  
66      private final CSVFormat format;
67  
68      protected AbstractCsvLayout(final Configuration config, final Charset charset, final CSVFormat csvFormat,
69              final String header, final String footer) {
70          super(config, charset,
71                  PatternLayout.newSerializerBuilder().setConfiguration(config).setPattern(header).build(),
72                  PatternLayout.newSerializerBuilder().setConfiguration(config).setPattern(footer).build());
73          this.format = csvFormat;
74      }
75  
76      @Override
77      public String getContentType() {
78          return CONTENT_TYPE + "; charset=" + this.getCharset();
79      }
80  
81      public CSVFormat getFormat() {
82          return format;
83      }
84  }