01: /*
02:
03: This software is OSI Certified Open Source Software.
04: OSI Certified is a certification mark of the Open Source Initiative.
05:
06: The license (Mozilla version 1.0) can be read at the MMBase site.
07: See http://www.MMBase.org/license
08:
09: */
10: package org.mmbase.util.transformers;
11:
12: import java.io.*;
13: import java.util.HashMap;
14: import java.util.Map;
15:
16: import org.mmbase.util.logging.*;
17:
18: /**
19:
20: * @author Michiel Meeuwissen
21: * @since MMBase-1.9
22: * @version $Id: BufferedReaderTransformer.java,v 1.7 2008/03/11 11:20:07 michiel Exp $
23: */
24:
25: public abstract class BufferedReaderTransformer extends
26: ReaderTransformer implements CharTransformer {
27:
28: private static Logger log = Logging
29: .getLoggerInstance(BufferedReaderTransformer.class);
30:
31: /**
32: * Override {@link #transform(PrintWriter, String)}
33: */
34: public final Writer transform(Reader r, Writer w) {
35: try {
36: BufferedReader br = new BufferedReader(r);
37: PrintWriter bw = new PrintWriter(new BufferedWriter(w));
38:
39: Status status = createNewStatus();
40: String line = br.readLine();
41: while (line != null) {
42: boolean nl = transform(bw, line, status);
43: line = br.readLine();
44: if (nl && line != null)
45: bw.write('\n');
46: }
47: br.close();
48: bw.flush();
49: } catch (java.io.IOException e) {
50: log.error(e.toString(), e);
51: }
52: return w;
53: }
54:
55: /**
56: * @param bw the writer to direct the output to
57: * @param line the input
58: * @param status this object could hold transformation status information. Or <code>null</code>
59: * @return
60: */
61: protected abstract boolean transform(PrintWriter bw, String line,
62: Status status);
63:
64: protected abstract Status createNewStatus();
65:
66: abstract static class Status {
67: }
68:
69: }
|