001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.rm;
020:
021: import java.io.*;
022:
023: import org.openharmonise.commons.xml.*;
024: import org.openharmonise.rm.publishing.*;
025: import org.openharmonise.rm.resources.publishing.*;
026: import org.w3c.dom.*;
027:
028: /**
029: * Abstract base class for all exceptions thrown within Harmonise.
030: *
031: * @author Michael Bell
032: * @version $Revision: 1.1 $
033: *
034: */
035: public abstract class HarmoniseException extends Exception implements
036: Publishable {
037:
038: /**
039: * Exception tag name
040: */
041: public static final String TAG_EXCEPTION = "Exception";
042:
043: /**
044: * Message tag name
045: */
046: public static final String TAG_MESSAGE = "Message";
047:
048: /**
049: * Stack trace tag name
050: */
051: public static final String TAG_STACKTRACE = "Stacktrace";
052:
053: /**
054: * Constructs an exception with no detail message and no cause
055: *
056: */
057: public HarmoniseException() {
058: super ();
059: }
060:
061: /**
062: * Constructs an exception with the specified detail message and no
063: * cause.
064: *
065: * @param s the detail message
066: */
067: public HarmoniseException(String s) {
068: super (s);
069: }
070:
071: /**
072: * Constructs an exception with the specified detail message and
073: * cause.
074: *
075: * @param s the detail message
076: * @param e the cause
077: */
078: public HarmoniseException(String s, Throwable e) {
079: super (s, e);
080: }
081:
082: /**
083: * Constructs an exception with the specified cause.
084: *
085: * @param e the cause
086: */
087: public HarmoniseException(Throwable e) {
088: super (e);
089: }
090:
091: /**
092: * Prints the causes stack trace to the specified <code>PrintWriter</code>.
093: *
094: * @param out the <code>PrintWriter</code>
095: */
096: public void printOriginalStackTrace(PrintWriter out) {
097: if (getCause() != null) {
098: getCause().printStackTrace(out);
099: }
100: }
101:
102: /**
103: * Prints the causes stack trace to the specified <code>PrintStream</code>.
104: *
105: * @param out the <code>PrintStream</code>
106: */
107: public void printOriginalStackTrace(PrintStream out) {
108: if (getCause() != null) {
109: getCause().printStackTrace(out);
110: }
111: }
112:
113: /**
114: * Returns the XML element representation of this exception using the
115: * given <code>XMLDocument</code> as the owner <code>Document</code>.
116: *
117: * @param xmlDoc the owner <code>Document</code>
118: * @return the XML element representation of this exception
119: */
120: public Element publish(XMLDocument xmlDoc) {
121: return publishThrowable(this , xmlDoc);
122: }
123:
124: /* (non-Javadoc)
125: * @see org.openharmonise.rm.publishing.Publishable#publish(org.openharmonise.rm.resources.publishing.Template, org.openharmonise.rm.publishing.HarmoniseOutput, org.openharmonise.rm.publishing.State)
126: */
127: public Element publish(Template template, HarmoniseOutput output,
128: State state) throws PublishException {
129: Element resultEl = null;
130:
131: try {
132: resultEl = publish(template.getTemplateRootElement(),
133: output, state);
134: } catch (DataAccessException e) {
135: throw new PublishException(e.getLocalizedMessage());
136: }
137:
138: return resultEl;
139: }
140:
141: /* (non-Javadoc)
142: * @see org.openharmonise.rm.publishing.Publishable#publish(org.w3c.dom.Element, org.openharmonise.rm.publishing.HarmoniseOutput, org.openharmonise.rm.publishing.State)
143: */
144: public Element publish(Element topEl, HarmoniseOutput output,
145: State state) throws PublishException {
146: return publish(output);
147: }
148:
149: /* (non-Javadoc)
150: * @see org.openharmonise.rm.publishing.Publishable#populate(org.w3c.dom.Element, org.openharmonise.rm.publishing.State)
151: */
152: public void populate(Element xmlElement, State state)
153: throws PopulateException {
154: //no need
155:
156: }
157:
158: /* (non-Javadoc)
159: * @see org.openharmonise.rm.publishing.Publishable#getTagName()
160: */
161: public String getTagName() {
162: return TAG_EXCEPTION;
163: }
164:
165: /**
166: * Returns the XML element representation of the specified
167: * <code>Throwable</code> using the given <code>XMLDocument</code>
168: * as the owner <code>Document</code>.
169: *
170: * @param thrown the <code>Throwable</code> object to publish
171: * @param xmlDoc the owner <code>Document</code>
172: * @return the XML element representation of the <code>Throwable</code>
173: */
174: private Element publishThrowable(Throwable thrown,
175: XMLDocument xmlDoc) {
176: Element exEl = xmlDoc.createElement(TAG_EXCEPTION);
177: Element msgEl = xmlDoc.createElement(TAG_MESSAGE);
178: Element trcEl = xmlDoc.createElement(TAG_STACKTRACE);
179: msgEl.appendChild(xmlDoc.createTextNode(thrown.getMessage()));
180: exEl.appendChild(msgEl);
181: StringWriter swriter = new StringWriter();
182: PrintWriter out = new PrintWriter(swriter);
183: thrown.printStackTrace(out);
184: trcEl.appendChild(xmlDoc.createTextNode(swriter.toString()));
185: out.close();
186: exEl.appendChild(trcEl);
187:
188: Throwable cause = thrown.getCause();
189:
190: if (cause != null) {
191: exEl.appendChild(publishThrowable(cause, xmlDoc));
192: }
193:
194: return exEl;
195: }
196:
197: }
|