1 | /* |
---|
2 | * StreamError.java |
---|
3 | * |
---|
4 | * Copyright (C) 2002-2005 Peter Graves |
---|
5 | * $Id: StreamError.java 14496 2013-05-11 12:19:08Z mevenson $ |
---|
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 StreamError extends LispError |
---|
39 | { |
---|
40 | private final Throwable cause; |
---|
41 | |
---|
42 | protected StreamError(LispClass cls) |
---|
43 | { |
---|
44 | super(cls); |
---|
45 | cause = null; |
---|
46 | } |
---|
47 | |
---|
48 | public StreamError(String message) |
---|
49 | { |
---|
50 | super(StandardClass.STREAM_ERROR); |
---|
51 | setFormatControl(message); |
---|
52 | setFormatArguments(NIL); |
---|
53 | setStream(NIL); |
---|
54 | cause = null; |
---|
55 | } |
---|
56 | |
---|
57 | public StreamError(Stream stream) |
---|
58 | { |
---|
59 | super(StandardClass.STREAM_ERROR); |
---|
60 | setStream(stream != null ? stream : NIL); |
---|
61 | cause = null; |
---|
62 | } |
---|
63 | |
---|
64 | public StreamError(String message, Stream stream) |
---|
65 | { |
---|
66 | super(StandardClass.STREAM_ERROR); |
---|
67 | setFormatControl(message); |
---|
68 | setFormatArguments(NIL); |
---|
69 | setStream(stream != null ? stream : NIL); |
---|
70 | cause = null; |
---|
71 | } |
---|
72 | |
---|
73 | public StreamError(LispObject initArgs) |
---|
74 | { |
---|
75 | super(StandardClass.STREAM_ERROR); |
---|
76 | initialize(initArgs); |
---|
77 | cause = null; |
---|
78 | } |
---|
79 | |
---|
80 | @Override |
---|
81 | protected void initialize(LispObject initArgs) |
---|
82 | { |
---|
83 | super.initialize(initArgs); |
---|
84 | while (initArgs != NIL) { |
---|
85 | LispObject first = initArgs.car(); |
---|
86 | initArgs = initArgs.cdr(); |
---|
87 | if (first == Keyword.STREAM) { |
---|
88 | setStream(initArgs.car()); |
---|
89 | break; |
---|
90 | } |
---|
91 | initArgs = initArgs.cdr(); |
---|
92 | } |
---|
93 | } |
---|
94 | |
---|
95 | public StreamError(Stream stream, String message) |
---|
96 | { |
---|
97 | super(StandardClass.STREAM_ERROR); |
---|
98 | setFormatControl(message); |
---|
99 | setFormatArguments(NIL); |
---|
100 | setStream(stream != null ? stream : NIL); |
---|
101 | cause = null; |
---|
102 | } |
---|
103 | |
---|
104 | public StreamError(Stream stream, Throwable cause) |
---|
105 | { |
---|
106 | super(StandardClass.STREAM_ERROR); |
---|
107 | setStream(stream != null ? stream : NIL); |
---|
108 | String message = cause.getMessage(); |
---|
109 | setFormatControl(message != null ? message : cause.toString()); |
---|
110 | setFormatArguments(NIL); |
---|
111 | this.cause = cause; |
---|
112 | } |
---|
113 | |
---|
114 | public final LispObject getStream() |
---|
115 | { |
---|
116 | return getInstanceSlotValue(Symbol.STREAM); |
---|
117 | } |
---|
118 | |
---|
119 | protected final void setStream(LispObject stream) |
---|
120 | { |
---|
121 | setInstanceSlotValue(Symbol.STREAM, stream); |
---|
122 | } |
---|
123 | |
---|
124 | @Override |
---|
125 | public LispObject typeOf() |
---|
126 | { |
---|
127 | return Symbol.STREAM_ERROR; |
---|
128 | } |
---|
129 | |
---|
130 | @Override |
---|
131 | public LispObject classOf() |
---|
132 | { |
---|
133 | return StandardClass.STREAM_ERROR; |
---|
134 | } |
---|
135 | |
---|
136 | @Override |
---|
137 | public LispObject typep(LispObject type) |
---|
138 | { |
---|
139 | if (type == Symbol.STREAM_ERROR) |
---|
140 | return T; |
---|
141 | if (type == StandardClass.STREAM_ERROR) |
---|
142 | return T; |
---|
143 | return super.typep(type); |
---|
144 | } |
---|
145 | |
---|
146 | @Override |
---|
147 | public String getMessage() |
---|
148 | { |
---|
149 | if (cause != null) { |
---|
150 | String s = cause.getMessage(); |
---|
151 | if (s != null && s.length() > 0) |
---|
152 | return s; |
---|
153 | } |
---|
154 | return null; |
---|
155 | } |
---|
156 | |
---|
157 | // ### stream-error-stream |
---|
158 | private static final Primitive STREAM_ERROR_STREAM = |
---|
159 | new Primitive("stream-error-stream", "condition") |
---|
160 | { |
---|
161 | @Override |
---|
162 | public LispObject execute(LispObject arg) |
---|
163 | { |
---|
164 | if (arg instanceof StreamError) |
---|
165 | return ((StreamError)arg).getStream(); |
---|
166 | return type_error(arg, Symbol.STREAM_ERROR); |
---|
167 | } |
---|
168 | }; |
---|
169 | } |
---|