1 package org.apache.maven.scm.provider.svn.svnexe.command.blame;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
37
38
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 }