KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ashkelon > db > ScriptParser


1 package org.ashkelon.db;
2 /**
3  * Copyright UptoData Inc. 2001
4  * March 2001
5  */

6
7 import java.io.BufferedReader JavaDoc;
8 import java.io.IOException JavaDoc;
9 import java.io.InputStream JavaDoc;
10 import java.io.InputStreamReader JavaDoc;
11 import java.util.ArrayList JavaDoc;
12 import java.util.List JavaDoc;
13
14 import org.ashkelon.util.Logger;
15 import org.ashkelon.util.StringUtils;
16
17 /**
18  * A class that can parse a .sql script into a list of database statements
19  * (ignores -- comments, blank lines, and properly handle multi-line statements
20  * terminated by ';'
21  *
22  * @author Eitan Suez
23  */

24 public class ScriptParser
25 {
26    public ScriptParser()
27    {
28    }
29    
30    public static List JavaDoc parse(String JavaDoc script)
31    {
32      return parse(script, true);
33    }
34
35    /**
36     * @param script the fully qualified path & name of the .sql script file
37     * to parse
38     * @return the list of sql statements in the script file, as strings
39     */

40    public static List JavaDoc parse(String JavaDoc script, boolean warn)
41    {
42       List JavaDoc commands = new ArrayList JavaDoc();
43       try
44       {
45          InputStream JavaDoc is = ClassLoader.getSystemClassLoader().getResourceAsStream(script);
46          if (is == null)
47          {
48             if (warn)
49               Logger.getInstance().brief("could not find resource: "+script);
50             return commands;
51          }
52          BufferedReader JavaDoc br = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(is));
53          String JavaDoc line = "";
54          while ((line = br.readLine()) != null)
55          {
56             line = clean_line(line);
57             if (StringUtils.isBlank(line))
58                continue;
59             int end_idx = line.indexOf(";");
60             while (end_idx == -1)
61             {
62                String JavaDoc nextline = br.readLine();
63                if (nextline == null) break;
64                line += clean_line(nextline);
65                end_idx = line.indexOf(";");
66             }
67             line = line.substring(0, end_idx);
68             commands.add(line);
69          }
70          br.close();
71          is.close();
72       } catch (IOException JavaDoc ex)
73       {
74          Logger.getInstance().error("Unable to print usage!");
75          Logger.getInstance().error("IOException: "+ex.getMessage());
76       }
77       
78       return commands;
79     }
80     
81     private static String JavaDoc clean_line(String JavaDoc line)
82     {
83        line = line.trim();
84        if (StringUtils.isBlank(line) || line.startsWith("--"))
85           return "";
86        
87        int comment_idx = line.indexOf("--");
88        if (comment_idx > 0)
89           line = line.substring(0, comment_idx).trim();
90        
91        return line + " "; // add space because will likely be joined with next line
92
}
93     
94     public static void main(String JavaDoc[] args)
95     {
96        /*
97        Connection conn = null;
98        DBMgr mgr = DBMgr.getInstance();
99        try
100        {
101           List commands = ScriptParser.parse("org/ashkelon/ashkelon.sql");
102           conn = mgr.getConnection();
103           DBUtils.submitBatch(conn, commands);
104        } catch (SQLException ex)
105        {
106           DBUtils.logSQLException(ex);
107        }
108        finally
109        {
110           if (conn!=null)
111              mgr.releaseConnection(conn);
112        }
113         */

114     }
115
116 }
117
Popular Tags