1   package eu.fbk.knowledgestore.data;
2   
3   import javax.annotation.Nullable;
4   
5   import com.google.common.base.Preconditions;
6   
7   /**
8    * Signals a failure in parsing a string according to some formal grammar.
9    * <p>
10   * This exception is thrown when a condition, expression or query string, or any other string
11   * obeying some formal grammar, cannot be parsed for any reason (e.g., syntax error).
12   * </p>
13   */
14  public class ParseException extends IllegalArgumentException {
15  
16      private static final long serialVersionUID = 1L;
17  
18      private final String parsedString;
19  
20      /**
21       * Creates a new instance with the parsed string and the optional error message specified.
22       * 
23       * @param parsedString
24       *            a parsed string, for debugging purposes
25       * @param message
26       *            an optional error message, to which the supplied query is concatenated
27       */
28      public ParseException(final String parsedString, @Nullable final String message) {
29          this(parsedString, message, null);
30      }
31  
32      /**
33       * Creates a new instance with the query, optional error message and cause specified.
34       * 
35       * @param parsedString
36       *            a parsed string, for debugging purposes
37       * @param message
38       *            an optional error message, to which the supplied parsed string is concatenated
39       * @param cause
40       *            the optional cause of this exception
41       */
42      public ParseException(final String parsedString, @Nullable final String message,
43              @Nullable final Throwable cause) {
44  
45          super(message + (parsedString == null ? "" : "\nParsed string:\n\n" + parsedString), cause);
46  
47          Preconditions.checkNotNull(parsedString);
48          this.parsedString = parsedString;
49      }
50  
51      /**
52       * Returns the parsed string. This property is intended for debugging purposes.
53       * 
54       * @return the parsed string
55       */
56      public final String getParsedString() {
57          return this.parsedString;
58      }
59  
60  }