01: package vicazh.hyperpool;
02:
03: import java.beans.*;
04: import java.io.*;
05: import java.util.logging.*;
06: import javax.xml.transform.*;
07:
08: /**
09: * The object writer
10: *
11: * @author Victor Zhigunov
12: * @version 0.4.0
13: */
14: public class Writer extends Processor implements ExceptionListener {
15: /**
16: * @param tout
17: * xsl result transformer
18: */
19: public Writer(String tout) throws TransformerException {
20: super (tout);
21: }
22:
23: static protected void put(byte[] b, File out) throws IOException {
24: File d = out.getParentFile();
25: if (d != null && !d.exists())
26: d.mkdirs();
27: put(b, new BufferedOutputStream(new FileOutputStream(out)));
28: }
29:
30: static private void put(byte[] b, OutputStream s)
31: throws IOException {
32: s.write(b);
33: s.flush();
34: s.close();
35: }
36:
37: public byte[] get(Object object) throws TransformerException {
38: ByteArrayOutputStream out = new ByteArrayOutputStream();
39: XMLEncoder e = new XMLEncoder(out);
40: e.setExceptionListener(this );
41: e.writeObject(object);
42: e.flush();
43: e.close();
44: byte[] b = out.toByteArray();
45: Start.logger.finest(new String(b));
46: return transform(b);
47: }
48:
49: /**
50: * Write object to the xml result file
51: *
52: * @param object
53: * object
54: * @param out
55: * xml result file
56: */
57: public void putObject(Object object, File out) throws IOException,
58: TransformerException {
59: put(get(object), out);
60: }
61:
62: public void exceptionThrown(Exception e) {
63: Start.logger.log(Level.SEVERE, e.getMessage(), e);
64: }
65: }
|