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   * @since 1.6
37   */
38  public class IntegrityBlameConsumer
39      implements StreamConsumer
40  {
41      private ScmLogger logger;
42  
43      private List<BlameLine> blameList;
44  
45      private SimpleDateFormat dateFormat;
46  
47      /**
48       * IntegrityBlameConsumer constructor requires a ScmLogger to log all the activity
49       *
50       * @param logger ScmLogger object
51       */
52      public IntegrityBlameConsumer( ScmLogger logger )
53      {
54          this.logger = logger;
55          this.blameList = new ArrayList<BlameLine>();
56          this.dateFormat = new SimpleDateFormat( "MMM dd, yyyy z" );
57      }
58  
59      /**
60       * {@inheritDoc}
61       */
62      public void consumeLine( String line )
63      {
64          // Parse the annotate output which should return the three pieces of data
65          logger.debug( line );
66          if ( null != line && line.trim().length() > 0 )
67          {
68              String[] tokens = StringUtils.split( line, "\t" );
69              if ( tokens.length != 3 )
70              {
71                  logger.warn( "Failed to parse line: " + line );
72              }
73              else
74              {
75                  try
76                  {
77                      blameList.add( new BlameLine( dateFormat.parse( tokens[0] ), tokens[1], tokens[2] ) );
78                  }
79                  catch ( ParseException e )
80                  {
81                      logger.error( "Failed to date string: " + tokens[0] );
82                  }
83              }
84          }
85      }
86  
87      /**
88       * Returns a list of BlameLine objects found from parsing the 'si annotate' output
89       *
90       * @return
91       */
92      public List<BlameLine> getBlameList()
93      {
94          return blameList;
95      }
96  }