001: /*
002: * Copyright 2006 Werner Guttmann
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.exolab.castor.xml;
017:
018: import java.io.IOException;
019: import java.io.OutputStream;
020: import java.io.Writer;
021: import java.lang.reflect.Method;
022:
023: import org.apache.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025: import org.castor.util.Messages;
026: import org.xml.sax.DocumentHandler;
027:
028: /**
029: * Xerces-specific implementation of the Serializer interface, used for
030: * JDK 5 only where Xerecs has been integrated with the core code base.
031: *
032: * @author <a href="mailto:werner DOT guttmann AT gmx DOT net">Werner Guttmann</a>
033: * @version $Revision: 6216 $ $Date: 2006-04-25 16:09:10 -0600 (Tue, 25 Apr 2006) $
034: * @since 1.1
035: */
036: public class XercesJDK5Serializer implements
037: org.exolab.castor.xml.Serializer {
038:
039: private static final String PACKAGE_NAME = "com.sun.org.apache.xml.internal.serialize";
040:
041: /**
042: * Logger instance for logging.
043: */
044: private static final Log LOG = LogFactory
045: .getLog(XercesJDK5Serializer.class);
046:
047: /**
048: * Xerces XMLSerializer instance to use for serialization.
049: */
050: private Object _serializer;
051:
052: /**
053: * Creates an instance of this class.
054: */
055: public XercesJDK5Serializer() {
056: try {
057: _serializer = Class
058: .forName(PACKAGE_NAME + ".XMLSerializer")
059: .newInstance();
060: } catch (Exception except) {
061: throw new RuntimeException(Messages.format(
062: "conf.failedInstantiateSerializer", PACKAGE_NAME
063: + ".XMLSerializer", except));
064: }
065: }
066:
067: /**
068: * @see org.exolab.castor.xml.Serializer#setOutputCharStream(java.io.Writer)
069: * {@inheritDoc}
070: */
071: public void setOutputCharStream(final Writer out) {
072: Method method;
073: try {
074: method = _serializer.getClass()
075: .getMethod("setOutputCharStream",
076: new Class[] { Writer.class });
077: method.invoke(_serializer, new Object[] { out });
078: } catch (Exception e) {
079: String msg = "Problem invoking XMLSerializer.setOutputCharStream()";
080: LOG.error(msg, e);
081: throw new RuntimeException(msg + e.getMessage());
082: }
083: // _serializer.setOutputCharStream(out);
084: }
085:
086: /**
087: * @see org.exolab.castor.xml.Serializer#asDocumentHandler()
088: * {@inheritDoc}
089: */
090: public DocumentHandler asDocumentHandler() throws IOException {
091: Method method;
092: try {
093: method = _serializer.getClass().getMethod(
094: "asDocumentHandler", (Class[]) null);
095: return (DocumentHandler) method.invoke(_serializer,
096: (Object[]) null);
097: } catch (Exception e) {
098: String msg = "Problem invoking XMLSerializer.asDocumentHandler()";
099: LOG.error(msg, e);
100: throw new RuntimeException(msg + e.getMessage());
101: }
102: // return _serializer.asDocumentHandler();
103: }
104:
105: /**
106: * @see org.exolab.castor.xml.Serializer
107: * #setOutputFormat(org.exolab.castor.xml.OutputFormat)
108: * {@inheritDoc}
109: */
110: public void setOutputFormat(final OutputFormat format) {
111: Method method;
112: try {
113: Class outputFormatClass = Class.forName(PACKAGE_NAME
114: + ".OutputFormat");
115: method = _serializer.getClass().getMethod(
116: "setOutputFormat",
117: new Class[] { outputFormatClass });
118: method.invoke(_serializer, new Object[] { format
119: .getFormat() });
120: } catch (Exception e) {
121: String msg = "Problem invoking XMLSerializer.setOutputFormat()";
122: LOG.error(msg, e);
123: throw new RuntimeException(msg + e.getMessage());
124: }
125: // _serializer.setOutputFormat((OutputFormat) format.getFormat());
126: }
127:
128: /**
129: * @see org.exolab.castor.xml.Serializer#setOutputByteStream(java.io.OutputStream)
130: * {@inheritDoc}
131: */
132: public void setOutputByteStream(final OutputStream output) {
133: Method method;
134: try {
135: method = _serializer.getClass().getMethod(
136: "setOutputByteStream",
137: new Class[] { OutputStream.class });
138: method.invoke(_serializer, new Object[] { output });
139: } catch (Exception e) {
140: String msg = "Problem invoking XMLSerializer.setOutputByteStream()";
141: LOG.error(msg, e);
142: throw new RuntimeException(msg + e.getMessage());
143: }
144: // _serializer.setOutputByteStream(output);
145: }
146: }
|