87Knut.Hatlen@Sun.COM/*
87Knut.Hatlen@Sun.COM * CDDL HEADER START
87Knut.Hatlen@Sun.COM *
87Knut.Hatlen@Sun.COM * The contents of this file are subject to the terms of the
87Knut.Hatlen@Sun.COM * Common Development and Distribution License (the "License").
87Knut.Hatlen@Sun.COM * You may not use this file except in compliance with the License.
87Knut.Hatlen@Sun.COM *
87Knut.Hatlen@Sun.COM * See LICENSE.txt included in this distribution for the specific
87Knut.Hatlen@Sun.COM * language governing permissions and limitations under the License.
87Knut.Hatlen@Sun.COM *
87Knut.Hatlen@Sun.COM * When distributing Covered Code, include this CDDL HEADER in each
87Knut.Hatlen@Sun.COM * file and include the License file at LICENSE.txt.
87Knut.Hatlen@Sun.COM * If applicable, add the following below this CDDL HEADER, with the
87Knut.Hatlen@Sun.COM * fields enclosed by brackets "[]" replaced with your own identifying
87Knut.Hatlen@Sun.COM * information: Portions Copyright [yyyy] [name of copyright owner]
87Knut.Hatlen@Sun.COM *
87Knut.Hatlen@Sun.COM * CDDL HEADER END
87Knut.Hatlen@Sun.COM */
87Knut.Hatlen@Sun.COM
87Knut.Hatlen@Sun.COM/*
87Knut.Hatlen@Sun.COM * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
87Knut.Hatlen@Sun.COM * Use is subject to license terms.
87Knut.Hatlen@Sun.COM */
87Knut.Hatlen@Sun.COM
87Knut.Hatlen@Sun.COMpackage org.opensolaris.opengrok.history;
87Knut.Hatlen@Sun.COM
878Lubos.Kosco@Sun.COMimport java.io.IOException;
878Lubos.Kosco@Sun.COMimport java.io.StringWriter;
878Lubos.Kosco@Sun.COMimport java.io.Writer;
87Knut.Hatlen@Sun.COMimport java.util.ArrayList;
878Lubos.Kosco@Sun.COMimport java.util.HashMap;
879Lubos.Kosco@Sun.COMimport java.util.HashSet;
878Lubos.Kosco@Sun.COMimport java.util.Iterator;
457jorgen.austvik@sun.comimport java.util.List;
879Lubos.Kosco@Sun.COMimport java.util.Map;
879Lubos.Kosco@Sun.COMimport java.util.Map.Entry;
879Lubos.Kosco@Sun.COMimport java.util.Set;
87Knut.Hatlen@Sun.COM
879Lubos.Kosco@Sun.COMimport java.util.logging.Logger;
878Lubos.Kosco@Sun.COMimport org.opensolaris.opengrok.web.Util;
878Lubos.Kosco@Sun.COM
87Knut.Hatlen@Sun.COM/**
87Knut.Hatlen@Sun.COM * Class representing file annotation, i.e., revision and author for the last
87Knut.Hatlen@Sun.COM * modification of each line in the file.
87Knut.Hatlen@Sun.COM */
87Knut.Hatlen@Sun.COMpublic class Annotation {
87Knut.Hatlen@Sun.COM
457jorgen.austvik@sun.com private final List<Line> lines = new ArrayList<Line>();
879Lubos.Kosco@Sun.COM private final Map<String, String> desc = new HashMap<String, String>();
87Knut.Hatlen@Sun.COM private int widestRevision;
87Knut.Hatlen@Sun.COM private int widestAuthor;
456jorgen.austvik@sun.com private final String filename;
879Lubos.Kosco@Sun.COM static final Logger log = Logger.getLogger(Annotation.class.getName());
1190trond.norbye@gmail.com
140trond.norbye@sun.com public Annotation(String filename) {
140trond.norbye@sun.com this.filename = filename;
140trond.norbye@sun.com }
1190trond.norbye@gmail.com
87Knut.Hatlen@Sun.COM /**
87Knut.Hatlen@Sun.COM * Gets the revision for the last change to the specified line.
87Knut.Hatlen@Sun.COM *
87Knut.Hatlen@Sun.COM * @param line line number (counting from 1)
91Knut.Hatlen@Sun.COM * @return revision string, or an empty string if there is no information
91Knut.Hatlen@Sun.COM * about the specified line
87Knut.Hatlen@Sun.COM */
87Knut.Hatlen@Sun.COM public String getRevision(int line) {
91Knut.Hatlen@Sun.COM try {
91Knut.Hatlen@Sun.COM return lines.get(line-1).revision;
91Knut.Hatlen@Sun.COM } catch (IndexOutOfBoundsException e) {
91Knut.Hatlen@Sun.COM return "";
91Knut.Hatlen@Sun.COM }
87Knut.Hatlen@Sun.COM }
87Knut.Hatlen@Sun.COM
87Knut.Hatlen@Sun.COM /**
878Lubos.Kosco@Sun.COM * Gets all revisions that are in use, first is the lowest one (sorted using natural order)
1190trond.norbye@gmail.com *
878Lubos.Kosco@Sun.COM * @return list of all revisions the file has
878Lubos.Kosco@Sun.COM */
879Lubos.Kosco@Sun.COM public Set<String> getRevisions() {
879Lubos.Kosco@Sun.COM Set<String> ret=new HashSet<String>();
878Lubos.Kosco@Sun.COM for (Iterator<Line> it = this.lines.iterator(); it.hasNext();) {
878Lubos.Kosco@Sun.COM Line ln = it.next();
1190trond.norbye@gmail.com ret.add(ln.revision);
1190trond.norbye@gmail.com }
878Lubos.Kosco@Sun.COM return ret;
878Lubos.Kosco@Sun.COM }
878Lubos.Kosco@Sun.COM
878Lubos.Kosco@Sun.COM /**
87Knut.Hatlen@Sun.COM * Gets the author who last modified the specified line.
87Knut.Hatlen@Sun.COM *
87Knut.Hatlen@Sun.COM * @param line line number (counting from 1)
91Knut.Hatlen@Sun.COM * @return author, or an empty string if there is no information about the
91Knut.Hatlen@Sun.COM * specified line
87Knut.Hatlen@Sun.COM */
87Knut.Hatlen@Sun.COM public String getAuthor(int line) {
91Knut.Hatlen@Sun.COM try {
91Knut.Hatlen@Sun.COM return lines.get(line-1).author;
91Knut.Hatlen@Sun.COM } catch (IndexOutOfBoundsException e) {
91Knut.Hatlen@Sun.COM return "";
91Knut.Hatlen@Sun.COM }
87Knut.Hatlen@Sun.COM }
87Knut.Hatlen@Sun.COM
87Knut.Hatlen@Sun.COM /**
168ldavis@fonix.com * Gets the enabled state for the last change to the specified line.
168ldavis@fonix.com *
168ldavis@fonix.com * @param line line number (counting from 1)
168ldavis@fonix.com * @return true if the xref for this revision is enabled, false otherwise
168ldavis@fonix.com */
168ldavis@fonix.com public boolean isEnabled(int line) {
168ldavis@fonix.com try {
168ldavis@fonix.com return lines.get(line-1).enabled;
168ldavis@fonix.com } catch (IndexOutOfBoundsException e) {
168ldavis@fonix.com return false;
168ldavis@fonix.com }
168ldavis@fonix.com }
168ldavis@fonix.com
168ldavis@fonix.com /**
87Knut.Hatlen@Sun.COM * Returns the size of the file (number of lines).
87Knut.Hatlen@Sun.COM *
87Knut.Hatlen@Sun.COM * @return number of lines
87Knut.Hatlen@Sun.COM */
87Knut.Hatlen@Sun.COM public int size() {
87Knut.Hatlen@Sun.COM return lines.size();
87Knut.Hatlen@Sun.COM }
87Knut.Hatlen@Sun.COM
87Knut.Hatlen@Sun.COM /**
87Knut.Hatlen@Sun.COM * Returns the widest revision string in the file (used for pretty
87Knut.Hatlen@Sun.COM * printing).
87Knut.Hatlen@Sun.COM *
87Knut.Hatlen@Sun.COM * @return number of characters in the widest revision string
87Knut.Hatlen@Sun.COM */
87Knut.Hatlen@Sun.COM public int getWidestRevision() {
87Knut.Hatlen@Sun.COM return widestRevision;
87Knut.Hatlen@Sun.COM }
87Knut.Hatlen@Sun.COM
87Knut.Hatlen@Sun.COM /**
87Knut.Hatlen@Sun.COM * Returns the widest author name in the file (used for pretty printing).
87Knut.Hatlen@Sun.COM *
87Knut.Hatlen@Sun.COM * @return number of characters in the widest author string
87Knut.Hatlen@Sun.COM */
87Knut.Hatlen@Sun.COM public int getWidestAuthor() {
87Knut.Hatlen@Sun.COM return widestAuthor;
87Knut.Hatlen@Sun.COM }
87Knut.Hatlen@Sun.COM
87Knut.Hatlen@Sun.COM /**
87Knut.Hatlen@Sun.COM * Adds a line to the file.
87Knut.Hatlen@Sun.COM *
87Knut.Hatlen@Sun.COM * @param revision revision number
87Knut.Hatlen@Sun.COM * @param author author name
87Knut.Hatlen@Sun.COM */
168ldavis@fonix.com void addLine(String revision, String author, boolean enabled) {
168ldavis@fonix.com final Line line = new Line(revision, author, enabled);
117ldavis@fonix.com lines.add(line);
117ldavis@fonix.com widestRevision = Math.max(widestRevision, line.revision.length());
117ldavis@fonix.com widestAuthor = Math.max(widestAuthor, line.author.length());
87Knut.Hatlen@Sun.COM }
87Knut.Hatlen@Sun.COM
1190trond.norbye@gmail.com void addDesc(String revision, String description) {
878Lubos.Kosco@Sun.COM desc.put(revision, Util.encode(description));
878Lubos.Kosco@Sun.COM }
878Lubos.Kosco@Sun.COM
1190trond.norbye@gmail.com public String getDesc(String revision) {
878Lubos.Kosco@Sun.COM return desc.get(revision);
878Lubos.Kosco@Sun.COM }
878Lubos.Kosco@Sun.COM
87Knut.Hatlen@Sun.COM /** Class representing one line in the file. */
87Knut.Hatlen@Sun.COM private static class Line {
87Knut.Hatlen@Sun.COM final String revision;
87Knut.Hatlen@Sun.COM final String author;
168ldavis@fonix.com final boolean enabled;
168ldavis@fonix.com Line(String rev, String aut, boolean ena) {
117ldavis@fonix.com revision = (rev == null) ? "" : rev;
117ldavis@fonix.com author = (aut == null) ? "" : aut;
168ldavis@fonix.com enabled = ena;
87Knut.Hatlen@Sun.COM }
87Knut.Hatlen@Sun.COM }
140trond.norbye@sun.com
140trond.norbye@sun.com public String getFilename() {
140trond.norbye@sun.com return filename;
140trond.norbye@sun.com }
878Lubos.Kosco@Sun.COM
878Lubos.Kosco@Sun.COM //TODO below might be useless, need to test with more SCMs and different commit messages
878Lubos.Kosco@Sun.COM // to see if it will not be usefull, if title attribute of <a> loses it's breath
878Lubos.Kosco@Sun.COM public void writeTooltipMap(Writer out) throws IOException {
1227trond.norbye@gmail.com out.append("<script type=\"text/javascript\">\nvar desc = new Object();\n");
878Lubos.Kosco@Sun.COM for (Entry<String, String> entry : desc.entrySet()) {
1227trond.norbye@gmail.com out.append("desc['");
1227trond.norbye@gmail.com out.append(entry.getKey());
1227trond.norbye@gmail.com out.append("'] = \"");
1227trond.norbye@gmail.com out.append(entry.getValue());
1227trond.norbye@gmail.com out.append("\";\n");
878Lubos.Kosco@Sun.COM }
1227trond.norbye@gmail.com out.append("</script>\n");
878Lubos.Kosco@Sun.COM }
878Lubos.Kosco@Sun.COM
878Lubos.Kosco@Sun.COM @Override
878Lubos.Kosco@Sun.COM public String toString() {
1227trond.norbye@gmail.com StringWriter sw = new StringWriter();
972Knut.Hatlen@Sun.COM for (Line line : lines) {
1227trond.norbye@gmail.com sw.append(line.revision);
1227trond.norbye@gmail.com sw.append("|");
1227trond.norbye@gmail.com sw.append(line.author);
1227trond.norbye@gmail.com sw.append(": \n");
972Knut.Hatlen@Sun.COM }
1227trond.norbye@gmail.com
972Knut.Hatlen@Sun.COM try {
972Knut.Hatlen@Sun.COM writeTooltipMap(sw);
972Knut.Hatlen@Sun.COM } catch (IOException e) {
972Knut.Hatlen@Sun.COM log.finest(e.getMessage());
972Knut.Hatlen@Sun.COM }
878Lubos.Kosco@Sun.COM
1227trond.norbye@gmail.com return sw.toString();
972Knut.Hatlen@Sun.COM }
}