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 public class XercesSerializer implements org.exolab.castor.xml.Serializer {
35 private static final Log LOG = LogFactory.getLog(XercesSerializer.class);
36
37
38
39
40 private Object _serializer;
41
42
43
44
45 public XercesSerializer() {
46 try {
47 _serializer =
48 Class.forName("org.apache.xml.serialize.XMLSerializer").newInstance();
49 } catch (Exception except) {
50 throw new RuntimeException(
51 Messages.format("conf.failedInstantiateSerializer",
52 "org.apache.xml.serialize.XMLSerializer", except));
53 }
54 }
55
56
57
58
59
60 public void setOutputCharStream(final Writer out) {
61 Method method;
62 try {
63 method = _serializer.getClass().getMethod(
64 "setOutputCharStream", new Class[] {Writer.class} );
65 method.invoke(_serializer, new Object[] { out });
66 } catch (Exception e) {
67 String msg = "Problem invoking XMLSerializer.setOutputCharStream()";
68 LOG.error(msg, e);
69 throw new RuntimeException(msg + e.getMessage());
70 }
71
72 }
73
74
75
76
77
78 public DocumentHandler asDocumentHandler() throws IOException {
79 Method method;
80 try {
81 method = _serializer.getClass().getMethod(
82 "asDocumentHandler", (Class[]) null);
83 return (DocumentHandler) method.invoke(_serializer, (Object[]) null);
84 } catch (Exception e) {
85 String msg = "Problem invoking XMLSerializer.asDocumentHandler()";
86 LOG.error(msg, e);
87 throw new RuntimeException(msg + e.getMessage());
88 }
89
90 }
91
92
93
94
95
96
97 public void setOutputFormat(final OutputFormat format) {
98 Method method;
99 try {
100 Class outputFormatClass =
101 Class.forName("org.apache.xml.serialize.OutputFormat");
102 method = _serializer.getClass().getMethod(
103 "setOutputFormat", new Class[] {outputFormatClass } );
104 method.invoke(_serializer, new Object[] { format.getFormat() });
105 } catch (Exception e) {
106 String msg = "Problem invoking XMLSerializer.setOutputFormat()";
107 LOG.error(msg, e);
108 throw new RuntimeException(msg + e.getMessage());
109 }
110
111 }
112
113
114
115
116
117 public void setOutputByteStream(final OutputStream output) {
118 Method method;
119 try {
120 method = _serializer.getClass().getMethod(
121 "setOutputByteStream", new Class[] {OutputStream.class} );
122 method.invoke(_serializer, new Object[] { output });
123 } catch (Exception e) {
124 String msg = "Problem invoking XMLSerializer.setOutputByteStream()";
125 LOG.error(msg, e);
126 throw new RuntimeException(msg + e.getMessage());
127 }
128
129 }
130 }