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 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
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 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
55
56 private RE lineRegexp;
57
58
59
60
61 private RE revisionRegexp;
62
63
64
65
66 private RE authorRegexp;
67
68
69
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 }