KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > driver > text > TextScriptExecutor


1 /*
2  * Copyright 2006-2007 The Scriptella Project Team.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
25 import java.io.BufferedWriter JavaDoc;
26 import java.io.Closeable JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.Reader JavaDoc;
29 import java.io.Writer JavaDoc;
30
31 /**
32  * Writes a text content to the output and performs properties expansion.
33  * <p>This class is a simplified template engine similar to Velocity,
34  * but of course less powerful. Nevertheless this executor
35  * is generally faster than external template engines and does not require any additional libraries.
36  * <p><u>Example:</u></p>
37  * <b>Script:</b>
38  * <code><pre>
39  * $rownum;$name;$surname;${email.trim().replaceAll('@','_at_')}
40  * </pre></code>
41  * <b>Parameters:</b>
42  * <table border=1>
43  * <tr>
44  * <th>rownum</th>
45  * <th>name</th>
46  * <th>surname</th>
47  * <th>email</th>
48  * </tr>
49  * <tr>
50  * <td>1</td>
51  * <td>John</td>
52  * <td>G</td>
53  * <td> john@nosuchhost.com</td>
54  * </tr>
55  * </table>
56  * <b>Result:</b>
57  * <code><pre>
58  * 1;John;G;john_at_nosuchhost.com
59  * </pre></code>
60  * @see PropertiesSubstitutor
61  *
62  * @author Fyodor Kupolov
63  * @version 1.0
64  */

65 public class TextScriptExecutor implements Closeable JavaDoc {
66     private BufferedWriter JavaDoc out;
67     private boolean trim;
68     private String JavaDoc eol;
69     private PropertiesSubstitutor ps=new PropertiesSubstitutor();
70
71     /**
72      * Creates an executor.
73      * @param out writer for output.
74      * @param trim true if each line in the output file should be trimmed
75      * @param eol EOL separator.
76      */

77     public TextScriptExecutor(Writer JavaDoc out, boolean trim, String JavaDoc eol) {
78         this.out = IOUtils.asBuffered(out);
79         this.trim = trim;
80         this.eol = eol;
81     }
82
83     /**
84      * Parses a script from read, expands properties and produces the output.
85      * @param reader script content.
86      * @param pc parameters for substitution.
87      * @param counter statements counter.
88      */

89     public void execute(Reader JavaDoc reader, ParametersCallback pc, AbstractConnection.StatementCounter counter) {
90         ps.setParameters(pc);
91         BufferedReader JavaDoc r = IOUtils.asBuffered(reader);
92         try {
93             for (String JavaDoc line; (line = r.readLine()) != null;) {
94                 EtlCancelledException.checkEtlCancelled();
95                 if (trim) {
96                     line = line.trim();
97                 }
98                 //If trimming is disabled (keeping format) or if line is not empty
99
if (!trim || line.length()>0) {
100                     try {
101                         out.write(ps.substitute(line));
102                         out.write(eol);
103                         counter.statements++;
104                     } catch (IOException JavaDoc e) {
105                         throw new TextProviderException("Failed writing to a text file", e);
106                     }
107                 }
108             }
109         } catch (IOException JavaDoc e) {
110             throw new TextProviderException("Failed reading a script file", e);
111         }
112     }
113
114     public void close() throws IOException JavaDoc {
115         IOUtils.closeSilently(out);
116         out = null;
117         ps=null;
118     }
119 }
120
Popular Tags