01: package org.andromda.translation.ocl.parser;
02:
03: import org.apache.commons.lang.StringUtils;
04:
05: import java.util.StringTokenizer;
06:
07: /**
08: * Retrieves information from the OCL parser exceptions in a more user friendly format.
09: */
10: public class OclParserException extends RuntimeException {
11: private StringBuffer messageBuffer;
12: private int errorLine;
13: private int errorColumn;
14:
15: /**
16: * Constructs an instance of OclParserException.
17: *
18: * @param message
19: */
20: public OclParserException(String message) {
21: super ();
22: if (StringUtils.isNotEmpty(message)) {
23: extractErrorPosition(message);
24: }
25: }
26:
27: /**
28: * @see java.lang.Throwable#getMessage()
29: */
30: public String getMessage() {
31: int position = 0;
32: if (this .errorLine != -1) {
33: String message = "line: " + errorLine + " ";
34: this .messageBuffer.insert(0, message);
35: position = message.length();
36: }
37: if (this .errorColumn != -1) {
38: String message = "column: " + errorColumn + " ";
39: this .messageBuffer.insert(position, message);
40: position = position + message.length();
41: }
42: this .messageBuffer.insert(position, "--> ");
43: return this .messageBuffer.toString();
44: }
45:
46: /**
47: * Extracts the error positioning from exception message, if possible. Assumes SableCC detail message format: "["
48: * <line>"," <col>"]" <error message>.
49: *
50: * @param message the mssage to exract.
51: */
52: private void extractErrorPosition(String message) {
53: this .messageBuffer = new StringBuffer();
54: if (message.charAt(0) == '[') {
55: // Positional data seems to be available
56: StringTokenizer tokenizer = new StringTokenizer(message
57: .substring(1), ",]");
58:
59: try {
60: this .errorLine = Integer
61: .parseInt(tokenizer.nextToken());
62: this .errorColumn = Integer.parseInt(tokenizer
63: .nextToken());
64:
65: this .messageBuffer.append(tokenizer.nextToken("")
66: .substring(2));
67: } catch (NumberFormatException ex) {
68: // No positional information
69: this .messageBuffer.append(message);
70: this .errorLine = -1;
71: this .errorColumn = -1;
72: }
73: } else {
74: // No positional information
75: this .messageBuffer.append(message);
76: this .errorLine = -1;
77: this .errorColumn = -1;
78: }
79: }
80: }
|