Subversion Repositories XServices

Rev

Rev 113 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
94 brianR 1
package net.brutex.xservices.util;
2
 
3
import java.io.BufferedReader;
4
import java.io.File;
5
import java.io.FileReader;
6
import java.io.IOException;
7
 
8
/**
9
 * A struct containing the various bits of information in a CVS root string,
10
 * allowing easy retrieval of individual items of information
11
 */
12
public class CVSRoot {
13
	public String connectionType;
14
	public String user;
15
	public String host;
16
	public String repository;
17
 
18
	public CVSRoot(String root) throws IllegalArgumentException {
19
		if (!root.startsWith(":"))
20
			throw new IllegalArgumentException();
21
 
22
		int oldColonPosition = 0;
23
		int colonPosition = root.indexOf(':', 1);
24
		if (colonPosition == -1)
25
			throw new IllegalArgumentException();
26
		connectionType = root.substring(oldColonPosition + 1, colonPosition);
27
		oldColonPosition = colonPosition;
28
		colonPosition = root.indexOf('@', colonPosition + 1);
29
		if (colonPosition == -1)
30
			throw new IllegalArgumentException();
31
		user = root.substring(oldColonPosition + 1, colonPosition);
32
		oldColonPosition = colonPosition;
33
		colonPosition = root.indexOf(':', colonPosition + 1);
34
		if (colonPosition == -1)
35
			throw new IllegalArgumentException();
36
		host = root.substring(oldColonPosition + 1, colonPosition);
37
		repository = root.substring(colonPosition + 1);
38
		if (connectionType == null || user == null || host == null
39
				|| repository == null)
40
			throw new IllegalArgumentException();
41
	}
42
 
43
	public String getCVSRoot(File directory) {
44
		String root = null;
45
		BufferedReader r = null;
46
		try {
47
			File rootFile = new File(directory, "CVS/Root");
48
			if (rootFile.exists()) {
49
				r = new BufferedReader(new FileReader(rootFile));
50
				root = r.readLine();
51
			}
52
		} catch (IOException e) {
53
			// ignore
54
		} finally {
55
			try {
56
				if (r != null)
57
					r.close();
58
			} catch (IOException e) {
59
				System.err.println("Warning: could not close CVS/Root file!");
60
			}
61
		}
62
		if (root == null) {
63
			root = System.getProperty("cvs.root");
64
		}
65
		return root;
66
	}
67
}