Java Resource File getSourceForResource(String s)

Here you can find the source of getSourceForResource(String s)

Description

Analyze the passed string and determine if it specifies an existing file resource or URL.

License

Open Source License

Return

a valid URL, existing file, or null. No exceptions are thrown.

Declaration

public static Object getSourceForResource(String s) 

Method Source Code

//package com.java2s;
/**/*  ww  w. j  av  a  2 s . c om*/
 * Copyright 2011 The ARIES Consortium (http://www.ariesonline.org) and
 * www.integratedmodelling.org. 
    
   This file is part of Thinklab.
    
   Thinklab is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published
   by the Free Software Foundation, either version 3 of the License,
   or (at your option) any later version.
    
   Thinklab 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
   General Public License for more details.
    
   You should have received a copy of the GNU General Public License
   along with Thinklab.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.File;

import java.net.MalformedURLException;

import java.net.URL;

public class Main {
    /**
     * Analyze the passed string and determine if it specifies an existing file resource
     * or URL. Return the appropriate object, that must be disambiguated using instanceof.
     * Meant to (inelegantly) solve problems coming from file name encodings in primitive
     * OS (e.g. Windows) that cannot be handled properly in file:// URLs. 
     * @return a valid URL, existing file, or null. No exceptions are thrown.
     */
    public static Object getSourceForResource(String s) {

        File f = new File(s);

        if (f.exists())
            return f;

        URL url = null;
        try {
            url = new URL(s);
        } catch (MalformedURLException e) {
        }

        if (url != null)
            return url;

        return null;
    }
}

Related

  1. getLastModificationForResource(String resourceId)
  2. getLastModified(final String resourceName)
  3. getParentResource(final Class c, final String resource)
  4. getSampleDir(File resourcesDir)
  5. getShortName(String resource, Map nsMap)
  6. getStreamFromResource(Class clazz, String resourceName)
  7. getTestResourceFile(String pName, Class pClass)
  8. inputStream(Class baseClass, String resourceName)
  9. isFullIRI(String resource)