1 package org.andromda.translation.ocl.parser;
2
3 import java.util.StringTokenizer;
4
5 import org.apache.commons.lang.StringUtils;
6
7
8
9
10 public class OclParserException
11 extends RuntimeException
12 {
13 private static final long serialVersionUID = 3340016550152777823L;
14
15 private StringBuffer messageBuffer;
16 private int errorLine;
17 private int errorColumn;
18
19
20
21
22
23
24 public OclParserException(String message)
25 {
26 super();
27 if (StringUtils.isNotBlank(message))
28 {
29 extractErrorPosition(message);
30 }
31 }
32
33
34
35
36 public String getMessage()
37 {
38 int position = 0;
39 if (this.errorLine != -1)
40 {
41 String message = "line: " + errorLine + ' ';
42 this.messageBuffer.insert(0, message);
43 position = message.length();
44 }
45 if (this.errorColumn != -1)
46 {
47 String message = "column: " + errorColumn + ' ';
48 this.messageBuffer.insert(position, message);
49 position = position + message.length();
50 }
51 this.messageBuffer.insert(position, "--> ");
52 return this.messageBuffer.toString();
53 }
54
55
56
57
58
59
60
61 private void extractErrorPosition(String message)
62 {
63 this.messageBuffer = new StringBuffer();
64 if (StringUtils.isNotBlank(message) && message.charAt(0) == '[')
65 {
66
67 StringTokenizer tokenizer = new StringTokenizer(message.substring(1), ",]");
68
69 try
70 {
71 this.errorLine = Integer.parseInt(tokenizer.nextToken());
72 this.errorColumn = Integer.parseInt(tokenizer.nextToken());
73
74 this.messageBuffer.append(tokenizer.nextToken("").substring(2));
75 }
76 catch (NumberFormatException ex)
77 {
78
79 this.messageBuffer.append(message);
80 this.errorLine = -1;
81 this.errorColumn = -1;
82 }
83 }
84 else
85 {
86
87 this.messageBuffer.append(message);
88 this.errorLine = -1;
89 this.errorColumn = -1;
90 }
91 }
92 }