Java tutorial
/* * Copyright (C) 2017 Marius de Wit * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package com.github.mariusdw.sqlsim.validator.sqlite; import com.github.mariusdw.sqlsim.validator.IValidator; import com.github.mariusdw.sqlsim.validator.ValidationException; import java.util.ArrayList; import java.util.BitSet; import java.util.List; import org.antlr.v4.runtime.ANTLRErrorListener; import org.antlr.v4.runtime.ANTLRInputStream; import org.antlr.v4.runtime.CommonTokenStream; import org.antlr.v4.runtime.Parser; import org.antlr.v4.runtime.RecognitionException; import org.antlr.v4.runtime.Recognizer; import org.antlr.v4.runtime.atn.ATNConfigSet; import org.antlr.v4.runtime.dfa.DFA; /** * * @author Marius de Wit */ public class SqLiteValidator implements IValidator { @Override public void validate(String query) throws ValidationException { SQLiteLexer lexer = new SQLiteLexer(new ANTLRInputStream(query)); SQLiteParser parser = new SQLiteParser(new CommonTokenStream(lexer)); List<String> errorStrs = new ArrayList<>(1); ANTLRErrorListener listener = new ANTLRErrorListener() { @Override public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) { String newMsg = "Error [line:" + line + "|char:" + charPositionInLine + "] " + msg; if (errorStrs.size() > 0) { newMsg += "\n" + errorStrs.get(0); errorStrs.set(0, newMsg); } else { errorStrs.add(newMsg); } } @Override public void reportAmbiguity(Parser parser, DFA dfa, int i, int i1, boolean bln, BitSet bitset, ATNConfigSet atncs) { //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public void reportAttemptingFullContext(Parser parser, DFA dfa, int i, int i1, BitSet bitset, ATNConfigSet atncs) { //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public void reportContextSensitivity(Parser parser, DFA dfa, int i, int i1, int i2, ATNConfigSet atncs) { //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } }; parser.addErrorListener(listener); try { parser.parse(); if (errorStrs.size() > 0) { throw new ValidationException("Validation error: " + errorStrs.get(0)); } } catch (RecognitionException e) { throw new ValidationException("Validation error:", e); } } }