Java Resource File getBaseLineFolder(String resourceFile)

Here you can find the source of getBaseLineFolder(String resourceFile)

Description

get Base Line Folder

License

LGPL

Declaration

public static String getBaseLineFolder(String resourceFile) throws MalformedURLException 

Method Source Code

//package com.java2s;
/*//from w  w w. jav a 2s.  c o m
 *  Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 *  "The contents of this file are subject to the Mozilla Public License
 *  Version 1.1 (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.mozilla.org/MPL/
 *
 *  Software distributed under the License is distributed on an "AS IS"
 *  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 *  License for the specific language governing rights and limitations under
 *  the License.
 *
 *  The Original Code is ICEfaces 1.5 open source software code, released
 *  November 5, 2006. The Initial Developer of the Original Code is ICEsoft
 *  Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
 *  2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
 *
 *  Contributor(s): _____________________.
 *
 *  Alternatively, the contents of this file may be used under the terms of
 *  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
 *  License), in which case the provisions of the LGPL License are
 *  applicable instead of those above. If you wish to allow use of your
 *  version of this file only under the terms of the LGPL License and not to
 *  allow others to use your version of this file under the MPL, indicate
 *  your decision by deleting the provisions above and replace them with
 *  the notice and other provisions required by the LGPL License. If you do
 *  not delete the provisions above, a recipient may use your version of
 *  this file under either the MPL or the LGPL License."
 *
 */

import java.net.MalformedURLException;
import java.net.URL;

public class Main {
    public static String getBaseLineFolder(String resourceFile) throws MalformedURLException {
        String result = null;
        try {
            ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
            URL localUrl = classLoader.getResource(resourceFile);
            if (localUrl != null) {
                result = convertFileUrlToPath(localUrl);
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }

        if (result == null) {
            throw new MalformedURLException("no such file as " + resourceFile);
        }
        return result;
    }

    /**
     * Kind of hack-ish attempt at solving problem that if the directory,
     *  where we're building the component-metadata in,  has special
     *  characters in its path, like spaces, then the URL to it will be
     *  escaped, which will be interpretted as a different directory,
     *  unless we unescape it.
     */
    private static String convertFileUrlToPath(URL url) {
        String path = url.getPath();
        if (url.toExternalForm().startsWith("file:")) {
            StringBuffer sb = new StringBuffer(path.length());
            int pathLength = path.length();
            for (int i = 0; i < pathLength;) {
                char c = path.charAt(i);
                if (c == '%') {
                    if ((i + 1) < pathLength && isHexDigit(path.charAt(i + 1))) {
                        int increment = 2;
                        if ((i + 2) < pathLength && isHexDigit(path.charAt(i + 2)))
                            increment++;
                        try {
                            char unescaped = (char) Integer.parseInt(path.substring(i + 1, i + increment), 16);

                            sb.append(unescaped);
                            i += increment;
                            continue;
                        } catch (NumberFormatException nfe) {
                            // Not a valid hex escape, so just fall through,
                            //  and append it to the path
                        }
                    }
                }
                sb.append(c);
                i++;
            }
            path = sb.toString();
        }
        return path;
    }

    private static boolean isHexDigit(char c) {
        return ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'));
    }
}

Related

  1. copyDecompressed(String resource, File output)
  2. copyResource(String name)
  3. expendResource(String fileName)
  4. getAllResources(Class clazz, String resource)
  5. getBundlesContainingResource(BundleContext bundleContext, String resourcePattern)
  6. getBytes(Class contextClass, String resourceName)
  7. getClassResource(Class clazz)
  8. getContentAsString(String resource)