KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > CVSEntry


1 /*
2  * CVSEntry.java
3  *
4  * Copyright (C) 2002 Peter Graves
5  * $Id: CVSEntry.java,v 1.1.1.1 2002/09/24 16:08:46 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 import java.util.Calendar JavaDoc;
25 import java.util.NoSuchElementException JavaDoc;
26 import java.util.StringTokenizer JavaDoc;
27 import java.util.TimeZone JavaDoc;
28
29 public final class CVSEntry
30 {
31     private String JavaDoc revision;
32     private long checkoutTime;
33
34     private CVSEntry(String JavaDoc revision, long checkoutTime)
35     {
36         this.revision = revision;
37         this.checkoutTime = checkoutTime;
38     }
39
40     public String JavaDoc getRevision()
41     {
42         return revision;
43     }
44
45     public long getCheckoutTime()
46     {
47         return checkoutTime;
48     }
49
50     public static CVSEntry parseEntryForFile(File file)
51     {
52         final String JavaDoc text = getEntryText(file);
53         if (text != null) {
54             String JavaDoc revision = null;
55             long checkoutTime = 0;
56             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(text, "/");
57             if (st.hasMoreTokens()) {
58                 // Ignore first token (filename).
59
st.nextToken();
60             }
61             if (st.hasMoreTokens())
62                 revision = st.nextToken();
63             String JavaDoc timeString = null;
64             if (st.hasMoreTokens())
65                 timeString = st.nextToken();
66             if (timeString == null || timeString.length() == 0 ||
67                 timeString.equals("dummy timestamp") ||
68                 timeString.equals("Result of merge"))
69                 return new CVSEntry(revision, 0);
70             st = new StringTokenizer JavaDoc(timeString, " :");
71             try {
72                 // Ignore first token (day of week).
73
st.nextToken();
74                 String JavaDoc monthName = st.nextToken();
75                 String JavaDoc months =
76                     "JanFebMarAprMayJunJulAugSepOctNovDec";
77                 // Month is zero-based.
78
int month = months.indexOf(monthName) / 3;
79                 int dayOfMonth = Integer.parseInt(st.nextToken());
80                 int hour = Integer.parseInt(st.nextToken());
81                 int minute = Integer.parseInt(st.nextToken());
82                 int second = Integer.parseInt(st.nextToken());
83                 int year = Integer.parseInt(st.nextToken());
84                 Calendar JavaDoc cal = Calendar.getInstance();
85                 cal.setTimeZone(TimeZone.getTimeZone("GMT+0000"));
86                 cal.set(year, month, dayOfMonth, hour, minute);
87                 cal.set(Calendar.SECOND, second);
88                 cal.set(Calendar.MILLISECOND, 0);
89                 checkoutTime = cal.getTime().getTime();
90             }
91             catch (NoSuchElementException JavaDoc e) {}
92             catch (NumberFormatException JavaDoc ex) {
93                 Log.error("parseEntryForFile NumberFormatException");
94                 Log.error("text = |" + text + "|");
95             }
96             if (revision != null && revision.length() > 0)
97                 return new CVSEntry(revision, checkoutTime);
98         }
99         return null;
100     }
101
102     private static String JavaDoc getEntryText(File file)
103     {
104         if (file == null)
105             return null;
106         if (file.isRemote())
107             return null;
108         File parentDir = file.getParentFile();
109         if (parentDir == null)
110             return null;
111         File cvsDir = File.getInstance(parentDir, "CVS");
112         if (cvsDir == null || !cvsDir.isDirectory())
113             return null;
114         File entriesFile = File.getInstance(cvsDir, "Entries");
115         if (entriesFile == null || !entriesFile.isFile())
116             return null;
117         String JavaDoc lookFor = "/".concat(file.getName()).concat("/");
118         SystemBuffer buf = new SystemBuffer(entriesFile);
119         buf.load();
120         for (Line line = buf.getFirstLine(); line != null; line = line.next()) {
121             String JavaDoc entry = line.getText();
122             if (entry.startsWith(lookFor))
123                 return entry;
124         }
125         return null;
126     }
127 }
128
Popular Tags