下面列出了java.net.URI#getSchemeSpecificPart ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private File urlToFile(URL url) {
if (!"file".equals(url.getProtocol())) {
return null;
}
URI uri;
try {
uri = url.toURI();
} catch (URISyntaxException e) {
throw new IllegalArgumentException("Unable to convert URL " + url + " to URI", e);
}
if (uri.isOpaque()) {
// It is like file:test%20file.c++
// getSchemeSpecificPart would return "test file.c++"
return new File(uri.getSchemeSpecificPart());
}
// See https://stackoverflow.com/a/17870390/1261287
return Paths.get(uri).toFile();
}
protected static Archive createArchive(Class clazz) throws Exception {
ProtectionDomain protectionDomain = clazz.getProtectionDomain();
CodeSource codeSource = protectionDomain.getCodeSource();
URI location = codeSource != null ? codeSource.getLocation().toURI() : null;
String path = location != null ? location.getSchemeSpecificPart() : null;
if (path == null) {
throw new IllegalStateException("Unable to determine code source archive");
} else {
File root = new File(path);
if (!root.exists()) {
throw new IllegalStateException("Unable to determine code source archive from " + root);
} else {
return (Archive)(root.isDirectory() ? new ExplodedArchive(root) : new JarFileArchive(root));
}
}
}
protected static String getPathFromURI(DocumentURI uri) {
String uriStr = uri.getUri();
try {
URI child = new URI(uriStr);
String childPath;
if (child.isOpaque()) {
childPath = child.getSchemeSpecificPart();
} else {
childPath = child.getPath();
}
return childPath;
} catch (Exception ex) {
LOG.error("Error parsing URI " + uriStr + ".");
return uriStr;
}
}
private @Nullable URI getThingConfigDescriptionURI(URI uri) {
// First, get the thing type so we get the generic config descriptions
ThingUID thingUID = new ThingUID(uri.getSchemeSpecificPart());
Thing thing = thingRegistry.get(thingUID);
if (thing == null) {
return null;
}
ThingType thingType = thingTypeRegistry.getThingType(thing.getThingTypeUID());
if (thingType == null) {
return null;
}
// Get the config description URI for this thing type
URI configURI = thingType.getConfigDescriptionURI();
return configURI;
}
@Test
public void testURLFormats() throws MalformedURLException, URISyntaxException {
URI file = new URI("file:/tmp/");
String path = file.getSchemeSpecificPart();
Assert.assertEquals("/tmp/", path);
URI fileInJar = new URI("jar:file:/tmp/my.jar!/");
String path1 = fileInJar.getSchemeSpecificPart();
Assert.assertEquals("file:/tmp/my.jar!/", path1);
URI fileInZip = new URI("zip:D:/bea/tmp/_WL_user/JuddiEAR/nk4cwv/war/WEB-INF/lib/juddi-core-3.0.1.jar!");
String path2 = fileInZip.getSchemeSpecificPart();
Assert.assertEquals("D:/bea/tmp/_WL_user/JuddiEAR/nk4cwv/war/WEB-INF/lib/juddi-core-3.0.1.jar!", path2);
URI fileInVfszip = new URI("vfsfile:/tmp/SOA%20Platform/jbossesb-registry.sar/juddi_custom_install_data/root_Publisher.xml");
String path3 = fileInVfszip.getSchemeSpecificPart();
Assert.assertEquals("/tmp/SOA Platform/jbossesb-registry.sar/juddi_custom_install_data/root_Publisher.xml", path3);
}
protected final Archive createArchive() throws Exception {
ProtectionDomain protectionDomain = getClass().getProtectionDomain();
CodeSource codeSource = protectionDomain.getCodeSource();
URI location = (codeSource == null ? null : codeSource.getLocation().toURI());
String path = (location == null ? null : location.getSchemeSpecificPart());
if (path == null) {
throw new IllegalStateException("Unable to determine code source archive");
}
File root = new File(path);
if (!root.exists()) {
throw new IllegalStateException(
"Unable to determine code source archive from " + root);
}
return (root.isDirectory() ? new ExplodedArchive(root)
: new JarFileArchive(root));
}
private static URI parseAssumeFileScheme(String pathIdentifier) {
File pathFile = new File(pathIdentifier);
if (pathFile.isAbsolute()) {
return pathFile.toURI();
}
try {
URI root = new File("").toURI();
URI relative = root.relativize(pathFile.toURI());
// Scheme is lost by relativize
return new URI("file", relative.getSchemeSpecificPart(), relative.getFragment());
} catch (URISyntaxException e) {
throw new IllegalArgumentException(e.getMessage(), e);
}
}
@Override
public Path getPath(URI uri) {
String spec = uri.getSchemeSpecificPart();
int sep = spec.indexOf("!/");
if (sep == -1)
throw new IllegalArgumentException("URI: "
+ uri
+ " does not contain path info ex. jar:file:/c:/foo.zip!/BAR");
return getFileSystem(uri).getPath(spec.substring(sep + 1));
}
private URI _getSpecURI(String spec) {
URI specURI = URI.create(spec);
if (_scheme.indexOf(':') == -1) {
return specURI;
}
String schemeSpecificPart = specURI.getSchemeSpecificPart();
return URI.create(schemeSpecificPart);
}
private String schemeSpecificPart(URI uri) {
String part = uri.getSchemeSpecificPart();
if (StringUtils.isEmpty(part)) {
return part;
}
return part.startsWith("//") ? part.substring(2) : part;
}
@Override
public Path getPath(URI uri) {
checkScheme(uri);
if (delegate == null)
throw new FileSystemNotFoundException();
// only allow absolute path
String path = uri.getSchemeSpecificPart();
if (! path.startsWith("///")) {
throw new IllegalArgumentException();
}
return new PassThroughFileSystem.PassThroughPath(delegate, delegate.root.resolve(path.substring(3)));
}
@Override
public Path getPath(URI uri) {
String spec = uri.getSchemeSpecificPart();
int sep = spec.indexOf("!/");
if (sep == -1)
throw new IllegalArgumentException("URI: "
+ uri
+ " does not contain path info ex. jar:file:/c:/foo.zip!/BAR");
return getFileSystem(uri).getPath(spec.substring(sep + 1));
}
@Override
public Path getPath(URI uri) {
checkScheme(uri);
if (delegate == null)
throw new FileSystemNotFoundException();
// only allow absolute path
String path = uri.getSchemeSpecificPart();
if (! path.startsWith("///")) {
throw new IllegalArgumentException();
}
return new PassThroughFileSystem.PassThroughPath(delegate, delegate.root.resolve(path.substring(3)));
}
public static InputStream getStreamForUri(URI uri, ClassLoader loader) throws IOException {
String protocol = uri.getScheme();
if (protocol.equals("classpath")) {
String path = uri.getSchemeSpecificPart();
return getStreamForClasspathFile(path, loader);
}
URL url = uri.toURL();
return url.openStream();
}
public CustomJavaFileObject(String binaryName, URI uri) {
this.uri = uri;
this.binaryName = binaryName;
name = uri.getPath() == null ? uri.getSchemeSpecificPart() : uri.getPath(); // for FS based URI the path is not null, for JAR URI the scheme specific part is not null
}
protected static URI unwrap(final URI uri) throws URISyntaxException {
return new URI(uri.getSchemeSpecificPart());
}
/** Return a new URI with a different fragment. */
private static URI refragUri(URI uri, String frag) throws URISyntaxException {
return new URI(uri.getScheme(), uri.getSchemeSpecificPart(), frag);
}
/**
* Determine if a uri is in origin-form according to 根据rfc7230, 5.3确定uri是否为原始形式。
* <a href="https://tools.ietf.org/html/rfc7230#section-5.3">rfc7230, 5.3</a>.
*/
public static boolean isOriginForm(URI uri) {
return uri.getScheme() == null && uri.getSchemeSpecificPart() == null &&
uri.getHost() == null && uri.getAuthority() == null;
}
public static String getOsAppropriatePath(String fileName, Class clazz) throws Exception {
URI uriToFile = clazz.getClassLoader().getResource(fileName).toURI();
return IS_WINDOWS
? uriToFile.getSchemeSpecificPart().substring(1)
: uriToFile.getSchemeSpecificPart();
}
/**
* Return the last component of a presumed hierarchical URI.
* From the scheme specific part of the URI, it returns the substring
* after the last "/" if any, or everything if no "/" is found.
* @param fo the file object
* @return the simple name of the file object
*/
public static String getSimpleName(FileObject fo) {
URI uri = fo.toUri();
String s = uri.getSchemeSpecificPart();
return s.substring(s.lastIndexOf("/") + 1); // safe when / not found
}