📄 configurationmanager.java
字号:
String[] names = ps.getNames(); outputHeader(4, writer, symbol.getName()); writer.println(" <component name=\"" + symbol.getName() + "\"" + "\n type=\"" + symbol.getObject().getClass().getName() + "\">"); for (int j = 0; j < names.length; j++) { Object obj = ps.getRawNoReplacment(names[j]); if (obj instanceof String) { String value = (String) obj; value = encodeValue(value); String pad = (value.length() > 25) ? "\n " : "" ; writer.println(" <property name=\"" + names[j] + "\"" + pad + " value=\"" + value + "\"/>"); } else if (obj instanceof List) { List list = (List) obj; writer.println(" <propertylist name=\"" + names[j] + "\">"); for (int k = 0; k < list.size(); k++) { writer.println(" <item>" + encodeValue(list.get(k).toString()) + "</item>"); } writer.println(" </propertylist>"); } else { throw new IOException("Ill-formed xml"); } } writer.println(" </component>"); writer.println(); } writer.println("</config>"); writer.println("<!-- Generated on " + new Date() + "-->"); writer.close(); } /** * Outputs the pretty header for a component * * @param indent the indentation level * @param writer where to write the header * @param name the component name */ private void outputHeader(int indent, PrintWriter writer, String name) throws IOException { writer.println(pad(' ', indent) + "<!-- " + pad('*', 50) + " -->"); writer.println(pad(' ', indent) + "<!-- " + name + pad(' ', 50 - name.length()) + " -->"); writer.println(pad(' ', indent) + "<!-- " + pad('*', 50) + " -->"); writer.println(); } /** * Encodes a value so that it is suitable for an xml property * * @param value the value to be encoded * @return the encoded value */ private String encodeValue(String value) { value = value.replaceAll("<", "<"); value = value.replaceAll(">", ">"); return value; } /** * Loads the configuration data from the given url and adds the info to the * symbol table. Throws an IOexception if there is a problem loading the * table. Note that this only performs a partial populating of the symbol * table entry * * @param url * the url to load from * @throws IOException * if an error occurs while loading the symbol table */ private Map loader(URL url) throws IOException { SaxLoader saxLoader = new SaxLoader(url, globalProperties); return saxLoader.load(); } /** * Applies the system properties to the raw property map. System properties * should be of the form compName[paramName]=paramValue * * List types cannot currently be set from system properties. * * @param rawMap * the map of raw property values * @param global * global properies * * @throws PropertyException * if an attempt is made to set a parameter for an unknown * component. */ private void applySystemProperties(Map rawMap, Map global) throws PropertyException { Properties props = System.getProperties(); for (Enumeration e = props.keys(); e.hasMoreElements();) { String param = (String) e.nextElement(); String value = props.getProperty(param); // search for params of the form component[param]=value // thise go in the property sheet for the component int lb = param.indexOf('['); int rb = param.indexOf(']'); if (lb > 0 && rb > lb) { String compName = param.substring(0, lb); String paramName = param.substring(lb + 1, rb); RawPropertyData rpd = (RawPropertyData) rawMap.get(compName); if (rpd != null) { rpd.add(paramName, value); } else { throw new PropertyException(null, param, "System property attempting to set parameter " + " for unknown component " + compName + " (" + param + ")"); } } // look for params of the form foo=fum // these go in the global map else if (param.indexOf('.') == -1) { String symbolName = "${" + param + "}"; global.put(symbolName, value); } } } /** * lookup the global symbol with the given name * * @param key * the symbol name to lookup * @return the symbol value */ String globalLookup(String key) { return (String) globalProperties.get(key); } /** * Gets the config properties for the configuration manager itself * * @param key * the name of the property * @return the property value or null if it doesn't exist. */ String getMyGlobalProperty(String key) { return globalLookup("${" + key + "}"); } /** * Registers the properties commont to all components * * @param registry * a component registry * @throws PropertyException * if an error occurs while registering the properties. */ private void registerCommonProperties(Registry registry) throws PropertyException { registry.register(PROP_COMMON_LOG_LEVEL, PropertyType.STRING); } /** * Configure the logger * */ private void configureLogger() { LogManager logManager = LogManager.getLogManager(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); Properties props = new Properties(); props.setProperty(".edu.cmu.sphinx.level", "FINEST"); props.setProperty("handlers", "java.util.logging.ConsoleHandler"); props.setProperty("java.util.logging.ConsoleHandler.level", "FINEST"); props.setProperty("java.util.logging.ConsoleHandler.formatter", "edu.cmu.sphinx.util.SphinxLogFormatter"); try { props.store(bos, ""); bos.close(); ByteArrayInputStream bis = new ByteArrayInputStream(bos .toByteArray()); logManager.readConfiguration(bis); bis.close(); } catch (IOException ioe) { System.err .println("Can't configure logger, using default configuration"); } // Now we find the SphinxLogFormatter that the log manager created // and configure it. Logger rootLogger = LogManager.getLogManager().getLogger(""); Handler[] handlers = rootLogger.getHandlers(); for (int i = 0; i < handlers.length; i++) { if (handlers[i] instanceof ConsoleHandler) { if (handlers[i].getFormatter() instanceof SphinxLogFormatter) { SphinxLogFormatter slf = (SphinxLogFormatter) handlers[i] .getFormatter(); slf.setTerse("true" .equals(getMyGlobalProperty(PROP_COMMON_LOG_TERSE))); } } } } /** * Shows the current configuration * */ public void showConfig() { System.out.println(" ============ config ============= "); String[] allNames = getInstanceNames(Object.class); for (int i = 0; i < allNames.length; i++) { showConfig(allNames[i]); } } /** * Show the configuration for the compnent with the given name * * @param name the component name */ public void showConfig(String name) { Symbol symbol = (Symbol) symbolTable.get(name); if (symbol == null) { System.out.println("No component: " + name); return; } System.out.println(symbol.getName() + ":"); Registry registry = symbol.getRegistry(); Collection propertyNames = registry.getRegisteredProperties(); PropertySheet properties = symbol.getPropertySheet(); for (Iterator j = propertyNames.iterator(); j.hasNext();) { String propName = (String) j.next(); System.out.print(" " + propName + " = "); Object obj; try { obj = properties.getRaw(propName); } catch (PropertyException e) { // this exception can occcur if a global name // can't be resolved ... obj = "[Unresolved!]"; } if (obj instanceof String) { System.out.println(obj); } else if (obj instanceof List) { List l = (List) obj; for (Iterator k = l.iterator(); k.hasNext();) { System.out.print(k.next()); if (k.hasNext()) { System.out.print(", "); } } System.out.println(); } else { System.out.println("[DEFAULT]"); } } } public void editConfig(String name) { Symbol symbol = (Symbol) symbolTable.get(name); boolean done = false; if (symbol == null) { System.out.println("No component: " + name); return; } System.out.println(symbol.getName() + ":"); Registry registry = symbol.getRegistry(); Collection propertyNames = registry.getRegisteredProperties(); PropertySheet properties = symbol.getPropertySheet(); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); for (Iterator j = propertyNames.iterator(); j.hasNext();) { try { String propName = (String) j.next(); Object value = properties.getRaw(propName); String svalue = null; if (value instanceof List) { continue; } else if (value instanceof String) { svalue = (String) value; } else { svalue = "DEFAULT"; } done = false; while (!done) { System.out.print(" " + propName + " [" + svalue + "]: "); String in = br.readLine(); if (in.length() == 0) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -