001package org.apache.maven.scm.provider.hg.command.changelog;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.util.ArrayList;
023import java.util.Date;
024import java.util.List;
025import java.util.Locale;
026
027import org.apache.maven.scm.ChangeFile;
028import org.apache.maven.scm.ChangeSet;
029import org.apache.maven.scm.ScmFileStatus;
030import org.apache.maven.scm.log.ScmLogger;
031import org.apache.maven.scm.provider.hg.command.HgConsumer;
032
033/**
034 * @author <a href="mailto:thurner.rupert@ymono.net">thurner rupert</a>
035 * @author <a href="mailto:hr.mohr@gmail.com">Mads Mohr Christensen</a>
036 */
037public class HgChangeLogConsumer
038    extends HgConsumer
039{
040
041    private static final String TIME_PATTERN = "yyyy-MM-dd HH:mm:ss Z";
042
043    private static final String REVNO_TAG = "changeset:";
044
045    private static final String TAG_TAG = "tag:";
046
047    private static final String BRANCH_TAG = "branch:";
048
049    private static final String AUTHOR_TAG = "user:";
050
051    private static final String TIME_STAMP_TOKEN = "date:";
052
053    private static final String MESSAGE_TOKEN = "description:";
054
055    private static final String FILES_TOKEN = "files:";
056
057    private List<ChangeSet> logEntries = new ArrayList<ChangeSet>();
058
059    private ChangeSet currentChange;
060
061    private String currentRevision;
062
063    @SuppressWarnings( "unused" )
064    private String currentTag; // don't know what to do with this
065
066    @SuppressWarnings( "unused" )
067    private String currentBranch; // don't know what to do with this
068
069    private String userDatePattern;
070
071    public HgChangeLogConsumer( ScmLogger logger, String userDatePattern )
072    {
073        super( logger );
074        this.userDatePattern = userDatePattern;
075    }
076
077    public List<ChangeSet> getModifications()
078    {
079        return logEntries;
080    }
081
082    /**
083     * {@inheritDoc}
084     */
085    public void consumeLine( String line )
086    {
087        // override default behaviour which tries to pick through things for some standard messages.  that
088        // does not apply here
089        String trimmedLine = line.trim();
090        doConsume( null, trimmedLine );
091    }
092
093    /**
094     * {@inheritDoc}
095     */
096    public void doConsume( ScmFileStatus status, String line )
097    {
098        String tmpLine;
099
100        // new changeset
101        if ( line.startsWith( REVNO_TAG ) )
102        {
103            //Init a new changeset
104            currentChange = new ChangeSet();
105            currentChange.setFiles( new ArrayList<ChangeFile>( 0 ) );
106            logEntries.add( currentChange );
107
108            // parse revision
109            tmpLine = line.substring( REVNO_TAG.length() ).trim();
110            currentRevision = tmpLine.substring( tmpLine.indexOf( ':' ) + 1 );
111            currentChange.setRevision( currentRevision );
112        }
113        else if ( line.startsWith( BRANCH_TAG ) )
114        {
115            tmpLine = line.substring( BRANCH_TAG.length() ).trim();
116            currentBranch = tmpLine;
117        }
118        else if ( line.startsWith( AUTHOR_TAG ) )
119        {
120            tmpLine = line.substring( AUTHOR_TAG.length() ).trim();
121            currentChange.setAuthor( tmpLine );
122        }
123        else if ( line.startsWith( TIME_STAMP_TOKEN ) )
124        {
125            tmpLine = line.substring( TIME_STAMP_TOKEN.length() ).trim();
126            Date date = parseDate( tmpLine, userDatePattern, TIME_PATTERN, Locale.ENGLISH );
127            currentChange.setDate( date );
128        }
129        else if ( line.startsWith( TAG_TAG ) )
130        {
131            tmpLine = line.substring( TAG_TAG.length() ).trim();
132            currentTag = tmpLine;
133        }
134        else if ( line.startsWith( FILES_TOKEN ) )
135        {
136            tmpLine = line.substring( FILES_TOKEN.length() ).trim();
137            String[] files = tmpLine.split( " " );
138            for ( int i = 0; i < files.length; i++ )
139            {
140                String file = files[i];
141                ChangeFile changeFile = new ChangeFile( file, currentRevision );
142                currentChange.addFile( changeFile );
143            }
144        }
145        else if ( line.startsWith( MESSAGE_TOKEN ) )
146        {
147            currentChange.setComment( "" );
148        }
149        else
150        {
151            StringBuilder comment = new StringBuilder( currentChange.getComment() );
152            comment.append( line );
153            comment.append( '\n' );
154            currentChange.setComment( comment.toString() );
155        }
156    }
157}