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