001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.maven.scm.util;
020
021import java.text.DateFormat;
022import java.text.ParseException;
023import java.text.SimpleDateFormat;
024import java.util.Date;
025import java.util.Locale;
026
027import org.codehaus.plexus.util.cli.StreamConsumer;
028import org.slf4j.Logger;
029import org.slf4j.LoggerFactory;
030
031/**
032 * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
033 */
034public abstract class AbstractConsumer implements StreamConsumer {
035    protected final Logger logger = LoggerFactory.getLogger(getClass());
036
037    /**
038     * Converts the date timestamp from the output into a date object.
039     *
040     * @param date TODO
041     * @param userPattern TODO
042     * @param defaultPattern TODO
043     * @return a date representing the timestamp of the log entry
044     */
045    protected Date parseDate(String date, String userPattern, String defaultPattern) {
046        return parseDate(date, userPattern, defaultPattern, null);
047    }
048
049    /**
050     * Converts the date timestamp from the output into a date object.
051     *
052     * @param date TODO
053     * @param userPattern TODO
054     * @param defaultPattern TODO
055     * @param locale TODO
056     * @return a date representing the timestamp of the log entry
057     */
058    protected Date parseDate(String date, String userPattern, String defaultPattern, Locale locale) {
059        DateFormat format;
060
061        String patternUsed = null;
062        Locale localeUsed = null;
063
064        if (userPattern != null && !userPattern.isEmpty()) {
065            if (locale != null) {
066                format = new SimpleDateFormat(userPattern, locale);
067                localeUsed = locale;
068            } else {
069                format = new SimpleDateFormat(userPattern);
070                localeUsed = Locale.getDefault();
071            }
072            patternUsed = userPattern;
073        } else {
074            if (defaultPattern != null && !defaultPattern.isEmpty()) {
075                if (locale != null) {
076                    format = new SimpleDateFormat(defaultPattern, locale);
077                    localeUsed = locale;
078                } else {
079                    format = new SimpleDateFormat(defaultPattern);
080                    localeUsed = Locale.getDefault();
081                }
082                patternUsed = defaultPattern;
083            } else {
084                // Use the English short date pattern if no pattern is specified
085                format = DateFormat.getDateInstance(DateFormat.SHORT, Locale.ENGLISH);
086                patternUsed = "DateFormat.SHORT";
087                localeUsed = Locale.ENGLISH;
088            }
089        }
090
091        try {
092            return format.parse(date);
093        } catch (ParseException e) {
094            if (logger.isWarnEnabled()) {
095                logger.warn(
096                        "skip ParseException: " + e.getMessage() + " during parsing date '" + date
097                                + "' with pattern '" + patternUsed + "' and locale '"
098                                + localeUsed + "'",
099                        e);
100            }
101
102            return null;
103        }
104    }
105}