001: /*
002: * Condition.java
003: *
004: * Copyright (C) 2003-2004 Peter Graves
005: * $Id: Condition.java,v 1.25 2004/08/18 17:14:38 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.lisp;
023:
024: public class Condition extends StandardObject {
025: private LispObject formatControl = NIL;
026: private LispObject formatArguments = NIL;
027:
028: private String message = null;
029:
030: public Condition() {
031: message = null;
032: }
033:
034: public Condition(LispClass cls, SimpleVector slots) {
035: super (cls, slots);
036: message = null;
037: }
038:
039: public Condition(LispObject initArgs) throws ConditionThrowable {
040: super (BuiltInClass.CONDITION, null);
041: LispObject formatControl = NIL;
042: LispObject formatArguments = NIL;
043: LispObject first, second;
044: while (initArgs instanceof Cons) {
045: first = initArgs.car();
046: initArgs = initArgs.cdr();
047: second = initArgs.car();
048: initArgs = initArgs.cdr();
049: if (first == Keyword.FORMAT_CONTROL)
050: formatControl = second;
051: else if (first == Keyword.FORMAT_ARGUMENTS)
052: formatArguments = second;
053: }
054: setFormatControl(formatControl);
055: setFormatArguments(formatArguments);
056: }
057:
058: public Condition(String message) {
059: this .message = message;
060: }
061:
062: public final LispObject getFormatControl() {
063: return formatControl;
064: }
065:
066: public final void setFormatControl(LispObject formatControl) {
067: this .formatControl = formatControl;
068: }
069:
070: public final LispObject getFormatArguments() {
071: return formatArguments;
072: }
073:
074: public final void setFormatArguments(LispObject formatArguments) {
075: this .formatArguments = formatArguments;
076: }
077:
078: public String getMessage() {
079: return message;
080: }
081:
082: public LispObject typeOf() {
083: LispClass c = getLispClass();
084: if (c != null)
085: return c.getSymbol();
086: return Symbol.CONDITION;
087: }
088:
089: public LispClass classOf() {
090: LispClass c = getLispClass();
091: if (c != null)
092: return c;
093: return BuiltInClass.CONDITION;
094: }
095:
096: public LispObject typep(LispObject type) throws ConditionThrowable {
097: if (type == Symbol.CONDITION)
098: return T;
099: if (type == BuiltInClass.CONDITION)
100: return T;
101: return super .typep(type);
102: }
103:
104: public String getConditionReport() throws ConditionThrowable {
105: String s = getMessage();
106: if (s != null)
107: return s;
108: if (formatControl != NIL) {
109: try {
110: return format(formatControl, formatArguments);
111: } catch (Throwable t) {
112: }
113: }
114: return unreadableString(typeOf().writeToString());
115: }
116:
117: public String writeToString() throws ConditionThrowable {
118: if (_PRINT_ESCAPE_.symbolValue() == NIL) {
119: String s = getMessage();
120: if (s != null)
121: return s;
122: if (formatControl != NIL)
123: return format(formatControl, formatArguments);
124: }
125: return unreadableString(typeOf().writeToString());
126: }
127:
128: // ### condition-report
129: private static final Primitive1 CONDITION_REPORT = new Primitive1(
130: "condition-report", PACKAGE_SYS, false, "condition") {
131: public LispObject execute(LispObject arg)
132: throws ConditionThrowable {
133: try {
134: String s = ((Condition) arg).getMessage();
135: return s != null ? new SimpleString(s) : NIL;
136: } catch (ClassCastException e) {
137: return signal(new TypeError(arg, Symbol.CONDITION));
138: }
139: }
140: };
141: }
|