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   */package org.exolab.castor.xml.wls8;
14  
15  import java.lang.reflect.Method;
16  
17  /**
18   * "Weblogic's refactored Xerces"-specific OutputFormat instance. Uses reflection to get the class
19   * and methods to avoid having dependencies towards Weblogic when Castor is not used in a Weblogic
20   * server.
21   * 
22   * @author Thierry Guerin
23   */
24  public class WeblogicXercesOutputFormat extends WeblogicXercesImplementation
25      implements org.exolab.castor.xml.OutputFormat {
26  
27    private static Class outputFormatClass;
28    private static Method setDoctype;
29    private static Method setEncoding;
30    private static Method setVersion;
31    private static Method setIndenting;
32    private static Method setMethod;
33    private static Method setOmitDocumentType;
34    private static Method setOmitXMLDeclaration;
35    private static Method setPreserveSpace;
36    // the actual Weblogic implementation instance
37    private Object outputFormat;
38  
39    static {
40      // use reflection to get the methods
41  
42      // get the class
43      String wlsOutputFormatClassFqcn = "weblogic.apache.xml.serialize.OutputFormat";
44      try {
45        outputFormatClass = Class.forName(wlsOutputFormatClassFqcn);
46      } catch (ClassNotFoundException e) {
47        handleStaticInitException("Could find class " + wlsOutputFormatClassFqcn, e);
48      }
49      // get the methods
50      // setDoctype
51      setDoctype =
52          getMethod(outputFormatClass, "setDoctype", new Class[] {String.class, String.class});
53  
54      // setEncoding
55      setEncoding = getMethod(outputFormatClass, "setEncoding", new Class[] {String.class});
56  
57      // setVersion
58      setEncoding = getMethod(outputFormatClass, "setVersion", new Class[] {String.class});
59  
60      // setIndenting
61      setIndenting = getMethod(outputFormatClass, "setIndenting", new Class[] {boolean.class});
62  
63      // setMethod
64      setMethod = getMethod(outputFormatClass, "setMethod", new Class[] {String.class});
65  
66      // setOmitDocumentType
67      setOmitDocumentType =
68          getMethod(outputFormatClass, "setOmitDocumentType", new Class[] {boolean.class});
69  
70      // setOmitXMLDeclaration
71      setOmitXMLDeclaration =
72          getMethod(outputFormatClass, "setOmitXMLDeclaration", new Class[] {boolean.class});
73  
74      // setPreserveSpace
75      setPreserveSpace =
76          getMethod(outputFormatClass, "setPreserveSpace", new Class[] {boolean.class});
77  
78    }
79  
80    /**
81     * Creates an instance of this class.
82     */
83    public WeblogicXercesOutputFormat() {
84      try {
85        outputFormat = outputFormatClass.newInstance();
86      } catch (InstantiationException e) {
87        throw new RuntimeException(e.toString()); // java 1.3, can't wrap using the 1.4 constructor
88      } catch (IllegalAccessException e) {
89        throw new RuntimeException(e.toString()); // java 1.3, can't wrap using the 1.4 constructor
90      }
91    }
92  
93    public void setMethod(String method) {
94      Object[] params = {method};
95      invoke(setMethod, params);
96    }
97  
98    public Object getFormat() {
99      return outputFormat;
100   }
101 
102   public void setIndenting(boolean indent) {
103     // wrap boolean primitive in a Boolean object because the invoke method does the following:
104     // Individual parameters are automatically unwrapped to match primitive formal parameters.
105     Boolean[] params = {Boolean.valueOf(indent)};
106     invoke(setIndenting, params);
107   }
108 
109   public void setPreserveSpace(boolean preserveSpace) {
110     Boolean[] params = {Boolean.valueOf(preserveSpace)};
111     invoke(setPreserveSpace, params);
112   }
113 
114   public void setDoctype(String type1, String type2) {
115     Object[] params = {type1, type2};
116     invoke(setDoctype, params);
117   }
118 
119   public void setOmitXMLDeclaration(boolean omitXMLDeclaration) {
120     Boolean[] params = {Boolean.valueOf(omitXMLDeclaration)};
121     invoke(setOmitXMLDeclaration, params);
122   }
123 
124   public void setOmitDocumentType(boolean omitDocumentType) {
125     Boolean[] params = {Boolean.valueOf(omitDocumentType)};
126     invoke(setOmitDocumentType, params);
127   }
128 
129   public void setEncoding(String encoding) {
130     String[] params = {encoding};
131     invoke(setEncoding, params);
132   }
133 
134   public void setVersion(String version) {
135     String[] params = {version};
136     invoke(setEncoding, params);
137   }
138 
139   private Object invoke(Method method, Object[] params) {
140     return invoke(outputFormat, method, params);
141   }
142 
143 }