1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.exolab.castor.xml;
15
16 import java.io.IOException;
17 import java.io.OutputStream;
18 import java.io.Writer;
19 import java.lang.reflect.Method;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.castor.core.util.Messages;
24 import org.xml.sax.DocumentHandler;
25
26
27
28
29
30
31
32
33
34 public abstract class BaseXercesJDK5Serializer implements org.exolab.castor.xml.Serializer {
35
36
37
38
39 private final Log LOG = LogFactory.getLog(getClass());
40
41
42
43
44 private Object _serializer;
45
46
47
48
49 public BaseXercesJDK5Serializer() {
50 try {
51 _serializer = Class.forName(getPackageName() + ".XMLSerializer").newInstance();
52 } catch (Exception except) {
53 throw new RuntimeException(Messages.format("conf.failedInstantiateSerializer",
54 getPackageName() + ".XMLSerializer", except));
55 }
56 }
57
58 protected abstract String getPackageName();
59
60
61
62
63 public void setOutputCharStream(final Writer out) {
64 Method method;
65 try {
66 method = _serializer.getClass().getMethod("setOutputCharStream", new Class[] {Writer.class});
67 method.invoke(_serializer, new Object[] {out});
68 } catch (Exception e) {
69 String msg = "Problem invoking XMLSerializer.setOutputCharStream()";
70 LOG.error(msg, e);
71 throw new RuntimeException(msg + e.getMessage());
72 }
73 }
74
75
76
77
78 public DocumentHandler asDocumentHandler() throws IOException {
79 Method method;
80 try {
81 method = _serializer.getClass().getMethod("asDocumentHandler", (Class[]) null);
82 return (DocumentHandler) method.invoke(_serializer, (Object[]) null);
83 } catch (Exception e) {
84 String msg = "Problem invoking XMLSerializer.asDocumentHandler()";
85 LOG.error(msg, e);
86 throw new RuntimeException(msg + e.getMessage());
87 }
88 }
89
90
91
92
93
94 public void setOutputFormat(final OutputFormat format) {
95 Method method;
96 try {
97 Class outputFormatClass = Class.forName(getPackageName() + ".OutputFormat");
98 method = _serializer.getClass().getMethod("setOutputFormat", new Class[] {outputFormatClass});
99 method.invoke(_serializer, new Object[] {format.getFormat()});
100 } catch (Exception e) {
101 String msg = "Problem invoking XMLSerializer.setOutputFormat()";
102 LOG.error(msg, e);
103 throw new RuntimeException(msg + e.getMessage());
104 }
105 }
106
107
108
109
110 public void setOutputByteStream(final OutputStream output) {
111 Method method;
112 try {
113 method =
114 _serializer.getClass().getMethod("setOutputByteStream", new Class[] {OutputStream.class});
115 method.invoke(_serializer, new Object[] {output});
116 } catch (Exception e) {
117 String msg = "Problem invoking XMLSerializer.setOutputByteStream()";
118 LOG.error(msg, e);
119 throw new RuntimeException(msg + e.getMessage());
120 }
121 }
122 }