1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package net.sourceforge.addam.impexp;
22
23 import org.apache.tools.ant.BuildException;
24
25 import java.io.BufferedReader;
26 import java.io.File;
27 import java.io.FileReader;
28 import java.io.IOException;
29 import java.util.ArrayList;
30 import java.util.List;
31 import java.util.StringTokenizer;
32
33 /**
34 * Provides a placeholder for SQL; used in TableImport as pre/post statements.
35 * The SQL can be provided as a file, or as text embedded in the elements.
36 *
37 * @author TIM3
38 * @since Aug 2, 2004
39 */
40 public class Script {
41
42 private String separator = ";";
43 private String text = "";
44 private File file = null;
45
46 /**
47 * sets the string (usually a character) to separate multiple statements
48 * defaults to ";"
49 *
50 * @param separator
51 */
52 void setStatementSeparator(String separator) {
53 this.separator = separator;
54 }
55
56 /**
57 * the text to be executed is read from the given file
58 *
59 * @param aFile
60 */
61 void setFile(File aFile) {
62 this.file = aFile;
63 }
64
65 /**
66 * adds to the SQL statement
67 *
68 * @param text
69 */
70 public void addText(String text) {
71 if (this.file != null) {
72 throw new BuildException("cannot provide character data when file already provided");
73 }
74 this.text = text;
75 }
76
77 /**
78 * this is used to get the statements from the script
79 *
80 * @return
81 */
82 public List getStatements() {
83 ArrayList list = new ArrayList();
84 StringTokenizer tok = new StringTokenizer(this.getText(), separator);
85 while (tok.hasMoreTokens()) {
86 list.add(tok.nextToken());
87 }
88 return list;
89 }
90
91 private String getText() throws BuildException {
92 if (text.length() == 0 && this.file != null) {
93 StringBuffer buf = new StringBuffer();
94 try {
95 BufferedReader reader = new BufferedReader(new FileReader(this.file));
96 for (String line = reader.readLine(); line != null;) {
97 if (!line.startsWith("--")) {
98 buf.append(line);
99 }
100 }
101 this.text = buf.toString();
102 } catch (IOException e) {
103 throw new BuildException("unable to read file " + this.file, e);
104 }
105 }
106 return text;
107 }
108
109 }