Java URL Load readProperties(final URL url)

Here you can find the source of readProperties(final URL url)

Description

Loads and returns a Properties from the passed URL

License

Open Source License

Parameter

Parameter Description
url The URL to read from

Return

the read properties which will be empty if no properties were read, or any error occurred while reading.

Declaration

public static Properties readProperties(final URL url) 

Method Source Code

//package com.java2s;
/**//from ww w  .j  av  a 2  s.c  o  m
 * Helios, OpenSource Monitoring
 * Brought to you by the Helios Development Group
 *
 * Copyright 2014, Helios Development Group and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This 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 software 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 this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org. 
 *
 */

import java.io.File;

import java.io.InputStream;

import java.net.URI;
import java.net.URL;

import java.util.Properties;

public class Main {
    /**
     * Loads and returns a {@link Properties} from the passed URL
     * @param url The URL to read from
     * @return the read properties which will be empty if no properties were read, or
     * any error occurred while reading.
     */
    public static Properties readProperties(final URL url) {
        Properties p = new Properties();
        InputStream is = null;
        try {
            is = url.openStream();
            if ("XML".equalsIgnoreCase(getExtension(url))) {
                p.loadFromXML(is);
            } else {
                p.load(is);
            }
        } catch (Exception e) {/* No Op */
        } finally {
            if (is != null)
                try {
                    is.close();
                } catch (Exception e) {
                    /* No Op */}
        }
        return p;
    }

    /**
     * Returns the extension of the passed URL's file
     * @param url The URL to get the extension of
     * @return the file extension, or null if the file has no extension
     */
    public static String getExtension(URL url) {
        return getExtension(url, null);
    }

    /**
     * Returns the extension of the passed URL's file
     * @param url The URL to get the extension of
     * @param defaultValue The default value to return if there is no extension
     * @return the file extension, or the default value if the file has no extension
     */
    public static String getExtension(URL url, String defaultValue) {
        if (url == null)
            throw new RuntimeException("The passed url was null", new Throwable());
        String file = url.getFile();
        if (file.lastIndexOf(".") == -1) {
            return defaultValue;
        }
        return file.substring(file.lastIndexOf(".") + 1);
    }

    /**
     * Returns the extension of the passed URL's file
     * @param url The URL to get the extension of
     * @return the file extension, or null if the file has no extension
     */
    public static String getExtension(CharSequence url) {
        return getExtension(url, null);
    }

    /**
     * Returns the extension of the passed URL's file
     * @param url The URL to get the extension of
     * @param defaultValue The default value to return if there is no extension
     * @return the file extension, or the default value if the file has no extension
     */
    public static String getExtension(CharSequence url, String defaultValue) {
        if (url == null)
            throw new RuntimeException("The passed url was null", new Throwable());
        return getExtension(toURL(url), defaultValue);
    }

    /**
     * Returns the URL for the passed file
     * @param file the file to get the URL for
     * @return a URL for the passed file
     */
    public static URL toURL(File file) {
        try {
            return nvl(file, "Passed file was null").toURI().toURL();
        } catch (Exception e) {
            throw new RuntimeException("Failed to get URL for file [" + file + "]", e);
        }
    }

    /**
     * Creates a URL from the passed string 
     * @param urlStr A char sequence containing a URL representation
     * @return a URL
     */
    public static URL toURL(final CharSequence urlStr) {
        if (urlStr == null || urlStr.toString().trim().isEmpty())
            throw new IllegalArgumentException("The passed URL stringy was null or empty");
        try {
            if (isFile(urlStr))
                return toURL(new File(urlStr.toString()));
            return new URL(nvl(urlStr, "Passed string was null").toString());
        } catch (Exception e) {
            throw new RuntimeException("Failed to create URL from string [" + urlStr + "]", e);
        }
    }

    /**
     * Creates a URI from the passed string 
     * @param uriStr A char sequence containing a URI representation
     * @return a URI
     */
    public static URI toURI(CharSequence uriStr) {
        try {
            return new URI(nvl(uriStr, "Passed string was null").toString());
        } catch (Exception e) {
            throw new RuntimeException("Failed to create URL from string [" + uriStr + "]", e);
        }
    }

    /**
     * Tests the passed object for nullness. Throws an {@link IllegalArgumentException} if the object is null 
     * @param t  The object to test
     * @param message The message to associate with the exception, Ignored if null
     * @return the object passed in if not null
     */
    public static <T> T nvl(T t, String message) {
        if (t == null)
            throw new IllegalArgumentException(message != null ? message : "Null parameter");
        return t;
    }

    /**
     * Determines if the passed stringy represents an existing file name
     * @param urlStr The stringy to test
     * @return true if the passed stringy represents an existing file name, false otherwise
     */
    public static boolean isFile(final CharSequence urlStr) {
        if (urlStr == null || urlStr.toString().trim().isEmpty())
            throw new IllegalArgumentException("The passed URL stringy was null or empty");
        return new File(urlStr.toString().trim()).exists();
    }
}

Related

  1. readModelFromUrl(URL file, Class clazz)
  2. readNetFile(String urlstr)
  3. readPage(final String urlStr)
  4. readPage(URL url)
  5. readProperties(final URL url)
  6. readProperties(URL resource)
  7. readPropertyFile(URL url)
  8. readResource(final URL filename)
  9. readResource(URL resource)