View Javadoc
1   /*
2    * Copyright 2006 Thierry Guerin
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.wls8;
15  
16  import java.io.IOException;
17  import java.io.OutputStream;
18  import java.io.Writer;
19  import java.lang.reflect.Method;
20  
21  /**
22   * Xerces-specific implementation of the Serializer interface. Uses reflection to get the class and
23   * methods to avoid having dependencies towards Weblogic when Castor is not used in a Weblogic
24   * server.
25   * 
26   * @author Thierry Guerin
27   */
28  public class WeblogicXercesSerializer extends WeblogicXercesImplementation
29      implements org.exolab.castor.xml.Serializer {
30  
31    private static Class _serializerClass;
32    private static Method _asDocumentHandler;
33    private static Method _setOutputByteStream;
34    private static Method _setOutputCharStream;
35    private static Method _setOutputFormat;
36  
37    /** Xerces XMLSerializer instance to use for serialization. */
38    private Object _serializer;
39    static {
40      // use reflection to get the methods
41  
42      // get the classes
43      Class weblogicOutputFormat = null;
44      try {
45        _serializerClass = Class.forName("weblogic.apache.xml.serialize.XMLSerializer");
46        weblogicOutputFormat = Class.forName("weblogic.apache.xml.serialize.OutputFormat");
47      } catch (ClassNotFoundException e) {
48        handleStaticInitException(e);
49      }
50      // get the methods
51      // asDocumentHandler
52      _asDocumentHandler = getMethod(_serializerClass, "asDocumentHandler", new Class[0]);
53  
54      // setOutputByteStream
55      Class[] parameterOutputStream = {OutputStream.class};
56      _setOutputByteStream =
57          getMethod(_serializerClass, "setOutputByteStream", parameterOutputStream);
58  
59      // setOutputCharStream
60      Class[] parameterWriter = {Writer.class};
61      _setOutputCharStream = getMethod(_serializerClass, "setOutputCharStream", parameterWriter);
62  
63      // setOutputByteStream
64      Class[] parameterOutputFormat = {weblogicOutputFormat};
65      _setOutputFormat = getMethod(_serializerClass, "setOutputFormat", parameterOutputFormat);
66  
67    }
68  
69    /**
70     * Creates an instance of this class.
71     */
72    public WeblogicXercesSerializer() {
73      try {
74        _serializer = _serializerClass.newInstance();
75      } catch (InstantiationException e) {
76        throw new RuntimeException(e.toString()); // java 1.3, can't wrap using the 1.4 constructor
77      } catch (IllegalAccessException e) {
78        throw new RuntimeException(e.toString()); // java 1.3, can't wrap using the 1.4 constructor
79      }
80    }
81  
82    /**
83     * @inheritDoc
84     * @deprecated
85     */
86    public org.xml.sax.DocumentHandler asDocumentHandler() throws IOException {
87      return (org.xml.sax.DocumentHandler) invoke(_asDocumentHandler, new Object[0]);
88    }
89  
90    /**
91     * @inheritDoc
92     */
93    public void setOutputByteStream(OutputStream output) {
94      Object[] params = {output};
95      invoke(_setOutputByteStream, params);
96    }
97  
98    /**
99     * @inheritDoc
100    */
101   public void setOutputCharStream(Writer out) {
102     Object[] params = {out};
103     invoke(_setOutputCharStream, params);
104   }
105 
106   /**
107    * @inheritDoc
108    */
109   public void setOutputFormat(org.exolab.castor.xml.OutputFormat format) {
110     Object[] params = {format.getFormat()};
111     invoke(_setOutputFormat, params);
112   }
113 
114   private Object invoke(Method method, Object[] params) {
115     return invoke(_serializer, method, params);
116   }
117 }