View Javadoc
1   /*
2    * Copyright (C) 2005-2015 Schlichtherle IT Services.
3    * All rights reserved. Use is subject to license terms.
4    */
5   package net.java.truevfs.access;
6   
7   import java.net.URI;
8   import java.net.URISyntaxException;
9   import javax.annotation.concurrent.ThreadSafe;
10  import net.java.truecommons.shed.Paths;
11  import net.java.truecommons.shed.QuotedUriSyntaxException;
12  import net.java.truecommons.shed.UriBuilder;
13  import static net.java.truevfs.kernel.spec.FsNodeName.*;
14  
15  /**
16   * Utility functions for {@link URI}s which represent file system path names.
17   *
18   * @author Christian Schlichtherle
19   */
20  @ThreadSafe
21  final class TUriHelper {
22      static final URI SEPARATOR_URI = URI.create(SEPARATOR);
23      static final URI DOT_URI = URI.create(".");
24      static final URI DOT_DOT_URI = URI.create("..");
25  
26      static int pathPrefixLength(final URI uri) {
27          final String ssp = uri.getSchemeSpecificPart();
28          final String a = uri.getAuthority();
29          final int al = null == a ? 0 : 2 + a.length();
30          final int pl = Paths.prefixLength(ssp, SEPARATOR_CHAR, true) - al;
31          return pl >= 0 ? pl : Paths.prefixLength(uri.getPath(), SEPARATOR_CHAR, false);
32      }
33  
34      static URI check(final URI uri) throws URISyntaxException {
35          if (null != uri.getFragment())
36              throw new QuotedUriSyntaxException(uri, "Fragment component defined");
37          return uri;
38      }
39  
40      /**
41       * Eventually recreates the given URI to work around
42       * <a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7198297">http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7198297</a>:
43       * <pre>
44       * {@code assert null == new URI("x/").resolve("..").getSchemeSpecificPart();}
45       * </pre>
46       *
47       * @param  uri the URI to fix.
48       * @return A fixed URI or {@code uri} if it doesn't need fixing.
49       */
50      static URI fix(final URI uri) {
51          final String ssp = uri.getSchemeSpecificPart();
52          final String a = uri.getAuthority();
53          // Workaround for http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7198297 :
54          // assert null == new URI("foo/").resolve(new URI("..")).getRawSchemeSpecificPart();
55          if (null == ssp
56                  || null == a && ssp.startsWith(SEPARATOR + SEPARATOR)) // empty authority
57              return new UriBuilder(uri).toUri();
58          return uri;
59      }
60  
61      /**
62       * Returns whether or not the given {@code uri} has an absolute path.
63       * A URI has an absolute path if it doesn't need to be combined with
64       * other path information in order to locate a file.
65       *
66       * @param  uri the URI to test.
67       * @return Whether or not the given URI has an absolute path.
68       */
69      static boolean hasAbsolutePath(URI uri) {
70          return !uri.isOpaque() && Paths.isAbsolute(
71                  uri.getSchemeSpecificPart(), SEPARATOR_CHAR);
72      }
73  
74      private TUriHelper() { }
75  }