View Javadoc
1   package org.apache.maven.scm.provider.integrity.command.blame;
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.command.blame.BlameLine;
23  import org.apache.maven.scm.log.ScmLogger;
24  import org.codehaus.plexus.util.StringUtils;
25  import org.codehaus.plexus.util.cli.StreamConsumer;
26  
27  import java.text.ParseException;
28  import java.text.SimpleDateFormat;
29  import java.util.ArrayList;
30  import java.util.List;
31  
32  /**
33   * Helper class to consume the standard output from running the IntegrityBlameCommand
34   *
35   * @author <a href="mailto:cletus@mks.com">Cletus D'Souza</a>
36   * @version $Id: IntegrityBlameConsumer.java 1.2 2011/08/22 13:06:16EDT Cletus D'Souza (dsouza) Exp  $
37   * @since 1.6
38   */
39  public class IntegrityBlameConsumer
40      implements StreamConsumer
41  {
42      private ScmLogger logger;
43  
44      private List<BlameLine> blameList;
45  
46      private SimpleDateFormat dateFormat;
47  
48      /**
49       * IntegrityBlameConsumer constructor requires a ScmLogger to log all the activity
50       *
51       * @param logger ScmLogger object
52       */
53      public IntegrityBlameConsumer( ScmLogger logger )
54      {
55          this.logger = logger;
56          this.blameList = new ArrayList<BlameLine>();
57          this.dateFormat = new SimpleDateFormat( "MMM dd, yyyy z" );
58      }
59  
60      /**
61       * {@inheritDoc}
62       */
63      public void consumeLine( String line )
64      {
65          // Parse the annotate output which should return the three pieces of data
66          logger.debug( line );
67          if ( null != line && line.trim().length() > 0 )
68          {
69              String[] tokens = StringUtils.split( line, "\t" );
70              if ( tokens.length != 3 )
71              {
72                  logger.warn( "Failed to parse line: " + line );
73              }
74              else
75              {
76                  try
77                  {
78                      blameList.add( new BlameLine( dateFormat.parse( tokens[0] ), tokens[1], tokens[2] ) );
79                  }
80                  catch ( ParseException e )
81                  {
82                      logger.error( "Failed to date string: " + tokens[0] );
83                  }
84              }
85          }
86      }
87  
88      /**
89       * Returns a list of BlameLine objects found from parsing the 'si annotate' output
90       *
91       * @return
92       */
93      public List<BlameLine> getBlameList()
94      {
95          return blameList;
96      }
97  }