1 16 package org.apache.cocoon.util.location; 17 18 import java.lang.reflect.Method ; 19 import java.util.ArrayList ; 20 import java.util.Collections ; 21 import java.util.List ; 22 23 import org.apache.commons.lang.exception.ExceptionUtils; 24 import org.apache.commons.lang.exception.NestableException; 25 26 33 public class LocatedException extends NestableException 34 implements LocatableException, MultiLocatable { 35 36 private List locations; 37 38 public LocatedException(String message) { 39 this(message, null, null); 40 } 41 42 public LocatedException(String message, Throwable cause) { 43 this(message, cause, null); 44 } 45 46 public LocatedException(String message, Location location) { 47 this(message, null, location); 48 } 49 50 public LocatedException(String message, Throwable cause, Location location) { 51 super(message, cause); 52 ensureCauseChainIsSet(cause); 53 addCauseLocations(this, cause); 54 addLocation(location); 55 } 56 57 private static Method INIT_CAUSE_METHOD = null; 58 static { 59 try { 60 INIT_CAUSE_METHOD = Throwable .class.getMethod("initCause", new Class [] { Throwable .class} ); 61 } catch(Exception e) { 62 } 64 } 65 66 71 public static void ensureCauseChainIsSet(Throwable thr) { 72 if (INIT_CAUSE_METHOD == null) 73 return; 74 75 while (thr != null && !(thr instanceof LocatedRuntimeException) && !(thr instanceof LocatedException)) { 77 Throwable parent = ExceptionUtils.getCause(thr); 78 if (parent != null) { 79 try { 80 INIT_CAUSE_METHOD.invoke(thr, new Object []{ parent }); 81 } catch (Exception e) { 82 } 84 } 85 thr = parent; 86 } 87 } 88 89 103 public static void addCauseLocations(MultiLocatable self, Throwable cause) { 104 if (cause == null || cause instanceof Locatable) { 105 return; 107 } 108 addCauseLocations(self, ExceptionUtils.getCause(cause)); 110 Location loc = LocationUtils.getLocation(cause); 112 if (LocationUtils.isKnown(loc)) { 113 String name = cause.getClass().getName(); 115 int pos = name.lastIndexOf('.'); 116 if (pos != -1) { 117 name = name.substring(pos+1); 118 } 119 loc = new LocationImpl("[" + name + "]", loc.getURI(), loc.getLineNumber(), loc.getColumnNumber()); 120 self.addLocation(loc); 121 } 122 } 123 124 public Location getLocation() { 125 return locations == null ? null : (Location)locations.get(0); 126 } 127 128 public List getLocations() { 129 return locations == null ? Collections.EMPTY_LIST : locations; 130 } 131 132 public String getRawMessage() { 133 return super.getMessage(); 134 } 135 136 145 public static String getMessage(String message, List locations) { 146 if (locations == null || locations.isEmpty()) { 147 return message; 148 } 149 150 StringBuffer buf = message == null ? new StringBuffer () : new StringBuffer (message); 152 for (int i = 0; i < locations.size(); i++) { 153 buf.append("\n\tat ").append(LocationUtils.toString((Location)locations.get(i))); 154 } 155 return buf.toString(); 156 } 157 158 public String getMessage() { 159 return getMessage(super.getMessage(), locations); 160 } 161 162 public void addLocation(Location loc) { 163 if (LocationUtils.isUnknown(loc)) 164 return; 165 166 if (locations == null) { 167 this.locations = new ArrayList (1); } 169 locations.add(LocationImpl.get(loc)); 170 } 171 } 172 | Popular Tags |