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 * @since 1.6
037 */
038public class IntegrityBlameConsumer
039    implements StreamConsumer
040{
041    private ScmLogger logger;
042
043    private List<BlameLine> blameList;
044
045    private SimpleDateFormat dateFormat;
046
047    /**
048     * IntegrityBlameConsumer constructor requires a ScmLogger to log all the activity
049     *
050     * @param logger ScmLogger object
051     */
052    public IntegrityBlameConsumer( ScmLogger logger )
053    {
054        this.logger = logger;
055        this.blameList = new ArrayList<BlameLine>();
056        this.dateFormat = new SimpleDateFormat( "MMM dd, yyyy z" );
057    }
058
059    /**
060     * {@inheritDoc}
061     */
062    public void consumeLine( String line )
063    {
064        // Parse the annotate output which should return the three pieces of data
065        logger.debug( line );
066        if ( null != line && line.trim().length() > 0 )
067        {
068            String[] tokens = StringUtils.split( line, "\t" );
069            if ( tokens.length != 3 )
070            {
071                logger.warn( "Failed to parse line: " + line );
072            }
073            else
074            {
075                try
076                {
077                    blameList.add( new BlameLine( dateFormat.parse( tokens[0] ), tokens[1], tokens[2] ) );
078                }
079                catch ( ParseException e )
080                {
081                    logger.error( "Failed to date string: " + tokens[0] );
082                }
083            }
084        }
085    }
086
087    /**
088     * Returns a list of BlameLine objects found from parsing the 'si annotate' output
089     *
090     * @return
091     */
092    public List<BlameLine> getBlameList()
093    {
094        return blameList;
095    }
096}