1 package org.ashkelon.db; 2 6 7 import java.io.BufferedReader ; 8 import java.io.IOException ; 9 import java.io.InputStream ; 10 import java.io.InputStreamReader ; 11 import java.util.ArrayList ; 12 import java.util.List ; 13 14 import org.ashkelon.util.Logger; 15 import org.ashkelon.util.StringUtils; 16 17 24 public class ScriptParser 25 { 26 public ScriptParser() 27 { 28 } 29 30 public static List parse(String script) 31 { 32 return parse(script, true); 33 } 34 35 40 public static List parse(String script, boolean warn) 41 { 42 List commands = new ArrayList (); 43 try 44 { 45 InputStream 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 br = new BufferedReader (new InputStreamReader (is)); 53 String 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 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 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 clean_line(String 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 + " "; } 93 94 public static void main(String [] args) 95 { 96 114 } 115 116 } 117 | Popular Tags |