Coverage Report - org.apache.maven.scm.provider.vss.commands.changelog.VssChangeLogConsumer
 
Classes in this File Line Coverage Branch Coverage Complexity
VssChangeLogConsumer
0 %
0/73
0 %
0/30
2,8
 
 1  
 package org.apache.maven.scm.provider.vss.commands.changelog;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  * http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 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.vss.repository.VssScmProviderRepository;
 26  
 import org.apache.maven.scm.util.AbstractConsumer;
 27  
 
 28  
 import java.text.SimpleDateFormat;
 29  
 import java.util.ArrayList;
 30  
 import java.util.Collections;
 31  
 import java.util.List;
 32  
 import java.util.Map;
 33  
 import java.util.TreeMap;
 34  
 import java.util.Vector;
 35  
 
 36  
 /**
 37  
  * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
 38  
  * @author Olivier Lamy
 39  
  * @version $Id: VssChangeLogConsumer.java 1134851 2011-06-12 01:24:05Z godin $
 40  
  */
 41  
 public class VssChangeLogConsumer
 42  
     extends AbstractConsumer
 43  
 {
 44  
     /**
 45  
      * Custom date/time formatter. Rounds ChangeLogEntry times to the nearest
 46  
      * minute.
 47  
      */
 48  0
     private static final SimpleDateFormat ENTRY_KEY_TIMESTAMP_FORMAT = new SimpleDateFormat( "yyyyMMddHHmm" );
 49  
 
 50  
     // state machine constants for reading Starteam output
 51  
 
 52  
     /**
 53  
      * expecting file information
 54  
      */
 55  
     private static final int GET_FILE = 1;
 56  
 
 57  
     /**
 58  
      * expecting file path information
 59  
      */
 60  
     private static final int GET_FILE_PATH = 2;
 61  
 
 62  
     /**
 63  
      * expecting date
 64  
      */
 65  
     private static final int GET_AUTHOR = 3;
 66  
 
 67  
     /**
 68  
      * expecting comments
 69  
      */
 70  
     private static final int GET_COMMENT = 4;
 71  
 
 72  
     /**
 73  
      * expecting revision
 74  
      */
 75  
     private static final int GET_REVISION = 5;
 76  
 
 77  
     /**
 78  
      * unknown vss history line status
 79  
      */
 80  
     private static final int GET_UNKNOWN = 6;
 81  
 
 82  
     /**
 83  
      * Marks start of file data
 84  
      */
 85  
     private static final String START_FILE = "*****  ";
 86  
 
 87  
     /**
 88  
      * Marks start of file data
 89  
      */
 90  
     private static final String START_FILE_PATH = "$/";
 91  
 
 92  
     /**
 93  
      * Marks start of revision
 94  
      */
 95  
     private static final String START_REVISION = "Version";
 96  
 
 97  
     /**
 98  
      * Marks author data
 99  
      */
 100  
     private static final String START_AUTHOR = "User: ";
 101  
 
 102  
     /**
 103  
      * Marks comment data
 104  
      */
 105  
     private static final String START_COMMENT = "Comment: ";
 106  
 
 107  
     /**
 108  
      * rcs entries, in reverse (date, time, author, comment) order
 109  
      */
 110  0
     private Map<String, ChangeSet> entries = new TreeMap<String, ChangeSet>( Collections.reverseOrder() );
 111  
 
 112  
     private ChangeFile currentFile;
 113  
 
 114  
     private ChangeSet currentChangeSet;
 115  
 
 116  
     /**
 117  
      * last status of the parser
 118  
      */
 119  0
     private int lastStatus = GET_FILE;
 120  
 
 121  
     private VssScmProviderRepository repo;
 122  
 
 123  
     private String userDatePattern;
 124  
 
 125  
     public VssChangeLogConsumer( VssScmProviderRepository repo, String userDatePattern, ScmLogger logger )
 126  
     {
 127  0
         super( logger );
 128  0
         this.userDatePattern = userDatePattern;
 129  0
         this.repo = repo;
 130  0
     }
 131  
 
 132  
     public List<ChangeSet> getModifications()
 133  
     {
 134  0
         return new ArrayList<ChangeSet>( entries.values() );
 135  
     }
 136  
 
 137  
     /** {@inheritDoc} */
 138  
     public void consumeLine( String line )
 139  
     {
 140  0
         switch ( getLineStatus( line ) )
 141  
         {
 142  
             case GET_FILE:
 143  0
                 processGetFile( line );
 144  0
                 break;
 145  
             case GET_REVISION:
 146  0
                 processGetRevision( line );
 147  0
                 break;
 148  
             case GET_AUTHOR:
 149  0
                 processGetAuthor( line );
 150  0
                 break;
 151  
             case GET_FILE_PATH:
 152  0
                 processGetFilePath( line );
 153  0
                 break;
 154  
             case GET_COMMENT:
 155  0
                 processGetComment( line );
 156  0
                 break;
 157  
             default:
 158  
                 break;
 159  
         }
 160  0
     }
 161  
 
 162  
     /**
 163  
      * Process the current input line in the Get Comment state.
 164  
      *
 165  
      * @param line a line of text from the VSS log output
 166  
      */
 167  
     private void processGetComment( String line )
 168  
     {
 169  0
         String[] commentLine = line.split( ":" );
 170  0
         if ( commentLine.length == 2 )
 171  
         {
 172  0
             currentChangeSet.setComment( commentLine[1] );
 173  
         }
 174  
         //Comment suite on a new line
 175  
         else
 176  
         {
 177  0
             String comment = currentChangeSet.getComment();
 178  0
             comment = comment + " " + line;
 179  0
             currentChangeSet.setComment( comment );
 180  
         }
 181  0
     }
 182  
 
 183  
     /**
 184  
      * Process the current input line in the Get Author state.
 185  
      *
 186  
      * @param line a line of text from the VSS log output
 187  
      */
 188  
     private void processGetAuthor( String line )
 189  
     {
 190  0
         String[] result = line.split( "\\s" );
 191  0
         Vector<String> vector = new Vector<String>();
 192  0
         for ( int i = 0; i < result.length; i++ )
 193  
         {
 194  0
             if ( !result[i].equals( "" ) )
 195  
             {
 196  0
                 vector.add( result[i] );
 197  
             }
 198  
         }
 199  0
         currentChangeSet.setAuthor( vector.get( 1 ) );
 200  0
         currentChangeSet.setDate(
 201  
             parseDate( vector.get( 3 ) + " " + vector.get( 5 ), userDatePattern, "dd.MM.yy HH:mm" ) );
 202  0
     }
 203  
 
 204  
     /**
 205  
      * Process the current input line in the Get File state.
 206  
      *
 207  
      * @param line a line of text from the VSS log output
 208  
      */
 209  
     private void processGetFile( String line )
 210  
     {
 211  0
         currentChangeSet = ( new ChangeSet() );
 212  0
         String[] fileLine = line.split( " " );
 213  0
         currentFile = new ChangeFile( fileLine[2] );
 214  0
     }
 215  
 
 216  
     /**
 217  
      * Process the current input line in the Get File Path state.
 218  
      *
 219  
      * @param line a line of text from the VSS log output
 220  
      */
 221  
     private void processGetFilePath( String line )
 222  
     {
 223  0
         if ( currentFile != null )
 224  
         {
 225  0
             String fileName = currentFile.getName();
 226  
 
 227  0
             String path = line.substring( line.indexOf( '$' ), line.length() );
 228  0
             String longPath = path.substring( repo.getProject()
 229  
                 .length() + 1, path.length() ) + "/" + fileName;
 230  0
             currentFile.setName( longPath );
 231  0
             addEntry( currentChangeSet, currentFile );
 232  
         }
 233  0
     }
 234  
 
 235  
     /**
 236  
      * Process the current input line in the Get Revision state.
 237  
      *
 238  
      * @param line a line of text from the VSS log output
 239  
      */
 240  
     private void processGetRevision( String line )
 241  
     {
 242  0
         String[] revisionLine = line.split( " " );
 243  0
         currentFile.setRevision( revisionLine[1] );
 244  0
     }
 245  
 
 246  
     /**
 247  
      * Identify the status of a vss history line
 248  
      *
 249  
      * @param line The line to process
 250  
      * @return status
 251  
      */
 252  
     private int getLineStatus( String line )
 253  
     {
 254  0
         int argument = GET_UNKNOWN;
 255  0
         if ( line.startsWith( START_FILE ) )
 256  
         {
 257  0
             argument = GET_FILE;
 258  
         }
 259  0
         else if ( line.startsWith( START_REVISION ) )
 260  
         {
 261  0
             argument = GET_REVISION;
 262  
         }
 263  0
         else if ( line.startsWith( START_AUTHOR ) )
 264  
         {
 265  0
             argument = GET_AUTHOR;
 266  
         }
 267  0
         else if ( line.indexOf( START_FILE_PATH ) != -1 )
 268  
         {
 269  0
             argument = GET_FILE_PATH;
 270  
         }
 271  0
         else if ( line.startsWith( START_COMMENT ) )
 272  
         {
 273  0
             argument = GET_COMMENT;
 274  
         }
 275  0
         else if ( lastStatus == GET_COMMENT )
 276  
         {
 277  
             //Comment suite on a new line
 278  0
             argument = lastStatus;
 279  
         }
 280  0
         lastStatus = argument;
 281  
 
 282  0
         return argument;
 283  
     }
 284  
 
 285  
     /**
 286  
      * Add a change log entry to the list (if it's not already there) with the
 287  
      * given file.
 288  
      *
 289  
      * @param entry a {@link ChangeSet}to be added to the list if another
 290  
      *              with the same key doesn't exist already. If the entry's author
 291  
      *              is null, the entry wont be added
 292  
      * @param file  a {@link ChangeFile}to be added to the entry
 293  
      */
 294  
     private void addEntry( ChangeSet entry, ChangeFile file )
 295  
     {
 296  
         // do not add if entry is not populated
 297  0
         if ( entry.getAuthor() == null )
 298  
         {
 299  0
             return;
 300  
         }
 301  
 
 302  0
         String key = ENTRY_KEY_TIMESTAMP_FORMAT.format( entry.getDate() ) + entry.getAuthor() + entry.getComment();
 303  
 
 304  0
         if ( !entries.containsKey( key ) )
 305  
         {
 306  0
             entry.addFile( file );
 307  0
             entries.put( key, entry );
 308  
         }
 309  
         else
 310  
         {
 311  0
             ChangeSet existingEntry = (ChangeSet) entries.get( key );
 312  0
             existingEntry.addFile( file );
 313  
         }
 314  0
     }
 315  
 }