ctf: Put Antlr-specific exceptions in a separate class
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / internal / ctf / core / event / metadata / exceptions / CtfAntlrException.java
CommitLineData
c0804cf6
AM
1/*******************************************************************************
2 * Copyright (c) 2013 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are made
5 * available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Alexandre Montplaisir - Initial API and implementation
11 * Matthew Khouzam - Addition to have more descriptive errors
12 *******************************************************************************/
13
14package org.eclipse.linuxtools.internal.ctf.core.event.metadata.exceptions;
15
16import java.lang.reflect.Field;
17
18import org.antlr.runtime.MismatchedTokenException;
19import org.antlr.runtime.RecognitionException;
20import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
21import org.eclipse.linuxtools.ctf.parser.CTFLexer;
22
23/**
24 * CTF Reader exception but dealing with Antlr-specific parsing problems.
25 *
26 * It is separated from the main {@link CTFReaderException} - and is not part of
27 * the API - to isolate the Antlr-specific classes and avoid pushing that
28 * dependency to the users of this plugin.
29 *
30 * @author Matthew Khouzam
31 */
32public class CtfAntlrException extends CTFReaderException {
33
34 private static final long serialVersionUID = -7078624493350073777L;
35
36 private int fErrorLine = -1;
37 private String fFile = ""; //$NON-NLS-1$
38 private String fExpectingName = ""; //$NON-NLS-1$
39 private int fExpectedValue = -1;
40 private String fActualName = ""; //$NON-NLS-1$
41 private int fActualValue = -1;
42
43
44 /**
45 * Re-throw the exception but read its data
46 *
47 * @param e
48 * the previous recognition exception (Antlr specific)
49 */
50 public CtfAntlrException(RecognitionException e) {
51 super(e);
52 this.fErrorLine = e.line;
53 this.fFile = "metadata"; //$NON-NLS-1$ // we're in CTF, the only thing using antlr is metadata
54 }
55
56 /**
57 * Re-throw the exception but read its data
58 *
59 * @param e
60 * the previous recognition exception (Antlr specific)
61 */
62 public CtfAntlrException(MismatchedTokenException e){
63 super(e);
64 this.fErrorLine = e.line;
65 this.fFile = "metadata"; //$NON-NLS-1$ // we're in CTF, the only thing using antlr is metadata
66 parseMismatchedException(e);
67 }
68
69 private void parseMismatchedException(MismatchedTokenException m) {
70 // Iterate through the tokens that are hidden in the CTFLexer
71 // They are private static final int fields.
72 for (Field f : CTFLexer.class.getDeclaredFields()) {
73 f.setAccessible(true);
74 String name;
75 int value;
76 try {
77 name = f.getName();
78 final boolean isInt = (f.getType().isPrimitive());
79 if (isInt) {
80 value = ((Integer) f.get(null)).intValue();
81 if (value == m.expecting) {
82 this.fExpectingName = name;
83 this.fExpectedValue = value;
84 }
85 if (value == m.c) {
86 this.fActualName = name;
87 this.fActualValue = value;
88 }
89 }
90 } catch (NullPointerException e1) {
91 // Pokemon, gotta catch em all!
92 // actually useful since f may not have a
93 // value
94 } catch (IllegalArgumentException e1) {
95 // Catch these exceptions (reflexion)
96 } catch (IllegalAccessException e1) {
97 // Catch these exceptions (reflexion)
98 }
99 if (!this.fExpectingName.isEmpty() && !this.fActualName.isEmpty()) {
100 return;
101 }
102 }
103 }
104
105 @Override
106 public String getMessage() {
107 final String message = super.getMessage();
108 if (fErrorLine == -1) {
109 return message;
110 }
111 String expected = "" + this.fExpectedValue; //$NON-NLS-1$
112 String actual = "" + this.fActualValue; //$NON-NLS-1$
113 String newMessage = message.replaceAll(expected, this.fExpectingName);
114 newMessage = newMessage.replaceAll(actual, this.fActualName);
115 return newMessage + " at " + fFile + ":" + fErrorLine; //$NON-NLS-1$ //$NON-NLS-2$
116 }
117
118}
This page took 0.028949 seconds and 5 git commands to generate.