package org.apache.maven.mkslib; /* ==================================================================== * Copyright 2005 The Apache Software Foundation. * * Licensed 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 Christoph Jerolimov * @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; public void init(ChangeLog changeLog) { } 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; } 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); } } }