KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lsmp > djep > xjep > ErrorCatchingVisitor


1 /* @author Rich Morris
2  * Created on 19-Jun-2003
3  *
4  * This code is covered by a Creative Commons
5  * Attribution, Non Commercial, Share Alike license
6  * <a HREF="http://creativecommons.org/licenses/by-nc-sa/1.0">License</a>
7  */

8 package org.lsmp.djep.xjep;
9 //import org.lsmp.djep.matrixParser.*;
10
import org.nfunk.jep.*;
11
12 /**
13  * An abstract ParserVisitor
14  * which adds some useful error handeling facilities.
15  * Visitors which require these facilities should extend this class.
16  * General format should be
17  * <pre>
18  * clearErrors();
19  * Object res = (Node) node.jjtAccept(this,data);
20  * if(hasErrors())
21  * throw new ParseException(getErrors());
22  *</pre>
23  * @author Rich Morris
24  * Created on 19-Jun-2003
25  */

26 abstract public class ErrorCatchingVisitor extends DoNothingVisitor
27 {
28     /** The current error list. */
29     private Exception JavaDoc error=null;
30
31     /** calls jjtAccept inside a try catch block, adding the error if necessary */
32     public Object JavaDoc acceptCatchingErrors(Node node,Object JavaDoc data)
33     {
34         Object JavaDoc res=null;
35         clearErrors();
36         try
37         {
38             res = node.jjtAccept(this,data);
39         }
40         catch (ParseException e) { addError(e); }
41         return res;
42     }
43     /** Reset the list of errors. */
44     public void clearErrors() { error = null; }
45
46     /** Are their any errors? */
47     public boolean hasErrors() { return error != null; }
48     
49     /** Adds an error message to the list of errors. */
50     public void addError(Exception JavaDoc e) {error = e; }
51
52     /** Returns the error messages. */
53     public String JavaDoc getErrorsMessage() {
54         if(error==null) return null;
55         else return error.getMessage();
56     }
57     /** Returns the Exception or null if no error. */
58     public Exception JavaDoc getError() { return error; }
59 }
60
Popular Tags