com.beginner.core.utils.PropertyUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.beginner.core.utils.PropertyUtil.java

Source

/*
 * Copyright 2015-9999 the original author or authors.
 *
 * 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.beginner.core.utils;

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Properties;
import java.util.ResourceBundle;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.XMLConfiguration;
import org.apache.commons.lang3.StringUtils;

import com.beginner.core.common.Const;

/**
* <b>??</b>ResourcesUtil<br/>
* <b>??</b>??<br/>
* <b></b>Hsiao Lin Studio<br/>
* <b></b><br/>
* <b></b>20150521 ?6:18:18<br/>
* <b></b><br/>
* @version 1.0.0<br/>
*/
public class PropertyUtil {

    /**
     * ?beginner.propertieskeyString
     * @param key         key
     * @return String      keyvalue
     */
    public static String getStr(String key) {
        return getProperty(key, Const.BEGINNER);
    }

    /**
     * ?beginner.propertieskeyint
     * @param key         key
     * @return int         keyvalue
     */
    public static int getInt(String key) {
        return Integer.parseInt(getProperty(key, Const.BEGINNER));
    }

    /**
     * ?Properties?
     * @param fileName      :beginner.properties
     * @return Properties    ?
     * @throws Exception   IO
     */
    public static Properties getObj(String fileName) throws Exception {
        Properties properties = new Properties();
        properties.load(PropertyUtil.class.getClassLoader().getResourceAsStream(fileName));
        return properties;
    }

    /**
     * ?Properties?
     * @param fileName      :/data/resources/beginner.properties
     * @return Properties    ?
     * @throws Exception   IO
     */
    public static Properties getObjExternal(String fileName) throws Exception {
        Properties properties = new Properties();
        properties.load(new FileInputStream(fileName));
        return properties;
    }

    /**
     * java.util.ResourceBundle?classpathproperties??TestResourcesUtil
     * @param key         key
     * @param fileName      +????.properties??
     * @return String      keyvalue
     */
    public static String getProperty(String key, String fileName) {

        if (StringUtils.isBlank(key))
            throw new IllegalArgumentException("The key cannot be null and cannot be empty.");

        if (StringUtils.isBlank(fileName))
            throw new IllegalArgumentException("The fileName cannot be null and cannot be empty.");

        Locale locale = Locale.getDefault();

        ResourceBundle resource = ResourceBundle.getBundle(fileName, locale);
        String value = StringUtils.EMPTY;
        try {
            value = resource.getString(key);
        } catch (Exception e) {
            return value;
        }
        return value;
    }

    /**
     * org.apache.commons.configuration.Configuration?properties??TestResourcesUtil
     * <p>
     * ????properties?<br>
     * 1?classpathkey=type fileName=beginner.properties             -mysql<br>
     * 2?key=type fileName=/data/app/beginner.properties    -mysql<br>
     * 3?key=type fileName=D:/data/app/beginner.properties    -mysql<br>
     * </p>
     * @param key         key
     * @param fileName      +??+???
     * @return String      keyvalue
     * @throws Exception   IO
     */
    public static String getProperties(String key, String fileName) throws Exception {

        if (StringUtils.isBlank(key))
            throw new IllegalArgumentException("The key cannot be null and cannot be empty.");

        if (StringUtils.isBlank(fileName))
            throw new IllegalArgumentException("The fileName cannot be null and cannot be empty.");

        String properties = StringUtils.EMPTY;
        Configuration config = null;
        try {
            config = new PropertiesConfiguration(fileName);
            properties = config.getString(key);
        } catch (Exception e) {
            throw new Exception();
        }
        return properties;
    }

    /**
     * ?XML?key?TestResourcesUtil
     * @param key          XMLkey
     * @param fileName       XML???
     * @return String       key
     * @throws Exception    
     */
    public static String getXmlProperty(String key, String fileName) throws Exception {

        if (StringUtils.isBlank(key))
            throw new IllegalArgumentException("The key cannot be null and cannot be empty.");

        if (StringUtils.isBlank(fileName))
            throw new IllegalArgumentException("The fileName cannot be null and cannot be empty.");

        String properties = StringUtils.EMPTY;
        XMLConfiguration config = null;
        try {
            config = new XMLConfiguration(fileName);
            properties = config.getString(key);
        } catch (Exception e) {
            throw new Exception();
        }
        return properties;
    }

    /**
     * ?XML?KEY?TestResourcesUtil
     * @param fileName      XML???
     * @return List<String>   ?
     * @throws Exception   ?
     */
    public static List<String> getXmlProperties(String fileName) throws Exception {

        if (StringUtils.isBlank(fileName))
            throw new IllegalArgumentException("The fileName cannot be null and cannot be empty.");

        XMLConfiguration config = null;
        Iterator<String> properties = null;
        List<String> list = new ArrayList<String>();
        try {
            config = new XMLConfiguration(fileName);
            properties = config.getKeys();
            while (properties.hasNext()) {
                String key = (String) properties.next();
                list.add(key);
            }
        } catch (Exception e) {
            throw new Exception();
        }
        return list;
    }
}