001package org.apache.maven.scm.provider.svn.svnexe.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.util.AbstractConsumer;
025
026import java.text.ParseException;
027import java.text.SimpleDateFormat;
028import java.util.ArrayList;
029import java.util.Date;
030import java.util.List;
031import java.util.TimeZone;
032import java.util.regex.Matcher;
033import java.util.regex.Pattern;
034
035/**
036 * @author Evgeny Mandrikov
037 * @author Olivier Lamy
038 * @since 1.4
039 */
040public class SvnBlameConsumer
041    extends AbstractConsumer
042{
043    private static final String SVN_TIMESTAMP_PATTERN = "yyyy-MM-dd HH:mm:ss";
044
045    private static final Pattern LINE_PATTERN = Pattern.compile( "line-number=\"(.*)\"" );
046
047    private static final Pattern REVISION_PATTERN = Pattern.compile( "revision=\"(.*)\"" );
048
049    private static final Pattern AUTHOR_PATTERN = Pattern.compile( "<author>(.*)</author>" );
050
051    private static final Pattern DATE_PATTERN = Pattern.compile( "<date>(.*)T(.*)\\.(.*)Z</date>" );
052
053
054    private SimpleDateFormat dateFormat;
055
056    private List<BlameLine> lines = new ArrayList<BlameLine>();
057
058    public SvnBlameConsumer( ScmLogger logger )
059    {
060        super( logger );
061
062        dateFormat = new SimpleDateFormat( SVN_TIMESTAMP_PATTERN );
063        dateFormat.setTimeZone( TimeZone.getTimeZone( "UTC" ) );
064    }
065
066    private int lineNumber;
067
068    private String revision;
069
070    private String author;
071
072    public void consumeLine( String line )
073    {
074        Matcher matcher;
075        if ( ( matcher = LINE_PATTERN.matcher( line ) ).find() )
076        {
077            String lineNumberStr = matcher.group( 1 );
078            lineNumber = Integer.parseInt( lineNumberStr );
079        }
080        else if ( ( matcher = REVISION_PATTERN.matcher( line ) ).find() )
081        {
082            revision = matcher.group( 1 );
083        }
084        else if ( ( matcher = AUTHOR_PATTERN.matcher( line ) ).find() )
085        {
086            author = matcher.group( 1 );
087        }
088        else if ( ( matcher = DATE_PATTERN.matcher( line ) ).find() )
089        {
090            String date = matcher.group( 1 );
091            String time = matcher.group( 2 );
092            Date dateTime = parseDateTime( date + " " + time );
093            lines.add( new BlameLine( dateTime, revision, author ) );
094            if ( getLogger().isDebugEnabled() )
095            {
096                getLogger().debug( "Author of line " + lineNumber + ": " + author + " (" + date + ")" );
097            }
098        }
099    }
100
101    protected Date parseDateTime( String dateTimeStr )
102    {
103        try
104        {
105            return dateFormat.parse( dateTimeStr );
106        }
107        catch ( ParseException e )
108        {
109            getLogger().error( "skip ParseException: " + e.getMessage() + " during parsing date " + dateTimeStr, e );
110            return null;
111        }
112    }
113
114    public List<BlameLine> getLines()
115    {
116        return lines;
117    }
118}