Java Resource Path Get getPath(final Object resource)

Here you can find the source of getPath(final Object resource)

Description

This method returns the path of a given resource which can be instance of File, URL, URI or String.

License

Open Source License

Parameter

Parameter Description
resource instance of File, URL, URI, or String path.

Exception

Parameter Description
MalformedURLException an exception

Return

The resource path.

Declaration

private static String getPath(final Object resource) throws MalformedURLException 

Method Source Code

//package com.java2s;
/*//from   w  w  w  . j  av a  2 s  .  c  o  m
 *    Geotoolkit - An Open Source Java GIS Toolkit
 *    http://www.geotoolkit.org
 *
 *    (C) 2008 - 2009, Geomatys
 *
 *    This library 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 2.1 of the License, or (at your option) any later version.
 *
 *    This library 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.
 */

import java.io.File;

import java.net.MalformedURLException;
import java.net.URI;

import java.net.URL;

public class Main {
    /**
     * <p>This method returns the path of a given resource which can be
     * instance of File, URL, URI or String. Returns null in other cases.</p>
     *
     * @param resource instance of File, URL, URI, or String path.
     * @return The resource path.
     * @throws MalformedURLException
     */
    private static String getPath(final Object resource) throws MalformedURLException {

        String extractPath = null;
        if (resource instanceof File) {
            extractPath = ((File) resource).getPath();
        } else if (resource instanceof URL) {
            extractPath = ((URL) resource).getPath();
        } else if (resource instanceof URI) {
            extractPath = (((URI) resource).toURL()).getPath();
        } else if (resource instanceof String) {
            extractPath = (String) resource;
        }
        return extractPath;
    }
}

Related

  1. getClassResources(Class clazz, String resPath)
  2. getExistingResourceAsFile(final String resourcePath)
  3. getFileFromBundle(String bundleName, String resourcePath)
  4. getFileFromResource(Class clazz, String path)
  5. getFileResourcePaths(final Class clazz, final String fileNameRegex)
  6. getPathOfResource(Class clazz, String fileName)
  7. getPluginResource(Bundle bundle, String resourcePath)
  8. getRelativeResource(Class clazz, String relativePath)
  9. getResource(Class cl, String path)