1 package org.ocltf.parser;
2
3 import java.util.StringTokenizer;
4
5 /***
6 * Retrieves information from the OCL parser exceptions in a more
7 * user friendly format.
8 */
9 public class OclParserException extends RuntimeException {
10
11 private StringBuffer detailMessage;
12 private int errorLine;
13 private int errorCol;
14
15 /***
16 * Constructs an instance of OclParserException.
17 *
18 * @param s
19 */
20 public OclParserException(String s) {
21 super();
22 if (s != null) {
23 extractErrorPosition(s);
24 }
25 }
26
27 /***
28 * @see java.lang.Throwable#getMessage()
29 */
30 public String getMessage() {
31 int position = 0;
32 if (errorLine != -1) {
33 String msg = "line: " + errorLine + " ";
34 detailMessage.insert(0, msg);
35 position = msg.length();
36 }
37 if (errorCol != -1) {
38 String msg = "column: " + errorCol + " ";
39 detailMessage.insert(position, msg);
40 position = position + msg.length();
41 }
42 detailMessage.insert(position, "--> ");
43 return detailMessage.toString();
44 }
45
46 /***
47 * The line of the error.
48 * @return int
49 */
50 public int getErrorLine() {
51 return errorLine;
52 }
53
54 /***
55 * The column of the error.
56 * @return int
57 */
58 public int getErrorCol() {
59 return errorCol;
60 }
61
62 /***
63 * Extract error position from detail message, if possible. Assumes SableCC
64 * detail message
65 * format: "[" <line> "," <col> "]" <error message>
66 *
67 * <p>Error line and column are stored in {@link #errorLine} and
68 * {@link #errorCol} so that they can be retrieved using
69 * {@link #getErrorLine} and {@link #getErrorCol}. The detail message without
70 * the position information is stored in {@link #detailMessage}</p>
71 * @param sDetailMessage
72 */
73 private void extractErrorPosition(String sDetailMessage) {
74 detailMessage = new StringBuffer();
75 if (sDetailMessage.charAt(0) == '[') {
76
77 StringTokenizer st =
78 new StringTokenizer(sDetailMessage.substring(1), ",]");
79
80 try {
81 errorLine = Integer.parseInt(st.nextToken());
82 errorCol = Integer.parseInt(st.nextToken());
83
84 detailMessage.append(st.nextToken("").substring(2));
85 } catch (NumberFormatException nfe) {
86 nfe.printStackTrace();
87
88 detailMessage.append(sDetailMessage);
89 errorLine = -1;
90 errorCol = -1;
91 }
92 } else {
93
94 detailMessage = detailMessage.append(sDetailMessage);
95 errorLine = -1;
96 errorCol = -1;
97 }
98 }
99 }