📄 tools.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.operator.OperatorException;
import edu.udo.cs.yale.operator.UserError;
import edu.udo.cs.yale.operator.Operator;
import edu.udo.cs.yale.tools.plugin.Plugin;
import java.util.*;
import java.util.jar.JarFile;
import java.util.jar.JarEntry;
import java.io.BufferedReader;
import java.io.Reader;
import java.io.PrintStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.FileReader;
import java.io.File;
import java.net.URL;
/** Tools for YALE.
* @author simon, ingo
* @version $Id: Tools.java,v 2.20 2004/08/27 11:57:45 ingomierswa Exp $
*/
public class Tools {
public static final String RESOURCE_PREFIX = "edu/udo/cs/yale/resources/";
/** Returns the name for an ordinal number. */
public static final String ordinalNumber(int n) {
if ((n % 10 == 1) && (n % 100 != 11))
{ return n+"st"; }
if ((n % 10 == 2) && (n % 100 != 12))
{ return n+"nd"; }
if ((n % 10 == 3) && (n % 100 != 13))
{ return n+"rd"; }
return n+"th";
}
/** Returns the class name of the given class without the package information. */
public static String classNameWOPackage(Class c) {
return c.getName().substring(c.getName().lastIndexOf(".")+1);
}
/** Reads the output of the reader and delivers it at string.
*/
public static String readOutput(BufferedReader in) throws IOException {
StringBuffer output = new StringBuffer();
String line = "";
while ((line=in.readLine())!=null) {
output.append(line);
output.append("\n");
}
return output.toString();
}
/** Creates a file relative to the given parent if name is not an absolute file name. */
public static File getFile(File parent, String name) {
File file = new File(name);
if (file.isAbsolute()) return file;
else return new File(parent, name);
}
/** Creates a directory including parent directories.
* @return true, if operation was successful. */
public static boolean mkdir(File dir) {
if (dir == null) return true;
if (dir.exists()) return true;
File parent = dir.getParentFile();
if (parent == null) {
return true;
} else if (!parent.exists()) {
if (!mkdir(parent)) return false;
}
return dir.mkdir();
}
/** Waits for process to die and writes log messages. Terminates if exit value is not 0. */
public static void waitForProcess(Operator operator,
Process process,
String name) throws OperatorException {
try {
LogService.logMessage("Waiting for process '"+name+"' to die.", LogService.MINIMUM);
int value= process.waitFor();
if (value == 0) {
LogService.logMessage("Process '"+name+"' terminated successfully.", LogService.TASK);
} else {
throw new UserError(operator, 306, new Object[] { name, new Integer(value) });
}
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted waiting for process '"+name+"' to die.", e);
}
}
/** Sends a mail to the given address, using the specified subject and contents.
* Subject must contain no whitespace! */
public static void sendEmail(String address, String subject, String content) {
try {
//subject = subject.replaceAll("\\s", "_"); // replace whitespace
String command = ParameterService.getProperty("yale.tools.sendmail.command");
// command = command.replaceAll("\\$A", address);
// command = command.replaceAll("\\$S", subject);
LogService.logMessage("Executing '"+command+"'", LogService.MINIMUM);
Process sendmail = Runtime.getRuntime().exec(new String[] { command, address });
PrintStream out = new PrintStream(sendmail.getOutputStream());
out.println("Subject: "+subject);
out.println("From: Yale");
out.println("To: "+address);
out.println();
out.println(content);
out.close();
waitForProcess(null, sendmail, command);
} catch (Throwable e) {
LogService.logException("Cannot send mail to "+address, e);
}
}
public static InputStream openResource(String name) throws IOException {
return getResource(name).openStream();
}
public static URL getResource(String name) {
return getResource(Tools.class.getClassLoader(), name);
}
public static URL getResource(ClassLoader loader, String name) {
return loader.getResource(RESOURCE_PREFIX + name);
}
public static String readTextFile(File file) throws IOException {
return readTextFile(new FileReader(file));
}
public static String readTextFile(Reader r) throws IOException {
StringBuffer contents = new StringBuffer();
BufferedReader reader = new BufferedReader(r);
String line = "";
while ((line = reader.readLine()) != null) {
contents.append(line + "\n");
}
reader.close();
return contents.toString();
}
public static final String[] TRUE_STRINGS = { "true", "on", "yes", "y" };
public static final String[] FALSE_STRINGS = { "false", "off", "no", "n" };
public static boolean booleanValue(String string, boolean deflt) {
if (string == null) return deflt;
string = string.toLowerCase().trim();
for (int i = 0; i < TRUE_STRINGS.length; i++) {
if (TRUE_STRINGS[i].equals(string)) {
return true;
}
}
for (int i = 0; i < FALSE_STRINGS.length; i++) {
if (FALSE_STRINGS[i].equals(string)) {
return false;
}
}
return deflt;
}
public static File findSourceFile(StackTraceElement e) {
try {
Class clazz = Class.forName(e.getClassName());
while (clazz.getDeclaringClass() != null)
clazz = clazz.getDeclaringClass();
String filename = clazz.getName().replace('.', File.separatorChar);
return ParameterService.getSourceFile(filename+".java");
} catch (Throwable t) {
}
String filename = e.getClassName().replace('.', File.separatorChar);
return ParameterService.getSourceFile(filename+".java");
}
public static Process launchFileEditor(File file, int line) throws IOException {
String editor = System.getProperty("yale.tools.editor");
if (editor == null) throw new IOException("Property 'yale.tools.editor' undefined.");
editor = editor.replaceAll("%f", file.getAbsolutePath());
editor = editor.replaceAll("%l", line + "");
return Runtime.getRuntime().exec(editor);
}
/** Replaces angle brackets by html entities.*/
public static String escapeHTML(String string) {
if (string == null) return "null";
string = string.replaceAll("<", "<");
string = string.replaceAll(">", ">");
return string;
}
public static void findImplementationsInJar(JarFile jar, Class superClass, List implementations) {
findImplementationsInJar(Tools.class.getClassLoader(), jar, superClass, implementations);
}
public static void findImplementationsInJar(ClassLoader loader, JarFile jar, Class superClass, List implementations) {
Enumeration e = jar.entries();
while (e.hasMoreElements()) {
JarEntry entry = (JarEntry)e.nextElement();
String name = entry.getName();
int dotClass = name.lastIndexOf(".class");
if (dotClass < 0) continue;
name = name.substring(0, dotClass);
name = name.replaceAll("/", "\\.");
//if (!name.startsWith(packageName)) continue;
try {
Class c = loader.loadClass(name);
if (superClass.isAssignableFrom(c)) {
if (!java.lang.reflect.Modifier.isAbstract(c.getModifiers())) {
implementations.add(name);
}
}
} catch (Throwable t) {
}
}
}
public static Class classForName(String className) throws ClassNotFoundException {
try {
return Class.forName(className);
} catch (ClassNotFoundException e) {}
Iterator i = Plugin.getAllPlugins().iterator();
while (i.hasNext()) {
Plugin p = (Plugin)i.next();
try {
return p.getClassLoader().loadClass(className);
} catch (ClassNotFoundException e) {
//System.out.println("not found");
}
}
throw new ClassNotFoundException(className);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -