01: /**
02: * Copyright 2008 Jens Dietrich Licensed under the Apache License, Version 2.0 (the "License");
03: * you may not use this file except in compliance with the License.
04: * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
05: * Unless required by applicable law or agreed to in writing, software distributed under the
06: * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
07: * either express or implied. See the License for the specific language governing permissions
08: * and limitations under the License.
09: */package nz.org.take.nscript;
10:
11: import org.apache.log4j.Logger;
12: import org.apache.log4j.Priority;
13:
14: /**
15: * Contains some utilities used by parsers, mainly for logging and exception handling.
16: * @author <a href="http://www-ist.massey.ac.nz/JBDietrich/">Jens Dietrich</a>
17: */
18:
19: public class ParserSupport {
20:
21: public Logger LOGGER = Logger.getLogger(Parser.class);
22:
23: protected void error(int no, Object... description)
24: throws ScriptException {
25: StringBuffer buf = new StringBuffer();
26: buf.append("Parser exception at line ");
27: buf.append(no);
28: buf.append(' ');
29: for (Object t : description)
30: buf.append(t);
31: error(no, buf.toString());
32: }
33:
34: protected void error(int no, Exception x, Object... description)
35: throws ScriptException {
36: StringBuffer buf = new StringBuffer();
37: buf.append("Parser exception at line ");
38: buf.append(no);
39: buf.append(' ');
40: for (Object t : description)
41: buf.append(t);
42: error(no, buf.toString(), x);
43: }
44:
45: protected void error(int no, String message) throws ScriptException {
46: throw new ScriptException(message, no);
47: }
48:
49: protected void error(int no, Exception x, String message)
50: throws ScriptException {
51: throw new ScriptException(message, x, no);
52: }
53:
54: protected void debug(Object... strings) {
55: if (LOGGER.isDebugEnabled()) {
56: StringBuffer b = new StringBuffer();
57: for (Object s : strings)
58: b.append(s);
59: LOGGER.debug(b);
60: }
61: }
62:
63: protected void warn(Object... strings) {
64: if (LOGGER.isEnabledFor(Priority.WARN)) {
65: StringBuffer b = new StringBuffer();
66: for (Object s : strings)
67: b.append(s);
68: LOGGER.warn(b);
69: }
70: }
71:
72: }
|