📄 parameterservice.java
字号:
/*
* YALE - Yet Another Learning Environment
* Copyright (C) 2001-2004
* Simon Fischer, Ralf Klinkenberg, Ingo Mierswa,
* Katharina Morik, Oliver Ritthoff
* Artificial Intelligence Unit
* Computer Science Department
* University of Dortmund
* 44221 Dortmund, Germany
* email: yale-team@lists.sourceforge.net
* 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.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 loads the yalrc property files and provides methods to access them.
* It also provides methods to create files relative to the yale home directory.
* As the {@link #getProperty(String)} method throws an exception if the parameter
* is not set, the <code>System.getProperty(String)</code> methods should be used
* if this is not desired.
*
* @author Simon, Ingo
* @version $Id: ParameterService.java,v 2.13 2004/08/27 11:57:45 ingomierswa Exp $
**/
public class ParameterService {
// -------------------- loading and initialization --------------------
/** 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",
operatorURL.openStream(),
null);
} catch (IOException e) {
LogService.logMessage("Cannot read 'operators.xml'.", LogService.ERROR);
}
String additionalOperators = System.getProperty("yale.operators.additional");
if (additionalOperators != null) {
String[] additionalOperatorFileNames = additionalOperators.split(File.pathSeparator);
for (int i = 0; i < additionalOperatorFileNames.length; i++) {
File additionalOperatorFile = new File(additionalOperatorFileNames[i]);
if (additionalOperatorFile.exists()) {
try {
OperatorService.registerOperators(additionalOperatorFile.getPath(),
new FileInputStream(additionalOperatorFile),
null);
} catch (IOException e) {
LogService.logMessage("Cannot read '"+additionalOperatorFile+"'.", LogService.ERROR);
}
} else {
LogService.logMessage("Cannot find operator description file '"+additionalOperatorFileNames[i]+"'",
LogService.ERROR);
}
}
}
}
public static File getUserConfigFile(String name) {
return new File(getUserYaleDir(), name);
}
public 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 + -