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) 2008, 2017, 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.IOException; 27 import java.io.InputStream; 28 import java.io.InputStreamReader; 29 import java.util.logging.Level; 30 import java.util.logging.Logger; 31 import java.util.regex.Matcher; 32 import java.util.regex.Pattern; 33 import org.opengrok.indexer.logger.LoggerFactory; 34 import org.opengrok.indexer.util.Executor; 35 36 /** 37 * handles parsing the output of the {@code accurev annotate} command 38 * into an annotation object. 39 */ 40 public class AccuRevAnnotationParser implements Executor.StreamHandler { 41 42 private static final Logger LOGGER = LoggerFactory.getLogger(AccuRevAnnotationParser.class); 43 44 private static final Pattern ANNOTATION_PATTERN 45 = Pattern.compile("^\\s+(\\d+.\\d+)\\s+(\\w+)"); // version, user 46 47 /** 48 * Store annotation created by processStream. 49 */ 50 private final Annotation annotation; 51 52 /** 53 * @param fileName the name of the file being annotated 54 */ AccuRevAnnotationParser(String fileName)55 public AccuRevAnnotationParser(String fileName) { 56 annotation = new Annotation(fileName); 57 } 58 59 /** 60 * Returns the annotation that has been created. 61 * 62 * @return annotation an annotation object 63 */ getAnnotation()64 public Annotation getAnnotation() { 65 return annotation; 66 } 67 68 @Override processStream(InputStream input)69 public void processStream(InputStream input) throws IOException { 70 try (BufferedReader reader 71 = new BufferedReader(new InputStreamReader(input))) { 72 String line; 73 int lineno = 0; 74 try { 75 while ((line = reader.readLine()) != null) { 76 ++lineno; 77 Matcher matcher = ANNOTATION_PATTERN.matcher(line); 78 79 if (matcher.find()) { 80 // On Windows machines version shows up as 81 // <number>\<number>. To get search annotation 82 // to work properly, need to flip '\' to '/'. 83 // This is a noop on Unix boxes. 84 String version = matcher.group(1).replace('\\', '/'); 85 String author = matcher.group(2); 86 annotation.addLine(version, author, true); 87 } else { 88 LOGGER.log(Level.SEVERE, 89 "Did not find annotation in line {0}: [{1}]", 90 new Object[]{lineno, line}); 91 } 92 } 93 } catch (IOException e) { 94 LOGGER.log(Level.SEVERE, 95 "Could not read annotations for " + annotation.getFilename(), e); 96 } 97 } 98 } 99 } 100