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:
14: import org.mmbase.util.externalprocess.CommandLauncher;
15: import org.mmbase.util.logging.*;
16:
17: /**
18: * If you want to transform a Reader stream by the use of an external command, than you can extend
19: * this class. Implement the 'getCommand' function.
20: *
21: * @author Michiel Meeuwissen
22: * @since MMBase-1.7
23: */
24:
25: public abstract class AbstractCommandStringTransformer extends
26: StringTransformer implements CharTransformer {
27: private static Logger log = Logging
28: .getLoggerInstance(AbstractCommandStringTransformer.class);
29:
30: protected abstract String[] getCommand();
31:
32: // javadoc inherited
33: public final String transform(String s) {
34: try {
35: String encoding = System.getProperty("file.encoding");
36: CommandLauncher launcher = new CommandLauncher(
37: "Transformer");
38: ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
39: InputStream inputStream = new ByteArrayInputStream(s
40: .getBytes(encoding));
41: ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
42: launcher.execute(getCommand());
43: launcher.waitAndWrite(inputStream, outputStream,
44: errorStream);
45: return new String(outputStream.toByteArray(), encoding)
46: + new String(errorStream.toByteArray(), encoding);
47: } catch (UnsupportedEncodingException uee) {
48: log.error(uee.toString());
49: } catch (org.mmbase.util.externalprocess.ProcessException pe) {
50: log.error(pe.toString());
51: }
52: return s;
53: }
54:
55: public String toString() {
56: return getCommand()[0].toUpperCase();
57: }
58: }
|