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   */package org.exolab.castor.xml.wls8;
16  
17  import java.lang.reflect.Method;
18  
19  /**
20   * "Weblogic's refactored Xerces"-specific OutputFormat instance. Uses reflection to
21   * get the class and methods to avoid having dependencies towards Weblogic when Castor
22   * is not used in a Weblogic server.
23   * 
24   * @author Thierry Guerin
25   */
26  public class WeblogicXercesOutputFormat extends WeblogicXercesImplementation implements org.exolab.castor.xml.OutputFormat {
27  
28      private static Class outputFormatClass;
29      private static Method setDoctype;
30      private static Method setEncoding;
31      private static Method setVersion;
32      private static Method setIndenting;
33      private static Method setMethod;
34      private static Method setOmitDocumentType;
35      private static Method setOmitXMLDeclaration;
36      private static Method setPreserveSpace;
37      // the actual Weblogic implementation instance
38      private Object outputFormat;
39      
40      static {
41          // use reflection to get the methods
42  
43          // get the class
44          String wlsOutputFormatClassFqcn = "weblogic.apache.xml.serialize.OutputFormat";
45          try {
46              outputFormatClass = Class.forName(wlsOutputFormatClassFqcn);
47          }
48          catch (ClassNotFoundException e) {
49              handleStaticInitException("Could find class " + wlsOutputFormatClassFqcn, e);
50          }
51          // get the methods
52          // setDoctype
53          setDoctype = getMethod(outputFormatClass, "setDoctype", new Class[] { String.class, String.class });
54  
55          // setEncoding
56          setEncoding = getMethod(outputFormatClass, "setEncoding", new Class[] { String.class });
57  
58          // setVersion
59          setEncoding = getMethod(outputFormatClass, "setVersion", new Class[] { String.class });
60  
61          // setIndenting
62          setIndenting = getMethod(outputFormatClass, "setIndenting", new Class[] { boolean.class });
63          
64          // setMethod
65          setMethod = getMethod(outputFormatClass, "setMethod", new Class[] { String.class });
66          
67          // setOmitDocumentType
68          setOmitDocumentType = getMethod(outputFormatClass, "setOmitDocumentType", new Class[] { boolean.class });
69          
70          // setOmitXMLDeclaration
71          setOmitXMLDeclaration = getMethod(outputFormatClass, "setOmitXMLDeclaration", new Class[] { boolean.class });
72          
73          // setPreserveSpace
74          setPreserveSpace = getMethod(outputFormatClass, "setPreserveSpace", new Class[] { boolean.class });
75          
76      }
77  
78      /**
79       * Creates an instance of this class. 
80       */
81      public WeblogicXercesOutputFormat() {
82          try {
83              outputFormat = outputFormatClass.newInstance();
84          }
85          catch (InstantiationException e) {
86              throw new RuntimeException(e.toString()); // java 1.3, can't wrap using the 1.4 constructor
87          }
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 = {new Boolean(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 }