Index: main/java/org/exolab/castor/xml/XercesJDK5Serializer.java =================================================================== --- main/java/org/exolab/castor/xml/XercesJDK5Serializer.java (Revision 0) +++ main/java/org/exolab/castor/xml/XercesJDK5Serializer.java (Revision 0) @@ -0,0 +1,136 @@ +/* + * Copyright 2006 Werner Guttmann + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.exolab.castor.xml; + +import java.io.IOException; +import java.io.OutputStream; +import java.io.Writer; +import java.lang.reflect.Method; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.castor.util.Messages; +import org.xml.sax.DocumentHandler; + +/** + * Xerces-specific implementation of the Serializer interface, used for + * JDK 5 only where Xerecs has been integrated with the core code base. + * + * @author Werner Guttmann + * @version $Revision: 6216 $ $Date: 2006-04-25 16:09:10 -0600 (Tue, 25 Apr 2006) $ + * @since 1.1 + */ +public class XercesJDK5Serializer implements org.exolab.castor.xml.Serializer { + + /** + * Logger instance for logging. + */ + private static final Log LOG = LogFactory.getLog(XercesJDK5Serializer.class); + + /** + * Xerces XMLSerializer instance to use for serialization. + */ + private Object _serializer; + + /** + * Creates an instance of this class. + */ + public XercesJDK5Serializer() { + try { + _serializer = + Class.forName("com.sun.org.apache.xml.serialize.XMLSerializer").newInstance(); + } catch (Exception except) { + throw new RuntimeException( + Messages.format("conf.failedInstantiateSerializer", + "com.sun.org.apache.xml.serialize.XMLSerializer", except)); + } + } + + /** + * @see org.exolab.castor.xml.Serializer#setOutputCharStream(java.io.Writer) + * {@inheritDoc} + */ + public void setOutputCharStream(final Writer out) { + Method method; + try { + method = _serializer.getClass().getMethod( + "setOutputCharStream", new Class[] {Writer.class} ); + method.invoke(_serializer, new Object[] { out }); + } catch (Exception e) { + String msg = "Problem invoking XMLSerializer.setOutputCharStream()"; + LOG.error(msg, e); + throw new RuntimeException(msg + e.getMessage()); + } + // _serializer.setOutputCharStream(out); + } + + /** + * @see org.exolab.castor.xml.Serializer#asDocumentHandler() + * {@inheritDoc} + */ + public DocumentHandler asDocumentHandler() throws IOException { + Method method; + try { + method = _serializer.getClass().getMethod( + "asDocumentHandler", (Class[]) null); + return (DocumentHandler) method.invoke(_serializer, (Object[]) null); + } catch (Exception e) { + String msg = "Problem invoking XMLSerializer.asDocumentHandler()"; + LOG.error(msg, e); + throw new RuntimeException(msg + e.getMessage()); + } + // return _serializer.asDocumentHandler(); + } + + /** + * @see org.exolab.castor.xml.Serializer + * #setOutputFormat(org.exolab.castor.xml.OutputFormat) + * {@inheritDoc} + */ + public void setOutputFormat(final OutputFormat format) { + Method method; + try { + Class outputFormatClass = + Class.forName("com.sun.org.apache.xml.serialize.OutputFormat"); + method = _serializer.getClass().getMethod( + "setOutputFormat", new Class[] {outputFormatClass } ); + method.invoke(_serializer, new Object[] { format.getFormat() }); + } catch (Exception e) { + String msg = "Problem invoking XMLSerializer.setOutputFormat()"; + LOG.error(msg, e); + throw new RuntimeException(msg + e.getMessage()); + } + // _serializer.setOutputFormat((OutputFormat) format.getFormat()); + } + + /** + * @see org.exolab.castor.xml.Serializer#setOutputByteStream(java.io.OutputStream) + * {@inheritDoc} + */ + public void setOutputByteStream(final OutputStream output) { + Method method; + try { + method = _serializer.getClass().getMethod( + "setOutputByteStream", new Class[] {OutputStream.class} ); + method.invoke(_serializer, new Object[] { output }); + } catch (Exception e) { + String msg = "Problem invoking XMLSerializer.setOutputByteStream()"; + LOG.error(msg, e); + throw new RuntimeException(msg + e.getMessage()); + } + // _serializer.setOutputByteStream(output); + } +} Index: main/java/org/exolab/castor/xml/XercesJDK5OutputFormat.java =================================================================== --- main/java/org/exolab/castor/xml/XercesJDK5OutputFormat.java (Revision 0) +++ main/java/org/exolab/castor/xml/XercesJDK5OutputFormat.java (Revision 0) @@ -0,0 +1,189 @@ +/* + * Copyright 2006 Werner Guttmann + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.exolab.castor.xml; + +import java.lang.reflect.Method; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.castor.util.Messages; + +/** + * Xerces-specific OutputFormat instance, used with JDK 5.0 only. + * + * @author Werner Guttmann + * @version $Revision: 6216 $ $Date: 2006-04-25 16:09:10 -0600 (Tue, 25 Apr 2006) $ + */ +public class XercesJDK5OutputFormat implements OutputFormat { + + /** + * Logger instance used for logging + */ + private static final Log LOG = LogFactory.getLog(XercesSerializer.class); + + /** + * Xerces-specific OutputFormat instance + */ + private Object _outputFormat; + + /** + * Creates an instance of this class. + */ + public XercesJDK5OutputFormat() { + try { + _outputFormat = + Class.forName("com.sun.org.apache.xml.serialize.OutputFormat").newInstance(); + } catch (Exception except) { + throw new RuntimeException( + Messages.format("conf.failedInstantiateOutputFormat", + "com.sun.org.apache.xml.serialize.OutputFormat", except)); + } + } + + /** + * @see org.exolab.castor.xml.OutputFormat#setMethod(java.lang.String) + * {@inheritDoc} + */ + public void setMethod(final String method) { + Method aMethod; + try { + aMethod = _outputFormat.getClass().getMethod( + "setMethod", new Class[] {String.class} ); + aMethod.invoke(_outputFormat, new Object[] { method }); + } catch (Exception e) { + String msg = "Problem invoking OutputFormat.setMethod()"; + LOG.error(msg, e); + throw new RuntimeException(msg + e.getMessage()); + } + // _outputFormat.setMethod(method); + } + + /** + * @see org.exolab.castor.xml.OutputFormat#setIndenting(boolean) + * {@inheritDoc} + */ + public void setIndenting(final boolean indent) { + Method method; + try { + method = _outputFormat.getClass().getMethod( + "setIndenting", new Class[] {boolean.class} ); + method.invoke(_outputFormat, new Object[] { new Boolean(indent) }); + } catch (Exception e) { + String msg = "Problem invoking OutputFormat.setIndenting()"; + LOG.error(msg, e); + throw new RuntimeException(msg + e.getMessage()); + } + // _outputFormat.setIndenting(indent); + } + + /** + * @see org.exolab.castor.xml.OutputFormat#setPreserveSpace(boolean) + * {@inheritDoc} + */ + public void setPreserveSpace(final boolean preserveSpace) { + Method method; + try { + method = _outputFormat.getClass().getMethod( + "setPreserveSpace", new Class[] {boolean.class} ); + method.invoke(_outputFormat, new Object[] { new Boolean(preserveSpace) }); + } catch (Exception e) { + String msg = "Problem invoking OutputFormat.setPreserveSpace()"; + LOG.error(msg, e); + throw new RuntimeException(msg + e.getMessage()); + } + // _outputFormat.setPreserveSpace(preserveSpace); + } + + /** + * @see org.exolab.castor.xml.OutputFormat#getFormat() + * {@inheritDoc} + */ + public Object getFormat() { + return _outputFormat; + } + + /** + * @see org.exolab.castor.xml.OutputFormat#setDoctype(java.lang.String, java.lang.String) + * {@inheritDoc} + */ + public void setDoctype(final String type1, final String type2) { + Method method; + try { + method = _outputFormat.getClass().getMethod( + "setDoctype", new Class[] {String.class, String.class} ); + method.invoke(_outputFormat, new Object[] { type1, type2}); + } catch (Exception e) { + String msg = "Problem invoking OutputFormat.setDoctype()"; + LOG.error(msg, e); + throw new RuntimeException(msg + e.getMessage()); + } + // _outputFormat.setDoctype(type1, type2); + } + + /** + * @see org.exolab.castor.xml.OutputFormat#setOmitXMLDeclaration(boolean) + * {@inheritDoc} + */ + public void setOmitXMLDeclaration(final boolean omitXMLDeclaration) { + Method method; + try { + method = _outputFormat.getClass().getMethod( + "setOmitXMLDeclaration", new Class[] {boolean.class} ); + method.invoke(_outputFormat, new Object[] { new Boolean(omitXMLDeclaration) }); + } catch (Exception e) { + String msg = "Problem invoking OutputFormat.setOmitXMLDeclaration()"; + LOG.error(msg, e); + throw new RuntimeException(msg + e.getMessage()); + } + // _outputFormat.setOmitXMLDeclaration(omitXMLDeclaration); + } + + /** + * @see org.exolab.castor.xml.OutputFormat#setOmitDocumentType(boolean) + * {@inheritDoc} + */ + public void setOmitDocumentType(final boolean omitDocumentType) { + Method method; + try { + method = _outputFormat.getClass().getMethod( + "setOmitDocumentType", new Class[] {boolean.class} ); + method.invoke(_outputFormat, new Object[] { new Boolean(omitDocumentType) }); + } catch (Exception e) { + String msg = "Problem invoking OutputFormat.setOmitDocumentType()"; + LOG.error(msg, e); + throw new RuntimeException(msg + e.getMessage()); + } + // _outputFormat.setOmitDocumentType(omitDocumentType); + } + + /** + * @see org.exolab.castor.xml.OutputFormat#setEncoding(java.lang.String) + * {@inheritDoc} + */ + public void setEncoding(final String encoding) { + Method method; + try { + method = _outputFormat.getClass().getMethod( + "setEncoding", new Class[] {String.class} ); + method.invoke(_outputFormat, new Object[] { encoding }); + } catch (Exception e) { + String msg = "Problem invoking OutputFormat.setEncoding()"; + LOG.error(msg, e); + throw new RuntimeException(msg + e.getMessage()); + } + // _outputFormat.setEncoding(encoding); + } +} Index: main/java/org/exolab/castor/xml/XercesJDK5XMLSerializerFactory.java =================================================================== --- main/java/org/exolab/castor/xml/XercesJDK5XMLSerializerFactory.java (Revision 0) +++ main/java/org/exolab/castor/xml/XercesJDK5XMLSerializerFactory.java (Revision 0) @@ -0,0 +1,42 @@ +/* + * Copyright 2006 Werner Guttmann + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.exolab.castor.xml; + +/** + * Xerces-specific implementation of the {@link XMLSerializerFactory} interface. + * Returns Xerces-specific instances of the {@link Serializer} and + * {@link OutputFormat} interfaces. + * + * @author Werner Guttmann + * @version $Revision: 6216 $ $Date: 2006-04-25 16:09:10 -0600 (Tue, 25 Apr 2006) $ + */ +public class XercesJDK5XMLSerializerFactory implements XMLSerializerFactory { + /** + * @see org.exolab.castor.xml.XMLSerializerFactory#getSerializer() + * {@inheritDoc} + */ + public Serializer getSerializer() { + return new XercesJDK5Serializer(); + } + + /** + * @see org.exolab.castor.xml.XMLSerializerFactory#getOutputFormat() + * {@inheritDoc} + */ + public OutputFormat getOutputFormat() { + return new XercesJDK5OutputFormat(); + } +} Index: main/resources/org/exolab/castor/castor.properties =================================================================== --- main/resources/org/exolab/castor/castor.properties (Revision 6723) +++ main/resources/org/exolab/castor/castor.properties (Arbeitskopie) @@ -16,9 +16,14 @@ org.exolab.castor.parser=org.apache.xerces.parsers.SAXParser # Defines the (default) XML serializer factory to use by Castor, which must -# implement org.exolab.castor.xml.SerializerFactory; -# Default is org.exolab.castor.xml.XercesXMLSerializerFactory +# implement org.exolab.castor.xml.SerializerFactory; default is +# org.exolab.castor.xml.XercesXMLSerializerFactory +# +# When using Castor XML with JDK 5.0, you may switch to the XercesJDK5XMLSerializerFactory +# which will use the Xerces instance as shipped with the JDK itself; this avoids +# having to download another Xerces instance and installing it. org.exolab.castor.xml.serializer.factory=org.exolab.castor.xml.XercesXMLSerializerFactory +#org.exolab.castor.xml.serializer.factory=org.exolab.castor.xml.XercesJDK5XMLSerializerFactory # Defines the NodeType for use with Java primitive types (int, long, boolean, # etc). This value is only used by the Introspector. Valid values are either