1 21 22 package org.armedbear.lisp; 23 24 public class Condition extends StandardObject 25 { 26 private LispObject formatControl = NIL; 27 private LispObject formatArguments = NIL; 28 29 private String message = null; 30 31 public Condition() 32 { 33 message = null; 34 } 35 36 public Condition(LispClass cls, SimpleVector slots) 37 { 38 super(cls, slots); 39 message = null; 40 } 41 42 public Condition(LispObject initArgs) throws ConditionThrowable 43 { 44 super(BuiltInClass.CONDITION, null); 45 LispObject formatControl = NIL; 46 LispObject formatArguments = NIL; 47 LispObject first, second; 48 while (initArgs instanceof Cons) { 49 first = initArgs.car(); 50 initArgs = initArgs.cdr(); 51 second = initArgs.car(); 52 initArgs = initArgs.cdr(); 53 if (first == Keyword.FORMAT_CONTROL) 54 formatControl = second; 55 else if (first == Keyword.FORMAT_ARGUMENTS) 56 formatArguments = second; 57 } 58 setFormatControl(formatControl); 59 setFormatArguments(formatArguments); 60 } 61 62 public Condition(String message) 63 { 64 this.message = message; 65 } 66 67 public final LispObject getFormatControl() 68 { 69 return formatControl; 70 } 71 72 public final void setFormatControl(LispObject formatControl) 73 { 74 this.formatControl = formatControl; 75 } 76 77 public final LispObject getFormatArguments() 78 { 79 return formatArguments; 80 } 81 82 public final void setFormatArguments(LispObject formatArguments) 83 { 84 this.formatArguments = formatArguments; 85 } 86 87 public String getMessage() 88 { 89 return message; 90 } 91 92 public LispObject typeOf() 93 { 94 LispClass c = getLispClass(); 95 if (c != null) 96 return c.getSymbol(); 97 return Symbol.CONDITION; 98 } 99 100 public LispClass classOf() 101 { 102 LispClass c = getLispClass(); 103 if (c != null) 104 return c; 105 return BuiltInClass.CONDITION; 106 } 107 108 public LispObject typep(LispObject type) throws ConditionThrowable 109 { 110 if (type == Symbol.CONDITION) 111 return T; 112 if (type == BuiltInClass.CONDITION) 113 return T; 114 return super.typep(type); 115 } 116 117 public String getConditionReport() throws ConditionThrowable 118 { 119 String s = getMessage(); 120 if (s != null) 121 return s; 122 if (formatControl != NIL) { 123 try { 124 return format(formatControl, formatArguments); 125 } 126 catch (Throwable t) {} 127 } 128 return unreadableString(typeOf().writeToString()); 129 } 130 131 public String writeToString() throws ConditionThrowable 132 { 133 if (_PRINT_ESCAPE_.symbolValue() == NIL) { 134 String s = getMessage(); 135 if (s != null) 136 return s; 137 if (formatControl != NIL) 138 return format(formatControl, formatArguments); 139 } 140 return unreadableString(typeOf().writeToString()); 141 } 142 143 private static final Primitive1 CONDITION_REPORT = 145 new Primitive1("condition-report", PACKAGE_SYS, false, "condition") 146 { 147 public LispObject execute(LispObject arg) throws ConditionThrowable 148 { 149 try { 150 String s = ((Condition)arg).getMessage(); 151 return s != null ? new SimpleString(s) : NIL; 152 } 153 catch (ClassCastException e) { 154 return signal(new TypeError(arg, Symbol.CONDITION)); 155 } 156 } 157 }; 158 } 159 | Popular Tags |