View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.scm.provider.hg.command.blame;
20  
21  import java.util.ArrayList;
22  import java.util.Date;
23  import java.util.List;
24  import java.util.Locale;
25  
26  import org.apache.maven.scm.ScmFileStatus;
27  import org.apache.maven.scm.command.blame.BlameLine;
28  import org.apache.maven.scm.provider.hg.command.HgConsumer;
29  
30  /**
31   * @author Evgeny Mandrikov
32   * @author Olivier Lamy
33   * @since 1.4
34   */
35  public class HgBlameConsumer extends HgConsumer {
36      private final List<BlameLine> lines = new ArrayList<>();
37  
38      private static final String HG_TIMESTAMP_PATTERN = "EEE MMM dd HH:mm:ss yyyy Z";
39  
40      public void doConsume(ScmFileStatus status, String trimmedLine) {
41          /* godin 0 Sun Jan 31 03:04:54 2010 +0300 */
42          String annotation;
43          if (trimmedLine.indexOf(": ") > -1) {
44              annotation = trimmedLine.substring(0, trimmedLine.indexOf(": ")).trim();
45          } else {
46              annotation = trimmedLine.substring(0, trimmedLine.lastIndexOf(":")).trim();
47          }
48  
49          String author = annotation.substring(0, annotation.indexOf(' '));
50          annotation = annotation.substring(annotation.indexOf(' ') + 1).trim();
51  
52          String revision = annotation.substring(0, annotation.indexOf(' '));
53          annotation = annotation.substring(annotation.indexOf(' ') + 1).trim();
54  
55          String dateStr = annotation;
56          Date dateTime = parseDate(dateStr, null, HG_TIMESTAMP_PATTERN, Locale.ENGLISH);
57  
58          lines.add(new BlameLine(dateTime, revision, author));
59      }
60  
61      public List<BlameLine> getLines() {
62          return lines;
63      }
64  }