KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtworks > xstream > converters > ConversionException


1 package com.thoughtworks.xstream.converters;
2
3 import java.util.HashMap JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.Map JavaDoc;
6
7 /**
8  * Thrown by {@link Converter} implementations when they cannot convert an object
9  * to/from textual data.
10  *
11  * When this exception is thrown it can be passed around to things that accept an
12  * {@link ErrorWriter}, allowing them to add diagnostics to the stack trace.
13  *
14  * @author Joe Walnes
15  *
16  * @see ErrorWriter
17  */

18 public class ConversionException extends RuntimeException JavaDoc implements ErrorWriter {
19
20     private Map JavaDoc stuff = new HashMap JavaDoc();
21
22     /**
23      * Plays nice with JDK1.3 and JDK1.4
24      */

25     protected Exception JavaDoc cause;
26
27     public ConversionException(String JavaDoc msg, Exception JavaDoc 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 JavaDoc msg) {
40         super(msg);
41     }
42
43     public ConversionException(Exception JavaDoc cause) {
44         this(cause.getMessage(), cause);
45     }
46
47     public String JavaDoc get(String JavaDoc errorKey) {
48         return (String JavaDoc) stuff.get(errorKey);
49     }
50
51     public void add(String JavaDoc name, String JavaDoc information) {
52         stuff.put(name, information);
53     }
54
55     public Iterator JavaDoc keys() {
56         return stuff.keySet().iterator();
57     }
58
59     public String JavaDoc getMessage() {
60         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
61         if (super.getMessage() != null) {
62             result.append(super.getMessage());
63         }
64         result.append("\n---- Debugging information ----");
65         for (Iterator JavaDoc iterator = keys(); iterator.hasNext();) {
66             String JavaDoc k = (String JavaDoc) iterator.next();
67             String JavaDoc 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 JavaDoc getCause() {
80         return cause;
81     }
82 }
83
Popular Tags