com.jpattern.orm.script.StreamParser Maven / Gradle / Ivy
package com.jpattern.orm.script;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import com.jpattern.orm.util.GenericWrapper;
/**
*
* @author Francesco Cina
*
* 01/lug/2011
*
* Parse a Stream identifying valid sql statement. For every statement found a call
* to the IParserCallback is performed.
*/
public class StreamParser implements Parser {
private final InputStream inputStream;
private final Charset charset;
private final boolean closeInputStream;
public StreamParser(InputStream inputStream, boolean closeInputStream) {
this(inputStream , closeInputStream, Charset.defaultCharset());
}
public StreamParser(InputStream inputStream, boolean closeInputStream, Charset charset) {
this.inputStream = inputStream;
this.closeInputStream = closeInputStream;
this.charset = charset;
}
@Override
public void parse(ParserCallback parserCallback) throws IOException {
parse(parserCallback, ";");
}
@Override
public void parse(ParserCallback parserCallback, String spearatorSymbol) throws IOException {
InputStreamReader inputStreamReader = null;
BufferedReader bufferedReader = null;
try {
inputStreamReader = new InputStreamReader(inputStream, charset );
bufferedReader = new BufferedReader(inputStreamReader);
findStatement(parserCallback, bufferedReader);
}
finally {
if (bufferedReader!=null) bufferedReader.close();
if (inputStreamReader!=null) inputStreamReader.close();
if (closeInputStream && inputStream!=null) inputStream.close();
}
}
private boolean findStatement(ParserCallback parserCallback, BufferedReader bufferedReader) throws IOException {
StringBuilder StringBuilder = new StringBuilder();
String line = null;
GenericWrapper apostrophes = new GenericWrapper(0);
while (true) {
if ( ! ((line = bufferedReader.readLine()) != null )) {
return false;
}
checkend(parserCallback, StringBuilder, line, apostrophes);
}
}
private void checkend(ParserCallback parserCallback, StringBuilder StringBuilder, String line, GenericWrapper apostrophesWrapper ) {
String trimmedline = line.trim();
if ( !trimmedline.isEmpty() && !trimmedline.startsWith("--") ) {
if ( line.contains(";") ) {
String[] splitted = line.split(";");
int position = 0;
for (int i=0 ; i
© 2015 - 2024 Weber Informatics LLC | Privacy Policy