Java File Name Create toFilename(String filenameOrURI)

Here you can find the source of toFilename(String filenameOrURI)

Description

Turn a file: URL or file name into a plain file name

License

Apache License

Declaration


public static String toFilename(String filenameOrURI) 

Method Source Code

//package com.java2s;
/*// www .  j a  va 2  s.com
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    /** Turn a file: URL or file name into a plain file name */

    public static String toFilename(String filenameOrURI) {
        // Requirements of windows and Linux differ slightly here
        // Windows wants "file:///c:/foo" => "c:/foo"
        // but Linux only wants "file:///foo" => "/foo"
        // Pragmatically, a path of "/c:/foo", or "/foo" works everywhere.
        // but not "//c:/foo" or "///c:/foo" 
        // else IKVM thinks its a network path on Windows.

        // If it's a a file: we apply %-decoding.
        // If there is no scheme name, we don't.

        if (!isFile(filenameOrURI))
            return null;
        // No scheme of file:
        String fn = filenameOrURI;

        if (!fn.startsWith("file:"))
            return fn;

        // file:
        // Convert absolute file names
        if (fn.startsWith("file:///"))
            fn = fn.substring("file://".length());
        else if (fn.startsWith("file://localhost/"))
            // NB Leaves the leading slash on. 
            fn = fn.substring("file://localhost".length());
        else
            // Just trim off the file:
            fn = fn.substring("file:".length());

        return decodeFileName(fn);
    }

    /** Check whether 'name' is possibly a file reference  
     * 
     * @param name
     * @return boolean False if clearly not a filename. 
     */
    public static boolean isFile(String name) {
        String scheme = getScheme(name);

        if (scheme == null)
            // No URI scheme - treat as filename
            return true;

        if (scheme.equals("file"))
            // file: URI scheme
            return true;

        // Windows: "c:" etc
        if (scheme.length() == 1)
            // file: URI scheme
            return true;

        return false;
    }

    public static String decodeFileName(String s) {
        if (s.indexOf('%') < 0)
            return s;
        int len = s.length();
        StringBuilder sbuff = new StringBuilder(len);

        for (int i = 0; i < len; i++) {
            char c = s.charAt(i);
            switch (c) {
            case '%':
                int codepoint = Integer.parseInt(s.substring(i + 1, i + 3), 16);
                char ch = (char) codepoint;
                sbuff.append(ch);
                i = i + 2;
                break;
            default:
                sbuff.append(c);
            }
        }
        return sbuff.toString();
    }

    public static String getScheme(String uri) {
        // Find "[^/:]*:.*"
        for (int i = 0; i < uri.length(); i++) {
            char ch = uri.charAt(i);
            if (ch == ':')
                return uri.substring(0, i);
            if (!isASCIILetter(ch))
                // Some illegal character before the ':' 
                break;
        }
        return null;
    }

    private static boolean isASCIILetter(char ch) {
        return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z');
    }
}

Related

  1. generateFileNameWithoutExtension(final String testClassName, final String testId, final boolean includeDate)
  2. getRandomFileName(@Nullable final String prefix)
  3. toFileName(final Object obj)
  4. toFileName(String address)
  5. toFileName(String className)
  6. toFilename(String key, final String suffix)
  7. toFileName(String moduleName)
  8. toFilename(String name, String extension)
  9. toFilename(String s)