1 package org.apache.maven.scm.provider.starteam.command.changelog;
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.ChangeFile;
23 import org.apache.maven.scm.ChangeSet;
24 import org.apache.maven.scm.log.ScmLogger;
25 import org.apache.maven.scm.provider.starteam.command.StarteamCommandLineUtils;
26 import org.apache.maven.scm.util.AbstractConsumer;
27
28 import java.io.File;
29 import java.text.SimpleDateFormat;
30 import java.util.ArrayList;
31 import java.util.Date;
32 import java.util.List;
33 import java.util.Locale;
34
35
36
37
38
39
40 public class StarteamChangeLogConsumer
41 extends AbstractConsumer
42 {
43 private SimpleDateFormat localFormat = new SimpleDateFormat( "", Locale.getDefault() );
44
45 private List<ChangeSet> entries = new ArrayList<ChangeSet>();
46
47 private String workingDirectory;
48
49 private String currentDir = "";
50
51
52
53
54
55
56 private static final int GET_FILE = 1;
57
58
59
60
61 private static final int GET_AUTHOR = 2;
62
63
64
65
66 private static final int GET_COMMENT = 3;
67
68
69
70
71 private static final int GET_REVISION = 4;
72
73
74
75
76
77 private static final String DIR_MARKER = "(working dir: ";
78
79
80
81
82 private static final String START_FILE = "History for: ";
83
84
85
86
87
88 private static final String END_FILE =
89 "===================================" + "==========================================";
90
91
92
93
94 private static final String START_REVISION = "----------------------------";
95
96
97
98
99 private static final String REVISION_TAG = "Branch Revision: ";
100
101
102
103
104 private static final String AUTHOR_TAG = "Author: ";
105
106
107
108
109 private static final String DATE_TAG = " Date: ";
110
111
112
113
114 private int status = GET_FILE;
115
116
117
118
119 private ChangeSet currentChange = null;
120
121
122
123
124 private ChangeFile currentFile = null;
125
126
127
128
129 private Date startDate;
130
131
132
133
134 private Date endDate;
135
136 private String userDateFormat;
137
138
139
140
141
142 public StarteamChangeLogConsumer( File workingDirectory, ScmLogger logger, Date startDate, Date endDate,
143 String userDateFormat )
144 {
145 super( logger );
146
147 this.workingDirectory = workingDirectory.getPath().replace( '\\', '/' );
148
149 this.startDate = startDate;
150
151 this.endDate = endDate;
152
153 this.userDateFormat = userDateFormat;
154
155
156
157
158
159 if ( "M/d/yy h:mm a".equals( localFormat.toLocalizedPattern() ) )
160 {
161 this.localFormat = new SimpleDateFormat( "M/d/yy h:mm:ss a z" );
162 }
163 }
164
165
166
167
168
169 public List<ChangeSet> getModifications()
170 {
171 return entries;
172 }
173
174
175
176
177
178
179 public void consumeLine( String line )
180 {
181 if ( getLogger().isDebugEnabled() )
182 {
183 getLogger().debug( line );
184 }
185
186 int pos = 0;
187
188 if ( ( pos = line.indexOf( DIR_MARKER ) ) != -1 )
189 {
190 processDirectory( line, pos );
191 return;
192 }
193
194
195
196
197
198
199 switch ( getStatus() )
200 {
201 case GET_FILE:
202 processGetFile( line );
203 break;
204 case GET_REVISION:
205 processGetRevision( line );
206 break;
207 case GET_AUTHOR:
208 processGetAuthor( line );
209 break;
210 case GET_COMMENT:
211 processGetComment( line );
212 break;
213 default:
214 throw new IllegalStateException( "Unknown state: " + status );
215 }
216 }
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231 private void addEntry( ChangeSet entry, ChangeFile file )
232 {
233
234 if ( entry.getAuthor() == null )
235 {
236 return;
237 }
238
239
240 if ( startDate != null && entry.getDate().before( startDate ) )
241 {
242 return;
243 }
244
245 if ( endDate != null && entry.getDate().after( endDate ) )
246 {
247 return;
248 }
249
250 entry.addFile( file );
251
252 entries.add( entry );
253 }
254
255 private void processDirectory( String line, int pos )
256 {
257 String dirPath = line.substring( pos + DIR_MARKER.length(), line.length() - 1 ).replace( '\\', '/' );
258 try
259 {
260 this.currentDir = StarteamCommandLineUtils.getRelativeChildDirectory( this.workingDirectory, dirPath );
261 }
262 catch ( IllegalStateException e )
263 {
264 String error = "Working and checkout directories are not on the same tree";
265
266 if ( getLogger().isErrorEnabled() )
267 {
268 getLogger().error( error );
269
270 getLogger().error( "Working directory: " + workingDirectory );
271
272 getLogger().error( "Checked out directory: " + dirPath );
273 }
274
275 throw new IllegalStateException( error );
276 }
277 }
278
279
280
281
282
283
284 private void processGetFile( String line )
285 {
286 if ( line.startsWith( START_FILE ) )
287 {
288 setCurrentChange( new ChangeSet() );
289
290 setCurrentFile(
291 new ChangeFile( this.currentDir + "/" + line.substring( START_FILE.length(), line.length() ) ) );
292
293 setStatus( GET_REVISION );
294 }
295 }
296
297
298
299
300
301
302 private void processGetRevision( String line )
303 {
304 int pos;
305
306 if ( ( pos = line.indexOf( REVISION_TAG ) ) != -1 )
307 {
308 getCurrentFile().setRevision( line.substring( pos + REVISION_TAG.length() ) );
309
310 setStatus( GET_AUTHOR );
311 }
312 else if ( line.startsWith( END_FILE ) )
313 {
314
315
316
317 setStatus( GET_FILE );
318
319 addEntry( getCurrentChange(), getCurrentFile() );
320 }
321 }
322
323
324
325
326
327
328 private void processGetAuthor( String line )
329 {
330 if ( line.startsWith( AUTHOR_TAG ) )
331 {
332 int posDateTag = line.indexOf( DATE_TAG );
333
334 String author = line.substring( AUTHOR_TAG.length(), posDateTag );
335
336 getCurrentChange().setAuthor( author );
337
338 String date = line.substring( posDateTag + DATE_TAG.length() );
339
340 Date dateObj = parseDate( date, userDateFormat, localFormat.toPattern() );
341
342 if ( dateObj != null )
343 {
344 getCurrentChange().setDate( dateObj );
345 }
346 else
347 {
348 getCurrentChange().setDate( date, userDateFormat );
349 }
350
351 setStatus( GET_COMMENT );
352 }
353 }
354
355
356
357
358
359
360 private void processGetComment( String line )
361 {
362 if ( line.startsWith( START_REVISION ) )
363 {
364
365 addEntry( getCurrentChange(), getCurrentFile() );
366
367
368 setCurrentChange( new ChangeSet() );
369
370
371 setCurrentFile( new ChangeFile( getCurrentFile().getName() ) );
372
373 setStatus( GET_REVISION );
374 }
375 else if ( line.startsWith( END_FILE ) )
376 {
377 addEntry( getCurrentChange(), getCurrentFile() );
378
379 setStatus( GET_FILE );
380 }
381 else
382 {
383
384 getCurrentChange().setComment( getCurrentChange().getComment() + line + "\n" );
385 }
386 }
387
388
389
390
391
392
393 private ChangeFile getCurrentFile()
394 {
395 return currentFile;
396 }
397
398
399
400
401
402
403 private void setCurrentFile( ChangeFile currentFile )
404 {
405 this.currentFile = currentFile;
406 }
407
408
409
410
411
412
413 private ChangeSet getCurrentChange()
414 {
415 return currentChange;
416 }
417
418
419
420
421
422
423 private void setCurrentChange( ChangeSet currentChange )
424 {
425 this.currentChange = currentChange;
426 }
427
428
429
430
431
432
433 private int getStatus()
434 {
435 return status;
436 }
437
438
439
440
441
442
443 private void setStatus( int status )
444 {
445 this.status = status;
446 }
447 }