View Javadoc
1   package org.apache.maven.scm.provider.svn.svnexe.command.blame;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.scm.command.blame.BlameLine;
23  import org.apache.maven.scm.log.ScmLogger;
24  import org.apache.maven.scm.util.AbstractConsumer;
25  
26  import java.text.ParseException;
27  import java.text.SimpleDateFormat;
28  import java.util.ArrayList;
29  import java.util.Date;
30  import java.util.List;
31  import java.util.TimeZone;
32  import java.util.regex.Matcher;
33  import java.util.regex.Pattern;
34  
35  /**
36   * @author Evgeny Mandrikov
37   * @author Olivier Lamy
38   * @since 1.4
39   */
40  public class SvnBlameConsumer
41      extends AbstractConsumer
42  {
43      private static final String SVN_TIMESTAMP_PATTERN = "yyyy-MM-dd HH:mm:ss";
44  
45      private static final Pattern LINE_PATTERN = Pattern.compile( "line-number=\"(.*)\"" );
46  
47      private static final Pattern REVISION_PATTERN = Pattern.compile( "revision=\"(.*)\"" );
48  
49      private static final Pattern AUTHOR_PATTERN = Pattern.compile( "<author>(.*)</author>" );
50  
51      private static final Pattern DATE_PATTERN = Pattern.compile( "<date>(.*)T(.*)\\.(.*)Z</date>" );
52  
53  
54      private SimpleDateFormat dateFormat;
55  
56      private List<BlameLine> lines = new ArrayList<BlameLine>();
57  
58      public SvnBlameConsumer( ScmLogger logger )
59      {
60          super( logger );
61  
62          dateFormat = new SimpleDateFormat( SVN_TIMESTAMP_PATTERN );
63          dateFormat.setTimeZone( TimeZone.getTimeZone( "UTC" ) );
64      }
65  
66      private int lineNumber;
67  
68      private String revision;
69  
70      private String author;
71  
72      public void consumeLine( String line )
73      {
74          Matcher matcher;
75          if ( ( matcher = LINE_PATTERN.matcher( line ) ).find() )
76          {
77              String lineNumberStr = matcher.group( 1 );
78              lineNumber = Integer.parseInt( lineNumberStr );
79          }
80          else if ( ( matcher = REVISION_PATTERN.matcher( line ) ).find() )
81          {
82              revision = matcher.group( 1 );
83          }
84          else if ( ( matcher = AUTHOR_PATTERN.matcher( line ) ).find() )
85          {
86              author = matcher.group( 1 );
87          }
88          else if ( ( matcher = DATE_PATTERN.matcher( line ) ).find() )
89          {
90              String date = matcher.group( 1 );
91              String time = matcher.group( 2 );
92              Date dateTime = parseDateTime( date + " " + time );
93              lines.add( new BlameLine( dateTime, revision, author ) );
94              if ( getLogger().isDebugEnabled() )
95              {
96                  getLogger().debug( "Author of line " + lineNumber + ": " + author + " (" + date + ")" );
97              }
98          }
99      }
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 }