KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > core > filesystem > CVSURI


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.team.internal.ccvs.core.filesystem;
12
13 import java.net.URI JavaDoc;
14 import java.net.URISyntaxException JavaDoc;
15 import java.util.StringTokenizer JavaDoc;
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 JavaDoc SCHEME = "cvs"; //$NON-NLS-1$
28
private final ICVSRepositoryLocation repository;
29     private final IPath path;
30     private final CVSTag tag;
31     private final String JavaDoc revision;
32
33     /**
34      * Convert the given URI to a CVSURI. There are two supported formats: the
35      * original opaque format and a newer hierarchical format.
36      * <ul>
37      * <li>cvs://[:]method:user[:password]@host:[port]/root/path#project/path[,tagName]</li>
38      * <li>cvs://_method_user[_password]~host_[port]!root!path/project/path[?<version,branch,date,revision>=tagName]</li>
39      * </ul>
40      * @param uri the URI
41      * @return a CVS URI
42      */

43     public static CVSURI fromUri(URI JavaDoc 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 JavaDoc 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 JavaDoc(NLS.bind(CVSMessages.CVSURI_InvalidURI, new String JavaDoc[] {uri.toString(), e.getMessage()}));
60         }
61     }
62
63     private static CVSTag getTag(URI JavaDoc uri) {
64         String JavaDoc query = uri.getQuery();
65         if (query == null)
66             return null;
67         StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(query, ","); //$NON-NLS-1$
68
while (tokens.hasMoreTokens()) {
69             String JavaDoc token = tokens.nextToken();
70             int index = token.indexOf('=');
71             if (index != -1) {
72                 String JavaDoc type = token.substring(0, index);
73                 String JavaDoc 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 JavaDoc getRevision(URI JavaDoc uri) {
85         String JavaDoc query = uri.getQuery();
86         if (query == null)
87             return null;
88         StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(query, ","); //$NON-NLS-1$
89
while (tokens.hasMoreTokens()) {
90             String JavaDoc token = tokens.nextToken();
91             int index = token.indexOf('=');
92             if (index != -1) {
93                 String JavaDoc type = token.substring(0, index);
94                 String JavaDoc value = token.substring(index + 1);
95                 if (type.equals("revision") && isValidRevision(value)) { //$NON-NLS-1$
96
return value;
97                 }
98             }
99         }
100         return null;
101     }
102
103     private static boolean isValidRevision(String JavaDoc value) {
104         return value.matches("\\d+\\.\\d+(?:\\.\\d+)*"); //$NON-NLS-1$
105
}
106
107     private static int getTagType(String JavaDoc type) {
108         if (type.equalsIgnoreCase("version")) //$NON-NLS-1$
109
return CVSTag.VERSION;
110         if (type.equalsIgnoreCase("branch")) //$NON-NLS-1$
111
return CVSTag.BRANCH;
112         if (type.equalsIgnoreCase("date")) //$NON-NLS-1$
113
return CVSTag.DATE;
114         return -1;
115     }
116
117     private static ICVSRepositoryLocation getRepository(URI JavaDoc uri) throws CVSException {
118         String JavaDoc 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 JavaDoc uri) {
128         String JavaDoc f = uri.getFragment();
129         int i = f.indexOf(',');
130         if (i == -1) {
131             return CVSTag.DEFAULT;
132         }
133         
134         return CVSTag.DEFAULT;//just use HEAD for now (name, CVSTag.BRANCH);
135
}
136
137     private static IPath getOldPath(URI JavaDoc uri) {
138         String JavaDoc 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 JavaDoc uri) throws CVSException {
147         String JavaDoc ssp = uri.getSchemeSpecificPart();
148         if (!ssp.startsWith(":")) { //$NON-NLS-1$
149
ssp = ":" + ssp; //$NON-NLS-1$
150
}
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 JavaDoc 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 JavaDoc 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 JavaDoc getLastSegment() {
177         return path.lastSegment();
178     }
179
180     public URI JavaDoc toURI() {
181         try {
182             String JavaDoc authority = repository.getLocation(false);
183             authority = ensureRegistryBasedAuthority(authority);
184             String JavaDoc pathString = path.toString();
185             if (!pathString.startsWith("/")) { //$NON-NLS-1$
186
pathString = "/" + pathString; //$NON-NLS-1$
187
}
188             String JavaDoc query = null;
189             if (tag != null && tag.getType() != CVSTag.HEAD) {
190                 query = getQueryType(tag) + "=" + tag.getName(); //$NON-NLS-1$
191
}
192             if (revision != null) {
193                 String JavaDoc string = "revision=" + revision; //$NON-NLS-1$
194
if (query == null) {
195                     query = string;
196                 } else {
197                     query = query + "," + string; //$NON-NLS-1$
198
}
199             }
200             return new URI JavaDoc(SCHEME, authority, pathString, query, null);
201         } catch (URISyntaxException JavaDoc e) {
202             CVSProviderPlugin.log(IStatus.ERROR, NLS.bind("An error occurred while creating a URI for {0} {1}", repository, path), e); //$NON-NLS-1$
203
throw new IllegalStateException JavaDoc(e.getMessage());
204         }
205     }
206
207     /*
208      * Ensure that the authority will not be confused with a
209      * server based authority. To do this, we need to convert
210      * any /, : and @ to another form.
211      */

212     private String JavaDoc ensureRegistryBasedAuthority(String JavaDoc authority) {
213         // Encode / so the authority doesn't conflict with the path
214
authority = encode('/', '!', authority);
215         // Encode @ to avoid URI interpreting the authority as a server based authority
216
authority = encode('@', '~', authority);
217         // Encode : to avoid URI interpreting the authority as a server based authority
218
authority = encode(':', '_', authority);
219         return authority;
220     }
221     
222     private static String JavaDoc decodeAuthority(String JavaDoc authority) {
223         authority = decode('/', '!', authority);
224         authority = decode('@', '~', authority);
225         authority = decode(':', '_', authority);
226         return authority;
227     }
228     
229     private String JavaDoc encode(char charToEncode, char encoding, String JavaDoc string) {
230         // First, escape any occurrences of the encoding character
231
String JavaDoc result = string.replaceAll(new String JavaDoc(new char[] { encoding }), new String JavaDoc(new char[] { encoding, encoding }));
232         // Convert / to ! to avoid URI parsing part of the authority as the path
233
return result.replace(charToEncode, encoding);
234     }
235     
236     private static String JavaDoc decode(char encodedChar, char encoding, String JavaDoc string) {
237         // Convert the encoded char back
238
String JavaDoc reuslt = string.replace(encoding, encodedChar);
239         // Convert any double occurrences of the encoded char back to the encoding
240
return reuslt.replaceAll(new String JavaDoc(new char[] { encodedChar, encodedChar }), new String JavaDoc(new char[] { encoding }));
241     }
242
243     private static String JavaDoc getQueryType(CVSTag tag) {
244         switch (tag.getType()) {
245         case CVSTag.BRANCH:
246             return "branch"; //$NON-NLS-1$
247
case CVSTag.DATE:
248             return "date"; //$NON-NLS-1$
249
}
250         return "version"; //$NON-NLS-1$
251
}
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 JavaDoc 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         // TODO: What about keyword mode?
279
return RemoteFile.create(path.toString(), repository, tag, revision);
280     }
281     
282     public String JavaDoc toString() {
283         return "[Path: "+this.path.toString()+" Tag: "+tag.getName()+ " Repo: " +repository.getRootDirectory() +"]"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
284
}
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 JavaDoc getRevision() {
306         return revision;
307     }
308 }
309
Popular Tags