View Javadoc
1   /*
2    * Copyright 2005 Werner Guttmann
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with the License. You may obtain a copy of the License at
6    *
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the License
10   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11   * or implied. See the License for the specific language governing permissions and limitations under
12   * the License.
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   * Xerces-specific implementation of the Serializer interface.
28   * 
29   * @author <a href="mailto:werner DOT guttmann AT gmx DOT net">Werner Guttmann</a>
30   * @version $Revision$ $Date: 2006-04-25 16:09:10 -0600 (Tue, 25 Apr 2006) $
31   */
32  public class XercesSerializer implements org.exolab.castor.xml.Serializer {
33    private static final Log LOG = LogFactory.getLog(XercesSerializer.class);
34  
35    /**
36     * Xerces XMLSerializer instance to use for serialization.
37     */
38    private Object _serializer;
39  
40    /**
41     * Creates an instance of this class.
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     * @see org.exolab.castor.xml.Serializer#setOutputCharStream(java.io.Writer) {@inheritDoc}
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      // _serializer.setOutputCharStream(out);
66    }
67  
68    /**
69     * @see org.exolab.castor.xml.Serializer#asDocumentHandler() {@inheritDoc}
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      // return _serializer.asDocumentHandler();
82    }
83  
84    /**
85     * @see org.exolab.castor.xml.Serializer #setOutputFormat(org.exolab.castor.xml.OutputFormat)
86     *      {@inheritDoc}
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      // _serializer.setOutputFormat((OutputFormat) format.getFormat());
100   }
101 
102   /**
103    * @see org.exolab.castor.xml.Serializer#setOutputByteStream(java.io.OutputStream) {@inheritDoc}
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     // _serializer.setOutputByteStream(output);
117   }
118 }