001: /*
002: * Gruntspud
003: *
004: * Copyright (C) 2002 Brett Smith.
005: *
006: * Written by: Brett Smith <t_magicthize@users.sourceforge.net>
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU Library General Public License
010: * as published by the Free Software Foundation; either version 2 of
011: * the License, or (at your option) any later version.
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 Library General Public License for more details.
016: *
017: * You should have received a copy of the GNU Library General Public
018: * License along with this program; if not, write to the Free Software
019: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
020: */
021:
022: package gruntspud;
023:
024: /**
025: * Description of the Class
026: *
027: * @author magicthize
028: */
029: public class CVSRoot {
030: private String connectionType = null;
031: private String user = null;
032: private String host = null;
033: private String repository = null;
034: private int port = 0;
035:
036: /**
037: * Construct a new empty <code>CVSRoot</code>
038: * @throws IllegalArgumentException DOCUMENT ME!
039: */
040: public CVSRoot() throws IllegalArgumentException {
041: port = -1;
042: user = "";
043: host = "";
044: repository = "";
045: connectionType = "";
046: }
047:
048: /**
049: * Construct a new <code>CVSRoot</code> given a string. The root will be
050: * split up type, user, host and repository (depending on the type some of
051: * these may not be applicable or available)
052: *
053: * @param s
054: *
055: * @exception IllegalArgumentException Description of the Exception
056: */
057: public CVSRoot(String s) throws IllegalArgumentException {
058: port = -1;
059:
060: if (!s.startsWith(":")) {
061: throw new IllegalArgumentException("CVSROOT '" + s
062: + "' does not start with :");
063: }
064:
065: int i = 0;
066: int j = s.indexOf(':', 1);
067:
068: if (j == -1) {
069: throw new IllegalArgumentException("CVSROOT '" + s
070: + "' has no connection method");
071: }
072:
073: connectionType = s.substring(i + 1, j);
074: i = j;
075: j = s.indexOf('@', j + 1);
076:
077: if (j != -1) {
078: user = s.substring(i + 1, j);
079: i = j;
080: } else {
081: j = i;
082:
083: // search for host
084: }
085: j = s.indexOf(':', j + 1);
086:
087: if (j == -1) {
088: // ??? j is -1
089: j = s.indexOf('/', j + 1);
090:
091: if (j != -1) {
092: host = s.substring(i + 1, j);
093: repository = s.substring(j);
094: } else {
095: repository = s;
096: }
097: } else {
098: if (connectionType.equals("local")) {
099:
100: // no host
101: repository = s.substring(i + 1);
102: } else {
103: host = s.substring(i + 1, j);
104: repository = s.substring(j + 1);
105: }
106: }
107:
108: boolean flag = true;
109: int k = 0;
110: String s1 = "";
111: int z = repository.indexOf(':', 0);
112:
113: StringBuffer nb = new StringBuffer();
114: for (int p = 0; p < repository.length(); p++) {
115: char ch = repository.charAt(p);
116: if (ch >= '0' && ch <= '9')
117: nb.append(ch);
118: else
119: break;
120: }
121: if (nb.length() != 0) {
122: try {
123: port = Integer.parseInt(nb.toString());
124: } catch (Exception e) {
125: }
126: repository = repository.substring(nb.length());
127: }
128: if (repository.charAt(0) == ':')
129: repository = repository.substring(1);
130:
131: if ((connectionType == null) || (repository == null)) {
132: throw new IllegalArgumentException(
133: "Somethind is missing from CVSROOT '" + s + "'");
134: } else {
135:
136: return;
137: }
138: }
139:
140: /**
141: * Description of the Method
142: *
143: * @param o Description of the Parameter
144: *
145: * @return Description of the Return Value
146: */
147: public boolean equals(Object o) {
148: return String.valueOf(o).equals(toString());
149: }
150:
151: /**
152: * Gets the connectionType attribute of the CVSRoot object
153: *
154: * @return The connectionType value
155: */
156: public String getConnectionType() {
157: return connectionType;
158: }
159:
160: /**
161: * Gets the user attribute of the CVSRoot object
162: *
163: * @return The user value
164: */
165: public String getUser() {
166: return user;
167: }
168:
169: /**
170: * Gets the host attribute of the CVSRoot object
171: *
172: * @return The host value
173: */
174: public String getHost() {
175: return host;
176: }
177:
178: /**
179: * Gets the repository attribute of the CVSRoot object
180: *
181: * @return The repository value
182: */
183: public String getRepository() {
184: return repository;
185: }
186:
187: /**
188: * Gets the port attribute of the CVSRoot object
189: *
190: * @return The port value
191: */
192: public int getPort() {
193: return port;
194: }
195:
196: /**
197: * Gets the connectionType attribute of the CVSRoot object
198: *
199: * @param connectionType DOCUMENT ME!
200: */
201: public void setConnectionType(String connectionType) {
202: this .connectionType = connectionType;
203: }
204:
205: /**
206: * Gets the user attribute of the CVSRoot object
207: *
208: * @param user DOCUMENT ME!
209: */
210: public void setUser(String user) {
211: this .user = user;
212: }
213:
214: /**
215: * Gets the host attribute of the CVSRoot object
216: *
217: * @param host DOCUMENT ME!
218: */
219: public void setHost(String host) {
220: this .host = host;
221: }
222:
223: /**
224: * Gets the repository attribute of the CVSRoot object
225: *
226: * @param repository DOCUMENT ME!
227: */
228: public void setRepository(String repository) {
229: this .repository = repository;
230: }
231:
232: /**
233: * Gets the port attribute of the CVSRoot object
234: *
235: * @param port DOCUMENT ME!
236: */
237: public void setPort(int port) {
238: this .port = port;
239: }
240:
241: /**
242: * Description of the Method
243: *
244: * @return Description of the Return Value
245: */
246: public String toString() {
247: StringBuffer buf = new StringBuffer();
248: buf.append(':');
249: buf.append(getConnectionType());
250: buf.append(':');
251:
252: if ((user != null) && !user.equals("")) {
253: buf.append(user);
254:
255: }
256: if ((host != null) && !host.equals("")) {
257: if ((user != null) && !user.equals("")) {
258: buf.append('@');
259:
260: }
261: buf.append(getHost());
262:
263: if (getPort() != -1) {
264: buf.append(':');
265: buf.append(getPort());
266:
267: //
268: // if ( (repository != null) && !repository.equals("")) {
269: // buf.append(":");
270: // }
271: } else if ((repository != null) && !repository.equals("")) {
272: buf.append(":");
273: }
274: }
275:
276: buf.append(((repository == null) || repository.equals("")) ? ""
277: : repository);
278:
279: return buf.toString();
280: }
281:
282: /**
283: * DOCUMENT ME!
284: *
285: * @param context DOCUMENT ME!
286: */
287: public void debugDetails(GruntspudContext context) {
288: Constants.SYSTEM_LOG.debug("CVSROOT ..");
289: Constants.SYSTEM_LOG.debug(" type = "
290: + getConnectionType());
291: Constants.SYSTEM_LOG.debug(" user = " + getUser());
292: Constants.SYSTEM_LOG.debug(" host = " + getHost());
293: Constants.SYSTEM_LOG.debug(" repository = " + getRepository());
294: Constants.SYSTEM_LOG.debug(" port = " + getPort());
295: }
296:
297: /**
298: * The main program for the CVSRoot class
299: *
300: * @param args The command line arguments
301: *
302: * @exception IllegalArgumentException Description of the Exception
303: */
304: public static void main(String[] args)
305: throws IllegalArgumentException {
306: CVSRoot r = new CVSRoot(args[0]);
307: }
308: }
|