xref: /OpenGrok/opengrok-indexer/src/main/java/org/opengrok/indexer/history/BitKeeperAnnotationParser.java (revision d1e826faf91bd41ab1a6d05ec6a5e19cb9865010)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * See LICENSE.txt included in this distribution for the specific
9  * language governing permissions and limitations under the License.
10  *
11  * When distributing Covered Code, include this CDDL HEADER in each
12  * file and include the License file at LICENSE.txt.
13  * If applicable, add the following below this CDDL HEADER, with the
14  * fields enclosed by brackets "[]" replaced with your own identifying
15  * information: Portions Copyright [yyyy] [name of copyright owner]
16  *
17  * CDDL HEADER END
18  */
19 
20 package org.opengrok.indexer.history;
21 
22 import java.io.BufferedReader;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.InputStreamReader;
26 import java.util.logging.Level;
27 import java.util.logging.Logger;
28 
29 import org.opengrok.indexer.logger.LoggerFactory;
30 import org.opengrok.indexer.util.Executor;
31 
32 /**
33  * handles parsing the output of the {@code bk annotate} command
34  * into an annotation object.
35  *
36  * @author James Service  {@literal <jas2701@googlemail.com>}
37  */
38 public class BitKeeperAnnotationParser implements Executor.StreamHandler {
39 
40     private static final Logger LOGGER = LoggerFactory.getLogger(BitKeeperAnnotationParser.class);
41 
42     /**
43      * Store annotation created by processStream.
44      */
45     private final Annotation annotation;
46 
47     /**
48      * @param fileName the name of the file being annotated
49      */
BitKeeperAnnotationParser(String fileName)50     public BitKeeperAnnotationParser(String fileName) {
51         annotation = new Annotation(fileName);
52     }
53 
54     /**
55      * Returns the annotation that has been created.
56      *
57      * @return annotation an annotation object
58      */
getAnnotation()59     public Annotation getAnnotation() {
60         return annotation;
61     }
62 
63     /**
64      * Process the output of a {@code bk annotate} command.
65      *
66      * Each input line should be in the following format:
67      *   USER\tREVISION\tTEXT
68      *
69      * @param input the executor input stream
70      * @throws IOException if the stream reader throws an IOException
71      */
72     @Override
processStream(InputStream input)73     public void processStream(InputStream input) throws IOException {
74         final BufferedReader in = new BufferedReader(new InputStreamReader(input));
75         for (String line = in.readLine(); line != null; line = in.readLine()) {
76             final String[] fields = line.split("\t");
77             if (fields.length >= 2) {
78                 final String author = fields[0];
79                 final String rev = fields[1];
80                 annotation.addLine(rev, author, true);
81             } else {
82                 LOGGER.log(Level.SEVERE, "Error: malformed BitKeeper annotate output {0}", line);
83             }
84         }
85     }
86 }
87