1 11 package org.eclipse.team.internal.ccvs.core.filesystem; 12 13 import java.net.URI ; 14 import java.net.URISyntaxException ; 15 import java.util.StringTokenizer ; 16 17 import org.eclipse.core.runtime.*; 18 import org.eclipse.osgi.util.NLS; 19 import org.eclipse.team.internal.ccvs.core.*; 20 import org.eclipse.team.internal.ccvs.core.connection.CVSRepositoryLocation; 21 import org.eclipse.team.internal.ccvs.core.resources.RemoteFile; 22 import org.eclipse.team.internal.ccvs.core.resources.RemoteFolder; 23 import org.eclipse.team.internal.ccvs.core.syncinfo.ResourceSyncInfo; 24 25 public class CVSURI { 26 27 private static final String SCHEME = "cvs"; private final ICVSRepositoryLocation repository; 29 private final IPath path; 30 private final CVSTag tag; 31 private final String revision; 32 33 43 public static CVSURI fromUri(URI uri) { 44 try { 45 ICVSRepositoryLocation repository = getRepository(uri); 46 if (repository != null) { 47 IPath path = new Path(null, uri.getPath()); 48 CVSTag tag = getTag(uri); 49 String revision = getRevision(uri); 50 return new CVSURI(repository, path, tag, revision); 51 } else { 52 repository = getOldRepository(uri); 53 IPath path = getOldPath(uri); 54 CVSTag tag = getOldTag(uri); 55 return new CVSURI(repository, path, tag); 56 } 57 } catch (CVSException e) { 58 CVSProviderPlugin.log(e); 59 throw new IllegalArgumentException (NLS.bind(CVSMessages.CVSURI_InvalidURI, new String [] {uri.toString(), e.getMessage()})); 60 } 61 } 62 63 private static CVSTag getTag(URI uri) { 64 String query = uri.getQuery(); 65 if (query == null) 66 return null; 67 StringTokenizer tokens = new StringTokenizer (query, ","); while (tokens.hasMoreTokens()) { 69 String token = tokens.nextToken(); 70 int index = token.indexOf('='); 71 if (index != -1) { 72 String type = token.substring(0, index); 73 String value = token.substring(index + 1); 74 if (value.length() > 0) { 75 int tagType = getTagType(type); 76 if (tagType != -1) 77 return new CVSTag(value, tagType); 78 } 79 } 80 } 81 return null; 82 } 83 84 private static String getRevision(URI uri) { 85 String query = uri.getQuery(); 86 if (query == null) 87 return null; 88 StringTokenizer tokens = new StringTokenizer (query, ","); while (tokens.hasMoreTokens()) { 90 String token = tokens.nextToken(); 91 int index = token.indexOf('='); 92 if (index != -1) { 93 String type = token.substring(0, index); 94 String value = token.substring(index + 1); 95 if (type.equals("revision") && isValidRevision(value)) { return value; 97 } 98 } 99 } 100 return null; 101 } 102 103 private static boolean isValidRevision(String value) { 104 return value.matches("\\d+\\.\\d+(?:\\.\\d+)*"); } 106 107 private static int getTagType(String type) { 108 if (type.equalsIgnoreCase("version")) return CVSTag.VERSION; 110 if (type.equalsIgnoreCase("branch")) return CVSTag.BRANCH; 112 if (type.equalsIgnoreCase("date")) return CVSTag.DATE; 114 return -1; 115 } 116 117 private static ICVSRepositoryLocation getRepository(URI uri) throws CVSException { 118 String authority = uri.getAuthority(); 119 if (authority.indexOf('/') != -1) 120 return null; 121 if (authority.indexOf('!') == -1) 122 return null; 123 authority = decodeAuthority(authority); 124 return CVSRepositoryLocation.fromString(authority); 125 } 126 127 private static CVSTag getOldTag(URI uri) { 128 String f = uri.getFragment(); 129 int i = f.indexOf(','); 130 if (i == -1) { 131 return CVSTag.DEFAULT; 132 } 133 134 return CVSTag.DEFAULT; } 136 137 private static IPath getOldPath(URI uri) { 138 String path = uri.getFragment(); 139 int i = path.indexOf(','); 140 if (i != -1) { 141 path = path.substring(0, i); 142 } 143 return new Path(path); 144 } 145 146 private static ICVSRepositoryLocation getOldRepository(URI uri) throws CVSException { 147 String ssp = uri.getSchemeSpecificPart(); 148 if (!ssp.startsWith(":")) { ssp = ":" + ssp; } 151 return CVSRepositoryLocation.fromString(ssp); 152 } 153 154 public CVSURI(ICVSRepositoryLocation repository, IPath path, CVSTag tag) { 155 this(repository, path, tag, null); 156 } 157 158 public CVSURI(ICVSRepositoryLocation repository, IPath path, CVSTag tag, String revision) { 159 this.repository = repository; 160 this.path = path; 161 this.tag = tag; 162 if (revision != null && !revision.equals(ResourceSyncInfo.ADDED_REVISION)) 163 this.revision = revision; 164 else 165 this.revision = null; 166 } 167 168 public CVSURI append(String name) { 169 return new CVSURI(repository, path.append(name), tag); 170 } 171 172 public CVSURI append(IPath childPath) { 173 return new CVSURI(repository, path.append(childPath), tag); 174 } 175 176 public String getLastSegment() { 177 return path.lastSegment(); 178 } 179 180 public URI toURI() { 181 try { 182 String authority = repository.getLocation(false); 183 authority = ensureRegistryBasedAuthority(authority); 184 String pathString = path.toString(); 185 if (!pathString.startsWith("/")) { pathString = "/" + pathString; } 188 String query = null; 189 if (tag != null && tag.getType() != CVSTag.HEAD) { 190 query = getQueryType(tag) + "=" + tag.getName(); } 192 if (revision != null) { 193 String string = "revision=" + revision; if (query == null) { 195 query = string; 196 } else { 197 query = query + "," + string; } 199 } 200 return new URI (SCHEME, authority, pathString, query, null); 201 } catch (URISyntaxException e) { 202 CVSProviderPlugin.log(IStatus.ERROR, NLS.bind("An error occurred while creating a URI for {0} {1}", repository, path), e); throw new IllegalStateException (e.getMessage()); 204 } 205 } 206 207 212 private String ensureRegistryBasedAuthority(String authority) { 213 authority = encode('/', '!', authority); 215 authority = encode('@', '~', authority); 217 authority = encode(':', '_', authority); 219 return authority; 220 } 221 222 private static String decodeAuthority(String authority) { 223 authority = decode('/', '!', authority); 224 authority = decode('@', '~', authority); 225 authority = decode(':', '_', authority); 226 return authority; 227 } 228 229 private String encode(char charToEncode, char encoding, String string) { 230 String result = string.replaceAll(new String (new char[] { encoding }), new String (new char[] { encoding, encoding })); 232 return result.replace(charToEncode, encoding); 234 } 235 236 private static String decode(char encodedChar, char encoding, String string) { 237 String reuslt = string.replace(encoding, encodedChar); 239 return reuslt.replaceAll(new String (new char[] { encodedChar, encodedChar }), new String (new char[] { encoding })); 241 } 242 243 private static String getQueryType(CVSTag tag) { 244 switch (tag.getType()) { 245 case CVSTag.BRANCH: 246 return "branch"; case CVSTag.DATE: 248 return "date"; } 250 return "version"; } 252 253 public boolean isRepositoryRoot() { 254 return path.segmentCount() == 0; 255 } 256 257 public CVSURI removeLastSegment() { 258 return new CVSURI(repository, path.removeLastSegments(1), tag); 259 } 260 261 public ICVSRemoteFolder getParentFolder() { 262 return removeLastSegment().toFolder(); 263 } 264 265 public String getRepositoryName() { 266 return repository.toString(); 267 } 268 269 public CVSURI getProjectURI(){ 270 return new CVSURI(repository, path.uptoSegment(1), tag); 271 } 272 273 public ICVSRemoteFolder toFolder() { 274 return new RemoteFolder(null, repository, path.toString(), tag); 275 } 276 277 public ICVSRemoteFile toFile() { 278 return RemoteFile.create(path.toString(), repository, tag, revision); 280 } 281 282 public String toString() { 283 return "[Path: "+this.path.toString()+" Tag: "+tag.getName()+ " Repo: " +repository.getRootDirectory() +"]"; } 285 286 public IPath getPath(){ 287 return path; 288 } 289 290 public IPath getProjectStrippedPath() { 291 if (path.segmentCount() > 1) 292 return path.removeFirstSegments(1); 293 294 return path; 295 } 296 297 public ICVSRepositoryLocation getRepository() { 298 return repository; 299 } 300 301 public CVSTag getTag() { 302 return tag; 303 } 304 305 public String getRevision() { 306 return revision; 307 } 308 } 309 | Popular Tags |