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 java.io.BufferedReader; 26 import java.io.File; 27 import java.io.IOException; 28 import java.io.InputStream; 29 import java.io.InputStreamReader; 30 import java.util.Map; 31 import java.util.logging.Level; 32 import java.util.logging.Logger; 33 import java.util.regex.Matcher; 34 import java.util.regex.Pattern; 35 import org.opengrok.indexer.logger.LoggerFactory; 36 import org.opengrok.indexer.util.Executor; 37 38 /** 39 * handles parsing into Annotation object. 40 */ 41 public class SCCSRepositoryAnnotationParser implements Executor.StreamHandler { 42 private static final Logger LOGGER = LoggerFactory.getLogger(SCCSRepositoryAnnotationParser.class); 43 44 /** 45 * Store annotation created by processStream. 46 */ 47 private final Annotation annotation; 48 49 private final Map<String, String> authors; 50 51 /** 52 * Pattern used to extract revision from the {@code sccs get} command. 53 */ 54 private static final Pattern ANNOTATION_PATTERN = Pattern.compile("^([\\d.]+)\\s+"); 55 SCCSRepositoryAnnotationParser(File file, Map<String, String> authors)56 SCCSRepositoryAnnotationParser(File file, Map<String, String> authors) { 57 this.annotation = new Annotation(file.getName()); 58 this.authors = authors; 59 } 60 61 /** 62 * Returns the annotation that has been created. 63 * 64 * @return annotation an annotation object 65 */ getAnnotation()66 public Annotation getAnnotation() { 67 return annotation; 68 } 69 70 @Override processStream(InputStream input)71 public void processStream(InputStream input) throws IOException { 72 try (BufferedReader in = new BufferedReader( 73 new InputStreamReader(input))) { 74 String line; 75 int lineno = 0; 76 while ((line = in.readLine()) != null) { 77 ++lineno; 78 Matcher matcher = ANNOTATION_PATTERN.matcher(line); 79 if (matcher.find()) { 80 String rev = matcher.group(1); 81 String author = authors.get(rev); 82 if (author == null) { 83 author = "unknown"; 84 } 85 86 annotation.addLine(rev, author, true); 87 } else { 88 LOGGER.log(Level.SEVERE, 89 "Error: did not find annotations in line {0}: [{1}]", 90 new Object[]{lineno, line}); 91 } 92 } 93 } 94 } 95 } 96