View Javadoc
1   /*
2    * Copyright 2006 Werner Guttmann
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Xerces-specific implementation of the Serializer interface, used for JDK 5
30   * only where Xerecs has been integrated with the core code base.
31   * 
32   * @author <a href="mailto:werner DOT guttmann AT gmx DOT net">Werner
33   *         Guttmann</a>
34   * @version $Revision: 6216 $ $Date: 2006-04-25 16:09:10 -0600 (Tue, 25 Apr
35   *          2006) $
36   * @since 1.1
37   */
38  public abstract class BaseXercesJDK5Serializer implements
39          org.exolab.castor.xml.Serializer {
40  
41      /**
42       * Logger instance for logging.
43       */
44      private final Log LOG = LogFactory.getLog(getClass());
45  
46      /**
47       * Xerces XMLSerializer instance to use for serialization.
48       */
49      private Object _serializer;
50  
51      /**
52       * Creates an instance of this class.
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       * @see org.exolab.castor.xml.Serializer#setOutputCharStream(java.io.Writer)
69       *      {@inheritDoc}
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       * @see org.exolab.castor.xml.Serializer#asDocumentHandler() {@inheritDoc}
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      * @see org.exolab.castor.xml.Serializer
103      *      #setOutputFormat(org.exolab.castor.xml.OutputFormat) {@inheritDoc}
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      * @see org.exolab.castor.xml.Serializer#setOutputByteStream(java.io.OutputStream)
122      *      {@inheritDoc}
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 }