001: /**
002: * MVEL (The MVFLEX Expression Language)
003: *
004: * Copyright (C) 2007 Christopher Brock, MVFLEX/Valhalla Project and the Codehaus
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: *
018: */package org.mvel.debug;
019:
020: import org.mvel.ast.LineLabel;
021: import org.mvel.compiler.CompiledExpression;
022: import org.mvel.integration.VariableResolverFactory;
023:
024: import java.util.HashMap;
025: import java.util.HashSet;
026: import java.util.Map;
027: import java.util.Set;
028:
029: public class DebuggerContext {
030: private Map<String, Set<Integer>> breakpoints;
031: private Debugger debugger;
032: private int debuggerState = 0;
033:
034: public DebuggerContext() {
035: breakpoints = new HashMap<String, Set<Integer>>();
036: }
037:
038: public Map<String, Set<Integer>> getBreakpoints() {
039: return breakpoints;
040: }
041:
042: public void setBreakpoints(Map<String, Set<Integer>> breakpoints) {
043: this .breakpoints = breakpoints;
044: }
045:
046: public Debugger getDebugger() {
047: return debugger;
048: }
049:
050: public void setDebugger(Debugger debugger) {
051: this .debugger = debugger;
052: }
053:
054: public int getDebuggerState() {
055: return debuggerState;
056: }
057:
058: public void setDebuggerState(int debuggerState) {
059: this .debuggerState = debuggerState;
060: }
061:
062: // utility methods
063:
064: public void registerBreakpoint(String sourceFile, int lineNumber) {
065: if (!breakpoints.containsKey(sourceFile))
066: breakpoints.put(sourceFile, new HashSet<Integer>());
067: breakpoints.get(sourceFile).add(lineNumber);
068: }
069:
070: public void removeBreakpoint(String sourceFile, int lineNumber) {
071: if (!breakpoints.containsKey(sourceFile))
072: return;
073: breakpoints.get(sourceFile).remove(lineNumber);
074: }
075:
076: public void clearAllBreakpoints() {
077: breakpoints.clear();
078: }
079:
080: public boolean hasBreakpoints() {
081: return breakpoints.size() != 0;
082: }
083:
084: public boolean hasBreakpoint(LineLabel label) {
085: return breakpoints.containsKey(label.getSourceFile())
086: && breakpoints.get(label.getSourceFile()).contains(
087: label.getLineNumber());
088: }
089:
090: public boolean hasBreakpoint(String sourceFile, int lineNumber) {
091: return breakpoints.containsKey(sourceFile)
092: && breakpoints.get(sourceFile).contains(lineNumber);
093: }
094:
095: public boolean hasDebugger() {
096: return debugger != null;
097: }
098:
099: public int checkBreak(LineLabel label,
100: VariableResolverFactory factory,
101: CompiledExpression expression) {
102: if (debuggerState == Debugger.STEP || hasBreakpoint(label)) {
103: if (debugger == null)
104: throw new RuntimeException(
105: "no debugger registered to handle breakpoint");
106: return debuggerState = debugger.onBreak(new Frame(label,
107: factory, expression.getParserContext()));
108:
109: }
110: return 0;
111: }
112:
113: }
|