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 (isNotNul(delimiter)) {
042            csvFormat = csvFormat.withDelimiter(delimiter);
043        }
044        if (isNotNul(escape)) {
045            csvFormat = csvFormat.withEscape(escape);
046        }
047        if (isNotNul(quote)) {
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 static boolean isNotNul(final Character character) {
063        return character != null && character.charValue() != 0;
064    }
065
066    private final CSVFormat format;
067
068    protected AbstractCsvLayout(final Configuration config, final Charset charset, final CSVFormat csvFormat,
069            final String header, final String footer) {
070        super(config, charset,
071                PatternLayout.newSerializerBuilder().setConfiguration(config).setPattern(header).build(),
072                PatternLayout.newSerializerBuilder().setConfiguration(config).setPattern(footer).build());
073        this.format = csvFormat;
074    }
075
076    @Override
077    public String getContentType() {
078        return CONTENT_TYPE + "; charset=" + this.getCharset();
079    }
080
081    public CSVFormat getFormat() {
082        return format;
083    }
084}