KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > driver > ldap > ldif > SubstitutingLineReader


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.ldap.ldif;
17
18 import scriptella.expression.PropertiesSubstitutor;
19 import scriptella.spi.ParametersCallback;
20
21 import java.io.BufferedReader JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.Reader JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.List JavaDoc;
26
27 /**
28  * Buffered reader which overrides {@link #readLine()} to
29  * allow substitution of context variables.
30  *
31  * @author Fyodor Kupolov
32  * @version 1.0
33  * @see PropertiesSubstitutor
34  */

35 public class SubstitutingLineReader extends BufferedReader JavaDoc {
36     private final PropertiesSubstitutor substitutor;
37     private List JavaDoc<String JavaDoc> lines;
38     private int len;
39
40
41     /**
42      * Creates instance of buffered reader.
43      *
44      * @param in a reader to wrap.
45      * @param callback callback to use for variables substitution.
46      */

47     public SubstitutingLineReader(final Reader JavaDoc in, final ParametersCallback callback) {
48         super(in);
49         this.substitutor = new PropertiesSubstitutor(callback);
50     }
51
52     public String JavaDoc readLine() throws IOException JavaDoc {
53         String JavaDoc s = super.readLine();
54         if (s==null || s.length()==0) {
55             return s;
56         }
57         s = substitutor.substitute(s);
58         if (lines!=null) { //if track lines
59
lines.add(s); //remember the string
60
len+=s.length()+1;//and increase the len (\n is included)
61
}
62         return s;
63     }
64
65     /**
66      * @return read lines after calling the {@link #trackLines()}.
67      */

68     public String JavaDoc getTrackedLines() {
69         if (lines==null) { //Check if tracking has been switched on.
70
throw new IllegalStateException JavaDoc("Lines tracking must be switched on prior to calling this method");
71         }
72         StringBuilder JavaDoc sb = new StringBuilder JavaDoc(len);
73         for (String JavaDoc s : lines) {
74             sb.append(s).append('\n');
75         }
76         return sb.toString();
77     }
78
79     /**
80      * Starts to track lines obtained by {@link #readLine()}.
81      * <p>The previously tracked content is cleared.
82      * @see #getTrackedLines()
83      */

84     public void trackLines() {
85         if (lines!=null) {
86             lines.clear();
87         } else {
88             lines=new ArrayList JavaDoc<String JavaDoc>();
89         }
90         len=0;
91     }
92
93
94 }
95
Popular Tags