1 55 56 package org.apache.bsf.engines.javascript; 57 58 import java.io.StringReader ; 59 import java.io.InputStream ; 60 import java.io.IOException ; 61 import java.lang.reflect.InvocationTargetException ; 62 import java.util.Vector ; 63 import java.util.Enumeration ; 64 65 import org.mozilla.javascript.*; 66 import org.mozilla.javascript.debug.*; 67 68 import org.apache.bsf.*; 69 import org.apache.bsf.util.BSFEngineImpl; 70 import org.apache.bsf.util.BSFFunctions; 71 72 import java.util.Hashtable ; 73 import org.apache.bsf.debug.jsdi.*; 74 75 import java.io.Reader ; 76 77 101 public class DocumentCell { 102 103 RhinoEngineDebugger m_rhinoDebugger; 104 105 String m_docName; 106 Vector m_fnOrScripts; 107 Vector m_breakpoints; 108 private boolean m_entryexit; 109 private FnOrScript m_lastFnOrScript; 110 111 Hashtable m_functionMap; 112 113 public DocumentCell(RhinoEngineDebugger rhinoDebugger, String name) { 114 m_rhinoDebugger = rhinoDebugger; 115 m_docName = name; 116 m_breakpoints = new Vector (); 117 m_functionMap = new Hashtable (); 118 m_fnOrScripts = new Vector (); 119 m_entryexit = false; 120 m_lastFnOrScript = null; 121 } 122 123 public String getName() { 124 return m_docName; 125 } 126 127 138 public void addBreakpointAtLine(int brkptId, int lineno) { 139 Enumeration e; 140 FnOrScript fnOrScript; 141 142 BreakPoint bp = new BreakPoint(this, brkptId); 143 bp.setLineNo(lineno); 144 145 e = m_fnOrScripts.elements(); 150 while (e.hasMoreElements()) { 151 fnOrScript = (FnOrScript) e.nextElement(); 152 try { 153 if (fnOrScript.contains(bp)) { 154 fnOrScript.addBreakpoint(bp); 155 return; 156 } 157 } catch (BSFException ex) { 158 } 159 } 160 m_breakpoints.addElement(bp); 161 } 162 163 167 public void addBreakpointAtOffset(int brkptId, int offset) { 168 Enumeration e; 169 FnOrScript fnOrScript; 170 171 BreakPoint bp = new BreakPoint(this, brkptId); 172 bp.setOffset(offset); 173 174 e = m_fnOrScripts.elements(); 175 while (e.hasMoreElements()) { 176 fnOrScript = (FnOrScript) e.nextElement(); 177 try { 178 if (fnOrScript.contains(bp)) { 179 fnOrScript.addBreakpoint(bp); 180 return; 181 } 182 } catch (BSFException ex) { 183 } 184 } 185 m_breakpoints.addElement(bp); 186 } 187 188 private void attachBreakpoints(FnOrScript fnOrScript) { 193 194 Enumeration e; 195 BreakPoint bp; 196 Vector toremove = new Vector (); 197 e = m_breakpoints.elements(); 198 while (e.hasMoreElements()) { 199 bp = (BreakPoint) e.nextElement(); 200 try { 201 if (fnOrScript.contains(bp)) { 202 fnOrScript.addBreakpoint(bp); 206 toremove.addElement(bp); 207 } 208 } catch (BSFException ex) { 209 } 210 } 211 e = toremove.elements(); 214 while (e.hasMoreElements()) { 215 bp = (BreakPoint) e.nextElement(); 216 m_breakpoints.removeElement(bp); 217 } 218 } 219 220 public BreakPoint findBreakpointAtLine(int lineno) throws BSFException { 221 Enumeration e; 222 BreakPoint bp; 223 FnOrScript fnOrScript; 224 225 e = m_fnOrScripts.elements(); 226 while (e.hasMoreElements()) { 227 fnOrScript = (FnOrScript) e.nextElement(); 228 bp = fnOrScript.findBreakpointAtLine(lineno); 229 if (bp != null) 230 return bp; 231 } 232 return null; 233 } 234 235 public BreakPoint findBreakpointAtOffset(int offset) throws BSFException { 236 Enumeration e; 237 BreakPoint bp; 238 FnOrScript fnOrScript; 239 240 e = m_fnOrScripts.elements(); 241 while (e.hasMoreElements()) { 242 fnOrScript = (FnOrScript) e.nextElement(); 243 bp = fnOrScript.findBreakpointAtOffset(offset); 244 if (bp != null) 245 return bp; 246 } 247 return null; 248 } 249 250 public FnOrScript findFnOrScript(int startLine, int column) { 251 Enumeration e; 252 FnOrScript fnOrScript; 253 e = m_fnOrScripts.elements(); 254 while (e.hasMoreElements()) { 255 fnOrScript = (FnOrScript) e.nextElement(); 256 if (fnOrScript.m_startLine == startLine) 257 if (fnOrScript.m_column == column) 258 return fnOrScript; 259 } 260 return null; 261 } 262 263 public FnOrScript findFnOrScriptContaining(int line) { 264 Enumeration e; 265 FnOrScript fnOrScript; 266 e = m_fnOrScripts.elements(); 267 while (e.hasMoreElements()) { 268 fnOrScript = (FnOrScript) e.nextElement(); 269 if (fnOrScript.m_startLine <= line && 270 (fnOrScript.m_startLine + fnOrScript.m_lineCount) >= line) 271 return fnOrScript; 272 } 273 return null; 274 } 275 276 public Enumeration fnOrScripts() { 277 return m_fnOrScripts.elements(); 278 } 279 280 public FnOrScript registerFnOrScriptLines(Reader reader, 281 int startLine, 282 int column) 283 throws BSFException { 284 285 FnOrScript fnOrScript; 286 Enumeration e; 287 e = m_fnOrScripts.elements(); 289 while (e.hasMoreElements()) { 290 fnOrScript = (FnOrScript) e.nextElement(); 291 if (fnOrScript.getFirstLine() == startLine) 292 if (fnOrScript.getColumn() == column) 293 return fnOrScript; 294 } 295 try { 296 297 fnOrScript = new FnOrScript(this); 298 m_fnOrScripts.addElement(fnOrScript); 299 300 fnOrScript.specifyLinesPos(reader, startLine, column); 301 302 this.attachBreakpoints(fnOrScript); 303 } catch (IOException ex) { 304 throw new BSFException( 305 BSFException.REASON_EXECUTION_ERROR, 306 "while registering script or function.", 307 ex); 308 } 309 return fnOrScript; 310 } 311 312 public FnOrScript registerFnOrScriptLines(String source, 313 int startLine, 314 int column) 315 throws BSFException { 316 Reader reader = new StringReader (source); 317 return registerFnOrScriptLines(reader, startLine, column); 318 } 319 320 public FnOrScript registerFnOrScriptRange(Reader reader, int offset) 321 throws BSFException { 322 323 FnOrScript fnOrScript; 324 try { 325 326 fnOrScript = new FnOrScript(this); 327 m_fnOrScripts.addElement(fnOrScript); 328 329 fnOrScript.specifyRange(reader, offset); 330 331 this.attachBreakpoints(fnOrScript); 332 } catch (IOException ex) { 333 throw new BSFException( 334 BSFException.REASON_EXECUTION_ERROR, 335 "while registering script or function.", 336 ex); 337 } 338 return fnOrScript; 339 } 340 341 public FnOrScript registerFnOrScriptRange(String source, int offset) 342 throws BSFException { 343 344 Reader reader = new StringReader (source); 345 return registerFnOrScriptRange(reader, offset); 346 } 347 348 359 public BreakPoint removeBreakpoint(int brkptId) { 360 Enumeration e; 361 BreakPoint bp=null; 362 FnOrScript fnOrScript; 363 364 e = m_breakpoints.elements(); 367 while (e.hasMoreElements()) { 368 bp = (BreakPoint) e.nextElement(); 369 if (bp.getId()==brkptId) { 370 m_breakpoints.removeElement(bp); 373 return bp; 374 } 375 } 376 e = m_fnOrScripts.elements(); 380 while (e.hasMoreElements()) { 381 fnOrScript = (FnOrScript) e.nextElement(); 382 bp = fnOrScript.removeBreakpoint(brkptId); 383 if (null!=bp) break; 384 } 385 return bp; 386 } 387 388 public void setEntryExit(boolean on_value) { 389 m_entryexit = on_value; 390 } 391 392 public boolean getEntryExit() { 393 return m_entryexit; 394 } 395 396 public void setLastFnOrScript(FnOrScript fnos) { 397 m_lastFnOrScript = fnos; 398 } 399 400 public FnOrScript getLastFnOrScript() { 401 return m_lastFnOrScript; 402 } 403 } 404 | Popular Tags |