KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > rm > HarmoniseException


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.rm;
20
21 import java.io.*;
22
23 import org.openharmonise.commons.xml.*;
24 import org.openharmonise.rm.publishing.*;
25 import org.openharmonise.rm.resources.publishing.*;
26 import org.w3c.dom.*;
27
28
29 /**
30  * Abstract base class for all exceptions thrown within Harmonise.
31  *
32  * @author Michael Bell
33  * @version $Revision: 1.1 $
34  *
35  */

36 public abstract class HarmoniseException extends Exception JavaDoc implements Publishable {
37     
38     /**
39      * Exception tag name
40      */

41     public static final String JavaDoc TAG_EXCEPTION = "Exception";
42     
43     /**
44      * Message tag name
45      */

46     public static final String JavaDoc TAG_MESSAGE = "Message";
47     
48     /**
49      * Stack trace tag name
50      */

51     public static final String JavaDoc TAG_STACKTRACE = "Stacktrace";
52
53
54     /**
55      * Constructs an exception with no detail message and no cause
56      *
57      */

58     public HarmoniseException() {
59         super();
60     }
61
62     /**
63      * Constructs an exception with the specified detail message and no
64      * cause.
65      *
66      * @param s the detail message
67      */

68     public HarmoniseException(String JavaDoc s) {
69         super(s);
70     }
71
72     /**
73      * Constructs an exception with the specified detail message and
74      * cause.
75      *
76      * @param s the detail message
77      * @param e the cause
78      */

79     public HarmoniseException(String JavaDoc s, Throwable JavaDoc e) {
80         super(s,e);
81     }
82     
83     /**
84      * Constructs an exception with the specified cause.
85      *
86      * @param e the cause
87      */

88     public HarmoniseException(Throwable JavaDoc e) {
89         super(e);
90     }
91
92     /**
93      * Prints the causes stack trace to the specified <code>PrintWriter</code>.
94      *
95      * @param out the <code>PrintWriter</code>
96      */

97     public void printOriginalStackTrace(PrintWriter out) {
98         if (getCause() != null) {
99             getCause().printStackTrace(out);
100         }
101     }
102
103     /**
104      * Prints the causes stack trace to the specified <code>PrintStream</code>.
105      *
106      * @param out the <code>PrintStream</code>
107      */

108     public void printOriginalStackTrace(PrintStream out) {
109         if (getCause() != null) {
110             getCause().printStackTrace(out);
111         }
112     }
113
114     /**
115      * Returns the XML element representation of this exception using the
116      * given <code>XMLDocument</code> as the owner <code>Document</code>.
117      *
118      * @param xmlDoc the owner <code>Document</code>
119      * @return the XML element representation of this exception
120      */

121     public Element publish(XMLDocument xmlDoc) {
122         return publishThrowable(this,xmlDoc);
123     }
124
125     /* (non-Javadoc)
126      * @see org.openharmonise.rm.publishing.Publishable#publish(org.openharmonise.rm.resources.publishing.Template, org.openharmonise.rm.publishing.HarmoniseOutput, org.openharmonise.rm.publishing.State)
127      */

128     public Element publish(Template template, HarmoniseOutput output, State state)
129         throws PublishException {
130         Element resultEl = null;
131
132
133             try {
134                 resultEl =
135                     publish(template.getTemplateRootElement(), output, state);
136             } catch (DataAccessException e) {
137                 throw new PublishException(e.getLocalizedMessage());
138             }
139     
140         return resultEl;
141     }
142
143     /* (non-Javadoc)
144      * @see org.openharmonise.rm.publishing.Publishable#publish(org.w3c.dom.Element, org.openharmonise.rm.publishing.HarmoniseOutput, org.openharmonise.rm.publishing.State)
145      */

146     public Element publish(Element topEl, HarmoniseOutput output, State state)
147         throws PublishException {
148         return publish(output);
149     }
150
151     /* (non-Javadoc)
152      * @see org.openharmonise.rm.publishing.Publishable#populate(org.w3c.dom.Element, org.openharmonise.rm.publishing.State)
153      */

154     public void populate(Element xmlElement, State state)
155         throws PopulateException {
156         //no need
157

158     }
159
160     /* (non-Javadoc)
161      * @see org.openharmonise.rm.publishing.Publishable#getTagName()
162      */

163     public String JavaDoc getTagName() {
164         return TAG_EXCEPTION;
165     }
166     
167     /**
168      * Returns the XML element representation of the specified
169      * <code>Throwable</code> using the given <code>XMLDocument</code>
170      * as the owner <code>Document</code>.
171      *
172      * @param thrown the <code>Throwable</code> object to publish
173      * @param xmlDoc the owner <code>Document</code>
174      * @return the XML element representation of the <code>Throwable</code>
175      */

176     private Element publishThrowable(Throwable JavaDoc thrown, XMLDocument xmlDoc) {
177         Element exEl = xmlDoc.createElement(TAG_EXCEPTION);
178         Element msgEl = xmlDoc.createElement(TAG_MESSAGE);
179         Element trcEl = xmlDoc.createElement(TAG_STACKTRACE);
180         msgEl.appendChild(xmlDoc.createTextNode(thrown.getMessage()));
181         exEl.appendChild(msgEl);
182         StringWriter swriter = new StringWriter();
183         PrintWriter out = new PrintWriter(swriter);
184         thrown.printStackTrace(out);
185         trcEl.appendChild(xmlDoc.createTextNode(swriter.toString()));
186         out.close();
187         exEl.appendChild(trcEl);
188         
189         Throwable JavaDoc cause = thrown.getCause();
190         
191         if(cause != null) {
192             exEl.appendChild(publishThrowable(cause,xmlDoc));
193         }
194         
195         return exEl;
196     }
197
198 }
Popular Tags