📄 tools.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.operator.OperatorException;import edu.udo.cs.yale.operator.UserError;import edu.udo.cs.yale.operator.Operator;import java.io.BufferedReader;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.9 2003/08/21 13:07:58 fischer 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) { URL url = Tools.class.getClassLoader().getResource(RESOURCE_PREFIX + name); return url; } public static String readTextFile(File file) throws IOException { StringBuffer contents = new StringBuffer(); BufferedReader reader = new BufferedReader(new FileReader(file)); 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) throws IOException { String editor = System.getProperty("yale.tools.editor"); if (editor == null) throw new IOException("Property 'yale.tools.editor' undefined."); return Runtime.getRuntime().exec(new String[] {editor, file.getAbsolutePath() }); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -