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