001package org.apache.maven.scm.provider.cvslib.command.blame;
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
022import java.util.ArrayList;
023import java.util.Date;
024import java.util.List;
025import java.util.Locale;
026import java.util.regex.Matcher;
027import java.util.regex.Pattern;
028
029import org.apache.maven.scm.command.blame.BlameLine;
030import org.apache.maven.scm.log.ScmLogger;
031import org.apache.maven.scm.util.AbstractConsumer;
032
033/**
034 * @author Evgeny Mandrikov
035 * @author Olivier Lamy
036 * @since 1.4
037 */
038public class CvsBlameConsumer
039    extends AbstractConsumer
040{
041    private static final String CVS_TIMESTAMP_PATTERN = "dd-MMM-yy";
042
043    /* 1.1          (tor      24-Mar-03): */
044
045    private static final Pattern LINE_PATTERN = Pattern.compile( "(.*)\\((.*)\\s+(.*)\\)" );
046
047    private List<BlameLine> lines = new ArrayList<BlameLine>();
048
049    public CvsBlameConsumer( ScmLogger logger )
050    {
051        super( logger );
052    }
053
054    public void consumeLine( String line )
055    {
056        if ( line != null && line.indexOf( ':' ) > 0 )
057        {
058            String annotation = line.substring( 0, line.indexOf( ':' ) );
059            Matcher matcher = LINE_PATTERN.matcher( annotation );
060            if ( matcher.matches() )
061            {
062                String revision = matcher.group( 1 ).trim();
063                String author = matcher.group( 2 ).trim();
064                String dateTimeStr = matcher.group( 3 ).trim();
065
066                Date dateTime = parseDate( dateTimeStr, null, CVS_TIMESTAMP_PATTERN, Locale.US );
067                lines.add( new BlameLine( dateTime, revision, author ) );
068
069                if ( getLogger().isDebugEnabled() )
070                {
071                    getLogger().debug( author + " " + dateTimeStr );
072                }
073            }
074        }
075    }
076
077    public List<BlameLine> getLines()
078    {
079        return lines;
080    }
081
082}