View Javadoc
1   /*
2    * Copyright 2006 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, used for JDK 5 only where Xerecs has
28   * been integrated with the core code base.
29   * 
30   * @author <a href="mailto:werner DOT guttmann AT gmx DOT net">Werner Guttmann</a>
31   * @version $Revision: 6216 $ $Date: 2006-04-25 16:09:10 -0600 (Tue, 25 Apr 2006) $
32   * @since 1.1
33   */
34  public abstract class BaseXercesJDK5Serializer implements org.exolab.castor.xml.Serializer {
35  
36    /**
37     * Logger instance for logging.
38     */
39    private final Log LOG = LogFactory.getLog(getClass());
40  
41    /**
42     * Xerces XMLSerializer instance to use for serialization.
43     */
44    private Object _serializer;
45  
46    /**
47     * Creates an instance of this class.
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     * @see org.exolab.castor.xml.Serializer#setOutputCharStream(java.io.Writer) {@inheritDoc}
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     * @see org.exolab.castor.xml.Serializer#asDocumentHandler() {@inheritDoc}
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     * @see org.exolab.castor.xml.Serializer #setOutputFormat(org.exolab.castor.xml.OutputFormat)
92     *      {@inheritDoc}
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    * @see org.exolab.castor.xml.Serializer#setOutputByteStream(java.io.OutputStream) {@inheritDoc}
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 }