1 | /* |
---|
2 | * Condition.java |
---|
3 | * |
---|
4 | * Copyright (C) 2003-2007 Peter Graves |
---|
5 | * $Id: Condition.java 12815 2010-07-20 21:04:41Z astalla $ |
---|
6 | * |
---|
7 | * This program is free software; you can redistribute it and/or |
---|
8 | * modify it under the terms of the GNU General Public License |
---|
9 | * as published by the Free Software Foundation; either version 2 |
---|
10 | * of the License, or (at your option) any later version. |
---|
11 | * |
---|
12 | * This program is distributed in the hope that it will be useful, |
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | * GNU General Public License for more details. |
---|
16 | * |
---|
17 | * You should have received a copy of the GNU General Public License |
---|
18 | * along with this program; if not, write to the Free Software |
---|
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
20 | * |
---|
21 | * As a special exception, the copyright holders of this library give you |
---|
22 | * permission to link this library with independent modules to produce an |
---|
23 | * executable, regardless of the license terms of these independent |
---|
24 | * modules, and to copy and distribute the resulting executable under |
---|
25 | * terms of your choice, provided that you also meet, for each linked |
---|
26 | * independent module, the terms and conditions of the license of that |
---|
27 | * module. An independent module is a module which is not derived from |
---|
28 | * or based on this library. If you modify this library, you may extend |
---|
29 | * this exception to your version of the library, but you are not |
---|
30 | * obligated to do so. If you do not wish to do so, delete this |
---|
31 | * exception statement from your version. |
---|
32 | */ |
---|
33 | |
---|
34 | package org.armedbear.lisp; |
---|
35 | |
---|
36 | import static org.armedbear.lisp.Lisp.*; |
---|
37 | |
---|
38 | public class Condition extends StandardObject |
---|
39 | { |
---|
40 | protected String message; |
---|
41 | |
---|
42 | public Condition() |
---|
43 | { |
---|
44 | super(StandardClass.CONDITION); |
---|
45 | Debug.assertTrue(slots.length == 2); |
---|
46 | setFormatArguments(NIL); |
---|
47 | } |
---|
48 | |
---|
49 | protected Condition(LispClass cls) |
---|
50 | { |
---|
51 | super(cls); |
---|
52 | Debug.assertTrue(slots.length >= 2); |
---|
53 | setFormatArguments(NIL); |
---|
54 | } |
---|
55 | |
---|
56 | public Condition(LispClass cls, int length) |
---|
57 | { |
---|
58 | super(cls, length); |
---|
59 | } |
---|
60 | |
---|
61 | public Condition(LispObject initArgs) |
---|
62 | { |
---|
63 | super(StandardClass.CONDITION); |
---|
64 | Debug.assertTrue(slots.length == 2); |
---|
65 | initialize(initArgs); |
---|
66 | } |
---|
67 | |
---|
68 | protected void initialize(LispObject initArgs) |
---|
69 | { |
---|
70 | LispObject control = null; |
---|
71 | LispObject arguments = null; |
---|
72 | LispObject first, second; |
---|
73 | while (initArgs instanceof Cons) |
---|
74 | { |
---|
75 | first = initArgs.car(); |
---|
76 | initArgs = initArgs.cdr(); |
---|
77 | second = initArgs.car(); |
---|
78 | initArgs = initArgs.cdr(); |
---|
79 | if (first == Keyword.FORMAT_CONTROL) |
---|
80 | { |
---|
81 | if (control == null) |
---|
82 | control = second; |
---|
83 | } |
---|
84 | else if (first == Keyword.FORMAT_ARGUMENTS) |
---|
85 | { |
---|
86 | if (arguments == null) |
---|
87 | arguments = second; |
---|
88 | } |
---|
89 | } |
---|
90 | if (control != null) |
---|
91 | setFormatControl(control); |
---|
92 | if (arguments == null) |
---|
93 | arguments = NIL; |
---|
94 | setFormatArguments(arguments); |
---|
95 | } |
---|
96 | |
---|
97 | public Condition(String message) |
---|
98 | { |
---|
99 | super(StandardClass.CONDITION); |
---|
100 | Debug.assertTrue(slots.length == 2); |
---|
101 | setFormatControl(message); |
---|
102 | setFormatArguments(NIL); |
---|
103 | } |
---|
104 | |
---|
105 | public final LispObject getFormatControl() |
---|
106 | { |
---|
107 | return getInstanceSlotValue(Symbol.FORMAT_CONTROL); |
---|
108 | } |
---|
109 | |
---|
110 | public final void setFormatControl(LispObject formatControl) |
---|
111 | |
---|
112 | { |
---|
113 | setInstanceSlotValue(Symbol.FORMAT_CONTROL, formatControl); |
---|
114 | } |
---|
115 | |
---|
116 | public final void setFormatControl(String s) |
---|
117 | { |
---|
118 | setFormatControl(new SimpleString(s)); |
---|
119 | } |
---|
120 | |
---|
121 | public final LispObject getFormatArguments() |
---|
122 | { |
---|
123 | return getInstanceSlotValue(Symbol.FORMAT_ARGUMENTS); |
---|
124 | } |
---|
125 | |
---|
126 | public final void setFormatArguments(LispObject formatArguments) |
---|
127 | |
---|
128 | { |
---|
129 | setInstanceSlotValue(Symbol.FORMAT_ARGUMENTS, formatArguments); |
---|
130 | } |
---|
131 | |
---|
132 | /** |
---|
133 | * Extending classes should override this method if they want to |
---|
134 | * customize how they will be printed. |
---|
135 | */ |
---|
136 | public String getMessage() |
---|
137 | { |
---|
138 | LispObject formatControl = getFormatControl(); |
---|
139 | return formatControl != UNBOUND_VALUE ? formatControl.writeToString() : null; |
---|
140 | } |
---|
141 | |
---|
142 | @Override |
---|
143 | public LispObject typeOf() |
---|
144 | { |
---|
145 | LispObject c = getLispClass(); |
---|
146 | if (c instanceof LispClass) |
---|
147 | return ((LispClass)c).getName(); |
---|
148 | else if (c != null) |
---|
149 | return Symbol.CLASS_NAME.execute(c); |
---|
150 | return Symbol.CONDITION; |
---|
151 | } |
---|
152 | |
---|
153 | @Override |
---|
154 | public LispObject classOf() |
---|
155 | { |
---|
156 | LispObject c = getLispClass(); |
---|
157 | if (c != null) |
---|
158 | return c; |
---|
159 | return StandardClass.CONDITION; |
---|
160 | } |
---|
161 | |
---|
162 | @Override |
---|
163 | public LispObject typep(LispObject type) |
---|
164 | { |
---|
165 | if (type == Symbol.CONDITION) |
---|
166 | return T; |
---|
167 | if (type == StandardClass.CONDITION) |
---|
168 | return T; |
---|
169 | return super.typep(type); |
---|
170 | } |
---|
171 | |
---|
172 | public String getConditionReport() |
---|
173 | { |
---|
174 | String s = getMessage(); |
---|
175 | if (s != null) |
---|
176 | return s; |
---|
177 | LispObject formatControl = getFormatControl(); |
---|
178 | if (formatControl != NIL) |
---|
179 | { |
---|
180 | return format(formatControl, getFormatArguments()); |
---|
181 | } |
---|
182 | return unreadableString(typeOf().writeToString()); |
---|
183 | } |
---|
184 | |
---|
185 | @Override |
---|
186 | public final String writeToString() |
---|
187 | { |
---|
188 | final LispThread thread = LispThread.currentThread(); |
---|
189 | if (Symbol.PRINT_ESCAPE.symbolValue(thread) == NIL) |
---|
190 | { |
---|
191 | String s = getMessage(); |
---|
192 | if (s != null) |
---|
193 | return s; |
---|
194 | LispObject formatControl = getFormatControl(); |
---|
195 | if (formatControl instanceof Function) |
---|
196 | { |
---|
197 | StringOutputStream stream = new StringOutputStream(); |
---|
198 | Symbol.APPLY.execute(formatControl, stream, getFormatArguments()); |
---|
199 | return stream.getString().getStringValue(); |
---|
200 | } |
---|
201 | if (formatControl instanceof AbstractString) |
---|
202 | { |
---|
203 | LispObject f = Symbol.FORMAT.getSymbolFunction(); |
---|
204 | if (f == null || f instanceof Autoload) |
---|
205 | return format(formatControl, getFormatArguments()); |
---|
206 | return Symbol.APPLY.execute(f, NIL, formatControl, getFormatArguments()).getStringValue(); |
---|
207 | } |
---|
208 | } |
---|
209 | final int maxLevel; |
---|
210 | LispObject printLevel = Symbol.PRINT_LEVEL.symbolValue(thread); |
---|
211 | if (printLevel instanceof Fixnum) |
---|
212 | maxLevel = ((Fixnum)printLevel).value; |
---|
213 | else |
---|
214 | maxLevel = Integer.MAX_VALUE; |
---|
215 | LispObject currentPrintLevel = |
---|
216 | _CURRENT_PRINT_LEVEL_.symbolValue(thread); |
---|
217 | int currentLevel = ((Fixnum)currentPrintLevel).value; |
---|
218 | if (currentLevel >= maxLevel) |
---|
219 | return "#"; |
---|
220 | return unreadableString(typeOf().writeToString()); |
---|
221 | } |
---|
222 | } |
---|