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.lang.reflect.Method;
19  
20  /**
21   * Xerces-specific OutputFormat instance.
22   * 
23   * @author <a href="mailto:werner DOT guttmann AT gmx DOT net">Werner Guttmann</a>
24   * @version $Revision: 7951 $ $Date: 2006-04-25 16:09:10 -0600 (Tue, 25 Apr 2006) $
25   */
26  public class BaseXercesOutputFormat implements OutputFormat {
27  
28      protected Object _outputFormat;
29      
30      public Object getFormat() {
31          return _outputFormat;
32      }
33      
34      public void setMethod(final String method) {
35      	Method aMethod;
36  		try {
37  			aMethod = _outputFormat.getClass().getMethod(
38                      "setMethod", new Class[] {String.class} );
39  	    	aMethod.invoke(_outputFormat, new Object[] { method });
40  		} catch (Exception e) {
41              throw new RuntimeException("Problem invoking OutputFormat.setMethod()", e); 
42  		}
43      }
44  
45      public void setIndenting(final boolean indent) {
46      	Method method;
47  		try {
48  			method = _outputFormat.getClass().getMethod(
49                      "setIndenting", new Class[] {boolean.class} );
50  	    	method.invoke(_outputFormat, new Object[] { Boolean.valueOf(indent) });
51  		} catch (Exception e) {
52              throw new RuntimeException("Problem invoking OutputFormat.setIndenting()", e); 
53  		}
54      }
55  
56      public void setPreserveSpace(final boolean preserveSpace) {
57      	Method method;
58  		try {
59  			method = _outputFormat.getClass().getMethod(
60                      "setPreserveSpace", new Class[] {boolean.class} );
61  	    	method.invoke(_outputFormat, new Object[] { Boolean.valueOf(preserveSpace) });
62  		} catch (Exception e) {
63              throw new RuntimeException("Problem invoking OutputFormat.setPreserveSpace()", e); 
64  		}
65      }
66  
67      public void setDoctype(final String type1, final String type2) {
68      	Method method;
69  		try {
70  			method = _outputFormat.getClass().getMethod(
71                      "setDoctype", new Class[] {String.class, String.class} );
72  	    	method.invoke(_outputFormat, new Object[] { type1, type2});
73  		} catch (Exception e) {
74              throw new RuntimeException("Problem invoking OutputFormat.setDoctype()", e); 
75  		}
76      }
77  
78      public void setOmitXMLDeclaration(final boolean omitXMLDeclaration) {
79      	Method method;
80  		try {
81  			method = _outputFormat.getClass().getMethod(
82                      "setOmitXMLDeclaration", new Class[] {boolean.class} );
83  	    	method.invoke(_outputFormat, new Object[] { Boolean.valueOf(omitXMLDeclaration) });
84  		} catch (Exception e) {
85              throw new RuntimeException("Problem invoking OutputFormat.setOmitXMLDeclaration()", e); 
86  		}
87      }
88  
89      public void setOmitDocumentType(final boolean omitDocumentType) {
90          Method method;
91          try {
92              method = _outputFormat.getClass().getMethod("setOmitDocumentType",
93                      boolean.class);
94              method.invoke(_outputFormat, Boolean.valueOf(omitDocumentType));
95          } catch (Exception e) {
96              throw new RuntimeException("Problem invoking OutputFormat.setOmitDocumentType()", e);
97          }
98      }
99  
100     public void setEncoding(final String encoding) {
101         Method method;
102         try {
103             method = _outputFormat.getClass().getMethod("setEncoding",
104                     String.class);
105             method.invoke(_outputFormat, encoding);
106         } catch (Exception e) {
107             throw new RuntimeException("Problem invoking OutputFormat.setEncoding()", e);
108         }
109     }
110     
111     public void setVersion(final String version) {
112         Method method;
113         try {
114             method = _outputFormat.getClass().getMethod(
115                     "setVersion", new Class[] {String.class} );
116             method.invoke(_outputFormat, new Object[] { version });
117         } catch (Exception e) {
118             throw new RuntimeException("Problem invoking OutputFormat.setVersion()", e); 
119         }
120     }
121 
122 }