maven-plugins/changelog/src/main/org/apache/maven/mkslib/MksChangeLogParser.java
2007-04-25 12:28:56 +00:00

361 lines
10 KiB
Java

package org.apache.maven.mkslib;
/* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.Map;
import java.util.TreeMap;
import org.apache.maven.changelog.ChangeLog;
import org.apache.maven.changelog.ChangeLogEntry;
import org.apache.maven.changelog.ChangeLogFile;
import org.apache.maven.changelog.ChangeLogParser;
/**
* This class parse mks log output.
*
* @author <a href="mailto:c.jerolimov@tarent.de">Christoph Jerolimov</a>
* @version $Id$
*/
public class MksChangeLogParser implements ChangeLogParser
{
/**
* This date/time formatter will be uses to parse a mks date.
*/
private static final SimpleDateFormat MKS_TIMESTAMP_FORMAT =
new SimpleDateFormat( "MMM d, yyyy - h:mm a" );
/**
* Custom date/time formatter. Rounds ChangeLogEntry times to the nearest
* minute.
*/
private static final SimpleDateFormat ENTRY_KEY_TIMESTAMP_FORMAT =
new SimpleDateFormat( "yyyyMMddHHmm" );
/** expecting file name */
private static final int GET_FILE_NAME = 1;
/** expecting file revision */
private static final int GET_FILE_REVISION = 2;
/** expecting entry revision */
private static final int WAITFOR_ENTRY_REVISION = 3;
/** expecting file revision */
private static final int GET_ENTRY_INFO = 4;
/** expecting file revision */
private static final int GET_ENTRY_COMMENT = 5;
/** rcs entries, in reverse (date, time, author, comment) order */
private Map entries = new TreeMap( Collections.reverseOrder() );
/** current status of the parser */
private int status = GET_FILE_NAME;
/** current changelog entry */
private ChangeLogEntry changeLogEntry;
/** current changelog file */
private ChangeLogFile changeLogFile;
/** date filter, only entries after this date match */
private Date filterDateStart;
/** date filter, only entries before this date match */
private Date filterDateEnd;
public void init( ChangeLog changeLog )
{
if (changeLog.getType().equals("range"))
{
try
{
if ( ( changeLog.getRange() != null )
&& ( changeLog.getRange().length() != 0 ) )
{
long range = Integer.parseInt(changeLog.getRange());
filterDateStart = new Date(System.currentTimeMillis()
- ( range * ( 24 * 60 * 60 * 1000 ) ) );
filterDateEnd = null;
}
else
{
throw new NumberFormatException("no range");
}
}
catch (NumberFormatException e)
{
e.printStackTrace();
filterDateStart = null;
filterDateEnd = null;
}
}
else if (changeLog.getType().equals("date"))
{
if ( ( changeLog.getMarkerStart() != null )
&& ( changeLog.getMarkerStart().length() != 0 ) )
{
try
{
filterDateStart = new SimpleDateFormat("y-M-d").
parse(changeLog.getMarkerStart());
}
catch (ParseException e)
{
e.printStackTrace();
filterDateStart = null;
}
}
if ( ( changeLog.getMarkerEnd() != null )
&& ( changeLog.getMarkerEnd().length() != 0 ) )
{
try
{
filterDateEnd = new SimpleDateFormat("y-M-d").
parse(changeLog.getMarkerEnd());
}
catch (ParseException e)
{
e.printStackTrace();
filterDateEnd = null;
}
}
}
else if (changeLog.getType().equals("tag"))
{
// MKS filter for 'tag' is not impl yet.
}
}
public void cleanup()
{
}
public void setDateFormatInFile( String dateFormat )
{
}
public Collection parse( InputStream in )
throws IOException
{
try
{
BufferedReader reader =
new BufferedReader( new InputStreamReader( in ) );
String line;
while ( ( line = reader.readLine() ) != null )
{
if ( line.startsWith(
"========================================" ) )
{
status = GET_FILE_NAME;
}
switch ( status )
{
case GET_FILE_NAME :
addEntry();
processGetFileName( line );
break;
case GET_FILE_REVISION :
processGetFileRevision( line );
break;
case WAITFOR_ENTRY_REVISION :
processWaitForEntryRevision( line );
break;
case GET_ENTRY_INFO :
processGetEntryInfo( line );
break;
case GET_ENTRY_COMMENT :
processGetEntryComment( line );
break;
default :
// wait for next entry
break;
}
}
addEntry();
}
catch ( RuntimeException e )
{
e.printStackTrace();
throw e;
}
catch ( IOException e )
{
e.printStackTrace();
throw e;
}
return entries.values();
}
protected void processGetFileName( String line )
{
if ( line.startsWith( "member name: " ) )
{
String filename;
if ( line.indexOf( ";" ) == -1 )
{
filename = line.substring( 13 );
}
else
{
filename = line.substring( 13, line.indexOf( ";" ) );
}
changeLogFile = new ChangeLogFile( filename );
status = GET_FILE_REVISION;
}
}
protected void processGetFileRevision( String line )
{
if ( line.startsWith( "member:\t" ) )
{
changeLogFile.setRevision( line.substring( 8 ) );
status = WAITFOR_ENTRY_REVISION;
}
}
protected void processWaitForEntryRevision( String line )
{
if ( line.equals( "-----------------------" ) )
{
status = GET_ENTRY_INFO;
}
}
protected void processGetEntryInfo( String line )
{
if ( line.startsWith( "date: " ) )
{
changeLogEntry = new ChangeLogEntry();
int posAuthor = line.indexOf( "; author: " );
if ( posAuthor == -1 )
{
return;
}
int posState = line.indexOf( "; state: " );
if ( posState == -1 )
{
return;
}
try
{
Date date =
MKS_TIMESTAMP_FORMAT.parse( line.substring( 6, posAuthor ) );
String author = line.substring( posAuthor + 10, posState );
changeLogEntry.setDate( date );
changeLogEntry.setAuthor( author );
status = GET_ENTRY_COMMENT;
}
catch ( ParseException e )
{
throw new IllegalArgumentException(
"I don't understand this date: "
+ line.substring( 6, posAuthor ) );
}
}
}
protected void processGetEntryComment( String line )
{
if ( line.equals( "-----------------------" ) )
{
addEntry();
status = GET_ENTRY_INFO;
return;
}
changeLogEntry.setComment( changeLogEntry.getComment() + line + "\n" );
}
protected void addEntry()
{
if ( ( changeLogEntry == null ) || ( changeLogFile == null ) )
{
return;
}
// do not add if entry is not populated
if ( ( changeLogEntry.getAuthor() == null )
|| ( changeLogEntry.getDate() == null ) )
{
return;
}
// do not add if entry doesn't match date range.
if ( (filterDateStart != null && changeLogEntry.getDate().before(filterDateStart)) ||
(filterDateEnd != null && changeLogEntry.getDate().after(filterDateEnd)) )
{
return;
}
String key =
ENTRY_KEY_TIMESTAMP_FORMAT.format( changeLogEntry.getDate() )
+ changeLogEntry.getAuthor() + changeLogEntry.getComment();
if ( !entries.containsKey( key ) )
{
changeLogEntry.addFile( changeLogFile );
entries.put( key, changeLogEntry );
}
else
{
( (ChangeLogEntry) entries.get( key ) ).addFile( changeLogFile );
}
}
}