1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
35
36
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 }