View Javadoc

1   //
2   // Copyright (c) 2004, International Decision Systems
3   /*
4    * Copyright (c) 2004 International Decision Systems, Inc.  All Rights Reserved.
5    *
6    * By using this Software, You acknowledge that the Software is a valuable asset
7    * and trade secret of either International Decision Systems, Inc. ("IDSI") or a
8    * third party supplier of IDSI and constitutes confidential and proprietary
9    * information.
10   *
11   * NEITHER IDSI NOR ANY AGENT OR PERSON ACTING FOR OR WITH IDSI HAS MADE OR DOES
12   * MAKE ANY STATEMENTS, AFFIRMATIONS, REPRESENTATIONS OR WARRANTIES WHATSOEVER
13   * TO YOU, WHETHER EXPRESS OR IMPLIED, AS TO THE SOFTWARE, THE QUALITY OR
14   * CONDITION OF THE SOFTWARE, OR THE OPERATING CHARACTERISTICS OR RELIABILITY OF
15   * THE SOFTWARE, OR ITS SUITABILITY FOR ANY GENERAL OR PARTICULAR PURPOSE, OR AS
16   * TO ANY OTHER MATTER WHATSOEVER; ANY AND ALL OTHER WARRANTIES INCLUDING
17   * WITHOUT LIMITATION ANY WARRANTIES IMPLIED BY LAW, SUCH AS THE IMPLIED
18   * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND TITLE,
19   * USE AND NON-INFRINGEMENT; ARE HEREBY EXPRESSLY DISCLAIMED AND EXCLUDED.
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 }