📄 parameterservice.java
字号:
/* * YALE - Yet Another Learning Environment * Copyright (C) 2002, 2003 * Simon Fischer, Ralf Klinkenberg, Ingo Mierswa, * Katharina Morik, Oliver Ritthoff * Artificial Intelligence Unit * Computer Science Department * University of Dortmund * 44221 Dortmund, Germany * email: yale@ls8.cs.uni-dortmund.de * web: http://yale.cs.uni-dortmund.de/ * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * USA. */package edu.udo.cs.yale.tools;import edu.udo.cs.yale.Yale;import edu.udo.cs.yale.tools.LogService;import edu.udo.cs.yale.tools.xml.XML2ObjectParser;import edu.udo.cs.yale.tools.xml.TextTagObject;import edu.udo.cs.yale.tools.param.OperatorParams;import edu.udo.cs.yale.tools.param.ParameterList;import edu.udo.cs.yale.tools.param.SingleParameter;import edu.udo.cs.yale.tools.xml.XMLException;import edu.udo.cs.yale.operator.Operator;import edu.udo.cs.yale.operator.OperatorChain;import java.io.Reader;import java.io.FileReader;import java.io.File;import java.io.InputStream;import java.io.FileInputStream;import java.io.InputStreamReader;import java.io.FileNotFoundException;import java.io.IOException;import java.net.URL;import java.util.List;import java.util.Iterator;/** This class provides static methods for loading and accessing config files. Config files * are XML based and should look similar to the following template:<br> * <code> * <op name="operator name" class="classname"><br> * <para key="key" value="singleValue"/><br> * <para key="key"><set>val1,val2,...</set><br> * <para key="key" group="group" value="value"/><br> * <op name="inner operator name"><br> * [...]<br> * </op><br> * [...]<br> * </op><br> * </code><br> * Where * <ul> * <li>classname is one of <code>edu.udo.cs.yale.tools.param.OperatorParams.OPERATOR_NAMES</code> * <li>the outer <code><op></code> block should be named "global" * <li>the text enclosed by a <code><set></code>-<code></set></code> pair is a comma separated list * </ul> * XML parsing is done by kxml (Stefan Haustein), which should appear somwhere in the * classpath.<br> * ParameterService provides useful methods for wrappers and operator chains: At any time * an operator can read a parameter value by calling * <code>getParameter(operator.name, key)</code>, but only one of the values in the set * is visible to it. Its parent operator can iterate over these set (or sets) by calling * <code>iterateInnerOperators(parentoperator.name)</code><br> * One can easily generate operators from the config-file by calling one of the * <code>instantiateXXX</code> methods. * * @author Simon, Ingo * @version $Id: ParameterService.java,v 2.7 2003/06/25 13:45:43 fischer Exp $ **/public class ParameterService { private static final Class[] TAG_CLASS = { SingleParameter.class, OperatorParams.class, ParameterList.class }; private static final String[] TAG_NAME = { "parameter", "operator", "list" }; // -------------------- loading and inistialization -------------------- /** Tries to find the yale.home directory if the porperty is not set and sets it. */ public static void ensureYaleHomeSet() { String home = System.getProperty("yale.home"); if (home != null) { LogService.logMessage("yale.home is '"+home+"'.", LogService.INIT); } else { LogService.logMessage("Property yale.home is not set. Guessing.", LogService.INIT); String classpath = System.getProperty("java.class.path"); LogService.logMessage("Classpath is "+classpath, LogService.INIT); String pathComponents[] = classpath.split(File.pathSeparator); for (int i = 0; i < pathComponents.length; i++) { String path = pathComponents[i].trim(); if (path.endsWith("yale.jar")) { File jar = new File(path).getAbsoluteFile(); String message = "Trying parent directory of '"+jar+"'..."; File dir = jar.getParentFile(); if (dir != null) { dir = dir.getParentFile(); if (dir != null) { //message += "'"+dir.getAbsolutePath()+"'..."; message += "gotcha!"; System.setProperty("yale.home", dir.getAbsolutePath()); } else { message += "failed"; } } else { message += "failed"; } LogService.logMessage(message, LogService.INIT); } } } getProperty("yale.home"); // throws fatal exception if necessary } /** Registers the operators and reads the rc file. */ public static void init() { ensureYaleHomeSet(); loadRCFile(); URL operatorURL = Tools.getResource("operators.xml"); try { OperatorService.registerOperators("operators.xml", new InputStreamReader(operatorURL.openStream()), null); } catch (IOException e) { LogService.logMessage("Cannot read 'operators.xml'.", LogService.ERROR); } String additionalOperatorFileName = System.getProperty("yale.operators.additional"); if (additionalOperatorFileName != null) { File additionalOperatorFile = new File(additionalOperatorFileName); if (additionalOperatorFile.exists()) { try { OperatorService.registerOperators(additionalOperatorFile.getPath(), new FileReader(additionalOperatorFile), null); } catch (IOException e) { LogService.logMessage("Cannot read '"+additionalOperatorFile+"'.", LogService.ERROR); } } else { LogService.logMessage("Cannot find operator description file '"+additionalOperatorFileName+"'", LogService.ERROR); } } } public static File getUserConfigFile(String name) { return new File(getUserYaleDir(), name); } private static File getUserYaleDir() { File homeDir = new File(System.getProperty("user.home")); File userHomeDir = new File(homeDir, ".yale"); if (!userHomeDir.exists()) { LogService.logMessage("Creating directory '"+userHomeDir+"'", LogService.INIT); userHomeDir.mkdir(); } return userHomeDir; } private static void loadRCFile() { File globalRC = getConfigFile("yalerc"); loadAllRCFiles(globalRC.getPath());// if (globalRC != null) loadAllRCFiles(globalRC.getPath());// else LogService.logMessage("Trying yale.globalrc. Property not specified...skipped", LogService.INIT); loadAllRCFiles(getUserConfigFile("yalerc").getAbsolutePath()); loadAllRCFiles(new File(new File(System.getProperty("user.dir")), "yalerc").getAbsolutePath()); String localRC = System.getProperty("yale.rcfile"); if (localRC != null) loadRCFile(localRC); else LogService.logMessage("Trying yale.rcfile. Property not specified...skipped", LogService.INIT); } private static void loadAllRCFiles(String rcFileName) { loadRCFile(rcFileName); loadRCFile(rcFileName+"."+System.getProperty("os.name")); } private static void loadRCFile(String rcFileName) { if (rcFileName == null) return; File rcFile = new File(rcFileName); if (!rcFile.exists()) { LogService.logMessage("Trying rcfile '" + rcFile + "'...skipped", LogService.INIT); return; } try { InputStream in = new FileInputStream(rcFile); System.getProperties().load(in); in.close(); LogService.logMessage("Read rcfile '" + rcFile + "'.", LogService.INIT); return; } catch (IOException e) { LogService.logMessage("Cannot load rcfile: " + rcFile, LogService.ERROR); return; } } // -------------------- parameters -------------------- /** Returns a system property and throws a runtime exception if the porperty is not set. */ public static String getProperty(String key) { String property = System.getProperty(key); if (property == null) { //LogService.logMessage("Property '" + key + "' not set!", LogService.FATAL); throw new RuntimeException("Property '" + key + "' not set!"); } return property; } public static File getYaleHome() { return new File(getProperty("yale.home")); } public static File getConfigFile(String name) { File home = new File(getProperty("yale.home")); return new File(home, "etc"+File.separator+name); } public static File getLibraryFile(String name) { File home = new File(getProperty("yale.home")); return new File(home, "lib"+File.separator+name); } public static File getSourceFile(String filename) { File home = new File(getProperty("yale.home")); return new File(home, "src"+File.separator+filename); } public static File getPluginDir() { return getLibraryFile("plugins"); } // -------------------- tools -------------------- /** Returns true if value is "true", "yes", "y" or "on". * Returns false if value is "false", "no", "n" or "off". * Otherwise returns <tt>deflt</tt>. */ public static boolean booleanValue(String value, boolean deflt) { if (value == null) return deflt; if (value.equals("true")) return true; else if (value.equals("yes")) return true; else if (value.equals("y")) return true; else if (value.equals("on")) return true; else if (value.equals("false")) return false; else if (value.equals("no")) return false; else if (value.equals("n")) return false; else if (value.equals("off")) return false; return deflt; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -