001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.log4j.core.layout;
018
019import java.nio.charset.Charset;
020
021import org.apache.commons.csv.CSVFormat;
022import org.apache.commons.csv.QuoteMode;
023import org.apache.logging.log4j.core.config.Configuration;
024
025/**
026 * A superclass for Comma-Separated Value (CSV) layouts.
027 * 
028 * Depends on Apache Commons CSV 1.2.
029 * 
030 * @since 2.4
031 */
032public abstract class AbstractCsvLayout extends AbstractStringLayout {
033
034    protected static final String DEFAULT_CHARSET = "UTF-8";
035    protected static final String DEFAULT_FORMAT = "Default";
036    private static final String CONTENT_TYPE = "text/csv";
037
038    protected static CSVFormat createFormat(final String format, final Character delimiter, final Character escape,
039            final Character quote, final QuoteMode quoteMode, final String nullString, final String recordSeparator) {
040        CSVFormat csvFormat = CSVFormat.valueOf(format);
041        if (delimiter != null) {
042            csvFormat = csvFormat.withDelimiter(delimiter);
043        }
044        if (escape != null) {
045            csvFormat = csvFormat.withEscape(escape);
046        }
047        if (quote != null) {
048            csvFormat = csvFormat.withQuote(quote);
049        }
050        if (quoteMode != null) {
051            csvFormat = csvFormat.withQuoteMode(quoteMode);
052        }
053        if (nullString != null) {
054            csvFormat = csvFormat.withNullString(nullString);
055        }
056        if (recordSeparator != null) {
057            csvFormat = csvFormat.withRecordSeparator(recordSeparator);
058        }
059        return csvFormat;
060    }
061
062    private final CSVFormat format;
063
064    protected AbstractCsvLayout(final Configuration config, final Charset charset, final CSVFormat csvFormat,
065            final String header, final String footer) {
066        super(config, charset, PatternLayout.createSerializer(config, null, header, null, null, false, false),
067                PatternLayout.createSerializer(config, null, footer, null, null, false, false));
068        this.format = csvFormat;
069    }
070
071    @Override
072    public String getContentType() {
073        return CONTENT_TYPE + "; charset=" + this.getCharset();
074    }
075
076    public CSVFormat getFormat() {
077        return format;
078    }
079}