001package org.apache.maven.scm.provider.integrity.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.command.blame.BlameLine;
023import org.apache.maven.scm.log.ScmLogger;
024import org.codehaus.plexus.util.StringUtils;
025import org.codehaus.plexus.util.cli.StreamConsumer;
026
027import java.text.ParseException;
028import java.text.SimpleDateFormat;
029import java.util.ArrayList;
030import java.util.List;
031
032/**
033 * Helper class to consume the standard output from running the IntegrityBlameCommand
034 *
035 * @author <a href="mailto:cletus@mks.com">Cletus D'Souza</a>
036 * @version $Id: IntegrityBlameConsumer.java 1.2 2011/08/22 13:06:16EDT Cletus D'Souza (dsouza) Exp  $
037 * @since 1.6
038 */
039public class IntegrityBlameConsumer
040    implements StreamConsumer
041{
042    private ScmLogger logger;
043
044    private List<BlameLine> blameList;
045
046    private SimpleDateFormat dateFormat;
047
048    /**
049     * IntegrityBlameConsumer constructor requires a ScmLogger to log all the activity
050     *
051     * @param logger ScmLogger object
052     */
053    public IntegrityBlameConsumer( ScmLogger logger )
054    {
055        this.logger = logger;
056        this.blameList = new ArrayList<BlameLine>();
057        this.dateFormat = new SimpleDateFormat( "MMM dd, yyyy z" );
058    }
059
060    /**
061     * {@inheritDoc}
062     */
063    public void consumeLine( String line )
064    {
065        // Parse the annotate output which should return the three pieces of data
066        logger.debug( line );
067        if ( null != line && line.trim().length() > 0 )
068        {
069            String[] tokens = StringUtils.split( line, "\t" );
070            if ( tokens.length != 3 )
071            {
072                logger.warn( "Failed to parse line: " + line );
073            }
074            else
075            {
076                try
077                {
078                    blameList.add( new BlameLine( dateFormat.parse( tokens[0] ), tokens[1], tokens[2] ) );
079                }
080                catch ( ParseException e )
081                {
082                    logger.error( "Failed to date string: " + tokens[0] );
083                }
084            }
085        }
086    }
087
088    /**
089     * Returns a list of BlameLine objects found from parsing the 'si annotate' output
090     *
091     * @return
092     */
093    public List<BlameLine> getBlameList()
094    {
095        return blameList;
096    }
097}