Java Resource Path Get getBasePath(Class clazz, String resource)

Here you can find the source of getBasePath(Class clazz, String resource)

Description

Get the path to the directory containing the resource (including the trailing slash).

License

Open Source License

Declaration

public static String getBasePath(Class clazz, String resource) 

Method Source Code


//package com.java2s;
/*/*from   w  w  w  .jav a2  s . c  o m*/
Copyright 2009 Semantic Discovery, Inc. (www.semanticdiscovery.com)
    
This file is part of the Semantic Discovery Toolkit.
    
The Semantic Discovery Toolkit is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
    
The Semantic Discovery Toolkit is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.
    
You should have received a copy of the GNU Lesser General Public License
along with The Semantic Discovery Toolkit.  If not, see <http://www.gnu.org/licenses/>.
*/

import java.io.*;

import java.net.URL;

public class Main {
    /**
     * Get the path to the directory containing the resource (including the trailing slash).
     */
    public static String getBasePath(Class clazz, String resource) {
        final String filename = getFilename(clazz, resource);
        return getBasePath(filename);
    }

    /**
     * Get the path to the directory of the file (including the trailing slash).
     */
    public static String getBasePath(String fullPath) {
        String result = null;
        if (fullPath != null) {
            final int pos = fullPath.lastIndexOf(File.separatorChar);
            if (pos >= 0) {
                result = fullPath.substring(0, pos + 1);
            }
        }
        return result;
    }

    /**
     * Get the filename for the class's resource suitable for opening.
     */
    public static String getFilename(Class clazz, String resource) {
        String filename = null;
        if (clazz == null) {
            filename = resource;
        } else {
            final URL url = getUrl(clazz, resource);
            if (url != null) {
                filename = url.toString();
            }
        }

        return filename;
    }

    /**
     * Extract the file name from the full path.
     */
    public static String getFileName(String fullPath) {
        String result = null;
        if (fullPath != null) {
            final int pos = fullPath.lastIndexOf(File.separatorChar);
            if (pos >= 0) {
                result = fullPath.substring(pos + 1);
            }
        }
        return result;
    }

    /**
     * Concatenate the base file name to the given path.
     * <p>
     * Assume fullPath is the path to a directory and deal appropriately with
     * the presence or absence of a trailing slash on the path.
     */
    public static String getFilename(String fullPath, String baseName) {
        final boolean hasTrailingSlash = (fullPath.charAt(fullPath.length() - 1) == File.separatorChar);
        return hasTrailingSlash ? (fullPath + baseName) : (fullPath + File.separatorChar + baseName);
    }

    /**
     * Concatenate the file name to the parent for a full absolute path.
     */
    public static String getFilename(File parent, String name) {
        return getFilename(parent.getAbsolutePath(), name);
    }

    public static URL getUrl(Class clazz, String resource) {
        if (File.separatorChar != '/') {
            if (File.separatorChar == '\\') {
                //resource = resource.replaceAll("/", "\\\\");
            } else {
                //resource = resource.replaceAll("/", File.separator);
            }
        }
        return clazz.getResource(resource);
    }
}

Related

  1. deserialize(Class parentOfResource, String resourcePath, Class targetClass)
  2. existsResource(String pathName)
  3. getAbsolutePathFromResource(Class reference, String resource)
  4. getAbsoluteResource(String path)
  5. getAlternateResourceFile(final String resourcePath)
  6. getChildResources(String path)
  7. getClassResource(Class clazz, String resPath)
  8. getClassResources(Class clazz, String resPath)
  9. getExistingResourceAsFile(final String resourcePath)