~ubuntu-branches/ubuntu/wily/gluegen2/wily

« back to all changes in this revision

Viewing changes to src/java/com/jogamp/common/util/IOUtil.java

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-12-17 09:29:23 UTC
  • mfrom: (9.1.18 sid)
  • Revision ID: package-import@ubuntu.com-20131217092923-19w78uqxd15jic2u
Tags: 2.1.3-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
68
68
    /** {@value} */
69
69
    public static final String JAR_SCHEME = "jar";
70
70
    /** A JAR subprotocol is separeted from the JAR entry w/ this separator {@value}. Even if no class is specified '!/' must follow!. */
71
 
    public static final String JAR_SCHEME_SEPARATOR = "!";
 
71
    public static final char JAR_SCHEME_SEPARATOR = '!';
72
72
 
73
73
    /** Std. temporary directory property key <code>java.io.tmpdir</code>. */
74
74
    private static final String java_io_tmpdir_propkey = "java.io.tmpdir";
313
313
     * @throws URISyntaxException if the resulting string does not comply w/ an RFC 2396 URI
314
314
     */
315
315
    public static URI toURISimple(File file) throws URISyntaxException {
316
 
        return new URI(FILE_SCHEME, null, encodeToURI(slashify(file.getAbsolutePath(), true, file.isDirectory())), null);
 
316
        return new URI(FILE_SCHEME, null, slashify(file.getAbsolutePath(), true /* startWithSlash */, file.isDirectory() /* endWithSlash */), null);
317
317
    }
318
318
 
319
319
    /**
321
321
     * @throws URISyntaxException if path is empty or has no parent directory available while resolving <code>../</code>
322
322
     * @throws URISyntaxException if the resulting string does not comply w/ an RFC 2396 URI
323
323
     */
324
 
    public static URI toURISimple(String protocol, String file, boolean isDirectory) throws URISyntaxException {
325
 
        return new URI(protocol, null, encodeToURI(slashify(file, true, isDirectory)), null);
 
324
    public static URI toURISimple(String protocol, String path, boolean isDirectory) throws URISyntaxException {
 
325
        return new URI(protocol, null, slashify(new File(path).getAbsolutePath(), true /* startWithSlash */, isDirectory /* endWithSlash */), null);
326
326
    }
327
327
 
328
328
    /**
412
412
     * @throws URISyntaxException if path is empty or has no parent directory available while resolving <code>../</code>
413
413
     */
414
414
    public static String getBasename(String fname) throws URISyntaxException {
415
 
        fname = slashify(fname, false, false);
 
415
        fname = slashify(fname, false /* startWithSlash */, false /* endWithSlash */);
416
416
        int lios = fname.lastIndexOf('/');  // strip off dirname
417
417
        if(lios>=0) {
418
418
            fname = fname.substring(lios+1);
425
425
     * @throws URISyntaxException if path is empty or has no parent directory available while resolving <code>../</code>
426
426
     */
427
427
    public static String getDirname(String fname) throws URISyntaxException {
428
 
        fname = slashify(fname, false, false);
 
428
        fname = slashify(fname, false /* startWithSlash */, false /* endWithSlash */);
429
429
        int lios = fname.lastIndexOf('/');  // strip off dirname
430
430
        if(lios>=0) {
431
431
            fname = fname.substring(0, lios+1);
451
451
        }
452
452
        String uriS = uri.toString();
453
453
        if( DEBUG ) {
454
 
            System.out.println("getURIDirname "+uri+", extForm: "+uriS);
 
454
            System.err.println("getURIDirname "+uri+", extForm: "+uriS);
455
455
        }
456
456
        return new URI( getURIDirname(uriS) );
457
457
    }
487
487
        uriS = uriS.substring(0, idx+1); // exclude jar name, include terminal '/' or ':'
488
488
 
489
489
        if( DEBUG ) {
490
 
            System.out.println("getJarURIDirname res: "+uriS);
 
490
            System.err.println("getJarURIDirname res: "+uriS);
491
491
        }
492
492
        return uriS;
493
493
    }
494
494
 
495
495
    /**
496
 
     * Converts an {@link URI} to an {@link URL} while using a non encoded path
497
 
     * for <i>file scheme</i>, i.e. <code>file:/</code>.
 
496
     * Converts an {@link URI} to an {@link URL} while using a non encoded path.
 
497
     * <p>
 
498
     * A <i>file scheme</i> path, i.e. path following <code>file:</code>, is converted as follows:<br/>
 
499
     * <code><br/>
 
500
            File file = new File( {@link #decodeFromURI(String) decodeFromURI}( specificURI.getPath() ) );<br/>
 
501
            String uriFilePath = {@link #encodeFilePathToURI(String) encodeFilePathToURI}( file.getPath() );<br/>
 
502
     * </code><br/>
 
503
     * above conversion results in a decoded <i>file path</i> appropriate to be used by subsequent file i/o operations (JarFile, zip, ..).
 
504
     * </p>
 
505
     * <p>
498
506
     * Otherwise the default {@link URL} translation {@link URI#toURL()} is being used.
 
507
     * </p>
499
508
     * <p>
500
509
     * The following cases are considered:
501
510
     * <ul>
502
511
     *   <li><i>file schema</i> is converted via <code>new File(uri).getPath()</code>.</li>
503
512
     *   <li><i>jar scheme</i>
504
513
     *   <ul>
505
 
     *     <li>sub-protocol is being converted as above, if <i>file scheme</i>.</li>
506
 
     *     <li>JAR entry is not converted but preserved.</li>
 
514
     *     <li>sub-protocol <i>file scheme</i> is being converted as above, other schema are preserved</li>
 
515
     *     <li>JAR entry is preserved.</li>
507
516
     *   </ul></li>
508
517
     * </ul>
509
518
     * </p>
523
532
        final URI specificURI = isJar ? JarUtil.getJarSubURI(uri) : uri;
524
533
        final boolean hasJarSubURI = specificURI != uri;
525
534
        if( DEBUG ) {
526
 
            System.out.println("IOUtil.toURL.0: isJAR "+isJar+", hasSubURI "+hasJarSubURI+Platform.getNewline()+
527
 
                               "\t, uri "+uri+Platform.getNewline()+
528
 
                               "\t -> "+specificURI.toString());
 
535
            System.err.println("IOUtil.toURL.0: isJAR "+isJar+", hasSubURI "+hasJarSubURI+PlatformPropsImpl.NEWLINE+
 
536
                               "\t, uri "+uri+PlatformPropsImpl.NEWLINE+
 
537
                               "\t str -> "+specificURI.toString()+PlatformPropsImpl.NEWLINE+
 
538
                               "\t ascii -> "+specificURI.toASCIIString()+PlatformPropsImpl.NEWLINE+
 
539
                               "\t ssp -> "+specificURI.getSchemeSpecificPart()+PlatformPropsImpl.NEWLINE+
 
540
                               "\t frag -> "+specificURI.getFragment()+PlatformPropsImpl.NEWLINE+
 
541
                               "\t path -> "+specificURI.getPath()+PlatformPropsImpl.NEWLINE+
 
542
                               "\t path.decoded -> "+decodeFromURI( specificURI.getPath() ) );
529
543
        }
530
544
        int mode = 0;
531
545
        if( IOUtil.FILE_SCHEME.equals( specificURI.getScheme() ) ) {
532
546
            File f;
533
547
            try {
534
 
                f = new File(specificURI);
 
548
                f = new File( decodeFromURI( specificURI.getPath() ) ); // validates uri, uses decoded uri.getPath() and normalizes it
535
549
            } catch(Exception iae) {
536
550
                if( DEBUG ) {
537
 
                    System.out.println("Catched "+iae.getClass().getSimpleName()+": new File("+specificURI+") failed: "+iae.getMessage());
 
551
                    System.err.println("Catched "+iae.getClass().getSimpleName()+": new File("+decodeFromURI( specificURI.getPath() )+") failed: "+iae.getMessage());
538
552
                    iae.printStackTrace();
539
553
                }
540
554
                f = null;
558
572
                        try {
559
573
                            final URI fUri = f.toURI();
560
574
                            final URL fUrl = fUri.toURL();
561
 
                            System.out.println("IOUtil.toURL.1b: fUri "+fUri+Platform.getNewline()+
 
575
                            System.err.println("IOUtil.toURL.1b: fUri "+fUri+PlatformPropsImpl.NEWLINE+
562
576
                                               "\t, fUrl "+fUrl);
563
577
                        } catch (Exception ee) {
564
 
                            System.out.println("Catched "+ee.getClass().getSimpleName()+": f.toURI().toURL() failed: "+ee.getMessage());
 
578
                            System.err.println("Catched "+ee.getClass().getSimpleName()+": f.toURI().toURL() failed: "+ee.getMessage());
565
579
                            ee.printStackTrace();
566
580
                        }
567
581
                    }
568
582
                    if( !hasJarSubURI ) {
569
583
                        urlS = IOUtil.FILE_SCHEME+IOUtil.SCHEME_SEPARATOR+fPathUriS;
570
584
                        if( DEBUG ) {
571
 
                            System.out.println("IOUtil.toURL.1: fPath "+fPath+Platform.getNewline()+
572
 
                                               "\t -> "+fPathUriS+Platform.getNewline()+
 
585
                            System.err.println("IOUtil.toURL.1: fPath "+fPath+PlatformPropsImpl.NEWLINE+
 
586
                                               "\t -> "+fPathUriS+PlatformPropsImpl.NEWLINE+
573
587
                                               "\t -> "+urlS);
574
588
                        }
575
589
                        url = new URL(urlS);
579
593
                        final String post = isJar ? IOUtil.JAR_SCHEME_SEPARATOR + jarEntry : "";
580
594
                        urlS = uriSchema+IOUtil.SCHEME_SEPARATOR+IOUtil.FILE_SCHEME+IOUtil.SCHEME_SEPARATOR+fPathUriS+post;
581
595
                        if( DEBUG ) {
582
 
                            System.out.println("IOUtil.toURL.2: fPath "+fPath+Platform.getNewline()+
583
 
                                               "\t -> "+fPathUriS+Platform.getNewline()+
584
 
                                               "\t, jarEntry "+jarEntry+Platform.getNewline()+
585
 
                                               "\t, post "+post+Platform.getNewline()+
 
596
                            System.err.println("IOUtil.toURL.2: fPath "+fPath+PlatformPropsImpl.NEWLINE+
 
597
                                               "\t -> "+fPathUriS+PlatformPropsImpl.NEWLINE+
 
598
                                               "\t, jarEntry "+jarEntry+PlatformPropsImpl.NEWLINE+
 
599
                                               "\t, post "+post+PlatformPropsImpl.NEWLINE+
586
600
                                               "\t -> "+urlS);
587
601
                        }
588
602
                        url = new URL(urlS);
590
604
                    }
591
605
                } catch (Exception mue) {
592
606
                    if( DEBUG ) {
593
 
                        System.out.println("Catched "+mue.getClass().getSimpleName()+": new URL("+urlS+") failed: "+mue.getMessage());
 
607
                        System.err.println("Catched "+mue.getClass().getSimpleName()+": new URL("+urlS+") failed: "+mue.getMessage());
594
608
                        mue.printStackTrace();
595
609
                    }
596
610
                }
602
616
                mode = 3;
603
617
            } catch (Exception e) {
604
618
                if( DEBUG ) {
605
 
                    System.out.println("Catched "+e.getClass().getSimpleName()+": "+uri+".toURL() failed: "+e.getMessage());
 
619
                    System.err.println("Catched "+e.getClass().getSimpleName()+": "+uri+".toURL() failed: "+e.getMessage());
606
620
                    e.printStackTrace();
607
621
                }
608
622
            }
609
623
        }
610
624
        if( DEBUG ) {
611
 
            System.err.println("IOUtil.toURL.X: mode "+mode+", "+uri+Platform.getNewline()+
 
625
            System.err.println("IOUtil.toURL.X: mode "+mode+", "+uri+PlatformPropsImpl.NEWLINE+
612
626
                               "\t -> "+url);
613
627
        }
614
628
        return url;
722
736
        if (baseLocation != null) {
723
737
            final File file = new File(baseLocation, relativeFile);
724
738
            // Handle things on Windows
725
 
            return slashify(file.getPath(), false, false);
 
739
            return slashify(file.getPath(), false /* startWithSlash */, false /* endWithSlash */);
726
740
        }
727
741
        return null;
728
742
    }
790
804
     * @throws URISyntaxException if path is empty or has no parent directory available while resolving <code>../</code>
791
805
     */
792
806
    public static URI getRelativeOf(URI baseURI, String relativePath) throws URISyntaxException {
793
 
        return compose(baseURI.getScheme(), baseURI.getSchemeSpecificPart(), encodeToURI(relativePath), baseURI.getFragment());
 
807
        return compose(baseURI.getScheme(), baseURI.getSchemeSpecificPart(), relativePath, baseURI.getFragment());
794
808
    }
795
809
 
796
810
    /**
857
871
 
858
872
    /**
859
873
     * Reverses escaping of characters as performed via {@link #encodeToURI(String)}.
 
874
     * <ul>
 
875
     *   <li>%20 -> SPACE</li>
 
876
     * </ul>
860
877
     */
861
878
    public static String decodeFromURI(String s) {
862
879
        return patternSpaceEnc.matcher(s).replaceAll(" ");
863
880
    }
864
881
 
865
882
    private static final Pattern patternSingleBS = Pattern.compile("\\\\{1,}");
 
883
    private static final Pattern patternSingleFS = Pattern.compile("/{1,}");
866
884
 
867
885
    /**
868
 
     * Escapes file path characters not complying w/ RFC 2396 and the {@link URI#URI(String)} ctor.
 
886
     * Encodes file path characters not complying w/ RFC 2396 and the {@link URI#URI(String)} ctor.
869
887
     * <p>
870
 
     * Processes input filePath if {@link File#separatorChar} <code> != '/'</code>
 
888
     * Implementation processes the <code>filePath</code> if {@link File#separatorChar} <code> != '/'</code>
871
889
     * as follows:
872
890
     * <ul>
873
891
     *   <li>backslash -> slash</li>
875
893
     * </ul>
876
894
     * </p>
877
895
     * <p>
 
896
     * Note that this method does not perform <i>space</i> encoding,
 
897
     * which can be utilized via {@link #encodeToURI(String)}.
 
898
     * </p>
 
899
     * <p>
878
900
     * Even though Oracle's JarURLStreamHandler can handle backslashes and
879
901
     * erroneous URIs w/ e.g. Windows file 'syntax', other may not (Netbeans).<br>
880
902
     * See Bug 857 - http://jogamp.org/bugzilla/show_bug.cgi?id=857
881
903
     * </p>
 
904
     * @see #encodeToURI(String)
882
905
     */
883
 
    public static String encodeFilePathToURI(String filePath) {
 
906
    public static String encodeFilePathToURI(final String filePath) {
884
907
        if( !File.separator.equals("/") ) {
885
908
            final String r = patternSingleBS.matcher(filePath).replaceAll("/");
886
909
            if( !r.startsWith("/") ) {
893
916
    }
894
917
 
895
918
    /**
 
919
     * Decodes uri-file path characters complying w/ RFC 2396 to native file-path.
 
920
     * <p>
 
921
     * Implementation decodes the space-encoding <code>path={@link #decodeFromURI(String) decodeFromURI}(uriPath)</code>.
 
922
     * </p>
 
923
     * <p>
 
924
     * Then it processes the <code>path</code> if {@link File#separatorChar} <code> != '/'</code>
 
925
     * as follows:
 
926
     * <ul>
 
927
     *   <li>drop a starting slash</li>
 
928
     *   <li>slash -> backslash</li>
 
929
     * </ul>
 
930
     * </p>
 
931
     * @see #decodeFromURI(String)
 
932
     */
 
933
    public static String decodeURIToFilePath(final String uriPath) {
 
934
        final String path = IOUtil.decodeFromURI(uriPath);
 
935
        if( !File.separator.equals("/") ) {
 
936
            final String r = patternSingleFS.matcher(path).replaceAll("\\\\");
 
937
            if( r.startsWith("\\") ) {
 
938
                return r.substring(1);
 
939
            } else {
 
940
                return r;
 
941
            }
 
942
        }
 
943
        return path;
 
944
    }
 
945
 
 
946
    /**
 
947
     * If <code>uri</code> is a <i>file scheme</i>,
 
948
     * implementation returns the decoded {@link URI#getPath()} via {@link #decodeURIToFilePath(String)},
 
949
     * otherwise it returns the {@link URI#toASCIIString()} encoded URI.
 
950
     *
 
951
     * @see #decodeFromURI(String)
 
952
     * @see #decodeURIToFilePath(String)
 
953
     */
 
954
    public static String decodeURIIfFilePath(final URI uri) {
 
955
        if( IOUtil.FILE_SCHEME.equals( uri.getScheme() ) ) {
 
956
            return decodeURIToFilePath( uri.getPath() );
 
957
        }
 
958
        return uri.toASCIIString();
 
959
    }
 
960
 
 
961
    /**
896
962
     * Returns the connected URLConnection, or null if not url is not available
897
963
     */
898
964
    public static URLConnection openURL(URL url) {
1202
1268
 
1203
1269
                    // 1) java.io.tmpdir/jogamp
1204
1270
                    if( null == tempRootExec && isStringSet(java_io_tmpdir) ) {
1205
 
                        if( Platform.OSType.MACOS == Platform.getOSType() ) {
 
1271
                        if( Platform.OSType.MACOS == PlatformPropsImpl.OS_TYPE ) {
1206
1272
                            // Bug 865: Safari >= 6.1 [OSX] May employ xattr on 'com.apple.quarantine' on 'PluginProcess.app'
1207
1273
                            // We attempt to fix this issue _after_ gluegen native lib is loaded, see JarUtil.fixNativeLibAttribs(File).
1208
1274
                            tempRootExec = getSubTempDir(new File(java_io_tmpdir), tmpSubDir, false /* executable */, "tempX1");