net.java.truevfs.access.TUriHelper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of truevfs-access Show documentation
Show all versions of truevfs-access Show documentation
This module provides convenient access to the (virtual federated) file system
space for TrueVFS client applications.
It features simple, uniform, transparent, thread-safe, read/write access to
archive files as if they were virtual directories in a file system path.
This module also provides Swing GUI classes for viewing file trees
and choosing entries in archive files.
/*
* Copyright (C) 2005-2015 Schlichtherle IT Services.
* All rights reserved. Use is subject to license terms.
*/
package net.java.truevfs.access;
import java.net.URI;
import java.net.URISyntaxException;
import javax.annotation.concurrent.ThreadSafe;
import net.java.truecommons.shed.Paths;
import net.java.truecommons.shed.QuotedUriSyntaxException;
import net.java.truecommons.shed.UriBuilder;
import static net.java.truevfs.kernel.spec.FsNodeName.*;
/**
* Utility functions for {@link URI}s which represent file system path names.
*
* @author Christian Schlichtherle
*/
@ThreadSafe
final class TUriHelper {
static final URI SEPARATOR_URI = URI.create(SEPARATOR);
static final URI DOT_URI = URI.create(".");
static final URI DOT_DOT_URI = URI.create("..");
static int pathPrefixLength(final URI uri) {
final String ssp = uri.getSchemeSpecificPart();
final String a = uri.getAuthority();
final int al = null == a ? 0 : 2 + a.length();
final int pl = Paths.prefixLength(ssp, SEPARATOR_CHAR, true) - al;
return pl >= 0 ? pl : Paths.prefixLength(uri.getPath(), SEPARATOR_CHAR, false);
}
static URI check(final URI uri) throws URISyntaxException {
if (null != uri.getFragment())
throw new QuotedUriSyntaxException(uri, "Fragment component defined");
return uri;
}
/**
* Eventually recreates the given URI to work around
* http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7198297:
*
* {@code assert null == new URI("x/").resolve("..").getSchemeSpecificPart();}
*
*
* @param uri the URI to fix.
* @return A fixed URI or {@code uri} if it doesn't need fixing.
*/
static URI fix(final URI uri) {
final String ssp = uri.getSchemeSpecificPart();
final String a = uri.getAuthority();
// Workaround for http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7198297 :
// assert null == new URI("foo/").resolve(new URI("..")).getRawSchemeSpecificPart();
if (null == ssp
|| null == a && ssp.startsWith(SEPARATOR + SEPARATOR)) // empty authority
return new UriBuilder(uri).toUri();
return uri;
}
/**
* Returns whether or not the given {@code uri} has an absolute path.
* A URI has an absolute path if it doesn't need to be combined with
* other path information in order to locate a file.
*
* @param uri the URI to test.
* @return Whether or not the given URI has an absolute path.
*/
static boolean hasAbsolutePath(URI uri) {
return !uri.isOpaque() && Paths.isAbsolute(
uri.getSchemeSpecificPart(), SEPARATOR_CHAR);
}
private TUriHelper() { }
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy