⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 propertiesparser.java

📁 定时器开源项目, 相对于 jcrontab, Quartz 算是更完整的一个项目, 随著开发的版本上来, 他已经脱离只是写在程序里面的计时器, 在指定的时间或区间, 处理所指定的事件. 也加入了 se
💻 JAVA
字号:
/* * Copyright James House (c) 2001-2004 *  * All rights reserved. *  * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: 1. * Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. 2. Redistributions in * binary form must reproduce the above copyright notice, this list of * conditions and the following disclaimer in the documentation and/or other * materials provided with the distribution. *  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */package org.quartz.utils;import java.util.Enumeration;import java.util.HashMap;import java.util.Properties;import java.util.StringTokenizer;import java.util.Vector;/** * <p> * This is an utility calss used to parse the properties. * </p> *  * @author James House */public class PropertiesParser {    /*     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     *      * Data members.     *      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     */    Properties props = null;    /*     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     *      * Constructors.     *      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     */    public PropertiesParser(Properties props) {        this.props = props;    }    /*     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     *      * Interface.     *      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     */    public Properties getUnderlyingProperties() {        return props;    }    public String getStringProperty(String name) {        String val = props.getProperty(name);        if (val == null) return null;        return val.trim();    }    public String getStringProperty(String name, String def) {        String val = props.getProperty(name, def);        if (val == null) return def;        val = val.trim();        if (val.length() == 0) return def;        return val;    }    public String[] getStringArrayProperty(String name) {        return getStringArrayProperty(name, null);    }    public String[] getStringArrayProperty(String name, String[] def) {        String vals = getStringProperty(name);        if (vals == null) return def;        if (vals != null && !vals.trim().equals("")) {            StringTokenizer stok = new StringTokenizer(vals, ",");            Vector strs = new Vector();            try {                while (stok.hasMoreTokens()) {                    strs.addElement(stok.nextToken());                }                String[] outStrs = new String[strs.size()];                for (int i = 0; i < strs.size(); i++)                    outStrs[i] = (String) strs.elementAt(i);                return outStrs;            } catch (Exception e) {                return def;            }        }        return def;    }    public boolean getBooleanProperty(String name) {        String val = getStringProperty(name);        if (val == null) return false;        return new Boolean(val).booleanValue();    }    public boolean getBooleanProperty(String name, boolean def) {        String val = getStringProperty(name);        if (val == null) return def;        return new Boolean(val).booleanValue();    }    public byte getByteProperty(String name) throws NumberFormatException {        String val = getStringProperty(name);        if (val == null) throw new NumberFormatException(" null string");        try {            return Byte.parseByte(val);        } catch (NumberFormatException nfe) {            throw new NumberFormatException(" '" + val + "'");        }    }    public byte getByteProperty(String name, byte def)            throws NumberFormatException {        String val = getStringProperty(name);        if (val == null) return def;        try {            return Byte.parseByte(val);        } catch (NumberFormatException nfe) {            throw new NumberFormatException(" '" + val + "'");        }    }    public char getCharProperty(String name) {        String param = getStringProperty(name);        if (param == null) return '\0';        if (param.length() == 0) return '\0';        return param.charAt(0);    }    public char getCharProperty(String name, char def) {        String param = getStringProperty(name);        if (param == null) return def;        if (param.length() == 0) return def;        return param.charAt(0);    }    public double getDoubleProperty(String name) throws NumberFormatException {        String val = getStringProperty(name);        if (val == null) throw new NumberFormatException(" null string");        try {            return Double.parseDouble(val);        } catch (NumberFormatException nfe) {            throw new NumberFormatException(" '" + val + "'");        }    }    public double getDoubleProperty(String name, double def)            throws NumberFormatException {        String val = getStringProperty(name);        if (val == null) return def;        try {            return Double.parseDouble(val);        } catch (NumberFormatException nfe) {            throw new NumberFormatException(" '" + val + "'");        }    }    public float getFloatProperty(String name) throws NumberFormatException {        String val = getStringProperty(name);        if (val == null) throw new NumberFormatException(" null string");        try {            return Float.parseFloat(val);        } catch (NumberFormatException nfe) {            throw new NumberFormatException(" '" + val + "'");        }    }    public float getFloatProperty(String name, float def)            throws NumberFormatException {        String val = getStringProperty(name);        if (val == null) return def;        try {            return Float.parseFloat(val);        } catch (NumberFormatException nfe) {            throw new NumberFormatException(" '" + val + "'");        }    }    public int getIntProperty(String name) throws NumberFormatException {        String val = getStringProperty(name);        if (val == null) throw new NumberFormatException(" null string");        try {            return Integer.parseInt(val);        } catch (NumberFormatException nfe) {            throw new NumberFormatException(" '" + val + "'");        }    }    public int getIntProperty(String name, int def)            throws NumberFormatException {        String val = getStringProperty(name);        if (val == null) return def;        try {            return Integer.parseInt(val);        } catch (NumberFormatException nfe) {            throw new NumberFormatException(" '" + val + "'");        }    }    public int[] getIntArrayProperty(String name) throws NumberFormatException {        return getIntArrayProperty(name, null);    }    public int[] getIntArrayProperty(String name, int[] def)            throws NumberFormatException {        String vals = getStringProperty(name);        if (vals == null) return def;        if (vals != null && !vals.trim().equals("")) {            StringTokenizer stok = new StringTokenizer(vals, ",");            Vector ints = new Vector();            try {                while (stok.hasMoreTokens()) {                    try {                        ints.addElement(new Integer(stok.nextToken()));                    } catch (NumberFormatException nfe) {                        throw new NumberFormatException(" '" + vals + "'");                    }                }                int[] outInts = new int[ints.size()];                for (int i = 0; i < ints.size(); i++)                    outInts[i] = ((Integer) ints.elementAt(i)).intValue();                return outInts;            } catch (Exception e) {                return def;            }        }        return def;    }    public long getLongProperty(String name) throws NumberFormatException {        String val = getStringProperty(name);        if (val == null) throw new NumberFormatException(" null string");        try {            return Long.parseLong(val);        } catch (NumberFormatException nfe) {            throw new NumberFormatException(" '" + val + "'");        }    }    public long getLongProperty(String name, long def)            throws NumberFormatException {        String val = getStringProperty(name);        if (val == null) return def;        try {            return Long.parseLong(val);        } catch (NumberFormatException nfe) {            throw new NumberFormatException(" '" + val + "'");        }    }    public short getShortProperty(String name) throws NumberFormatException {        String val = getStringProperty(name);        if (val == null) throw new NumberFormatException(" null string");        try {            return Short.parseShort(val);        } catch (NumberFormatException nfe) {            throw new NumberFormatException(" '" + val + "'");        }    }    public short getShortProperty(String name, short def)            throws NumberFormatException {        String val = getStringProperty(name);        if (val == null) return def;        try {            return Short.parseShort(val);        } catch (NumberFormatException nfe) {            throw new NumberFormatException(" '" + val + "'");        }    }    public String[] getPropertyGroups(String prefix) {        Enumeration keys = props.propertyNames();        HashMap groups = new HashMap(10);        if (!prefix.endsWith(".")) prefix += ".";        while (keys.hasMoreElements()) {            String key = (String) keys.nextElement();            if (key.startsWith(prefix)) {                String groupName = key.substring(prefix.length(), key.indexOf(                        '.', prefix.length()));                groups.put(groupName, groupName);            }        }        return (String[]) groups.values().toArray(new String[groups.size()]);    }    public Properties getPropertyGroup(String prefix) {        return getPropertyGroup(prefix, false);    }    public Properties getPropertyGroup(String prefix, boolean stripPrefix) {        Enumeration keys = props.propertyNames();        Properties group = new Properties();        if (!prefix.endsWith(".")) prefix += ".";        while (keys.hasMoreElements()) {            String key = (String) keys.nextElement();            if (key.startsWith(prefix)) {                if (stripPrefix) group.put(key.substring(prefix.length()),                        props.getProperty(key));                else                    group.put(key, props.getProperty(key));            }        }        return group;    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -