001package org.apache.maven.scm.provider.jazz.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.apache.maven.scm.provider.ScmProviderRepository;
025import org.apache.maven.scm.provider.jazz.command.consumer.AbstractRepositoryConsumer;
026import org.apache.regexp.RE;
027import org.apache.regexp.RESyntaxException;
028
029import java.text.SimpleDateFormat;
030import java.util.ArrayList;
031import java.util.Date;
032import java.util.List;
033import java.util.TimeZone;
034
035//STATUS: NOT DONE
036
037/**
038 * Consume the output of the scm command for the "blame" operation.
039 *
040 * @author <a href="mailto:ChrisGWarp@gmail.com">Chris Graham</a>
041 */
042public class JazzBlameConsumer
043    extends AbstractRepositoryConsumer
044{
045    private static final String JAZZ_TIMESTAMP_PATTERN = "yyyy-MM-dd";
046
047//  1 Deb (1008) 2011-12-14                       Test.txt
048//  2 Deb (1005) 2011-12-14 59 My commit comment.
049
050    private static final String LINE_PATTERN = "(\\d+) (.*) \\((\\d+)\\) (\\d+-\\d+-\\d+) (.*)";
051
052    /**
053     * @see #LINE_PATTERN
054     */
055    private RE lineRegexp;
056
057    private List<BlameLine> fLines = new ArrayList<BlameLine>();
058
059    private SimpleDateFormat dateFormat;
060
061    /**
062     * Construct the JazzBlameCommand consumer.
063     *
064     * @param repository The repository we are working with.
065     * @param logger     The logger to use.
066     */
067    public JazzBlameConsumer( ScmProviderRepository repository, ScmLogger logger )
068    {
069        super( repository, logger );
070
071        dateFormat = new SimpleDateFormat( JAZZ_TIMESTAMP_PATTERN );
072        dateFormat.setTimeZone( TimeZone.getTimeZone( "UTC" ) );
073
074        try
075        {
076            lineRegexp = new RE( LINE_PATTERN );
077        }
078        catch ( RESyntaxException ex )
079        {
080            throw new RuntimeException(
081                "INTERNAL ERROR: Could not create regexp to parse jazz scm blame output. This shouldn't happen. Something is probably wrong with the oro installation.",
082                ex );
083        }
084    }
085
086    /**
087     * Process one line of output from the execution of the "scm annotate" command.
088     *
089     * @param line The line of output from the external command that has been pumped to us.
090     * @see org.codehaus.plexus.util.cli.StreamConsumer#consumeLine(java.lang.String)
091     */
092    public void consumeLine( String line )
093    {
094        super.consumeLine( line );
095
096        if ( lineRegexp.match( line ) )
097        {
098            String lineNumberStr = lineRegexp.getParen( 1 );
099            String owner = lineRegexp.getParen( 2 );
100            String changeSetNumberStr = lineRegexp.getParen( 3 );
101            String dateStr = lineRegexp.getParen( 4 );
102            Date date = parseDate( dateStr, JAZZ_TIMESTAMP_PATTERN, null );
103            fLines.add( new BlameLine( date, changeSetNumberStr, owner ) );
104        }
105    }
106
107    public List<BlameLine> getLines()
108    {
109        return fLines;
110    }
111}