01: /* Copyright 2004 The JA-SIG Collaborative. All rights reserved.
02: * See license distributed with this file and
03: * available online at http://www.uportal.org/license.html
04: */
05:
06: package org.jasig.portal.channels.error.error2xml;
07:
08: import org.jasig.portal.ExceptionHelper;
09: import org.w3c.dom.Document;
10: import org.w3c.dom.Element;
11: import org.apache.commons.logging.Log;
12: import org.apache.commons.logging.LogFactory;
13:
14: /**
15: * Generic ThrowableToElement which will translate any Throwable to an
16: * Element representing it.
17: * Other IThrowableToElement implementations may use this implementation as
18: * a utility to perform the generic Throwable translation and then add to the results.
19: * @author andrew.petro@yale.edu
20: * @version $Revision: 35593 $ $Date: 2005-04-17 13:54:14 -0700 (Sun, 17 Apr 2005) $
21: * @since uPortal 2.5
22: */
23: public class ThrowableToElement implements IThrowableToElement {
24:
25: private Log log = LogFactory.getLog(getClass());
26:
27: /* (non-Javadoc)
28: * @see org.jasig.portal.channels.error.tt.ThrowableToElement#throwableToElement(java.lang.Throwable, org.w3c.dom.Document)
29: */
30: public Element throwableToElement(Throwable t, Document parentDoc)
31: throws IllegalArgumentException {
32: if (this .log.isTraceEnabled())
33: log.trace("Entering throwableToElement for throwable [" + t
34: + "]");
35: if (t == null)
36: throw new IllegalArgumentException(
37: "Cannot translate a null throwable.");
38: if (parentDoc == null)
39: throw new IllegalArgumentException(
40: "Cannot create an element for a null Document.");
41:
42: Element excEl = parentDoc.createElement("throwable");
43: String m = t.getMessage();
44: if (m != null) {
45: Element emEl = parentDoc.createElement("message");
46: emEl.appendChild(parentDoc.createTextNode(m));
47: excEl.appendChild(emEl);
48: }
49:
50: Element stEl = parentDoc.createElement("stack");
51: String stackTrace = ExceptionHelper.shortStackTrace(t);
52: stEl.appendChild(parentDoc.createTextNode(stackTrace));
53: excEl.appendChild(stEl);
54:
55: excEl.setAttribute("class", t.getClass().getName());
56: excEl.setAttribute("renderedAs", Throwable.class.getName());
57:
58: return excEl;
59: }
60:
61: /* (non-Javadoc)
62: * @see org.jasig.portal.channels.error.tt.ThrowableToElement#supports(java.lang.Class)
63: */
64: public boolean supports(Class c) throws IllegalArgumentException {
65: if (c == null)
66: throw new IllegalArgumentException(
67: "Supports is undefined on null");
68: if (!Throwable.class.isAssignableFrom(c))
69: throw new IllegalArgumentException(
70: "c is not a class extending Throwable. c=["
71: + c.getName() + "]");
72: // if it's a throwable, we support it.
73: return true;
74: }
75:
76: }
|