1433N/A/*
1433N/A * CDDL HEADER START
1433N/A *
1433N/A * The contents of this file are subject to the terms of the
1433N/A * Common Development and Distribution License (the "License").
1433N/A * You may not use this file except in compliance with the License.
1433N/A *
1433N/A * See LICENSE.txt included in this distribution for the specific
1433N/A * language governing permissions and limitations under the License.
1433N/A *
1433N/A * When distributing Covered Code, include this CDDL HEADER in each
1433N/A * file and include the License file at LICENSE.txt.
1433N/A * If applicable, add the following below this CDDL HEADER, with the
1433N/A * fields enclosed by brackets "[]" replaced with your own identifying
1433N/A * information: Portions Copyright [yyyy] [name of copyright owner]
1433N/A *
1433N/A * CDDL HEADER END
1433N/A */
1433N/A
1433N/A/*
1433N/A * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
1433N/A */
1433N/Apackage org.opensolaris.opengrok.history;
1433N/A
1433N/Aimport java.io.BufferedReader;
1433N/Aimport java.io.File;
1433N/Aimport java.io.IOException;
1433N/Aimport java.io.InputStream;
1433N/Aimport java.io.InputStreamReader;
1434N/Aimport java.text.DateFormat;
1434N/Aimport java.text.ParseException;
1433N/Aimport java.util.ArrayList;
1433N/Aimport java.util.Date;
1433N/Aimport java.util.List;
1433N/Aimport java.util.logging.Level;
1461N/Aimport java.util.logging.Logger;
1461N/A
1433N/Aimport org.opensolaris.opengrok.util.Executor;
1433N/A
1433N/A/**
1433N/A * Parse source history for a AccuRev Repository
1433N/A *
1433N/A * @author Steven Haehn
1433N/A */
1433N/Apublic class AccuRevHistoryParser implements Executor.StreamHandler {
1461N/A private static final Logger logger =
1461N/A Logger.getLogger(AccuRevHistoryParser.class.getName());
1433N/A private AccuRevRepository repository;
1433N/A private History history;
1433N/A private boolean foundHistory;
1433N/A
1433N/A /**
1433N/A * Parse the history for the specified file.
1433N/A *
1433N/A * @param file the file to parse history for
1433N/A * @param repos Pointer to the {@code AccuRevRepository}
1461N/A * @return the file's history
1433N/A */
1461N/A History parse(File file, Repository repos) {
1433N/A repository = (AccuRevRepository) repos;
1433N/A history = null;
1433N/A foundHistory = false;
1461N/A String relPath = AccuRevRepository.getDepotRelativePath(file);
1433N/A
1461N/A /* When the path given is really just the root to the source workarea,
1461N/A * no history is available, create fake. */
1434N/A if (relPath.equals("/./")) {
1433N/A List<HistoryEntry> entries = new ArrayList<HistoryEntry>();
1481N/A entries.add(new HistoryEntry("", null, new Date(), "OpenGrok",
1461N/A "Workspace Root", true));
1434N/A history = new History(entries);
1433N/A } else {
1461N/A Executor executor = repository.getHistoryLogExecutor(file);
1461N/A executor.exec(true, this);
1461N/A /* Try again because there was no 'keep' history. */
1461N/A if (!foundHistory) {
1461N/A executor = repository.getHistoryLogExecutor(file);
1434N/A executor.exec(true, this);
1433N/A }
1433N/A }
1433N/A return history;
1433N/A }
1433N/A
1461N/A /**
1461N/A * {@inheritDoc}
1461N/A */
1461N/A @Override
1434N/A public void processStream(InputStream input) throws IOException {
1434N/A BufferedReader in = new BufferedReader(new InputStreamReader(input));
1433N/A List<HistoryEntry> entries = new ArrayList<HistoryEntry>();
1433N/A DateFormat df = repository.getDateFormat();
1433N/A String line;
1433N/A HistoryEntry entry = null;
1433N/A history = new History();
1434N/A
1461N/A /* Accurev history of an element (directory or file) looks like:
1434N/A *
1461N/A * element: /./path/to/element
1461N/A * eid: 238865
1461N/A * transaction 1486194; purge; 2012/02/28 12:46:55 ; user: tluksha
1461N/A * version 2541/1 (2539/1)
1434N/A *
1434N/A * transaction 1476285; purge; 2012/02/03 12:16:25 ; user: shaehn
1461N/A * version 4241/1 (4241/1)
1434N/A *
1461N/A * transaction 1317224; promote; 2011/05/03 11:37:56 ; user: mtrinh
1461N/A * # changes to ngb to compile on windows
1461N/A * version 13/93 (1000/2)
1434N/A * ...
1434N/A *
1434N/A * What is of interest then is: user (author), version (revision), date,
1434N/A * comment (message)
1434N/A *
1434N/A * Any lines not recognized are ignored.
1434N/A */
1434N/A while ((line = in.readLine()) != null) {
1434N/A if (line.startsWith("t")) { // found transaction
1461N/A foundHistory = true;
1434N/A String data[] = line.split("; ");
1461N/A String user = data[3].replaceFirst("user: ", "");
1461N/A Date date = null;
1433N/A try {
1434N/A date = df.parse(data[2]);
1434N/A } catch (ParseException pe) {
1461N/A logger.log(Level.WARNING, "Could not parse date: " + line);
1461N/A logger.log(Level.FINE, "processStream", pe);
1433N/A }
1481N/A entry = new HistoryEntry(null, null, date, user, "", false);
1461N/A } else if (entry != null) {
1461N/A if (line.startsWith(" #")) { // found comment
1461N/A entry.appendMessage(line.substring(3));
1461N/A } else if (line.startsWith(" v")) { // found version
1461N/A String data[] = line.split("\\s+");
1461N/A entry.setRevision(data[2]);
1461N/A entry.setActive(true);
1461N/A entries.add(entry);
1461N/A entry = null;
1461N/A }
1433N/A }
1433N/A }
1434N/A history.setHistoryEntries(entries);
1433N/A }
1433N/A}