1 package com.thoughtworks.xstream.converters; 2 3 import java.util.HashMap ; 4 import java.util.Iterator ; 5 import java.util.Map ; 6 7 18 public class ConversionException extends RuntimeException implements ErrorWriter { 19 20 private Map stuff = new HashMap (); 21 22 25 protected Exception cause; 26 27 public ConversionException(String msg, Exception cause) { 28 super(msg); 29 if (msg != null) { 30 add("message", msg); 31 } 32 if (cause != null) { 33 add("cause-exception", cause.getClass().getName()); 34 add("cause-message", cause.getMessage()); 35 this.cause = cause; 36 } 37 } 38 39 public ConversionException(String msg) { 40 super(msg); 41 } 42 43 public ConversionException(Exception cause) { 44 this(cause.getMessage(), cause); 45 } 46 47 public String get(String errorKey) { 48 return (String ) stuff.get(errorKey); 49 } 50 51 public void add(String name, String information) { 52 stuff.put(name, information); 53 } 54 55 public Iterator keys() { 56 return stuff.keySet().iterator(); 57 } 58 59 public String getMessage() { 60 StringBuffer result = new StringBuffer (); 61 if (super.getMessage() != null) { 62 result.append(super.getMessage()); 63 } 64 result.append("\n---- Debugging information ----"); 65 for (Iterator iterator = keys(); iterator.hasNext();) { 66 String k = (String ) iterator.next(); 67 String v = get(k); 68 result.append("\n" + k); 69 int padding = 20 - k.length(); 70 for (int i = 0; i < padding; i++) { 71 result.append(' '); 72 } 73 result.append(": " + v + " "); 74 } 75 result.append("\n-------------------------------"); 76 return result.toString(); 77 } 78 79 public Throwable getCause() { 80 return cause; 81 } 82 } 83 | Popular Tags |