1 /*
2 Bloof - visualize the evolution of your software project
3 Copyright ( C ) 2003 Lukasz Pekacki <lukasz@pekacki.de>
4 http://bloof.sf.net/
5
6 This file was part of JavaCVS by Nicholas Allen ( nallen@freenet.co.uk ).
7
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License along with
17 this program; if not, write to the Free Software Foundation, Inc.,
18 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
20 $RCSfile: RepositoryLocation.java,v $
21 Created on $Date: 2003/09/06 08:35:09 $
22 */
23
24 package net.sf.bloof.scm.cvsplugin;
25
26 /***
27 * Defines the location of a repository.
28 * @author Nicholas Allen
29 * @author Lukasz Pekacki
30 * */
31
32 public class RepositoryLocation {
33
34 /***
35 * Creates a CVSRepositoryLocation object using the specified parameters
36 * @param aMethod method of connection
37 * @param aUserName login of the user
38 * @param aHostName name of cvs host
39 * @param aRepositoryPath path to CVS repository
40 * @throws InvalidRepositoryLocationException if the specified String does not match
41 */
42 public RepositoryLocation(
43 CvsConnectionMethod aMethod,
44 String aUserName,
45 String aHostName,
46 String aRepositoryPath)
47 throws InvalidRepositoryLocationException {
48 if (aMethod == null) {
49 throw new InvalidRepositoryLocationException("Method not specified");
50 }
51
52 if (!aMethod.equals(CvsConnectionMethod.LOCAL)
53 && (aHostName == null || aHostName.trim().length() == 0)) {
54 throw new InvalidRepositoryLocationException("Host name not specified");
55 }
56
57 if (aRepositoryPath == null || aRepositoryPath.trim().length() == 0) {
58 throw new InvalidRepositoryLocationException("Repository path not specified");
59 }
60
61 mConnectionMethod = aMethod;
62 mUserName = aUserName;
63 mHostName = aHostName;
64 mRepositoryPath = aRepositoryPath;
65 }
66
67 /***
68 * Creates a CVSRepositoryLocation object using sLocation as the location.
69 * @param aLocation the location of the repository. This is of the form:<p>
70 * <pre>:method:user@host:/path/to/repository</pre>
71 * @throws InvalidRepositoryLocationException if the specified String does not match
72 * */
73 public RepositoryLocation(String aLocation) throws InvalidRepositoryLocationException {
74 if (aLocation == null || aLocation.length() < 10) {
75 throw new InvalidRepositoryLocationException(
76 "The repository location is null or too short:" + aLocation);
77 }
78
79 int nEndOfConnectionMethod = -1;
80
81 // Check if repository connection method is explicitly defined
82
83 if (aLocation.charAt(0) == ':') {
84 nEndOfConnectionMethod = aLocation.indexOf(':', 1);
85
86 if (nEndOfConnectionMethod == -1) {
87 throw new InvalidRepositoryLocationException(
88 "Connection method is not" + "contained between two ':' characters");
89 }
90
91 String sConnectionMethod = aLocation.substring(1, nEndOfConnectionMethod);
92
93 if (sConnectionMethod.equals("local")) {
94 mConnectionMethod = CvsConnectionMethod.LOCAL;
95 } else if (sConnectionMethod.equals("sample")) {
96 mConnectionMethod = CvsConnectionMethod.SAMPLE;
97 } else if (sConnectionMethod.equals("logfile")) {
98 mConnectionMethod = CvsConnectionMethod.LOGFILE;
99 } else if (sConnectionMethod.equals("pserver")) {
100 mConnectionMethod = CvsConnectionMethod.PSERVER;
101 } else if (
102 sConnectionMethod.equals("ssh")
103 || sConnectionMethod.equals("ext")
104 || sConnectionMethod.equals("server")) {
105 mConnectionMethod = CvsConnectionMethod.EXT;
106 } else {
107 throw new InvalidRepositoryLocationException(
108 "Unrecognized connection method: " + sConnectionMethod);
109 }
110 } else {
111 if (aLocation.indexOf(':') == -1) {
112 mConnectionMethod = CvsConnectionMethod.LOCAL;
113 } else {
114 throw new InvalidRepositoryLocationException(
115 "Unrecognized connection method: " + aLocation);
116 }
117 }
118
119 if (!mConnectionMethod.equals(CvsConnectionMethod.LOCAL)) {
120 // Check for user and host
121
122 int nEndOfHost = aLocation.indexOf(':', nEndOfConnectionMethod + 1);
123 // Host specified
124 if (nEndOfHost != -1) {
125 String sUserAndHostName =
126 aLocation.substring(nEndOfConnectionMethod + 1, nEndOfHost);
127 int nEndOfUser = sUserAndHostName.indexOf('@');
128 // User name specified
129 if (nEndOfUser != -1) {
130 mUserName = sUserAndHostName.substring(0, nEndOfUser).trim();
131 }
132 mHostName = sUserAndHostName.substring(nEndOfUser + 1).trim();
133
134 if (mHostName.length() == 0) {
135 throw new InvalidRepositoryLocationException("The host name was not specified");
136 }
137 } else {
138 // host not specified
139 throw new InvalidRepositoryLocationException("The host was not specified");
140 }
141
142 mRepositoryPath = aLocation.substring(nEndOfHost + 1);
143 } else {
144 if (aLocation.startsWith(":local:")) {
145 mRepositoryPath = aLocation.substring(7);
146 } else {
147 mRepositoryPath = aLocation;
148 }
149 }
150
151 if (mRepositoryPath.endsWith("/")) {
152 mRepositoryPath = mRepositoryPath.substring(0, mRepositoryPath.length() - 1);
153 }
154
155 mRepositoryPath = mRepositoryPath.trim();
156
157 if (mRepositoryPath.length() == 0) {
158 throw new InvalidRepositoryLocationException("Repository path not specified");
159 }
160 }
161
162 /***
163 * Creates a RepositoryLocation with asserted valid parameters
164 * This method is used internally for local logfile and sample log file access
165 * @param aMethod method
166 * @param aUserName username
167 * @param aHostName hostname
168 * @param aRepositoryPath path
169 * @return RepositoryLocation
170 */
171 public static RepositoryLocation createValidLocation(
172 CvsConnectionMethod aMethod,
173 String aUserName,
174 String aHostName,
175 String aRepositoryPath) {
176 try {
177 return new RepositoryLocation(aMethod, aUserName, aHostName, aRepositoryPath);
178 } catch (InvalidRepositoryLocationException e) {
179 // ignore
180 }
181 return null;
182 }
183
184 /***
185 * Gets the connection method for this location
186 * @return CvsConnectionMethod
187 */
188 public CvsConnectionMethod getConnectionMethod() {
189 return mConnectionMethod;
190 }
191
192 /***
193 * Returns the CVSROOT Representation of this Location
194 * @return CVSROOT Representation of this Location
195 */
196 public String getCvsRoot() {
197 if (mStringRepresentation == null) {
198 StringBuffer buffer = new StringBuffer(30);
199
200 buffer.append(':');
201 buffer.append(mConnectionMethod.getCvsRootMethodName());
202 buffer.append(':');
203
204 if (!mConnectionMethod.equals(CvsConnectionMethod.LOCAL)) {
205 if (mUserName != null) {
206 buffer.append(mUserName);
207 buffer.append('@');
208 }
209
210 buffer.append(mHostName);
211 buffer.append(':');
212 }
213
214 buffer.append(mRepositoryPath);
215
216 mStringRepresentation = buffer.toString();
217 }
218
219 return mStringRepresentation;
220 }
221
222 /***
223 * Gets the host name.
224 * @return String
225 */
226 public String getHostName() {
227 return mHostName;
228 }
229
230 /***
231 * Gets the path to the repository.
232 * @return String
233 */
234 public String getRepositoryPath() {
235 return mRepositoryPath;
236 }
237
238 /***
239 * Gets the user name.
240 * @return String
241 */
242 public String getUserName() {
243 return mUserName;
244 }
245 /***
246 * @param aMethod method
247 */
248 public void setConnectionMethod(CvsConnectionMethod aMethod) {
249 mConnectionMethod = aMethod;
250 }
251
252 /***
253 * Converts this location to a string of the form:<p>
254 * <pre>:method:user@host:/path/to/repository</pre>
255 * @return String
256 */
257 public String toString() {
258 if (mStringRepresentation == null) {
259 StringBuffer buffer = new StringBuffer(30);
260
261 buffer.append(':');
262 buffer.append(mConnectionMethod.toString());
263 buffer.append(':');
264
265 if (!mConnectionMethod.equals(CvsConnectionMethod.LOCAL)) {
266 if (mUserName != null) {
267 buffer.append(mUserName);
268 buffer.append('@');
269 }
270
271 buffer.append(mHostName);
272 buffer.append(':');
273 }
274
275 buffer.append(mRepositoryPath);
276
277 mStringRepresentation = buffer.toString();
278 }
279
280 return mStringRepresentation;
281 }
282 private CvsConnectionMethod mConnectionMethod;
283 private String mStringRepresentation;
284 private String mUserName, mHostName, mRepositoryPath;
285
286 }
This page was automatically generated by Maven