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.StringUtils;
028import org.codehaus.plexus.util.cli.StreamConsumer;
029import org.slf4j.Logger;
030import org.slf4j.LoggerFactory;
031
032/**
033 * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
034 *
035 */
036public abstract class AbstractConsumer implements StreamConsumer {
037    protected final Logger logger = LoggerFactory.getLogger(getClass());
038
039    /**
040     * Converts the date timestamp from the output into a date object.
041     *
042     * @param date TODO
043     * @param userPattern TODO
044     * @param defaultPattern TODO
045     * @return A date representing the timestamp of the log entry.
046     */
047    protected Date parseDate(String date, String userPattern, String defaultPattern) {
048        return parseDate(date, userPattern, defaultPattern, null);
049    }
050
051    /**
052     * Converts the date timestamp from the output into a date object.
053     *
054     * @param date TODO
055     * @param userPattern TODO
056     * @param defaultPattern TODO
057     * @param locale TODO
058     * @return A date representing the timestamp of the log entry.
059     */
060    protected Date parseDate(String date, String userPattern, String defaultPattern, Locale locale) {
061        DateFormat format;
062
063        String patternUsed = null;
064        Locale localeUsed = null;
065
066        if (StringUtils.isNotEmpty(userPattern)) {
067            if (locale != null) {
068                format = new SimpleDateFormat(userPattern, locale);
069                localeUsed = locale;
070            } else {
071                format = new SimpleDateFormat(userPattern);
072                localeUsed = Locale.getDefault();
073            }
074            patternUsed = userPattern;
075        } else {
076            if (StringUtils.isNotEmpty(defaultPattern)) {
077                if (locale != null) {
078                    format = new SimpleDateFormat(defaultPattern, locale);
079                    localeUsed = locale;
080                } else {
081                    format = new SimpleDateFormat(defaultPattern);
082                    localeUsed = Locale.getDefault();
083                }
084                patternUsed = defaultPattern;
085            } else {
086                // Use the English short date pattern if no pattern is specified
087                format = DateFormat.getDateInstance(DateFormat.SHORT, Locale.ENGLISH);
088                patternUsed = "DateFormat.SHORT";
089                localeUsed = Locale.ENGLISH;
090            }
091        }
092
093        try {
094            return format.parse(date);
095        } catch (ParseException e) {
096            if (logger.isWarnEnabled()) {
097                logger.warn(
098                        "skip ParseException: " + e.getMessage() + " during parsing date '" + date
099                                + "' with pattern '" + patternUsed + "' and locale '"
100                                + localeUsed + "'",
101                        e);
102            }
103
104            return null;
105        }
106    }
107}