1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package net.sf.statcvs.input;
24
25 import java.io.BufferedReader;
26 import java.io.FileReader;
27 import java.io.IOException;
28 import java.util.Hashtable;
29 import java.util.StringTokenizer;
30 import java.util.logging.Logger;
31
32 import net.sf.statcvs.util.FileUtils;
33
34
35
36
37
38
39
40
41
42 public class RepositoryFileManager {
43 private static Logger logger;
44 private final String path;
45 private final Hashtable revByFilename = new Hashtable();
46
47
48
49
50
51
52 public RepositoryFileManager(final String pathName) {
53 path = pathName;
54 logger = Logger.getLogger(getClass().getName());
55 }
56
57
58
59
60
61
62
63
64
65 public int getLinesOfCode(final String filename) throws NoLineCountException {
66 final String absoluteName = FileUtils.getAbsoluteName(this.path, filename);
67 try {
68 final FileReader freader = new FileReader(absoluteName);
69 final BufferedReader reader = new BufferedReader(freader);
70 final int linecount = getLineCount(reader);
71 logger.finer("line count for '" + absoluteName + "': " + linecount);
72 freader.close();
73 return linecount;
74 } catch (final IOException e) {
75 throw new NoLineCountException("could not get line count for '" + absoluteName + "': " + e);
76 }
77 }
78
79 private int getLineCount(final BufferedReader reader) throws IOException {
80 int linecount = 0;
81 while (reader.readLine() != null) {
82 linecount++;
83 }
84 return linecount;
85 }
86
87
88
89
90
91
92
93 public String getRevision(final String filename) throws IOException {
94 String rev = (String) revByFilename.get(filename);
95 if (rev != null) {
96 return rev;
97 }
98
99 final String baseDir = FileUtils.getParentDirectoryPath(filename);
100 final String entriesFilename = baseDir + "CVS" + FileUtils.getDefaultDirSeparator() + "Entries";
101
102
103 final String absoluteName = FileUtils.getAbsoluteName(this.path, entriesFilename);
104 final BufferedReader in = new BufferedReader(new FileReader(absoluteName));
105 try {
106 String line;
107 while ((line = in.readLine()) != null) {
108 if (line.startsWith("D")) {
109
110 } else {
111
112 final StringTokenizer t = new StringTokenizer(line, "/");
113 if (t.countTokens() >= 2) {
114 revByFilename.put(baseDir + t.nextToken(), t.nextToken());
115 } else {
116
117 }
118 }
119 }
120
121 rev = (String) revByFilename.get(filename);
122 if (rev != null) {
123 return rev;
124 } else {
125 throw new IOException("File " + filename + " has no revision");
126 }
127 } finally {
128 in.close();
129 }
130 }
131
132 }