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