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.util.AbstractConsumer;
24  
25  import java.text.ParseException;
26  import java.text.SimpleDateFormat;
27  import java.util.ArrayList;
28  import java.util.Date;
29  import java.util.List;
30  import java.util.TimeZone;
31  import java.util.regex.Matcher;
32  import java.util.regex.Pattern;
33  
34  /**
35   * @author Evgeny Mandrikov
36   * @author Olivier Lamy
37   * @since 1.4
38   */
39  public class SvnBlameConsumer
40      extends AbstractConsumer
41  {
42      private static final String SVN_TIMESTAMP_PATTERN = "yyyy-MM-dd HH:mm:ss";
43  
44      private static final Pattern LINE_PATTERN = Pattern.compile( "line-number=\"(.*)\"" );
45  
46      private static final Pattern REVISION_PATTERN = Pattern.compile( "revision=\"(.*)\"" );
47  
48      private static final Pattern AUTHOR_PATTERN = Pattern.compile( "<author>(.*)</author>" );
49  
50      private static final Pattern DATE_PATTERN = Pattern.compile( "<date>(.*)T(.*)\\.(.*)Z</date>" );
51  
52  
53      private final SimpleDateFormat dateFormat;
54  
55      private final List<BlameLine> lines = new ArrayList<>();
56  
57      public SvnBlameConsumer()
58      {
59          dateFormat = new SimpleDateFormat( SVN_TIMESTAMP_PATTERN );
60          dateFormat.setTimeZone( TimeZone.getTimeZone( "UTC" ) );
61      }
62  
63      private int lineNumber;
64  
65      private String revision;
66  
67      private String author;
68  
69      public void consumeLine( String line )
70      {
71          Matcher matcher;
72          if ( ( matcher = LINE_PATTERN.matcher( line ) ).find() )
73          {
74              String lineNumberStr = matcher.group( 1 );
75              lineNumber = Integer.parseInt( lineNumberStr );
76          }
77          else if ( ( matcher = REVISION_PATTERN.matcher( line ) ).find() )
78          {
79              revision = matcher.group( 1 );
80          }
81          else if ( ( matcher = AUTHOR_PATTERN.matcher( line ) ).find() )
82          {
83              author = matcher.group( 1 );
84          }
85          else if ( ( matcher = DATE_PATTERN.matcher( line ) ).find() )
86          {
87              String date = matcher.group( 1 );
88              String time = matcher.group( 2 );
89              Date dateTime = parseDateTime( date + " " + time );
90              lines.add( new BlameLine( dateTime, revision, author ) );
91              if ( logger.isDebugEnabled() )
92              {
93                  logger.debug( "Author of line " + lineNumber + ": " + author + " (" + date + ")" );
94              }
95          }
96      }
97  
98      protected Date parseDateTime( String dateTimeStr )
99      {
100         try
101         {
102             return dateFormat.parse( dateTimeStr );
103         }
104         catch ( ParseException e )
105         {
106             logger.error( "skip ParseException: " + e.getMessage() + " during parsing date " + dateTimeStr, e );
107             return null;
108         }
109     }
110 
111     public List<BlameLine> getLines()
112     {
113         return lines;
114     }
115 }