xref: /OpenGrok/opengrok-indexer/src/main/java/org/opengrok/indexer/history/RCSAnnotationParser.java (revision c6f0939b1c668e9f8e1e276424439c3106b3a029)
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 /*
21  * Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
22  */
23 package org.opengrok.indexer.history;
24 
25 import org.opengrok.indexer.logger.LoggerFactory;
26 import org.opengrok.indexer.util.Executor;
27 
28 import java.io.IOException;
29 import java.io.BufferedReader;
30 import java.io.File;
31 import java.io.InputStream;
32 import java.io.InputStreamReader;
33 import java.util.logging.Level;
34 import java.util.logging.Logger;
35 import java.util.regex.Matcher;
36 import java.util.regex.Pattern;
37 
38 public class RCSAnnotationParser implements Executor.StreamHandler {
39     private static final Logger LOGGER = LoggerFactory.getLogger(RCSAnnotationParser.class);
40 
41     private Annotation annotation = null;
42     private final File file;
43 
44     /**
45      * Pattern used to extract author/revision from {@code blame}.
46      */
47     private static final Pattern ANNOTATION_PATTERN
48             = Pattern.compile("^([\\d\\.]+)\\s*\\((\\S+)\\s*\\S+\\): ");
49 
RCSAnnotationParser(File file)50     RCSAnnotationParser(File file) {
51         this.file = file;
52     }
53 
54     @Override
processStream(InputStream input)55     public void processStream(InputStream input) throws IOException {
56         annotation = new Annotation(file.getName());
57         String line;
58         int lineno = 0;
59         Matcher matcher = ANNOTATION_PATTERN.matcher("");
60 
61         try (BufferedReader in = new BufferedReader(new InputStreamReader(input))) {
62             while ((line = in.readLine()) != null) {
63                 ++lineno;
64                 matcher.reset(line);
65                 if (matcher.find()) {
66                     String rev = matcher.group(1);
67                     String author = matcher.group(2);
68                     annotation.addLine(rev, author, true);
69                 } else {
70                     LOGGER.log(Level.SEVERE,
71                             "Error: did not find annotation in line {0}: [{1}]",
72                             new Object[]{lineno, line});
73                 }
74             }
75         }
76     }
77 
getAnnotation()78     Annotation getAnnotation() {
79         return this.annotation;
80     }
81 }
82