1 16 package org.apache.cocoon.generation; 17 18 import java.io.IOException ; 19 import java.util.List ; 20 import java.util.Map ; 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 ; 34 import org.xml.sax.ContentHandler ; 35 import org.xml.sax.SAXException ; 36 37 46 public class ExceptionGenerator extends AbstractGenerator { 47 48 private Throwable thr; 49 50 public static String EXCEPTION_NS = "http://apache.org/cocoon/exception/1.0"; 51 52 public void setup(SourceResolver resolver, Map objectModel, String src, Parameters par) throws ProcessingException, SAXException , IOException { 53 super.setup(resolver, objectModel, src, par); 54 thr = (Throwable )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 , SAXException , ProcessingException { 61 this.contentHandler.startDocument(); 62 toSAX(thr, this.contentHandler); 63 this.contentHandler.endDocument(); 64 } 65 66 public static void toSAX(Throwable thr, ContentHandler handler) throws SAXException { 67 Throwable 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 Location loc = LocationUtils.getLocation(root); 77 if (LocationUtils.isKnown(loc)) { 78 attr.clear(); 79 dumpLocation(loc, attr, handler); 80 } 81 82 attr.clear(); 84 String message = root instanceof LocatableException ? ((LocatableException)root).getRawMessage() : root.getMessage(); 85 simpleElement("message", attr, message, handler); 86 87 handler.startElement(EXCEPTION_NS, "cocoon-stacktrace", "ex:cocoon-stacktrace", attr); 89 Throwable current = thr; 90 while (current != null) { 91 loc = LocationUtils.getLocation(current); 92 if (LocationUtils.isKnown(loc)) { 93 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 locations = ((MultiLocatable)current).getLocations(); 105 for (int i = 1; i < locations.size(); i++) { 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 current = ExceptionUtils.getCause(current); 117 } 118 119 handler.endElement(EXCEPTION_NS, "cocoon-stacktrace", "ex:cocoon-stacktrace"); 120 121 attr.clear(); 123 simpleElement("stacktrace", attr, ExceptionUtils.getStackTrace(root), handler); 124 125 if (thr != root) { 127 String 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 handler) throws SAXException { 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 name, Attributes attr, String value, ContentHandler handler) throws SAXException { 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 |