KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > utils > ErrorListener


1 /*
2
3 [The "BSD licence"]
4 Copyright (c) 2005 Jean Bovet
5 All rights 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 2. Redistributions in binary form must reproduce the above copyright
14 notice, this list of conditions and the following disclaimer in the
15 documentation and/or other materials provided with the distribution.
16 3. The name of the author may not be used to endorse or promote products
17 derived from this software without specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 */

31
32 package org.antlr.works.utils;
33
34 import org.antlr.analysis.DecisionProbe;
35 import org.antlr.tool.ANTLRErrorListener;
36 import org.antlr.tool.Message;
37 import org.antlr.tool.ToolMessage;
38 import org.antlr.works.editor.EditorConsole;
39
40 import java.util.LinkedList JavaDoc;
41 import java.util.List JavaDoc;
42
43 public class ErrorListener implements ANTLRErrorListener {
44
45     protected static ErrorListener shared = new ErrorListener();
46
47     public List JavaDoc<String JavaDoc> infos = new LinkedList JavaDoc<String JavaDoc>();
48     public List JavaDoc<Message> errors = new LinkedList JavaDoc<Message>();
49     public List JavaDoc<Message> warnings = new LinkedList JavaDoc<Message>();
50
51     public boolean printToConsole = true;
52     public ErrorListener forwardListener = null;
53
54     public static ErrorListener shared() {
55         return shared;
56     }
57
58     public void setPrintToConsole(boolean flag) {
59         this.printToConsole = flag;
60     }
61
62     public void setForwardListener(ErrorListener listener) {
63         this.forwardListener = listener;
64     }
65
66     public void clear() {
67         infos.clear();
68         errors.clear();
69         warnings.clear();
70     }
71
72     public boolean hasErrors() {
73         return errors.size() > 0;
74     }
75
76     public String JavaDoc getFirstErrorMessage() {
77         if(hasErrors()) {
78             return errors.get(0).toString();
79         } else {
80             return null;
81         }
82     }
83
84     public void info(String JavaDoc msg) {
85         infos.add(msg);
86         if(forwardListener != null)
87             forwardListener.info(msg);
88         print(msg, Console.LEVEL_NORMAL);
89     }
90
91     public void error(Message msg) {
92         errors.add(msg);
93         if(forwardListener != null)
94             forwardListener.error(msg);
95         print(msg.toString(), Console.LEVEL_ERROR);
96     }
97
98     public void warning(Message msg) {
99         warnings.add(msg);
100         if(forwardListener != null)
101             forwardListener.warning(msg);
102         print(msg.toString(), Console.LEVEL_WARNING);
103     }
104
105     public void error(ToolMessage msg) {
106         errors.add(msg);
107         if(forwardListener != null)
108             forwardListener.error(msg);
109         print(msg.toString(), Console.LEVEL_ERROR);
110     }
111
112     public void print(String JavaDoc msg, int level) {
113         if(!printToConsole)
114             return;
115
116         boolean previousVerbose = DecisionProbe.verbose;
117         DecisionProbe.verbose = false;
118         try {
119             EditorConsole.getCurrent().println(msg, level);
120         } catch(Exception JavaDoc e) {
121             e.printStackTrace();
122         }
123         DecisionProbe.verbose = previousVerbose;
124     }
125 }
126
Popular Tags