1 package org.antmod.scm; 2 3 import java.text.ParseException ; 4 import java.util.StringTokenizer ; 5 6 import org.apache.commons.lang.math.NumberUtils; 7 8 49 public class ScmUrl { 50 private String urlString; 51 private String type; 52 private String protocol; 53 private String user; 54 private String password; 55 private String host; 56 private String path; 57 private String module; 58 59 64 public ScmUrl(String urlString) throws IllegalArgumentException , ParseException { 65 this.urlString = urlString; 66 StringTokenizer st = new StringTokenizer (urlString, ":"); 67 if (!st.hasMoreTokens() || !st.nextToken().equals("scm")) { 68 throw new IllegalArgumentException ("Invalid URL \"" + urlString + "\": does not start with 'scm:'"); 69 } 70 this.type = st.nextToken(); 71 72 if (this.type.equals("cvs")) { 73 cvsParse(st); 74 } else if (this.type.equals("svn")) { 75 svnParse(st); 76 } else { 77 throw new IllegalArgumentException ("Unknown URL type: '" + this.type + "'"); 78 } 79 } 80 81 82 private void cvsParse(StringTokenizer st) throws ParseException { 83 if (!st.hasMoreTokens()) { 85 throw new ParseException ("Element 'protocol' missing from CVS url", 1); 86 } 87 this.protocol = st.nextToken(); 88 89 if (!st.hasMoreTokens()) { 91 throw new ParseException ("Element 'user@host' missing from CVS url", 2); 92 } 93 94 String userHost = st.nextToken(); 96 if (!this.protocol.equals("local")) { 97 int atIndex = userHost.indexOf("@"); 98 if (atIndex < 0) { 99 this.user = userHost; 101 102 if (!st.hasMoreTokens()) { 103 throw new ParseException ("After username \"" + this.user + "\" the element 'password@host' is missing from CVS url", 3); 104 } 105 String passHost = st.nextToken(); 106 atIndex = passHost.indexOf("@"); 107 if (atIndex < 0) { 108 throw new ParseException ("After username \"" + this.user + "\" the element 'password@host' is missing from CVS url", 3); 109 } else { 110 this.password = passHost.substring(0, atIndex); 111 this.host = passHost.substring(atIndex + 1); 112 } 113 } else { 114 this.user = userHost.substring(0, atIndex); 116 this.host = userHost.substring(atIndex + 1); 117 } 118 } 119 120 if (this.protocol.equals("local") && !userHost.equalsIgnoreCase("ignored")) { 122 this.path = userHost; 123 } else { 124 if (!st.hasMoreTokens()) { 125 throw new ParseException ("Element 'path' missing from CVS url", 4); 126 } 127 this.path = st.nextToken(); 128 } 129 130 if (this.path.length() == 1) { 132 this.path = this.path + ":" + st.nextToken(); 134 this.protocol = null; 135 } 136 137 int slashIndex = this.path.indexOf("/"); 139 if (slashIndex > 0 && NumberUtils.isNumber(this.path.substring(0, slashIndex))) { 140 this.host = this.host + ":" + this.path.substring(0, slashIndex); 141 this.path = this.path.substring(slashIndex); 142 } 143 144 if (st.hasMoreTokens()) { 146 this.module = st.nextToken(); 147 } 148 } 149 150 151 private void svnParse(StringTokenizer st) throws ParseException { 152 if (!st.hasMoreTokens()) { 154 throw new ParseException ("Element 'protocol' missing from Subversion url", 1); 155 } 156 this.protocol = st.nextToken(); 157 158 if (!st.hasMoreTokens()) { 160 throw new ParseException ("Subversion url seems to have only protocol part.", 2); 161 } 162 163 String userHost = st.nextToken(); 164 if (!userHost.startsWith("//")) { 165 throw new ParseException ("Subversion url does not have '//' after protocol.", 3); 166 } 167 168 if (protocol.equals("file")) { 169 while (userHost.startsWith("/")) { 170 userHost = userHost.substring(1); 171 } 172 this.path = userHost; 173 } else { 174 userHost = userHost.substring(2); 176 177 int atIndex = userHost.indexOf("@"); 178 int slashIndex = userHost.indexOf("/"); 179 if (atIndex > 0) { 180 this.user = userHost.substring(0, atIndex); 182 userHost = userHost.substring(atIndex + 1); 183 } else if (slashIndex < 0) { 184 this.user = userHost; 186 187 if (!st.hasMoreTokens()) { 188 throw new ParseException ("After username \"" + this.user + "\" the element 'password@host' is missing from Subversion url", 4); 189 } 190 String passHost = st.nextToken(); 191 atIndex = passHost.indexOf("@"); 192 if (atIndex < 0) { 193 throw new ParseException ("After username \"" + this.user + "\" the element 'password@host' is missing from CVS url", 4); 194 } 195 this.password = passHost.substring(0, atIndex); 196 userHost = passHost.substring(atIndex + 1); 197 } else { 198 } 200 201 slashIndex = userHost.indexOf("/"); 203 if (slashIndex < 0) { 204 if (st.hasMoreTokens()) { 206 String portPath = st.nextToken(); 207 slashIndex = portPath.indexOf("/"); 208 if (slashIndex < 0) { 209 throw new ParseException ("Element 'path' missing from Subversion url, no '/' found that separates host and path", 5); 210 } else { 211 this.host = userHost + portPath.substring(0, slashIndex); 212 this.path = portPath.substring(slashIndex); 213 } 214 } else { 215 throw new ParseException ("Element 'path' missing from Subversion url, no '/' found that separates host and path", 5); 216 } 217 } else { 218 this.host = userHost.substring(0, slashIndex); 219 this.path = userHost.substring(slashIndex); 220 } 221 } 222 223 if (this.path.endsWith("/")) { 225 this.path = this.path.substring(0, this.path.length() - 1); 226 } 227 if (this.path.length() == 1) { 228 this.path = this.path + ":" + st.nextToken(); 230 } 231 232 if (st.hasMoreTokens()) { 234 this.module = st.nextToken(); 235 } 236 } 237 238 239 242 public String getHost() { 243 return host; 244 } 245 246 249 public String getModule() { 250 return module; 251 } 252 253 256 public String getPassword() { 257 return password; 258 } 259 260 263 public String getPath() { 264 return path; 265 } 266 267 270 public String getProtocol() { 271 return protocol; 272 } 273 274 277 public String getType() { 278 return type; 279 } 280 281 284 public String getUser() { 285 return user; 286 } 287 288 291 public String getUrlString() { 292 return urlString; 293 } 294 295 299 public String getUrlStringNoModule() { 300 if (getModule() != null) { 301 int lastColon = urlString.lastIndexOf(":"); 302 if (lastColon <= 0) { 303 throw new IllegalStateException ("URL string appears to have a module element but no ':' in its url string."); 304 } 305 return urlString.substring(0, lastColon); 306 } else { 307 return urlString; 308 } 309 } 310 } 311 | Popular Tags |