rcp: Move plugins to their own sub-directory
[deliverable/tracecompass.git] / org.eclipse.tracecompass.ctf.core / src / org / eclipse / tracecompass / internal / ctf / core / event / metadata / exceptions / CtfAntlrException.java
1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 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
14 package org.eclipse.tracecompass.internal.ctf.core.event.metadata.exceptions;
15
16 import java.lang.reflect.Field;
17
18 import org.antlr.runtime.MismatchedTokenException;
19 import org.antlr.runtime.RecognitionException;
20 import org.eclipse.tracecompass.ctf.core.CTFException;
21 import org.eclipse.tracecompass.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 CTFException} - and is not part of the
27 * API - to isolate the Antlr-specific classes and avoid pushing that dependency
28 * to the users of this plugin.
29 *
30 * @author Matthew Khouzam
31 */
32 public class CtfAntlrException extends CTFException {
33
34 private static final long serialVersionUID = -7078624493350073777L;
35
36 private final int fErrorLine;
37 private final String fFile;
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 private final int fCharPositionInLine;
44
45 /**
46 * Re-throw the exception but read its data
47 *
48 * @param e
49 * the previous recognition exception (Antlr specific)
50 */
51 public CtfAntlrException(MismatchedTokenException e) {
52 super(e);
53 fErrorLine = e.line;
54 fCharPositionInLine = e.charPositionInLine;
55 fFile = "metadata"; //$NON-NLS-1$ // we're in CTF, the only thing using antlr is metadata
56 parseMismatchedException(e);
57 }
58
59 /**
60 * Re-throw the exception but read its data
61 *
62 * @param e
63 * the previous recognition exception (Antlr specific)
64 */
65 public CtfAntlrException(RecognitionException e) {
66 super(e);
67 fErrorLine = e.line;
68 fCharPositionInLine = e.charPositionInLine;
69 fFile = "metadata"; //$NON-NLS-1$ // we're in CTF, the only thing using antlr is metadata
70 }
71
72 /**
73 * Re-throw the exception but read its data
74 *
75 * @param e
76 * the previous rewrite exception (Antlr specific)
77 */
78 public CtfAntlrException(Exception e) {
79 super(e);
80 fErrorLine = -1;
81 fCharPositionInLine = -1;
82 fFile = "metadata"; //$NON-NLS-1$ // we're in CTF, the only thing using antlr is metadata
83 }
84
85 private void parseMismatchedException(MismatchedTokenException m) {
86 // Iterate through the tokens that are hidden in the CTFLexer
87 // They are private static final int fields.
88 for (Field f : CTFLexer.class.getDeclaredFields()) {
89 f.setAccessible(true);
90 String name;
91 int value;
92 try {
93 name = f.getName();
94 final boolean isInt = (f.getType().isPrimitive());
95 if (isInt) {
96 value = ((Integer) f.get(null)).intValue();
97 if (value == m.expecting) {
98 this.fExpectingName = name;
99 this.fExpectedValue = value;
100 }
101 if (value == m.c) {
102 this.fActualName = name;
103 this.fActualValue = value;
104 }
105 }
106 } catch (NullPointerException e1) {
107 // Pokemon, gotta catch em all!
108 // actually useful since f may not have a
109 // value
110 } catch (IllegalArgumentException e1) {
111 // Catch these exceptions (reflexion)
112 } catch (IllegalAccessException e1) {
113 // Catch these exceptions (reflexion)
114 }
115 if (!this.fExpectingName.isEmpty() && !this.fActualName.isEmpty()) {
116 return;
117 }
118 }
119 }
120
121 @Override
122 public String getMessage() {
123 final String message = super.getMessage();
124 if (fErrorLine == -1) {
125 return message;
126 }
127 String expected = "" + this.fExpectedValue; //$NON-NLS-1$
128 String actual = "" + this.fActualValue; //$NON-NLS-1$
129 String newMessage = message.replaceAll(expected, this.fExpectingName);
130 newMessage = newMessage.replaceAll(actual, this.fActualName);
131 return newMessage + " at " + fFile + ":" + fErrorLine + ":" + fCharPositionInLine; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
132 }
133
134 }
This page took 0.037259 seconds and 5 git commands to generate.