source: branches/0.22.x/abcl/src/org/armedbear/lisp/StreamError.java

Last change on this file was 12320, checked in by ehuelsmann, 15 years ago

Print StreamErrors? with a readable text,

even when created based on a Throwable.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.7 KB
Line 
1/*
2 * StreamError.java
3 *
4 * Copyright (C) 2002-2005 Peter Graves
5 * $Id: StreamError.java 12320 2010-01-01 15:38:33Z ehuelsmann $
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
34package org.armedbear.lisp;
35
36import static org.armedbear.lisp.Lisp.*;
37
38public 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        setFormatControl(cause.getMessage());
109        setFormatArguments(NIL);
110        this.cause = cause;
111    }
112
113    public final LispObject getStream()
114    {
115        return getInstanceSlotValue(Symbol.STREAM);
116    }
117
118    protected final void setStream(LispObject stream)
119    {
120        setInstanceSlotValue(Symbol.STREAM, stream);
121    }
122
123    @Override
124    public LispObject typeOf()
125    {
126        return Symbol.STREAM_ERROR;
127    }
128
129    @Override
130    public LispObject classOf()
131    {
132        return StandardClass.STREAM_ERROR;
133    }
134
135    @Override
136    public LispObject typep(LispObject type)
137    {
138        if (type == Symbol.STREAM_ERROR)
139            return T;
140        if (type == StandardClass.STREAM_ERROR)
141            return T;
142        return super.typep(type);
143    }
144
145    @Override
146    public String getMessage()
147    {
148        if (cause != null) {
149            String s = cause.getMessage();
150            if (s != null && s.length() > 0)
151                return s;
152        }
153        return null;
154    }
155
156    // ### stream-error-stream
157    private static final Primitive STREAM_ERROR_STREAM =
158        new Primitive("stream-error-stream", "condition")
159    {
160        @Override
161        public LispObject execute(LispObject arg)
162        {
163            if (arg instanceof StreamError)
164                return ((StreamError)arg).getStream();
165            return error(new TypeError(arg, Symbol.STREAM_ERROR));
166        }
167    };
168}
Note: See TracBrowser for help on using the repository browser.