001: /*
002: * CVSEntry.java
003: *
004: * Copyright (C) 2002 Peter Graves
005: * $Id: CVSEntry.java,v 1.1.1.1 2002/09/24 16:08:46 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j;
023:
024: import java.util.Calendar;
025: import java.util.NoSuchElementException;
026: import java.util.StringTokenizer;
027: import java.util.TimeZone;
028:
029: public final class CVSEntry {
030: private String revision;
031: private long checkoutTime;
032:
033: private CVSEntry(String revision, long checkoutTime) {
034: this .revision = revision;
035: this .checkoutTime = checkoutTime;
036: }
037:
038: public String getRevision() {
039: return revision;
040: }
041:
042: public long getCheckoutTime() {
043: return checkoutTime;
044: }
045:
046: public static CVSEntry parseEntryForFile(File file) {
047: final String text = getEntryText(file);
048: if (text != null) {
049: String revision = null;
050: long checkoutTime = 0;
051: StringTokenizer st = new StringTokenizer(text, "/");
052: if (st.hasMoreTokens()) {
053: // Ignore first token (filename).
054: st.nextToken();
055: }
056: if (st.hasMoreTokens())
057: revision = st.nextToken();
058: String timeString = null;
059: if (st.hasMoreTokens())
060: timeString = st.nextToken();
061: if (timeString == null || timeString.length() == 0
062: || timeString.equals("dummy timestamp")
063: || timeString.equals("Result of merge"))
064: return new CVSEntry(revision, 0);
065: st = new StringTokenizer(timeString, " :");
066: try {
067: // Ignore first token (day of week).
068: st.nextToken();
069: String monthName = st.nextToken();
070: String months = "JanFebMarAprMayJunJulAugSepOctNovDec";
071: // Month is zero-based.
072: int month = months.indexOf(monthName) / 3;
073: int dayOfMonth = Integer.parseInt(st.nextToken());
074: int hour = Integer.parseInt(st.nextToken());
075: int minute = Integer.parseInt(st.nextToken());
076: int second = Integer.parseInt(st.nextToken());
077: int year = Integer.parseInt(st.nextToken());
078: Calendar cal = Calendar.getInstance();
079: cal.setTimeZone(TimeZone.getTimeZone("GMT+0000"));
080: cal.set(year, month, dayOfMonth, hour, minute);
081: cal.set(Calendar.SECOND, second);
082: cal.set(Calendar.MILLISECOND, 0);
083: checkoutTime = cal.getTime().getTime();
084: } catch (NoSuchElementException e) {
085: } catch (NumberFormatException ex) {
086: Log.error("parseEntryForFile NumberFormatException");
087: Log.error("text = |" + text + "|");
088: }
089: if (revision != null && revision.length() > 0)
090: return new CVSEntry(revision, checkoutTime);
091: }
092: return null;
093: }
094:
095: private static String getEntryText(File file) {
096: if (file == null)
097: return null;
098: if (file.isRemote())
099: return null;
100: File parentDir = file.getParentFile();
101: if (parentDir == null)
102: return null;
103: File cvsDir = File.getInstance(parentDir, "CVS");
104: if (cvsDir == null || !cvsDir.isDirectory())
105: return null;
106: File entriesFile = File.getInstance(cvsDir, "Entries");
107: if (entriesFile == null || !entriesFile.isFile())
108: return null;
109: String lookFor = "/".concat(file.getName()).concat("/");
110: SystemBuffer buf = new SystemBuffer(entriesFile);
111: buf.load();
112: for (Line line = buf.getFirstLine(); line != null; line = line
113: .next()) {
114: String entry = line.getText();
115: if (entry.startsWith(lookFor))
116: return entry;
117: }
118: return null;
119: }
120: }
|