1 16 package scriptella.driver.text; 17 18 import scriptella.core.EtlCancelledException; 19 import scriptella.expression.PropertiesSubstitutor; 20 import scriptella.spi.AbstractConnection; 21 import scriptella.spi.ParametersCallback; 22 import scriptella.util.IOUtils; 23 24 import java.io.BufferedReader ; 25 import java.io.BufferedWriter ; 26 import java.io.Closeable ; 27 import java.io.IOException ; 28 import java.io.Reader ; 29 import java.io.Writer ; 30 31 65 public class TextScriptExecutor implements Closeable { 66 private BufferedWriter out; 67 private boolean trim; 68 private String eol; 69 private PropertiesSubstitutor ps=new PropertiesSubstitutor(); 70 71 77 public TextScriptExecutor(Writer out, boolean trim, String eol) { 78 this.out = IOUtils.asBuffered(out); 79 this.trim = trim; 80 this.eol = eol; 81 } 82 83 89 public void execute(Reader reader, ParametersCallback pc, AbstractConnection.StatementCounter counter) { 90 ps.setParameters(pc); 91 BufferedReader r = IOUtils.asBuffered(reader); 92 try { 93 for (String line; (line = r.readLine()) != null;) { 94 EtlCancelledException.checkEtlCancelled(); 95 if (trim) { 96 line = line.trim(); 97 } 98 if (!trim || line.length()>0) { 100 try { 101 out.write(ps.substitute(line)); 102 out.write(eol); 103 counter.statements++; 104 } catch (IOException e) { 105 throw new TextProviderException("Failed writing to a text file", e); 106 } 107 } 108 } 109 } catch (IOException e) { 110 throw new TextProviderException("Failed reading a script file", e); 111 } 112 } 113 114 public void close() throws IOException { 115 IOUtils.closeSilently(out); 116 out = null; 117 ps=null; 118 } 119 } 120 | Popular Tags |