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.ResourceMissingException;
09: import org.w3c.dom.Document;
10: import org.w3c.dom.Element;
11:
12: /**
13: * Translates ResourceMissingException instances to XML Elements.
14: * @author andrew.petro@yale.edu
15: * @version $Revision: 35593 $ $Date: 2005-04-17 13:54:14 -0700 (Sun, 17 Apr 2005) $
16: * @since uPortal 2.5
17: */
18: public class ResourceMissingExceptionToElement implements
19: IThrowableToElement {
20:
21: private ThrowableToElement throwableToElement = new ThrowableToElement();
22:
23: /* (non-Javadoc)
24: * @see org.jasig.portal.channels.error.tt.IThrowableToElement#throwableToElement(java.lang.Throwable, org.w3c.dom.Document)
25: */
26: public Element throwableToElement(Throwable t, Document parentDoc)
27: throws IllegalArgumentException {
28: if (t == null)
29: throw new IllegalArgumentException(
30: "Can only translate non-null throwables.");
31: if (!(t instanceof ResourceMissingException))
32: throw new IllegalArgumentException(
33: "Can only translate ResourceMissingExceptions.");
34: if (parentDoc == null)
35: throw new IllegalArgumentException(
36: "Must have non-null parent doc.");
37:
38: ResourceMissingException rme = (ResourceMissingException) t;
39:
40: // use basic ThrowableToElement to boostrap a basic Element.
41: Element element = this .throwableToElement.throwableToElement(t,
42: parentDoc);
43: element.setAttribute("renderedAs",
44: ResourceMissingException.class.getName());
45:
46: Element resourceEl = parentDoc.createElement("resource");
47: Element uriEl = parentDoc.createElement("uri");
48: uriEl.appendChild(parentDoc
49: .createTextNode(rme.getResourceURI()));
50: resourceEl.appendChild(uriEl);
51: Element descriptionEl = parentDoc.createElement("description");
52: descriptionEl.appendChild(parentDoc.createTextNode(rme
53: .getResourceDescription()));
54: resourceEl.appendChild(descriptionEl);
55: element.appendChild(resourceEl);
56:
57: return element;
58: }
59:
60: /* (non-Javadoc)
61: * @see org.jasig.portal.channels.error.tt.IThrowableToElement#supports(java.lang.Class)
62: */
63: public boolean supports(Class c) throws IllegalArgumentException {
64: if (c == null)
65: throw new IllegalArgumentException(
66: "Supports undefined on null");
67: if (!Throwable.class.isAssignableFrom(c))
68: throw new IllegalArgumentException(
69: "Supports undefined on classes "
70: + "which are not and do not extend Throwable: "
71: + c.getName());
72:
73: return (ResourceMissingException.class.isAssignableFrom(c));
74: }
75:
76: }
|