1 16 package org.outerj.daisy.frontend; 17 18 import org.apache.cocoon.generation.Generator; 19 import org.apache.cocoon.ProcessingException; 20 import org.apache.cocoon.util.location.LocationUtils; 21 import org.apache.cocoon.util.location.LocatableException; 22 import org.apache.cocoon.util.location.MultiLocatable; 23 import org.apache.cocoon.util.location.Location; 24 import org.apache.cocoon.environment.SourceResolver; 25 import org.apache.cocoon.environment.ObjectModelHelper; 26 import org.apache.cocoon.environment.Request; 27 import org.apache.cocoon.xml.XMLConsumer; 28 import org.apache.cocoon.xml.AttributesImpl; 29 import org.apache.cocoon.xml.SaxBuffer; 30 import org.apache.avalon.framework.parameters.Parameters; 31 import org.apache.avalon.framework.service.Serviceable; 32 import org.apache.avalon.framework.service.ServiceManager; 33 import org.apache.avalon.framework.service.ServiceException; 34 import org.apache.avalon.framework.context.Contextualizable; 35 import org.apache.avalon.framework.context.Context; 36 import org.apache.avalon.framework.context.ContextException; 37 import org.apache.commons.lang.exception.ExceptionUtils; 38 import org.xml.sax.SAXException ; 39 import org.xml.sax.ContentHandler ; 40 import org.outerj.daisy.repository.clientimpl.infrastructure.DaisyPropagatedException; 41 import org.outerj.daisy.repository.clientimpl.infrastructure.MyStackTraceElement; 42 import org.outerj.daisy.repository.Repository; 43 import org.outerj.daisy.repository.LocalizedException; 44 import org.outerj.daisy.frontend.components.siteconf.SiteConf; 45 46 import java.io.IOException ; 47 import java.util.Map ; 48 import java.util.Locale ; 49 import java.util.List ; 50 51 public class ErrorGenerator implements Generator, Serviceable, Contextualizable { 52 private Throwable throwable; 53 private XMLConsumer consumer; 54 private String mountPoint; 55 private SiteConf siteConf; 56 private String layoutType; 57 private String skin; 58 private Repository repository; 59 private ServiceManager serviceManager; 60 private Context context; 61 private Locale locale; 62 63 public void service(ServiceManager serviceManager) throws ServiceException { 64 this.serviceManager = serviceManager; 65 } 66 67 public void contextualize(Context context) throws ContextException { 68 this.context = context; 69 } 70 71 public void setup(SourceResolver sourceResolver, Map objectModel, String s, Parameters parameters) throws ProcessingException, SAXException , IOException { 72 throwable = ObjectModelHelper.getThrowable(objectModel); 73 Request request = ObjectModelHelper.getRequest(objectModel); 74 75 mountPoint = (String )request.getAttribute("mountPoint"); 76 siteConf = (SiteConf)request.getAttribute("siteConf"); 77 skin = (String )request.getAttribute("skin"); 78 79 try { 80 repository = WikiHelper.getRepository(request, serviceManager); 81 } catch (Exception e) { 82 } 84 85 if ("true".equals(request.getAttribute("smallErrorPage"))) 86 layoutType = "plain"; 87 else 88 layoutType = null; 89 90 locale = (Locale )request.getAttribute("locale"); 91 if (locale == null) 92 locale = Locale.US; 93 94 if (throwable == null) 95 throw new ProcessingException("No throwable found in object model."); 96 } 97 98 public void generate() throws IOException , SAXException , ProcessingException { 99 consumer.startDocument(); 100 consumer.startElement("", "page", "page", new AttributesImpl()); 101 102 if (siteConf != null) 103 new PageContext(mountPoint, siteConf, repository, layoutType, skin, context).toSAX(consumer); 104 else { 105 SaxBuffer skinConf = null; 106 try { 107 skinConf = SkinConfHelper.getGlobalSkinConf(serviceManager); 108 } catch (Exception e) { 109 } 111 new PageContext(mountPoint, repository, layoutType, skin, skinConf, context).toSAX(consumer); 112 } 113 114 consumer.startElement("", "error", "error", new AttributesImpl()); 115 116 generateCocoonStacktrace(); 120 121 consumer.startElement("", "exceptionChain", "exceptionChain", new AttributesImpl()); 125 126 while (throwable != null) { 127 generateThrowable(throwable); 128 throwable = ExceptionUtils.getCause(throwable); 129 } 130 131 consumer.endElement("", "exceptionChain", "exceptionChain"); 132 consumer.endElement("", "error", "error"); 133 consumer.endElement("", "page", "page"); 134 consumer.endDocument(); 135 } 136 137 private void generateThrowable(Throwable throwable) throws SAXException { 138 if (throwable instanceof DaisyPropagatedException) { 139 DaisyPropagatedException dpe = (DaisyPropagatedException)throwable; 140 AttributesImpl attrs = new AttributesImpl(); 141 attrs.addCDATAAttribute("message", String.valueOf(dpe.getUserMessage())); 142 attrs.addCDATAAttribute("class", dpe.getRemoteClassName()); 143 consumer.startElement("", "throwable", "throwable", attrs); 144 145 AttributesImpl stackTraceAttrs = new AttributesImpl(); 146 stackTraceAttrs.addCDATAAttribute("remote", "true"); 147 consumer.startElement("", "stackTrace", "stackTrace", stackTraceAttrs); 148 MyStackTraceElement[] stacktrace = dpe.getRemoteStackTrace(); 149 for (int i = 0; i < stacktrace.length; i++) { 150 AttributesImpl steAttrs = new AttributesImpl(); 151 if (stacktrace[i].isNativeMethod()) 152 steAttrs.addCDATAAttribute("nativeMethod", String.valueOf(stacktrace[i].isNativeMethod())); 153 steAttrs.addCDATAAttribute("className", stacktrace[i].getClassName()); 154 steAttrs.addCDATAAttribute("fileName", stacktrace[i].getFileName()); 155 steAttrs.addCDATAAttribute("lineNumber", String.valueOf(stacktrace[i].getLineNumber())); 156 steAttrs.addCDATAAttribute("methodName", String.valueOf(stacktrace[i].getMethodName())); 157 consumer.startElement("", "stackTraceElement", "stackTraceElement", steAttrs); 158 consumer.endElement("", "stackTraceElement", "stackTraceElement"); 159 } 160 consumer.endElement("", "stackTrace", "stackTrace"); 161 162 consumer.endElement("", "throwable", "throwable"); 163 } else { 164 String message; 165 if (throwable instanceof LocalizedException) 166 message = ((LocalizedException)throwable).getMessage(locale); 167 else if (throwable instanceof LocatableException) 168 message = ((LocatableException)throwable).getRawMessage(); 169 else 170 message = throwable.getMessage(); 171 if (message == null) 172 message = ""; 173 174 AttributesImpl attrs = new AttributesImpl(); 175 attrs.addCDATAAttribute("message", message); 176 attrs.addCDATAAttribute("class", throwable.getClass().getName()); 177 consumer.startElement("", "throwable", "throwable", attrs); 178 179 consumer.startElement("", "stackTrace", "stackTrace", new AttributesImpl()); 180 StackTraceElement [] stacktrace = throwable.getStackTrace(); 181 for (int i = 0; i < stacktrace.length; i++) { 182 AttributesImpl steAttrs = new AttributesImpl(); 183 if (stacktrace[i].isNativeMethod()) 184 steAttrs.addCDATAAttribute("nativeMethod", String.valueOf(stacktrace[i].isNativeMethod())); 185 steAttrs.addCDATAAttribute("className", stacktrace[i].getClassName()); 186 steAttrs.addCDATAAttribute("fileName", stacktrace[i].getFileName()); 187 steAttrs.addCDATAAttribute("lineNumber", String.valueOf(stacktrace[i].getLineNumber())); 188 steAttrs.addCDATAAttribute("methodName", String.valueOf(stacktrace[i].getMethodName())); 189 consumer.startElement("", "stackTraceElement", "stackTraceElement", steAttrs); 190 consumer.endElement("", "stackTraceElement", "stackTraceElement"); 191 } 192 consumer.endElement("", "stackTrace", "stackTrace"); 193 194 consumer.endElement("", "throwable", "throwable"); 195 } 196 197 } 198 199 private void generateCocoonStacktrace() throws SAXException { 200 AttributesImpl attr = new AttributesImpl(); 202 consumer.startElement("", "cocoonStackTrace", "cocoonStackTrace", attr); 203 Throwable current = throwable; 204 while (current != null) { 205 Location loc = LocationUtils.getLocation(current); 206 if (LocationUtils.isKnown(loc)) { 207 consumer.startElement("", "exception", "exception", attr); 209 210 String message = current instanceof LocatableException ? ((LocatableException)current).getRawMessage() : current.getMessage(); 211 consumer.startElement("", "message", "message", new AttributesImpl()); 212 consumer.characters(message.toCharArray(), 0, message.length()); 213 consumer.endElement("", "message", "message"); 214 215 attr.clear(); 216 consumer.startElement("", "locations", "locations", attr); 217 dumpLocation(loc, attr); 218 219 if (current instanceof MultiLocatable) { 220 List locations = ((MultiLocatable)current).getLocations(); 221 for (int i = 1; i < locations.size(); i++) { attr.clear(); 223 dumpLocation((Location)locations.get(i), attr); 224 } 225 } 226 consumer.endElement("", "locations", "locations"); 227 consumer.endElement("", "exception", "exception"); 228 } 229 230 231 current = ExceptionUtils.getCause(current); 233 } 234 235 consumer.endElement("", "cocoonStackTrace", "cocoonStackTrace"); 236 } 237 238 private void dumpLocation(Location loc, AttributesImpl attr) throws SAXException { 240 attr.addCDATAAttribute("uri", loc.getURI()); 241 attr.addCDATAAttribute("line", Integer.toString(loc.getLineNumber())); 242 attr.addCDATAAttribute("column", Integer.toString(loc.getColumnNumber())); 243 consumer.startElement("", "location", "location", attr); 244 String description = loc.getDescription(); 245 if (description != null) 246 consumer.characters(description.toCharArray(), 0, description.length()); 247 consumer.endElement("", "location", "location"); 248 } 249 250 public void setConsumer(XMLConsumer xmlConsumer) { 251 this.consumer = xmlConsumer; 252 } 253 254 } 255 | Popular Tags |