View Javadoc
1   /*
2    * Copyright 2005 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.
30   * 
31   * @author <a href="mailto:werner DOT guttmann AT gmx DOT net">Werner Guttmann</a>
32   * @version $Revision$ $Date: 2006-04-25 16:09:10 -0600 (Tue, 25 Apr 2006) $
33   */
34  public class XercesSerializer implements org.exolab.castor.xml.Serializer {
35  	private static final Log LOG = LogFactory.getLog(XercesSerializer.class);
36  	
37      /**
38       * Xerces XMLSerializer instance to use for serialization.
39       */
40      private Object _serializer;
41  
42      /**
43       * Creates an instance of this class.
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       * @see org.exolab.castor.xml.Serializer#setOutputCharStream(java.io.Writer)
58       * {@inheritDoc}
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          // _serializer.setOutputCharStream(out);
72      }
73  
74      /**
75       * @see org.exolab.castor.xml.Serializer#asDocumentHandler()
76       * {@inheritDoc}
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          // return _serializer.asDocumentHandler();
90      }
91  
92      /**
93       * @see org.exolab.castor.xml.Serializer
94       *      #setOutputFormat(org.exolab.castor.xml.OutputFormat)
95       * {@inheritDoc}
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         // _serializer.setOutputFormat((OutputFormat) format.getFormat());
111     }
112 
113     /**
114      * @see org.exolab.castor.xml.Serializer#setOutputByteStream(java.io.OutputStream)
115      * {@inheritDoc}
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         // _serializer.setOutputByteStream(output);
129     }
130 }