KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > bsf > engines > javascript > BreakPoint


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2002 The Apache Software Foundation. All rights
5  * reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in
16  * the documentation and/or other materials provided with the
17  * distribution.
18  *
19  * 3. The end-user documentation included with the redistribution, if
20  * any, must include the following acknowlegement:
21  * "This product includes software developed by the
22  * Apache Software Foundation (http://www.apache.org/)."
23  * Alternately, this acknowlegement may appear in the software itself,
24  * if and wherever such third-party acknowlegements normally appear.
25  *
26  * 4. The names "Apache BSF", "Apache", and "Apache Software Foundation"
27  * must not be used to endorse or promote products derived from
28  * this software without prior written permission. For written
29  * permission, please contact apache@apache.org.
30  *
31  * 5. Products derived from this software may not be called "Apache"
32  * nor may "Apache" appear in their names without prior written
33  * permission of the Apache Group.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many individuals
50  * on behalf of the Apache Software Foundation and was originally created by
51  * Sanjiva Weerawarana and others at International Business Machines
52  * Corporation. For more information on the Apache Software Foundation,
53  * please see <http://www.apache.org/>.
54  */

55
56 package org.apache.bsf.engines.javascript;
57
58 import org.apache.bsf.*;
59
60 /**
61  * Materializes a breakpoint.
62  * A breakpoint can be defined at a line number or an
63  * offset character in its document. For JavaScript,
64  * we only support lineno for the underlying Rhino
65  * engine does support only the line numbers.
66  * Note: this is unfortunate for this prevents
67  * setting a breakpoint on different statements on the
68  * same line...
69  *
70  * A breakpoint is remembered at either the document level
71  * (DocumentCell) or the function/script level (FnOrScript).
72  * It is remembered at the document level when a FnOrScript
73  * is not yet known for the breakpoint line number.
74  * When a matching FnOrScrip will become known (compiled in Rhino),
75  * the breakpoint will be remembered at the FnOrScript level.
76  *
77  * Additionally, a breakpoint can be correlated or not
78  * to a compilation unit. When not correlated (m_unit==null),
79  * it means a compilation unit for the breakpoint
80  * line has not been seen yet. It may be because the
81  * breakpoint is on an invalid line but it may be
82  * also that the breakpoint is in a script or function
83  * that has not been compiled yet.
84  * Indeed, there is a delay between knowing about a FnOrScript
85  * and knowing about all its compilation unit. When a FnOrScript
86  * is about to be compiled in Rhino, we create the FnOrScript,
87  * with no known compilation unit. As a side effect of compiling,
88  * the Rhino engine calls us back with compilation units such
89  * as independent functions or scripts. This is when those units,
90  * represented by our class CompilationUnit are added to the
91  * corresponding FnOrScript.
92  *
93  * @author: Olivier Gruber
94  */

95 public class BreakPoint {
96
97     protected int m_brkptId;
98     protected int m_lineno;
99     protected int m_offset;
100     protected boolean m_lineDefined;
101
102     protected DocumentCell m_cell;
103     protected FnOrScript m_fnOrScript;
104
105     protected CompilationUnit m_unit;
106
107     public BreakPoint(BreakPoint bp) {
108         m_fnOrScript = bp.m_fnOrScript;
109         m_cell = bp.m_cell;
110         m_brkptId = bp.m_brkptId;
111         m_lineno = bp.m_lineno;
112         m_offset = bp.m_offset;
113         m_lineDefined = bp.m_lineDefined;
114         m_unit = bp.m_unit;
115     }
116     public BreakPoint(DocumentCell cell, int brkptid) {
117         super();
118         m_fnOrScript = null;
119         m_cell = cell;
120         m_brkptId = brkptid;
121         m_lineno = -1;
122         m_lineDefined = true;
123         m_unit=null;
124     }
125     public void setUnit(CompilationUnit unit) {
126         m_unit = unit;
127     }
128     
129     /**
130      * Propagating the breakpoint to its corresponding
131      * compilation unit, the side effect of that is to
132      * set the breakpoint in the Rhino engine.
133      */

134     public void propagate() {
135         if (m_unit != null) {
136             m_unit.propagate(m_lineno);
137         }
138     }
139     /**
140      * Unpropagating the breakpoint to its corresponding
141      * compilation unit, the side effect of that is to
142      * unset the breakpoint in the Rhino engine.
143      */

144     public void unpropagate() {
145         if (m_unit != null) {
146             m_unit.unpropagate(m_lineno);
147         }
148     }
149     
150     /**
151      * attaches this breakpoint to the specified FnOrScript.
152      */

153     public void attachToFnOrScript(FnOrScript fnOrScript) {
154         m_fnOrScript = fnOrScript;
155     }
156     /**
157      * @return the identifier of the breakpoint.
158      */

159     public int getId() {
160         return m_brkptId;
161     }
162     /**
163      * @return the line number of that breakpoint.
164      * This method will succeed only if the breakpoint as been
165      * defined at a line number.
166      * There is no automated translation between line number and
167      * offsets...
168      */

169     public int getLineNo() throws BSFException {
170         if (!m_lineDefined)
171             throw new BSFException(
172                 BSFException.REASON_INVALID_ARGUMENT,
173                 "Breakpoint is offset defined, can't provide its line number.");
174         return m_lineno;
175     }
176     /**
177      * @return the character offset of that breakpoint.
178      * This method will succeed only if the breakpoint as been
179      * defined at an offset.
180      * There is no automated translation between line number and
181      * offsets...
182      */

183     public int getOffset() throws BSFException {
184         if (m_lineDefined)
185             throw new BSFException(
186                 BSFException.REASON_INVALID_ARGUMENT,
187                 "Breakpoint is line defined, can't provide its offset.");
188         return m_offset;
189     }
190     
191     public void setLineNo(int lineno) {
192         m_lineno = lineno;
193     }
194     public void setOffset(int offset) {
195         m_offset = offset;
196     }
197 }
198
Popular Tags