From: Jakub Skoczen Date: Mon, 17 Mar 2014 12:17:13 +0000 (+0100) Subject: Fix STDIN reading X-Git-Tag: v1.12~6 X-Git-Url: http://jsfdemo.indexdata.com/cgi-bin?a=commitdiff_plain;h=59200080b45184be8779f545d21ce87c4020ccf4;p=cql-java-moved-to-github.git Fix STDIN reading And remove null byte handling from the lexer --- diff --git a/src/main/java/org/z3950/zing/cql/CQLGenerator.java b/src/main/java/org/z3950/zing/cql/CQLGenerator.java index 77e4fce..f03235d 100644 --- a/src/main/java/org/z3950/zing/cql/CQLGenerator.java +++ b/src/main/java/org/z3950/zing/cql/CQLGenerator.java @@ -299,15 +299,12 @@ public class CQLGenerator { String configFile = args[0]; InputStream f = new FileInputStream(configFile); - if (f == null) - throw new FileNotFoundException(configFile); - Properties params = new Properties(); params.load(f); f.close(); for (int i = 1; i < args.length; i += 2) params.setProperty(args[i], args[i+1]); - + CQLGenerator generator = new CQLGenerator(params); CQLNode tree = generator.generate(); System.out.println(tree.toCQL()); diff --git a/src/main/java/org/z3950/zing/cql/CQLLexer.java b/src/main/java/org/z3950/zing/cql/CQLLexer.java index d7f3b77..d7adeac 100644 --- a/src/main/java/org/z3950/zing/cql/CQLLexer.java +++ b/src/main/java/org/z3950/zing/cql/CQLLexer.java @@ -5,6 +5,10 @@ */ package org.z3950.zing.cql; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; + /** * Implementation of the CQL lexical syntax analyzer * @author jakub @@ -26,7 +30,7 @@ public class CQLLexer implements CQLTokenizer { @Override public void move() { //eat whitespace - while (qi < ql && strchr(" \t\r\n\0", qs.charAt(qi))) + while (qi < ql && strchr(" \t\r\n", qs.charAt(qi))) qi++; //eof if (qi == ql) { @@ -98,8 +102,7 @@ public class CQLLexer implements CQLTokenizer { buf.setLength(0); //reset buffer while (qi < ql && !strchr("()/<>= \t\r\n", qs.charAt(qi))) { - if (qs.charAt(qi) != '\0') - buf.append(qs.charAt(qi)); + buf.append(qs.charAt(qi)); qi++; } val = buf.toString(); @@ -183,15 +186,20 @@ public class CQLLexer implements CQLTokenizer { if (args.length == 1) { cql = args[0]; } else { - byte[] bytes = new byte[10000]; + BufferedReader buff = new BufferedReader(new InputStreamReader(System.in)); try { - // Read in the whole of standard input in one go - int nbytes = System.in.read(bytes); - } catch (java.io.IOException ex) { + // read a single line of input + cql = buff.readLine(); + if (cql == null) { + System.err.println("Can't read query from stdin"); + System.exit(2); + return; + } + } catch (IOException ex) { System.err.println("Can't read query: " + ex.getMessage()); System.exit(2); + return; } - cql = new String(bytes); } CQLTokenizer lexer = new CQLLexer(cql, true); diff --git a/src/main/java/org/z3950/zing/cql/CQLParser.java b/src/main/java/org/z3950/zing/cql/CQLParser.java index 31bd7fb..a4f2fc8 100644 --- a/src/main/java/org/z3950/zing/cql/CQLParser.java +++ b/src/main/java/org/z3950/zing/cql/CQLParser.java @@ -1,10 +1,12 @@ package org.z3950.zing.cql; +import java.io.BufferedReader; import java.io.IOException; import java.util.Properties; import java.io.InputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; +import java.io.InputStreamReader; import java.io.Reader; import java.io.StringReader; import java.util.ArrayList; @@ -446,47 +448,54 @@ public class CQLParser { if (argv.size() == 1) { cql = (String) argv.get(0); } else { - byte[] bytes = new byte[10000]; + BufferedReader buff = new BufferedReader(new InputStreamReader(System.in)); try { - // Read in the whole of standard input in one go - int nbytes = System.in.read(bytes); + // read a single line of input + cql = buff.readLine(); + if (cql == null) { + System.err.println("Can't read query from stdin"); + System.exit(2); + return; + } } catch (IOException ex) { System.err.println("Can't read query: " + ex.getMessage()); System.exit(2); + return; } - cql = new String(bytes); } CQLParser parser = new CQLParser(compat); - CQLNode root = null; + CQLNode root; try { root = parser.parse(cql); } catch (CQLParseException ex) { System.err.println("Syntax error: " + ex.getMessage()); System.exit(3); + return; //compiler } catch (IOException ex) { System.err.println("Can't compile query: " + ex.getMessage()); System.exit(4); + return; //compiler } try { if (mode == 'c') { System.out.println(root.toCQL()); } else if (mode == 'p') { + try { InputStream f = new FileInputStream(pfile); - if (f == null) - throw new FileNotFoundException(pfile); - Properties config = new Properties(); config.load(f); f.close(); System.out.println(root.toPQF(config)); + } catch (IOException ex) { + System.err.println("Can't load PQF properties:" + + ex.getMessage()); + System.exit(5); + } } else { System.out.print(root.toXCQL()); } - } catch (IOException ex) { - System.err.println("Can't render query: " + ex.getMessage()); - System.exit(5); } catch (UnknownIndexException ex) { System.err.println("Unknown index: " + ex.getMessage()); System.exit(6); @@ -501,8 +510,8 @@ public class CQLParser { System.err.println("Unknown position: " + ex.getMessage()); System.exit(9); } catch (PQFTranslationException ex) { - // We catch all of this class's subclasses, so -- - throw new Error("can't get a PQFTranslationException"); + System.err.println("Cannot translate to PQF: " + ex.getMessage()); + System.exit(10); } } }