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

📄 configurationmanager.java

📁 It is the Speech recognition software. It is platform independent. To execute the source code,
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* *  * 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.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.PrintStream;import java.io.PrintWriter;import java.io.BufferedReader;import java.io.InputStreamReader;import java.net.URL;import java.util.ArrayList;import java.util.Collection;import java.util.Enumeration;import java.util.HashMap;import java.util.Iterator;import java.util.LinkedHashMap;import java.util.List;import java.util.Map;import java.util.Properties;import java.util.logging.ConsoleHandler;import java.util.Date;import java.util.logging.Handler;import java.util.logging.Level;import java.util.logging.LogManager;import java.util.logging.Logger;import java.util.regex.Pattern;import java.util.regex.Matcher;import edu.cmu.sphinx.util.SphinxLogFormatter;/** * Manages the configuration for the system. The configuration manager provides * the following services: *  * <ul> * <li>Loads configuration data from an XML-based configuration file. * <li>Manages the component life-cycle for Configurable objects * <li>Allows discovery of components via name or type. * </ul> * <p> * For an overview of how to use this configuration management system to create * and configure components please see: <b><a * href="doc-files/ConfigurationManagement.html"> Sphinx-4 Configuration * Management </a> </b> * <p> * For a description of how to create your own configurable components see: <b> * {@link edu.cmu.sphinx.util.props.Configurable}</b> *   */public class ConfigurationManager {    /**     * Sphinx Property that defines whether or not the configuration manager     * will trace object creations     */    public final static String PROP_SHOW_CREATIONS = "showCreations";    /**     * The default value for PROP_SHOW_CREATIONS     */    public final static boolean PROP_SHOW_CREATIONS_DEFAULT = false;    /**     * A common property (used by all components) that sets the log level for     * the component.     *      * @see java.util.logging.Level     */    public final static String PROP_COMMON_LOG_LEVEL = "logLevel";        /**     * A common property (used by all components) that sets the tersness     * of the log output     *      */    public final static String PROP_COMMON_LOG_TERSE = "logTerse";    private Map symbolTable = new LinkedHashMap();    private Map rawPropertyMap;    private Map globalProperties = new LinkedHashMap();    private boolean showCreations;    // this pattern matches strings of the form '${word}'    private static Pattern globalSymbolPattern =         Pattern.compile("\\$\\{(\\w+)\\}");    /**     * Creates a new configuration manager. Initial properties are loaded from     * the given URL. No need to keep the notion of 'context' around anymore we     * will just pass around this property manager.     *      * @param url     *                place to load initial properties from     * @throws IOException     *                 if an error occurs while loading properties from the URL     *       */    public ConfigurationManager(URL url) throws IOException, PropertyException {        rawPropertyMap = loader(url);        applySystemProperties(rawPropertyMap, globalProperties);        configureLogger();        // we can't config the configuration manager with itself so we        // do some of these config items manually.        showCreations = "true".equals(getMyGlobalProperty(PROP_SHOW_CREATIONS));    }    /**     * Returns the property sheet for the given object instance     *      * @param instanceName     *                the instance name of the object     * @return the property sheet for the object.     */    public PropertySheet getPropertySheet(String instanceName) {        PropertySheet propertySheet = null;        Symbol symbol = (Symbol) symbolTable.get(instanceName);        if (symbol != null) {            propertySheet = symbol.getPropertySheet();        }        return propertySheet;    }    /**     * Returns the registry for the given object instance     *      * @param instanceName     *                the instance name of the object     * @return the property sheet for the object.     */    public Registry getRegistry(String instanceName) {        Registry registry = null;        Symbol symbol = (Symbol) symbolTable.get(instanceName);        if (symbol != null) {            registry = symbol.getRegistry();        }        return registry;    }    /**     * Gets all instances that are of the given type or are assignable to that     * type. Object.class matches all.     *      * @param type     *                the desired type of instance     * @return the set of all instances     */    public String[] getInstanceNames(Class type) {        List list = new ArrayList();        for (Iterator i = symbolTable.values().iterator(); i.hasNext();) {            Symbol symbol = (Symbol) i.next();            if (type.isInstance(symbol.getObject())) {                list.add(symbol.getName());            }        }        return (String[]) list.toArray(new String[list.size()]);    }    /**     * Looks up a configurable component by name. Creates it if necessary     *      * @param name     *                the name of the component     * @return the compnent, or null if a component was not found.     *      * @throws InstantiationException     *                 if the requested object could not be properly created,     *                 or is not a configurable object.     *      * @throws PropertyException     *                 if an error occurs while setting a property     */    public Configurable lookup(String name) throws InstantiationException,            PropertyException {        Symbol symbol = (Symbol) symbolTable.get(name);        if (symbol == null) {            if (showCreations) {                System.out.println("Creating: " + name);            }            // it is not in the symbol table, so construct            // it based upon our raw property data            RawPropertyData rpd = (RawPropertyData) rawPropertyMap.get(name);            if (rpd != null) {                String className = rpd.getClassName();                try {                    Class cls = Class.forName(className);                    Configurable configurable = (Configurable) cls                            .newInstance();                    Registry registry = new Registry(configurable);                    registerCommonProperties(registry);                    configurable.register(name, registry);                    ValidatingPropertySheet propertySheet = new ValidatingPropertySheet(                            this, registry, rpd);                    symbol = new Symbol(name, propertySheet, registry,                            configurable);                    symbolTable.put(name, symbol);                    configurable.newProperties(propertySheet);                    return symbol.getObject();                } catch (ClassNotFoundException e) {                    throw new InstantiationException("Can't find class "                            + className + " object:" + name);                } catch (IllegalAccessException e) {                    throw new InstantiationException("Can't access class "                            + className + " object:" + name);                } catch (ClassCastException e) {                    throw new InstantiationException("Not configurable class "                            + className + " object:" + name);                }            }        } else {            return symbol.getObject();        }        return null;    }    /**     * Sets the property of the given component to the given value.     * Component must be an exisiting, instantiatied component     *     * @param component an existing component     * @param prop the property name     * @param value the new value.     */    public void setProperty(String component, String prop, String value)             throws PropertyException {        Symbol symbol = (Symbol) symbolTable.get(component);        if (symbol != null) {            PropertySheet ps = symbol.getPropertySheet();            Configurable c = symbol.getObject();            Object old = ps.getRawNoReplacment(prop);            ps.setRaw(prop, value);            try {                c.newProperties(ps);            } catch (PropertyException pe) {                // if the newProperties throws an objection                // then we need to restore the previous value                // roll back the old value if we can                ps.setRaw(prop, old);                c.newProperties(ps);                throw pe;            }        } else {            throw new PropertyException(null, prop,                "Can't find component " + component);        }    }    /**     * Saves the current configuration to the given file     *      * @param file     *                place to save the configuration     * @throws IOException     *                 if an error occurs while writing to the file     */    public void save(File file) throws IOException {        FileOutputStream fos = new FileOutputStream(file);        PrintWriter writer = new PrintWriter(fos);        writer.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");        writer.println();        outputHeader(0, writer, "Sphinx-4 Configuration File");        writer.println();        writer.println("<config>");        // save global symbols        outputHeader(2, writer, "Global Properties");        for (Iterator i = globalProperties.keySet().iterator(); i.hasNext(); ) {            String name = (String) i.next();            String value = (String) globalProperties.get(name);            value = encodeValue(value);            writer.println("        <property name=\"" +                stripGlobalSymbol(name) + "\" value=\"" + value + "\"/>");        }        writer.println();        outputHeader(2, writer, "Components");        String[] allNames = getInstanceNames(Object.class);        for (int i = 0; i < allNames.length; i++) {            Symbol symbol = (Symbol) symbolTable.get(allNames[i]);            PropertySheet ps = symbol.getPropertySheet();

⌨️ 快捷键说明

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