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 org.mmbase.util.logging.*;
14:
15: /**
16: * You need only to implement transform(String) you have the simplest
17: * kind of tranformer (which is not 'streamable'). The name becoming your class name.
18: *
19: * @author Michiel Meeuwissen
20: * @since MMBase-1.7
21: */
22:
23: public abstract class StringTransformer implements CharTransformer {
24: private static final Logger log = Logging
25: .getLoggerInstance(StringTransformer.class);
26:
27: // javadoc inherited
28: public abstract String transform(String r);
29:
30: // javadoc inherited
31: public final Writer transformBack(Reader r) {
32: return transformBack(r, new StringWriter());
33: }
34:
35: // javadoc inherited
36: public final Writer transform(Reader r) {
37: return transform(r, new StringWriter());
38: }
39:
40: // javadoc inherited
41: public String transformBack(String r) {
42: throw new UnsupportedOperationException(
43: "transformBack is not supported for this transformer");
44: }
45:
46: /**
47: * An implemention for tranform(Reader, Writer) based on transform(String).
48: * These functions can be used by extensions to implement transform and transformBack
49: */
50: public Writer transform(Reader r, Writer w) {
51: try {
52: StringWriter sw = new StringWriter();
53: while (true) {
54: int c = r.read();
55: if (c == -1)
56: break;
57: sw.write(c);
58: }
59: String result = transform(sw.toString());
60: w.write(result);
61: } catch (java.io.IOException e) {
62: log.error(e.toString());
63: log.debug(Logging.stackTrace(e));
64: }
65: return w;
66: }
67:
68: public Writer transformBack(Reader r, Writer w) {
69: try {
70: StringWriter sw = new StringWriter();
71: while (true) {
72: int c = r.read();
73: if (c == -1)
74: break;
75: sw.write(c);
76: }
77: String result = transformBack(sw.toString());
78: w.write(result);
79: } catch (java.io.IOException e) {
80: log.error(e.toString());
81: log.debug(Logging.stackTrace(e));
82: }
83: return w;
84: }
85: }
|