001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019
020package lucee.runtime.sql.old;
021
022import java.io.DataInputStream;
023import java.io.FileInputStream;
024import java.io.FileNotFoundException;
025import java.io.InputStream;
026import java.util.Vector;
027
028// Referenced classes of package Zql:
029//            ZqlJJParser, ParseException, ZUtils, ZStatement, 
030//            ZExp
031
032public final class ZqlParser
033{
034
035    public static void main(String args[])
036        throws ParseException
037    {
038        ZqlParser zqlparser = null;
039        if(args.length < 1)
040        {
041            System.out.println("/* Reading from stdin (exit; to finish) */");
042            zqlparser = new ZqlParser(System.in);
043        } else
044        {
045            try
046            {
047                zqlparser = new ZqlParser(new DataInputStream(new FileInputStream(args[0])));
048            }
049            catch(FileNotFoundException filenotfoundexception)
050            {
051                System.out.println("/* File " + args[0] + " not found. Reading from stdin */");
052                zqlparser = new ZqlParser(System.in);
053            }
054        }
055        if(args.length > 0)
056            System.out.println("/* Reading from " + args[0] + "*/");
057        for(ZStatement zstatement = null; (zstatement = zqlparser.readStatement()) != null;)
058            System.out.println(zstatement.toString() + ";");
059
060        System.out.println("exit;");
061        System.out.println("/* Parse Successful */");
062    }
063
064    public ZqlParser(InputStream inputstream)
065    {
066        _parser = null;
067        initParser(inputstream);
068    }
069
070    public ZqlParser()
071    {
072        _parser = null;
073    }
074
075    public void initParser(InputStream inputstream)
076    {
077        if(_parser == null)
078            _parser = new ZqlJJParser(inputstream);
079        else
080            _parser.ReInit(inputstream);
081    }
082
083    public void addCustomFunction(String s, int i)
084    {
085        ZUtils.addCustomFunction(s, i);
086    }
087
088    public ZStatement readStatement()
089        throws ParseException
090    {
091        if(_parser == null)
092            throw new ParseException("Parser not initialized: use initParser(InputStream);");
093        return _parser.SQLStatement();
094    }
095
096    public Vector readStatements()
097        throws ParseException
098    {
099        if(_parser == null)
100            throw new ParseException("Parser not initialized: use initParser(InputStream);");
101        return _parser.SQLStatements();
102    }
103
104    public ZExp readExpression()
105        throws ParseException
106    {
107        if(_parser == null)
108            throw new ParseException("Parser not initialized: use initParser(InputStream);");
109        return _parser.SQLExpression();
110    }
111
112    ZqlJJParser _parser;
113}