View Javadoc
1   package org.andromda.translation.ocl.parser;
2   
3   import java.util.StringTokenizer;
4   
5   import org.apache.commons.lang.StringUtils;
6   
7   /**
8    * Retrieves information from the OCL parser exceptions in a more user friendly format.
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       * Constructs an instance of OclParserException.
21       *
22       * @param message
23       */
24      public OclParserException(String message)
25      {
26          super();
27          if (StringUtils.isNotBlank(message))
28          {
29              extractErrorPosition(message);
30          }
31      }
32  
33      /**
34       * @see Throwable#getMessage()
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       * Extracts the error positioning from exception message, if possible. Assumes SableCC detail message format: "["
57       * <line>"," <col>"]" <error message>.
58       *
59       * @param message the message to extract.
60       */
61      private void extractErrorPosition(String message)
62      {
63          this.messageBuffer = new StringBuffer();
64          if (StringUtils.isNotBlank(message) && message.charAt(0) == '[')
65          {
66              // Positional data seems to be available
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                  // No positional information
79                  this.messageBuffer.append(message);
80                  this.errorLine = -1;
81                  this.errorColumn = -1;
82              }
83          }
84          else
85          {
86              // No positional information
87              this.messageBuffer.append(message);
88              this.errorLine = -1;
89              this.errorColumn = -1;
90          }
91      }
92  }