com.feilong.core.util.PropertiesUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.feilong.core.util.PropertiesUtil.java

Source

/*
 * Copyright (C) 2008 feilong
 *
 * Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.feilong.core.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.commons.lang3.Validate;

import com.feilong.core.UncheckedIOException;
import com.feilong.core.lang.ClassLoaderUtil;

/**
 * ?{@link Properties}?.
 * 
 * <h3>{@link "Properties#load0(LineReader)"}:</h3>
 * 
 * <blockquote>
 * <ol>
 * <li>:'#''!'.<br>
 * :' ', '\t', '\f'.<br>
 * key/value:'='':'.<br>
 * :'\r','\n','\r\n'.</li>
 * <li>??.<br>
 * ?,???'\'?;</li>
 * <li>?;</li>
 * <li>;</li>
 * <li>key<span style="color:red">?</span>(?)?'=', ':'??;</li>
 * <li>key???"="":",??;</li>
 * <li>??keyvalue;</li>
 * <li>value:,?key/value(?'=',':',' ', '\t', '\f')??<span style="color:red">?(?' ', '\t', '\f')</span>
 * ?;</li>
 * <li>?key/value("="":"),<br>
 * key/value,???value.<br>
 * :"key1 value1",???keyvalue"key1", "value1".
 * </li>
 * <li>?key/value("="":"),<br>
 * ,key/value???value.<br>
 * :"key1 :value1",???keyvalue"key1""value1",?"key1"":value1".
 * </li>
 * </ol>
 * </blockquote>
 * 
 * <h3>properties value ?:</h3>
 * 
 * <blockquote>
 * 
 * <p>
 * ?properties,?,?,???,???.
 * ?\???.:
 * </p>
 * 
 * <pre class="code">
name=Hello world \
My Name is ferreousbox
 * </pre>
 * 
 * <p>
 * ?name??:Hello world My Name is ferreousbox.properties?,
 * ????\?,??,??,?:-).
 * </p>
 * 
 * </blockquote>
 *
 * @author <a href="http://feitianbenyue.iteye.com/">feilong</a>
 * @see "org.apache.velocity.texen.util.PropertiesUtil"
 * @see "org.apache.cxf.common.util.PropertiesLoaderUtils"
 * @see "org.springframework.core.io.support.PropertiesLoaderUtils"
 * @see "org.springframework.core.io.support.PropertiesLoaderSupport"
 * @see "org.apache.commons.configuration.PropertiesConfiguration"
 * @since 1.4.0
 */
public final class PropertiesUtil {

    /** Don't let anyone instantiate this class. */
    private PropertiesUtil() {
        //AssertionError?. ?????. ???.
        //see Effective Java 2nd
        throw new AssertionError("No " + getClass().getName() + " instances for you!");
    }

    // [start] getProperties

    /**
     * klassClassLoader,ClassLoader ?Properties.
     * 
     * <h3> <code>propertiesPath</code>:</h3>
     * <blockquote>
     * ? class {@link PropertiesUtil},? src/main/resources?, messages/feilong-core-message_en_US.properties<br>
     * 
     * <p>
     * ?
     * <code>getPropertiesWithClassLoader(PropertiesUtil.class,<b>"messages/feilong-core-message_en_US.properties"</b>)</code>
     * <br>
     * ??? <b>"messages/feilong-core-message_en_US.properties"</b> (??"/"),ClassLoader
     * JVMBootstrapLoader?.?
     * </p>
     * 
     * <p>
     * ? {@link #getProperties(Class, String)} 
     * </p>
     * </blockquote>
     *
     * @param klass
     *             klass  ClassLoader,? getResourceAsStream
     * @param propertiesPath
     *            the properties path
     * @return  <code>klass</code> null, {@link NullPointerException}<br>
     *          <code>propertiesPath</code> null, {@link NullPointerException}<br>
     *          <code>propertiesPath</code> blank, {@link IllegalArgumentException}<br>
     * @see ClassLoaderUtil#getClassLoaderByClass(Class)
     * @see java.lang.ClassLoader#getResourceAsStream(String)
     * @see #getProperties(InputStream)
     * @see #getProperties(Class, String)
     */
    public static Properties getPropertiesWithClassLoader(Class<?> klass, String propertiesPath) {
        Validate.notBlank(propertiesPath, "propertiesPath can't be blank!");
        ClassLoader classLoader = null;

        //ClassLoaderUtil.getClassLoaderByClass(klass);
        return getProperties(classLoader.getResourceAsStream(propertiesPath));
    }

    /**
     * {@link Class#getResourceAsStream(String)}  InputStream,? {@link #getProperties(InputStream)}.
     * 
     * <h3> <code>propertiesPath</code>:</h3>
     * 
     * <blockquote>
     * ? class {@link PropertiesUtil},? src/main/resources?, messages/feilong-core-message_en_US.properties<br>
     * <p>
     * ? <code>getProperties(PropertiesUtil.class,<b>"/messages/feilong-core-message_en_US.properties"</b>)</code> <br>
     * ??? <b>"/messages/feilong-core-message_en_US.properties"</b>, ???
     * </p>
     * 
     * <p>
     * ? {@link #getPropertiesWithClassLoader(Class, String)} 
     * </p>
     * </blockquote>
     * 
     * @param klass
     *            , klass 
     * @param propertiesPath
     *            the properties path
     * @return  <code>klass</code> null, {@link NullPointerException}<br>
     *          <code>propertiesPath</code> null, {@link NullPointerException}<br>
     *          <code>propertiesPath</code> blank, {@link IllegalArgumentException}<br>
     * @see java.lang.Class#getResourceAsStream(String)
     * @see #getProperties(InputStream)
     * @see #getPropertiesWithClassLoader(Class, String)
     */
    public static Properties getProperties(Class<?> klass, String propertiesPath) {
        Validate.notNull(klass, "klass can't be null!");
        Validate.notBlank(propertiesPath, "propertiesPath can't be blank!");
        // klass.getResourceAsStream classLoader.getResourceAsStream
        // ?,???ClassLoader??.
        return getProperties(klass.getResourceAsStream(propertiesPath));
    }

    /**
     * ?{@link Properties}.
     *
     * @param inputStream
     *            inputStream
     * @return  <code>inputStream</code> null, {@link NullPointerException}<br>
     *         , {@link java.util.Properties#load(InputStream)}
     * @see java.util.Properties#load(InputStream)
     * @see "org.springframework.core.io.support.PropertiesLoaderUtils#loadProperties(Resource)"
     */
    public static Properties getProperties(InputStream inputStream) {
        Validate.notNull(inputStream, "inputStream can't be null!");
        Properties properties = new Properties();
        try {
            properties.load(inputStream);
            return properties;
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }
    // [end]
}