001package org.apache.maven.scm.provider.perforce.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 org.apache.maven.scm.log.ScmLogger;
023import org.apache.maven.scm.util.AbstractConsumer;
024import org.apache.regexp.RE;
025
026import java.util.Date;
027import java.util.HashMap;
028import java.util.Map;
029
030/**
031 * @author Evgeny Mandrikov
032 * @since 1.4
033 */
034public class PerforceFilelogConsumer
035    extends AbstractConsumer
036{
037    private static final String PERFORCE_TIMESTAMP_PATTERN = "yyyy/MM/dd";
038
039    private static final String LINE_PATTERN = "#(\\d+).*on (.*) by (.*)@";
040
041    private RE lineRegexp;
042
043    private Map<String, Date> dates = new HashMap<String,Date>();
044
045    private Map<String,String> authors = new HashMap<String,String>();
046
047    public PerforceFilelogConsumer( ScmLogger logger )
048    {
049        super( logger );
050        lineRegexp = new RE( LINE_PATTERN );
051    }
052
053    /** {@inheritDoc} */
054    public void consumeLine( String line )
055    {
056        if ( lineRegexp.match( line ) )
057        {
058            String revision = lineRegexp.getParen( 1 );
059            String dateTimeStr = lineRegexp.getParen( 2 );
060            String author = lineRegexp.getParen( 3 );
061
062            Date dateTime = parseDate( dateTimeStr, null, PERFORCE_TIMESTAMP_PATTERN );
063
064            dates.put( revision, dateTime );
065            authors.put( revision, author );
066        }
067    }
068
069    public String getAuthor( String revision )
070    {
071        return (String) authors.get( revision );
072    }
073
074    public Date getDate( String revision )
075    {
076        return (Date) dates.get( revision );
077    }
078}