KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > filesystem > URIUtil


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 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.core.filesystem;
12
13 import java.io.File JavaDoc;
14 import java.net.URI JavaDoc;
15 import java.net.URISyntaxException JavaDoc;
16 import org.eclipse.core.runtime.*;
17
18 /**
19  * This class provides utility methods for comparing, inspecting, and manipulating
20  * URIs. More specifically, this class is useful for dealing with URIs that represent
21  * file systems represented by the <tt>org.eclipse.core.filesystem.filesystems</tt>
22  * extension point. For such URIs the file system implementation can be consulted
23  * to interpret the URI in a way that is not possible at a generic level.
24  * <p>
25  * This class is not intended to be instantiated or subclassed.
26  * </p>
27  *
28  * @since org.eclipse.core.filesystem 1.0
29  */

30 public class URIUtil {
31
32     /**
33      * Tests two URIs for equality. This method delegates equality testing
34      * to the registered file system for the URIs. If either URI does not
35      * have a registered file system, the default {@link URI#equals(Object)}
36      * method is used.
37      *
38      * @param one The first URI to test for equality
39      * @param two The second URI to test for equality
40      * @return <code>true</code> if the first URI is equal to the second,
41      * as defined by the file systems for those URIs, and <code>false</code> otherwise.
42      */

43     public static boolean equals(URI JavaDoc one, URI JavaDoc two) {
44         try {
45             return EFS.getStore(one).equals(EFS.getStore(two));
46         } catch (CoreException e) {
47             // fall back to default equality test
48
return one.equals(two);
49         }
50     }
51
52     /**
53      * Replaces any colon characters in the provided string with their equivalent
54      * URI escape sequence.
55      */

56     private static String JavaDoc escapeColons(String JavaDoc string) {
57         final String JavaDoc COLON_STRING = "%3A"; //$NON-NLS-1$
58
if (string.indexOf(':') == -1)
59             return string;
60         int length = string.length();
61         StringBuffer JavaDoc result = new StringBuffer JavaDoc(length);
62         for (int i = 0; i < length; i++) {
63             char c = string.charAt(i);
64             if (c == ':')
65                 result.append(COLON_STRING);
66             else
67                 result.append(c);
68         }
69         return result.toString();
70     }
71
72     /**
73      * Returns an {@link IPath} representing this {@link URI}
74      * in the local file system, or <code>null</code> if this URI does
75      * not represent a file in the local file system.
76      *
77      * @param uri The URI to convert
78      * @return The path representing the provided URI, <code>null</code>
79      */

80     public static IPath toPath(URI JavaDoc uri) {
81         Assert.isNotNull(uri);
82         if (EFS.SCHEME_FILE.equals(uri.getScheme()))
83             return new Path(uri.getSchemeSpecificPart());
84         return null;
85     }
86
87     /**
88      * Converts an {@link IPath} representing a local file system path to a {@link URI}.
89      *
90      * @param path The path to convert
91      * @return The URI representing the provided path
92      */

93     public static URI JavaDoc toURI(IPath path) {
94         if (path == null)
95             return null;
96         if (path.isAbsolute())
97             return toURI(path.toFile().getAbsolutePath());
98         try {
99             //try to preserve the path as a relative path
100
return new URI JavaDoc(escapeColons(path.toString()));
101         } catch (URISyntaxException JavaDoc e) {
102             return toURI(path.toFile().getAbsolutePath());
103         }
104     }
105
106     /**
107      * Converts a String representing a local file system path to a {@link URI}.
108      * For example, this method can be used to create a URI from the output
109      * of {@link File#getAbsolutePath()}.
110      *
111      * @param pathString The path string to convert
112      * @return The URI representing the provided path string
113      */

114     public static URI JavaDoc toURI(String JavaDoc pathString) {
115         if (File.separatorChar != '/')
116             pathString = pathString.replace(File.separatorChar, '/');
117         final int length = pathString.length();
118         StringBuffer JavaDoc pathBuf = new StringBuffer JavaDoc(length + 1);
119         //There must be a leading slash in a hierarchical URI
120
if (length > 0 && (pathString.charAt(0) != '/'))
121             pathBuf.append('/');
122         //additional double-slash for UNC paths to distinguish from host separator
123
if (pathString.startsWith("//")) //$NON-NLS-1$
124
pathBuf.append('/').append('/');
125         pathBuf.append(pathString);
126         try {
127             return new URI JavaDoc(EFS.SCHEME_FILE, null, pathBuf.toString(), null);
128         } catch (URISyntaxException JavaDoc e) {
129             //try java.io implementation
130
return new File JavaDoc(pathString).toURI();
131         }
132     }
133
134     /**
135      * Private constructor to prevent instantiation.
136      */

137     private URIUtil() {
138         super();
139     }
140 }
141
Popular Tags