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

📄 validatingpropertysheet.java

📁 It is the Speech recognition software. It is platform independent. To execute the source code,
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* *  * Copyright 1999-2004 Carnegie Mellon University.   * Portions Copyright 2004 Sun Microsystems, Inc.   * Portions Copyright 2004 Mitsubishi Electric Research Laboratories. * All Rights Reserved.  Use is subject to license terms. *  * See the file "license.terms" for information on usage and * redistribution of this file, and for a DISCLAIMER OF ALL  * WARRANTIES. * */package edu.cmu.sphinx.util.props;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.ListIterator;import java.util.Map;import java.util.Set;import java.util.logging.Level;import java.util.regex.Pattern;import java.util.regex.Matcher;import java.util.logging.Logger;import java.io.PrintStream;import java.net.URL;import java.net.MalformedURLException;/** * An implementation of the property sheet that validates the properties * against a registry. *   */class ValidatingPropertySheet implements PropertySheet {    private ConfigurationManager cm;    private Map properties = new HashMap();    private Registry registry;    private String className;    private final static List EMPTY = new ArrayList();    /**     * Creates a buildable property sheet     *      * @param cm     *            the configuration manager     *      * @param registry     *            controls what properties are allowed in this property sheet.     * @throws PropertyException     *             if there is a problem with any of the properties.     */    ValidatingPropertySheet(ConfigurationManager cm, Registry registry,            RawPropertyData rpd) throws PropertyException {        this.cm = cm;        this.registry = registry;        // for each property in the raw property data, check that it        // is a registered property, and that it is of the proper type.        Map raw = rpd.getProperties();        className = rpd.getClassName();        for (Iterator i = raw.keySet().iterator(); i.hasNext();) {            String key = (String) i.next();            Object val = raw.get(key);            setRaw(key, val);        }    }    /**     * Adds a new property to the set of properties     *      * @param name     *            the name of the property     * @param value     *            the value of the proberty     * @throws PropertyException     *             if the property is not a registered property or the value is     *             not of the proper type.     */    void addProperty(String name, String value) {        properties.put(name, value);    }    /**     * Sets the given property to the given name     *      * @param name     *            the simple property name     * @param value     *            the value for the property     *      *       */    public void setString(String name, String value) {        throw new UnsupportedOperationException();    }    /**     * Sets the given property to the given name     *      * @param name     *            the simple property name     * @param value     *            the value for the property     */    public void setInt(String name, int value) {        throw new UnsupportedOperationException();    }    /**     * Sets the given property to the given name     *      * @param name     *            the simple property name     * @param value     *            the value for the property     * @throws PropertyException     *             if the property is not a registered property or the value is     *             not of the proper type.     */    public void setFloat(String name, float value) {        throw new UnsupportedOperationException();    }    /**     * Sets the property     *      * @param key     *            the property name     * @param val     *            the property (either a String or a String[])     * @throws PropertyException     *             if the property is not a registered property or the value is     *             not of the proper type.     */    public void setRaw(String key, Object val) throws PropertyException {        PropertyType type = registry.lookup(key);        if (type == null) {            throw new PropertyException(registry.getOwner(), key,                    "Attempt to set unregistered property");        } else if (val instanceof String) {            String sval = (String) val;                        if (!isGlobalVariable(sval) && !type.isValid(val)) {                throw new PropertyException(registry.getOwner(), key,                         "value (" + sval + ")" + " is not a valid " + type);            } else {                properties.put(key, val);            }        }  else if (!type.isValid(val)) {            throw new PropertyException(registry.getOwner(), key, val                    + " is not a valid " + type);        } else {            properties.put(key, val);        }    }    /**     * Gets a property by name from the property map     *      * @param name     *            the name of the property     * @return the return value     */    public Object getRaw(String name) throws PropertyException {        Object value = getRawNoReplacment(name);                if (value == null) {            return null;        } else if (value instanceof String) {            String sval = (String) value;            if (sval.startsWith("${")) {                value = cm.globalLookup(sval);                if (value == null) {                    throw new PropertyException(registry.getOwner(), name,                            "Can't find global property " + sval);                }            }        } else if (value instanceof List) {            List lval = (List) value;            for (ListIterator i = lval.listIterator(); i.hasNext(); ) {                String sval = (String) i.next();                if (sval.startsWith("${")) {                    String itemVal = cm.globalLookup(sval);                    if (itemVal == null) {                        throw new PropertyException(registry.getOwner(), name,                                "Can't find global property " + sval);                    } else {                        i.set(itemVal);                    }                }            }        }        return value;    }    /**     * Gets a property by name from the property map, no global symbol     * replacement is done     *      * @param name     *            the name of the property     * @return the return value     */    public Object getRawNoReplacment(String name) {        return properties.get(name);    }    /**     * Gets the value associated with this name. Note that is considered legal     * to get a string version of any property     *      * @param name     *            the name     * @param defaultValue     *            the default value for the property     * @return the value     */    public String getString(String name, String defaultValue)            throws PropertyException {        checkType(name, PropertyType.STRING);        String value = (String) getRaw(name);        if (value == null) {            value = defaultValue;        }        return value;    }    /**     * Gets the value associated with this name     *      * @param name     *            the name     * @param defaultValue     *            the default value for the property     * @return the value     * @throws PropertyException     *             if the property is not a registered property or the value is     *             not of the proper type.     */    public int getInt(String name, int defaultValue) throws PropertyException {        checkType(name, PropertyType.INT);        try {            String val = (String) getRaw(name);            if (val == null) {                return defaultValue;            } else {                return Integer.parseInt(val);            }        } catch (NumberFormatException e) {            throw new PropertyException(registry.getOwner(), name,                    "bad integer format");        }    }    /**     * Gets the value associated with this name     *      * @param name     *            the name     * @param defaultValue     *            the default value     * @return the value     */    public float getFloat(String name, float defaultValue)            throws PropertyException {        checkType(name, PropertyType.FLOAT);        try {            String val = (String) getRaw(name);            if (val == null) {                return defaultValue;            } else {                return Float.parseFloat(val);            }        } catch (NumberFormatException e) {            throw new PropertyException(registry.getOwner(), name,                    "bad float format");        }    }        /**     * Gets the value associated with this name     *      * @param name     *            the name     * @param defaultValue     *            the default value     * @return the value     */    public double getDouble(String name, double defaultValue)            throws PropertyException {        checkType(name, PropertyType.DOUBLE);        try {            String val = (String) getRaw(name);            if (val == null) {                return defaultValue;            } else {                return Double.parseDouble(val);            }        } catch (NumberFormatException e) {            throw new PropertyException(registry.getOwner(), name,                    "bad double format");        }    }        /**     * Gets the value associated with this name     *      * @param name     *            the name     * @param defaultValue     *            the default value     * @return the value     */    public boolean getBoolean(String name, boolean defaultValue)            throws PropertyException {        checkType(name, PropertyType.BOOLEAN);        String val = (String) getRaw(name);        if (val == null) {            return defaultValue;        } else {            return Boolean.valueOf((String) getRaw(name)).booleanValue();        }    }    /**     * a regular expression that matches our own resource 'protocol'.

⌨️ 快捷键说明

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