KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > generation > ExceptionGenerator


1 /*
2  * Copyright 2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.generation;
17
18 import java.io.IOException JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import org.apache.avalon.framework.parameters.Parameters;
23 import org.apache.cocoon.ProcessingException;
24 import org.apache.cocoon.environment.ObjectModelHelper;
25 import org.apache.cocoon.environment.SourceResolver;
26 import org.apache.cocoon.util.location.LocatableException;
27 import org.apache.cocoon.util.location.Location;
28 import org.apache.cocoon.util.location.LocationUtils;
29 import org.apache.cocoon.util.location.MultiLocatable;
30 import org.apache.cocoon.xml.AttributesImpl;
31 import org.apache.commons.lang.SystemUtils;
32 import org.apache.commons.lang.exception.ExceptionUtils;
33 import org.xml.sax.Attributes JavaDoc;
34 import org.xml.sax.ContentHandler JavaDoc;
35 import org.xml.sax.SAXException JavaDoc;
36
37 /**
38  * A generator that dumps an XML representation of the exception raised during a pipeline execution.
39  * <p>
40  * The Cocoon stack trace is produced, reflecting all locations the original exception went through,
41  * along with the root exception stacktrace and the full exception stacktrace.
42  *
43  * @since 2.1.8
44  * @version $Id: ExceptionGenerator.java 280628 2005-09-13 19:14:35Z sylvain $
45  */

46 public class ExceptionGenerator extends AbstractGenerator {
47     
48     private Throwable JavaDoc thr;
49     
50     public static String JavaDoc EXCEPTION_NS = "http://apache.org/cocoon/exception/1.0";
51
52     public void setup(SourceResolver resolver, Map JavaDoc objectModel, String JavaDoc src, Parameters par) throws ProcessingException, SAXException JavaDoc, IOException JavaDoc {
53         super.setup(resolver, objectModel, src, par);
54         thr = (Throwable JavaDoc)objectModel.get(ObjectModelHelper.THROWABLE_OBJECT);
55         if (thr == null) {
56             throw new ProcessingException("ExceptionGenerator should be used in <map:handle-errors>");
57         }
58     }
59
60     public void generate() throws IOException JavaDoc, SAXException JavaDoc, ProcessingException {
61         this.contentHandler.startDocument();
62         toSAX(thr, this.contentHandler);
63         this.contentHandler.endDocument();
64     }
65     
66     public static void toSAX(Throwable JavaDoc thr, ContentHandler JavaDoc handler) throws SAXException JavaDoc {
67         Throwable JavaDoc root = ExceptionUtils.getRootCause(thr);
68         if (root == null) root = thr;
69
70         AttributesImpl attr = new AttributesImpl();
71         handler.startPrefixMapping("ex", EXCEPTION_NS);
72         attr.addCDATAAttribute("class", root.getClass().getName());
73         handler.startElement(EXCEPTION_NS, "exception-report", "ex:exception-report", attr);
74         
75         // Root exception location
76
Location loc = LocationUtils.getLocation(root);
77         if (LocationUtils.isKnown(loc)) {
78             attr.clear();
79             dumpLocation(loc, attr, handler);
80         }
81
82         // Root exception message
83
attr.clear();
84         String JavaDoc message = root instanceof LocatableException ? ((LocatableException)root).getRawMessage() : root.getMessage();
85         simpleElement("message", attr, message, handler);
86         
87         // Cocoon stacktrace: dump all located exceptions in the exception stack
88
handler.startElement(EXCEPTION_NS, "cocoon-stacktrace", "ex:cocoon-stacktrace", attr);
89         Throwable JavaDoc current = thr;
90         while (current != null) {
91             loc = LocationUtils.getLocation(current);
92             if (LocationUtils.isKnown(loc)) {
93                 // One or more locations: dump it
94
handler.startElement(EXCEPTION_NS, "exception", "ex:exception", attr);
95                 
96                 message = current instanceof LocatableException ? ((LocatableException)current).getRawMessage() : current.getMessage();
97                 simpleElement("message", attr, message, handler);
98
99                 attr.clear();
100                 handler.startElement(EXCEPTION_NS, "locations", "ex:locations", attr);
101                 dumpLocation(loc, attr, handler);
102                 
103                 if (current instanceof MultiLocatable) {
104                     List JavaDoc locations = ((MultiLocatable)current).getLocations();
105                     for (int i = 1; i < locations.size(); i++) { // start at 1 because we already dumped the first one
106
attr.clear();
107                         dumpLocation((Location)locations.get(i), attr, handler);
108                     }
109                 }
110                 handler.endElement(EXCEPTION_NS, "locations", "ex:locations");
111                 handler.endElement(EXCEPTION_NS, "exception", "ex:exception");
112             }
113             
114             
115             // Dump parent location
116
current = ExceptionUtils.getCause(current);
117         }
118         
119         handler.endElement(EXCEPTION_NS, "cocoon-stacktrace", "ex:cocoon-stacktrace");
120         
121         // Root exception stacktrace
122
attr.clear();
123         simpleElement("stacktrace", attr, ExceptionUtils.getStackTrace(root), handler);
124         
125         // Full stack trace (if exception is chained)
126
if (thr != root) {
127             String JavaDoc trace = SystemUtils.isJavaVersionAtLeast(140) ?
128                     ExceptionUtils.getStackTrace(thr) :
129                     ExceptionUtils.getFullStackTrace(thr);
130
131             simpleElement("full-stacktrace", attr, trace, handler);
132         }
133         
134         handler.endElement(EXCEPTION_NS, "exception-report", "ex:exception-report");
135         handler.endPrefixMapping("ex");
136     }
137     
138     private static void dumpLocation(Location loc, AttributesImpl attr, ContentHandler JavaDoc handler) throws SAXException JavaDoc {
139         attr.addCDATAAttribute("uri", loc.getURI());
140         attr.addCDATAAttribute("line", Integer.toString(loc.getLineNumber()));
141         attr.addCDATAAttribute("column", Integer.toString(loc.getColumnNumber()));
142         simpleElement("location", attr, loc.getDescription(), handler);
143     }
144
145     private static void simpleElement(String JavaDoc name, Attributes JavaDoc attr, String JavaDoc value, ContentHandler JavaDoc handler) throws SAXException JavaDoc {
146         handler.startElement(EXCEPTION_NS, name, "ex:" + name, attr);
147         if (value != null && value.length() > 0) {
148             handler.characters(value.toCharArray(), 0, value.length());
149         }
150         handler.endElement(EXCEPTION_NS, name, "ex:" + name);
151     }
152 }
153
Popular Tags