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  import org.apache.regexp.RE;
26  import org.apache.regexp.RESyntaxException;
27  
28  import java.text.ParseException;
29  import java.text.SimpleDateFormat;
30  import java.util.ArrayList;
31  import java.util.Date;
32  import java.util.List;
33  import java.util.TimeZone;
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 String LINE_PATTERN = "line-number=\"(.*)\"";
46  
47      private static final String REVISION_PATTERN = "revision=\"(.*)\"";
48  
49      private static final String AUTHOR_PATTERN = "<author>(.*)</author>";
50  
51      private static final String DATE_PATTERN = "<date>(.*)T(.*)\\.(.*)Z</date>";
52  
53      /**
54       * @see #LINE_PATTERN
55       */
56      private RE lineRegexp;
57  
58      /**
59       * @see #REVISION_PATTERN
60       */
61      private RE revisionRegexp;
62  
63      /**
64       * @see #AUTHOR_PATTERN
65       */
66      private RE authorRegexp;
67  
68      /**
69       * @see #DATE_PATTERN
70       */
71      private RE dateRegexp;
72  
73      private SimpleDateFormat dateFormat;
74  
75      private List<BlameLine> lines = new ArrayList<BlameLine>();
76  
77      public SvnBlameConsumer( ScmLogger logger )
78      {
79          super( logger );
80  
81          dateFormat = new SimpleDateFormat( SVN_TIMESTAMP_PATTERN );
82          dateFormat.setTimeZone( TimeZone.getTimeZone( "UTC" ) );
83  
84          try
85          {
86              lineRegexp = new RE( LINE_PATTERN );
87              revisionRegexp = new RE( REVISION_PATTERN );
88              authorRegexp = new RE( AUTHOR_PATTERN );
89              dateRegexp = new RE( DATE_PATTERN );
90          }
91          catch ( RESyntaxException ex )
92          {
93              throw new RuntimeException(
94                  "INTERNAL ERROR: Could not create regexp to parse git log file. This shouldn't happen. Something is probably wrong with the oro installation.",
95                  ex );
96          }
97      }
98  
99      private int lineNumber;
100 
101     private String revision;
102 
103     private String author;
104 
105     public void consumeLine( String line )
106     {
107         if ( lineRegexp.match( line ) )
108         {
109             String lineNumberStr = lineRegexp.getParen( 1 );
110             lineNumber = Integer.parseInt( lineNumberStr );
111         }
112         else if ( revisionRegexp.match( line ) )
113         {
114             revision = revisionRegexp.getParen( 1 );
115         }
116         else if ( authorRegexp.match( line ) )
117         {
118             author = authorRegexp.getParen( 1 );
119         }
120         else if ( dateRegexp.match( line ) )
121         {
122             String date = dateRegexp.getParen( 1 );
123             String time = dateRegexp.getParen( 2 );
124             Date dateTime = parseDateTime( date + " " + time );
125             lines.add( new BlameLine( dateTime, revision, author ) );
126             if ( getLogger().isDebugEnabled() )
127             {
128                 getLogger().debug( "Author of line " + lineNumber + ": " + author + " (" + date + ")" );
129             }
130         }
131     }
132 
133     protected Date parseDateTime( String dateTimeStr )
134     {
135         try
136         {
137             return dateFormat.parse( dateTimeStr );
138         }
139         catch ( ParseException e )
140         {
141             getLogger().error( "skip ParseException: " + e.getMessage() + " during parsing date " + dateTimeStr, e );
142             return null;
143         }
144     }
145 
146     public List<BlameLine> getLines()
147     {
148         return lines;
149     }
150 }