1 16 package org.apache.cocoon.components.flow.javascript; 17 18 import java.util.ArrayList ; 19 import java.util.List ; 20 21 import org.apache.cocoon.ProcessingException; 22 import org.apache.cocoon.util.location.Location; 23 import org.apache.cocoon.util.location.LocationImpl; 24 import org.apache.cocoon.util.location.LocationUtils; 25 import org.apache.commons.lang.exception.ExceptionUtils; 26 import org.mozilla.javascript.Context; 27 import org.mozilla.javascript.EcmaError; 28 import org.mozilla.javascript.JavaScriptException; 29 import org.mozilla.javascript.Scriptable; 30 import org.mozilla.javascript.debug.DebugFrame; 31 import org.mozilla.javascript.debug.DebuggableScript; 32 import org.mozilla.javascript.debug.Debugger; 33 34 45 public class LocationTrackingDebugger implements Debugger { 46 47 private static final LocationUtils.LocationFinder rhinoLocFinder = new LocationUtils.LocationFinder() { 49 50 public Location getLocation(Object obj, String description) { 51 if (obj instanceof EcmaError) { 52 EcmaError ex = (EcmaError)obj; 53 if (ex.getSourceName() != null) { 54 return new LocationImpl(ex.getName(), ex.getSourceName(), ex.getLineNumber(), ex.getColumnNumber()); 55 } else { 56 return Location.UNKNOWN; 57 } 58 59 } else if (obj instanceof JavaScriptException) { 60 JavaScriptException ex = (JavaScriptException)obj; 61 if (ex.sourceName() != null) { 62 return new LocationImpl(description, ex.sourceName(), ex.lineNumber(), -1); 63 } else { 64 return Location.UNKNOWN; 65 } 66 } 67 68 return null; 69 } 70 }; 71 72 73 static { 74 ExceptionUtils.addCauseMethodName("getWrappedException"); 76 LocationUtils.addFinder(rhinoLocFinder); 77 } 78 79 private List locations; 80 private Throwable throwable; 81 82 85 public void handleCompilationDone(Context cx, DebuggableScript fnOrScript, StringBuffer source) { 86 } 88 89 92 public DebugFrame enterFrame(Context cx, Scriptable scope, Scriptable thisObj, Object [] args, DebuggableScript fnOrScript) { 93 return new StackTrackingFrame(fnOrScript); 94 } 95 96 99 public void handleCompilationDone(Context cx, DebuggableScript fnOrScript, String source) { 100 } 102 103 106 public DebugFrame getFrame(Context cx, DebuggableScript fnOrScript) { 107 return new StackTrackingFrame(fnOrScript); 109 } 110 111 120 public Exception getException(String description, Exception originalException) throws ProcessingException { 121 if (throwable == null || locations == null) { 122 return originalException; 124 } 125 126 Throwable cause = ExceptionUtils.getCause(throwable); 129 if (cause != null) 130 throwable = cause; 131 132 return ProcessingException.throwLocated(description, throwable, locations); 133 } 134 135 private class StackTrackingFrame implements DebugFrame { 136 137 DebuggableScript script; 138 int line; 139 140 public StackTrackingFrame(DebuggableScript script) { 141 this.script = script; 142 } 143 144 public void onEnter(Context cx, Scriptable activation, Scriptable thisObj, Object [] args) { 145 } 147 148 public void onLineChange(Context cx, int lineNumber, boolean breakpoint) { 150 line = lineNumber; 151 } 152 153 public void onLineChange(Context cx, int lineNumber) { 155 line = lineNumber; 156 } 157 158 public void onExceptionThrown(Context cx, Throwable ex) { 159 throwable = ex; 160 } 161 162 public void onExit(Context cx, boolean byThrow, Object resultOrException) { 163 if (byThrow) { 164 String name = null; 165 172 if (locations == null) { 173 locations = new ArrayList (1); } 175 176 locations.add(new LocationImpl(name, script.getSourceName(), line, -1)); 177 178 } else if (locations != null) { 179 locations = null; 181 } 182 } 183 } 184 } 185 186 | Popular Tags |