/* * 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. */ package org.apache.maven.svnlib; import java.util.Collection; import java.util.List; import java.util.ArrayList; import java.util.Date; import java.util.Collections; import java.util.Comparator; import java.util.Iterator; import java.text.SimpleDateFormat; import java.io.IOException; import org.apache.maven.changelog.ChangeLogParser; import org.apache.maven.changelog.ChangeLogEntry; import org.apache.tools.ant.types.Commandline; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * A Subversion implementation of the {@link org.apache.maven.ChangeLogGenerator} * which which overcomess Subversion Bug 752. * * This implementation uses the svn log command to retrieve ALL log entries * and then filters out those not in the specified date range. * * @author Niall Pemberton * @version $Id$ * @see Subversion Bug 752 */ public class SvnBug752ChangeLogGenerator extends SvnChangeLogGenerator { /** Logger */ private static final Log LOG = LogFactory.getLog(SvnBug752ChangeLogGenerator.class); /** First Date to process */ private Date internalStartDate = new Date((long)0); /** Last Date to process */ private Date internalEndDate = new Date(); /** * Sets the log start string based on the given date. * @param start date the log started. */ protected void setLogStart(Date start) { super.setLogStart(start); // reset start date to 00:00:00 SimpleDateFormat yyyyMMdd = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat yyyyMMddHHmmss = new SimpleDateFormat("yyyy-MM-ddHH:mm:ss z"); String tempDate = yyyyMMdd.format(start) + "00:00:00 GMT"; try { internalStartDate = yyyyMMddHHmmss.parse(tempDate); } catch (Exception e) { internalStartDate = start; } } /** * Sets the log end string based on the given date. * @param end date the log ended. */ protected void setLogEnd(Date end) { super.setLogEnd(end); // reset end date to 23:59:59 SimpleDateFormat yyyyMMdd = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat yyyyMMddHHmmss = new SimpleDateFormat("yyyy-MM-ddHH:mm:ss z"); String tempDate = yyyyMMdd.format(end) + "23:59:59 GMT"; try { internalEndDate = yyyyMMddHHmmss.parse(tempDate); } catch (Exception e) { internalEndDate = end; } } /** * Return a Collection of ChangeLogEntry objects. * * This implementations retrieves all log entries, filters * them on date and sorts. * * @param parser The parser to process the svn log output. * @return A Collection of ChangeLogEntry entries parsed * from the svn log output. * @throws IOException When there are issues executing svm. */ public Collection getEntries(ChangeLogParser parser) throws IOException { // Get All log entries super.getEntries(parser); if (LOG.isInfoEnabled()) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z"); LOG.info("Start=[" + format.format(internalStartDate) + "] End=[" + format.format(internalEndDate) + "] entries=[" + entries.size() +"]"); } // Filter the collection on the date range List filteredEntries = new ArrayList(); Iterator iterator = entries.iterator(); while (iterator.hasNext()) { ChangeLogEntry entry = (ChangeLogEntry)iterator.next(); Date entryDate = entry.getDate(); if (entryDate.after(internalStartDate) && entryDate.before(internalEndDate)) { filteredEntries.add(entry); } } // Create Date Comparator for ChangeLogEntry's Comparator comparator = new Comparator() { public int compare(Object object1, Object object2) { ChangeLogEntry entry1 = (ChangeLogEntry)object1; ChangeLogEntry entry2 = (ChangeLogEntry)object2; // Compare Dates int compare = entry1.getDate().compareTo(entry2.getDate()); // Invert so that sort is descending return (compare * -1); } }; // Sort the entries Collections.sort(filteredEntries, comparator); if (LOG.isInfoEnabled()) { LOG.info("Filtered Entries = " + filteredEntries.size()); } return filteredEntries; } /** * Construct the command line to execute the svn log * command for all revisions. * * @return The command to be executed. */ protected Commandline getScmLogCommand() { Commandline command = new Commandline(); command.setExecutable("svn"); command.createArgument().setValue("log"); command.createArgument().setValue("-v"); return command; } }