001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.core.rolling.helper;
015
016import java.time.ZoneId;
017import java.util.Date;
018import java.util.List;
019
020import ch.qos.logback.core.CoreConstants;
021import ch.qos.logback.core.pattern.DynamicConverter;
022import ch.qos.logback.core.util.CachingDateFormatter;
023import ch.qos.logback.core.util.DatePatternToRegexUtil;
024
025/**
026 * Returns a date formatted by SimpleDateFormatter.
027 * 
028 * @author Ceki Gülcü
029 */
030public class DateTokenConverter<E> extends DynamicConverter<E> implements MonoTypedConverter {
031
032    /**
033     * The conversion word/character with which this converter is registered.
034     */
035    public final static String CONVERTER_KEY = "d";
036    public final static String AUXILIARY_TOKEN = "AUX";
037    public static final String DEFAULT_DATE_PATTERN = CoreConstants.DAILY_DATE_PATTERN;
038
039    private String datePattern;
040    private ZoneId zoneId;
041    private CachingDateFormatter cdf;
042    // is this token converter primary or auxiliary? Only the primary converter
043    // determines the rolling period
044    private boolean primary = true;
045
046    public void start() {
047        this.datePattern = getFirstOption();
048        if (this.datePattern == null) {
049            this.datePattern = DEFAULT_DATE_PATTERN;
050        }
051
052        final List<String> optionList = getOptionList();
053        if (optionList != null) {
054            for (int optionIndex = 1; optionIndex < optionList.size(); optionIndex++) {
055                String option = optionList.get(optionIndex);
056                if (AUXILIARY_TOKEN.equalsIgnoreCase(option)) {
057                    primary = false;
058                } else {
059                    zoneId = ZoneId.of(option);
060                }
061            }
062        }
063
064        cdf = new CachingDateFormatter(datePattern, zoneId);
065    }
066
067    public String convert(Date date) {
068        return cdf.format(date.getTime());
069    }
070
071    public String convert(Object o) {
072        if (o == null) {
073            throw new IllegalArgumentException("Null argument forbidden");
074        }
075        if (o instanceof Date) {
076            return convert((Date) o);
077        }
078        throw new IllegalArgumentException("Cannot convert " + o + " of type" + o.getClass().getName());
079    }
080
081    /**
082     * Return the date pattern.
083     */
084    public String getDatePattern() {
085        return datePattern;
086    }
087
088    public ZoneId getZoneId() {
089        return zoneId;
090    }
091
092    public boolean isApplicable(Object o) {
093        return (o instanceof Date);
094    }
095
096    public String toRegex() {
097        DatePatternToRegexUtil datePatternToRegexUtil = new DatePatternToRegexUtil(datePattern);
098        return datePatternToRegexUtil.toRegex();
099    }
100
101    public boolean isPrimary() {
102        return primary;
103    }
104}