Coverage Report - net.sourceforge.addam.impexp.Script
 
Classes in this File Line Coverage Branch Coverage Complexity
Script
0%
0/30
0%
0/5
2.6
 
 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  0
 public class Script {
 41  
 
 42  0
     private String separator = ";";
 43  0
     private String text = "";
 44  0
     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  0
         this.separator = separator;
 54  0
     }
 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  0
         this.file = aFile;
 63  0
     }
 64  
 
 65  
     /**
 66  
      * adds to the SQL statement
 67  
      *
 68  
      * @param text
 69  
      */
 70  
     public void addText(String text) {
 71  0
         if (this.file != null) {
 72  0
             throw new BuildException("cannot provide character data when file already provided");
 73  
         }
 74  0
         this.text = text;
 75  0
     }
 76  
 
 77  
     /**
 78  
      * this is used to get the statements from the script
 79  
      *
 80  
      * @return
 81  
      */
 82  
     public List getStatements() {
 83  0
         ArrayList list = new ArrayList();
 84  0
         StringTokenizer tok = new StringTokenizer(this.getText(), separator);
 85  0
         while (tok.hasMoreTokens()) {
 86  0
             list.add(tok.nextToken());
 87  0
         }
 88  0
         return list;
 89  
     }
 90  
 
 91  
     private String getText() throws BuildException {
 92  0
         if (text.length() == 0 && this.file != null) {
 93  0
             StringBuffer buf = new StringBuffer();
 94  
             try {
 95  0
                 BufferedReader reader = new BufferedReader(new FileReader(this.file));
 96  0
                 for (String line = reader.readLine(); line != null;) {
 97  0
                     if (!line.startsWith("--")) {
 98  0
                         buf.append(line);
 99  0
                     }
 100  
                 }
 101  0
                 this.text = buf.toString();
 102  0
             } catch (IOException e) {
 103  0
                 throw new BuildException("unable to read file " + this.file, e);
 104  0
             }
 105  
         }
 106  0
         return text;
 107  
     }
 108  
 
 109  
 }