📄 usererror.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.operator;
import edu.udo.cs.yale.Yale;
import edu.udo.cs.yale.Experiment;
import java.util.ResourceBundle;
import java.text.MessageFormat;
/** Exception class whose instances are thrown due to a user error, for example missing files or wrong
* operator architecture.
* <br>
* In order to create a UserError, do the following:
* <ul>
* <li>Open the file <code>UserErrorMessages.properties</code> in the <code>resources</code> directory.
* Look for an appropriate messsage. If you find one, remember its id number. If not, create a new
* one in the correct group</li>
* <li>The entry must include name, short message and long message. The name and long message will
* be presented to the user litarally. The short message will be parsed by
* <code>java.text.MessageFormat</code>. Especially, any ocurrence of curly brackets will be
* replaced. Be careful with quotes; it might be a good idea to read the ducumentation of
* MessageFormat first.</li>
* <li>Create a UserError by using this id. If the UserError is created because
* of another exception, e.g. a FileNotFoundException, this exception should be passed
* to the UserError in the constructor.</li>
* </ul>
*
* @version $Id: UserError.java,v 2.11 2004/08/27 11:57:34 ingomierswa Exp $
*/
public class UserError extends OperatorException {
private static ResourceBundle messages = null;
private static final MessageFormat formatter = new MessageFormat("");
static {
try {
messages = ResourceBundle.getBundle("edu.udo.cs.yale.resources.UserErrorMessages");
} catch (Throwable t) {
t.printStackTrace();
}
}
private int code;
private Operator operator;
/** Creates a new UserError.
* @param operator The {@link Operator} in which the exception occured. If operator is null,
* the operator is assumed to be the current operator of the current experiment (returned
* by {@link Yale#getExperiment()}. This may not work properly if Yale is invoked other than
* by calling one of the main methods.
* @param cause The exception that caused the user error. May be null. Using this
* makes debugging a lot easier.
* @param code The error code referring to a message in the file
* <code>UserErrorMessages.properties</code>
* @param arguments Arguments for the short message.
*/
public UserError(Operator operator, Throwable cause, int code, Object[] arguments) {
super(getErrorMessage(code, arguments), cause);
this.code = code;
if (operator != null) {
this.operator = operator;
} else {
Experiment experiment = Yale.getExperiment();
if (experiment != null) {
this.operator = experiment.getCurrentOperator();
} else {
this.operator = null;
}
}
}
/** Convenience constructor for messages with no arguments and cause. */
public UserError(Operator operator, Throwable cause, int code) {
this(operator, code, new Object[0], cause);
}
public UserError(Operator operator, int code, Object[] arguments) {
this(operator, null, code, arguments);
}
/** Convenience constructor for messages with no arguments. */
public UserError(Operator operator, int code) {
this(operator, null, code, new Object[0]);
}
/** Convenience constructor for messages with exactly one argument. */
public UserError(Operator operator, int code, Object argument1) {
this(operator, null, code, new Object[] {argument1});
}
/** Convenience constructor for messages with exactly one arguments and cause. */
public UserError(Operator operator, Throwable cause, int code, Object argument1) {
this(operator, cause, code, new Object[] {argument1});
}
/** Convenience constructor for messages with exactly two arguments. */
public UserError(Operator operator, int code, Object argument1, Object argument2) {
this(operator, null, code, new Object[] {argument1, argument2} );
}
public String getDetails() {
return getResourceString(code, "long", "Description missing.");
}
public String getErrorName() {
return getResourceString(code, "name", "Unnamed error.");
}
public int getCode() {
return code;
}
public Operator getOperator() {
return operator;
}
private static String getErrorMessage(int code, Object[] arguments) {
String message = getResourceString(code, "short", "No message.");
try {
formatter.applyPattern(message);
String formatted = formatter.format(arguments);
return formatted;
} catch (Throwable t) {
return message;
}
}
private static String getResourceString(int code, String key, String deflt) {
if (messages == null) return deflt;
try {
return messages.getString("error."+code+"."+key);
} catch (java.util.MissingResourceException e) {
return deflt;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -