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