01: /*
02: * Copyright (C) 2003, 2004, 2005 Joe Walnes.
03: * Copyright (C) 2006, 2007 XStream Committers.
04: * All rights reserved.
05: *
06: * The software in this package is published under the terms of the BSD
07: * style license a copy of which has been included with this distribution in
08: * the LICENSE.txt file.
09: *
10: * Created on 26. September 2003 by Joe Walnes
11: */
12: package com.thoughtworks.xstream.converters;
13:
14: import com.thoughtworks.xstream.XStreamException;
15: import com.thoughtworks.xstream.core.util.OrderRetainingMap;
16:
17: import java.util.Iterator;
18: import java.util.Map;
19:
20: /**
21: * Thrown by {@link Converter} implementations when they cannot convert an object
22: * to/from textual data.
23: *
24: * When this exception is thrown it can be passed around to things that accept an
25: * {@link ErrorWriter}, allowing them to add diagnostics to the stack trace.
26: *
27: * @author Joe Walnes
28: * @author Jörg Schaible
29: *
30: * @see ErrorWriter
31: */
32: public class ConversionException extends XStreamException implements
33: ErrorWriter {
34:
35: private static final String SEPARATOR = "\n-------------------------------";
36: private Map stuff = new OrderRetainingMap();
37:
38: public ConversionException(String msg, Throwable cause) {
39: super (msg, cause);
40: if (msg != null) {
41: add("message", msg);
42: }
43: if (cause != null) {
44: add("cause-exception", cause.getClass().getName());
45: add(
46: "cause-message",
47: cause instanceof ConversionException ? ((ConversionException) cause)
48: .getShortMessage()
49: : cause.getMessage());
50: }
51: }
52:
53: public ConversionException(String msg) {
54: super (msg);
55: }
56:
57: public ConversionException(Throwable cause) {
58: this (cause.getMessage(), cause);
59: }
60:
61: public String get(String errorKey) {
62: return (String) stuff.get(errorKey);
63: }
64:
65: public void add(String name, String information) {
66: stuff.put(name, information);
67: }
68:
69: public Iterator keys() {
70: return stuff.keySet().iterator();
71: }
72:
73: public String getMessage() {
74: StringBuffer result = new StringBuffer();
75: if (super .getMessage() != null) {
76: result.append(super .getMessage());
77: }
78: if (!result.toString().endsWith(SEPARATOR)) {
79: result.append("\n---- Debugging information ----");
80: }
81: for (Iterator iterator = keys(); iterator.hasNext();) {
82: String k = (String) iterator.next();
83: String v = get(k);
84: result.append('\n').append(k);
85: result.append(" ".substring(k.length()));
86: result.append(": ").append(v);
87: }
88: result.append(SEPARATOR);
89: return result.toString();
90: }
91:
92: public String getShortMessage() {
93: return super.getMessage();
94: }
95: }
|