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