ohair@286: /*
mkos@397: * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
ohair@286: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohair@286: *
ohair@286: * This code is free software; you can redistribute it and/or modify it
ohair@286: * under the terms of the GNU General Public License version 2 only, as
ohair@286: * published by the Free Software Foundation. Oracle designates this
ohair@286: * particular file as subject to the "Classpath" exception as provided
ohair@286: * by Oracle in the LICENSE file that accompanied this code.
ohair@286: *
ohair@286: * This code is distributed in the hope that it will be useful, but WITHOUT
ohair@286: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohair@286: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohair@286: * version 2 for more details (a copy is included in the LICENSE file that
ohair@286: * accompanied this code).
ohair@286: *
ohair@286: * You should have received a copy of the GNU General Public License version
ohair@286: * 2 along with this work; if not, write to the Free Software Foundation,
ohair@286: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohair@286: *
ohair@286: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@286: * or visit www.oracle.com if you need additional information or have any
ohair@286: * questions.
ohair@286: */
ohair@286:
ohair@286: package javax.xml.bind.helpers;
ohair@286:
ohair@286: import javax.xml.bind.JAXBException;
ohair@286: import javax.xml.bind.Marshaller;
ohair@286: import javax.xml.bind.PropertyException;
ohair@286: import javax.xml.bind.ValidationEventHandler;
ohair@286: import javax.xml.bind.annotation.adapters.XmlAdapter;
ohair@286: import javax.xml.bind.attachment.AttachmentMarshaller;
ohair@286: import javax.xml.stream.XMLEventWriter;
ohair@286: import javax.xml.stream.XMLStreamWriter;
ohair@286: import javax.xml.transform.dom.DOMResult;
ohair@286: import javax.xml.transform.sax.SAXResult;
ohair@286: import javax.xml.transform.stream.StreamResult;
ohair@286: import javax.xml.validation.Schema;
ohair@286: import java.io.UnsupportedEncodingException;
ohair@286: import java.io.File;
ohair@286: import java.io.OutputStream;
ohair@286: import java.io.FileOutputStream;
ohair@286: import java.io.BufferedOutputStream;
ohair@286: import java.io.IOException;
ohair@286: // J2SE1.4 feature
ohair@286: // import java.nio.charset.Charset;
ohair@286: // import java.nio.charset.UnsupportedCharsetException;
ohair@286:
ohair@286: /**
ohair@286: * Partial default Marshaller implementation.
ohair@286: *
ohair@286: *
ohair@286: * This class provides a partial default implementation for the
ohair@286: * {@link javax.xml.bind.Marshaller} interface.
ohair@286: *
ohair@286: *
ohair@286: * The only methods that a JAXB Provider has to implement are
ohair@286: * {@link Marshaller#marshal(Object, javax.xml.transform.Result) marshal(Object, javax.xml.transform.Result)},
ohair@286: * {@link Marshaller#marshal(Object, javax.xml.transform.Result) marshal(Object, javax.xml.stream.XMLStreamWriter)}, and
ohair@286: * {@link Marshaller#marshal(Object, javax.xml.transform.Result) marshal(Object, javax.xml.stream.XMLEventWriter)}.
ohair@286: *
ohair@286: * @author
- Kohsuke Kawaguchi, Sun Microsystems, Inc.
ohair@286: * @see javax.xml.bind.Marshaller
ohair@286: * @since JAXB1.0
ohair@286: */
ohair@286: public abstract class AbstractMarshallerImpl implements Marshaller
ohair@286: {
ohair@286: /** handler that will be used to process errors and warnings during marshal */
ohair@286: private ValidationEventHandler eventHandler =
ohair@286: new DefaultValidationEventHandler();
ohair@286:
ohair@286: //J2SE1.4 feature
ohair@286: //private Charset encoding = null;
ohair@286:
ohair@286: /** store the value of the encoding property. */
ohair@286: private String encoding = "UTF-8";
ohair@286:
ohair@286: /** store the value of the schemaLocation property. */
ohair@286: private String schemaLocation = null;
ohair@286:
ohair@286: /** store the value of the noNamespaceSchemaLocation property. */
ohair@286: private String noNSSchemaLocation = null;
ohair@286:
ohair@286: /** store the value of the formattedOutput property. */
ohair@286: private boolean formattedOutput = false;
ohair@286:
ohair@286: /** store the value of the fragment property. */
ohair@286: private boolean fragment = false;
ohair@286:
ohair@286: public final void marshal( Object obj, java.io.OutputStream os )
ohair@286: throws JAXBException {
ohair@286:
ohair@286: checkNotNull( obj, "obj", os, "os" );
ohair@286: marshal( obj, new StreamResult(os) );
ohair@286: }
ohair@286:
ohair@286: public void marshal(Object jaxbElement, File output) throws JAXBException {
ohair@286: checkNotNull(jaxbElement, "jaxbElement", output, "output" );
ohair@286: try {
ohair@286: OutputStream os = new BufferedOutputStream(new FileOutputStream(output));
ohair@286: try {
ohair@286: marshal( jaxbElement, new StreamResult(os) );
ohair@286: } finally {
ohair@286: os.close();
ohair@286: }
ohair@286: } catch (IOException e) {
ohair@286: throw new JAXBException(e);
ohair@286: }
ohair@286: }
ohair@286:
ohair@286: public final void marshal( Object obj, java.io.Writer w )
ohair@286: throws JAXBException {
ohair@286:
ohair@286: checkNotNull( obj, "obj", w, "writer" );
ohair@286: marshal( obj, new StreamResult(w) );
ohair@286: }
ohair@286:
ohair@286: public final void marshal( Object obj, org.xml.sax.ContentHandler handler )
ohair@286: throws JAXBException {
ohair@286:
ohair@286: checkNotNull( obj, "obj", handler, "handler" );
ohair@286: marshal( obj, new SAXResult(handler) );
ohair@286: }
ohair@286:
ohair@286: public final void marshal( Object obj, org.w3c.dom.Node node )
ohair@286: throws JAXBException {
ohair@286:
ohair@286: checkNotNull( obj, "obj", node, "node" );
ohair@286: marshal( obj, new DOMResult(node) );
ohair@286: }
ohair@286:
ohair@286: /**
ohair@286: * By default, the getNode method is unsupported and throw
ohair@286: * an {@link java.lang.UnsupportedOperationException}.
ohair@286: *
ohair@286: * Implementations that choose to support this method must
ohair@286: * override this method.
ohair@286: */
ohair@286: public org.w3c.dom.Node getNode( Object obj ) throws JAXBException {
ohair@286:
ohair@286: checkNotNull( obj, "obj", Boolean.TRUE, "foo" );
ohair@286:
ohair@286: throw new UnsupportedOperationException();
ohair@286: }
ohair@286:
ohair@286: /**
ohair@286: * Convenience method for getting the current output encoding.
ohair@286: *
ohair@286: * @return the current encoding or "UTF-8" if it hasn't been set.
ohair@286: */
ohair@286: protected String getEncoding() {
ohair@286: return encoding;
ohair@286: }
ohair@286:
ohair@286: /**
ohair@286: * Convenience method for setting the output encoding.
ohair@286: *
ohair@286: * @param encoding a valid encoding as specified in the Marshaller class
ohair@286: * documentation
ohair@286: */
ohair@286: protected void setEncoding( String encoding ) {
ohair@286: this.encoding = encoding;
ohair@286: }
ohair@286:
ohair@286: /**
ohair@286: * Convenience method for getting the current schemaLocation.
ohair@286: *
ohair@286: * @return the current schemaLocation or null if it hasn't been set
ohair@286: */
ohair@286: protected String getSchemaLocation() {
ohair@286: return schemaLocation;
ohair@286: }
ohair@286:
ohair@286: /**
ohair@286: * Convenience method for setting the schemaLocation.
ohair@286: *
ohair@286: * @param location the schemaLocation value
ohair@286: */
ohair@286: protected void setSchemaLocation( String location ) {
ohair@286: schemaLocation = location;
ohair@286: }
ohair@286:
ohair@286: /**
ohair@286: * Convenience method for getting the current noNamespaceSchemaLocation.
ohair@286: *
ohair@286: * @return the current noNamespaceSchemaLocation or null if it hasn't
ohair@286: * been set
ohair@286: */
ohair@286: protected String getNoNSSchemaLocation() {
ohair@286: return noNSSchemaLocation;
ohair@286: }
ohair@286:
ohair@286: /**
ohair@286: * Convenience method for setting the noNamespaceSchemaLocation.
ohair@286: *
ohair@286: * @param location the noNamespaceSchemaLocation value
ohair@286: */
ohair@286: protected void setNoNSSchemaLocation( String location ) {
ohair@286: noNSSchemaLocation = location;
ohair@286: }
ohair@286:
ohair@286: /**
ohair@286: * Convenience method for getting the formatted output flag.
ohair@286: *
ohair@286: * @return the current value of the formatted output flag or false if
ohair@286: * it hasn't been set.
ohair@286: */
ohair@286: protected boolean isFormattedOutput() {
ohair@286: return formattedOutput;
ohair@286: }
ohair@286:
ohair@286: /**
ohair@286: * Convenience method for setting the formatted output flag.
ohair@286: *
ohair@286: * @param v value of the formatted output flag.
ohair@286: */
ohair@286: protected void setFormattedOutput( boolean v ) {
ohair@286: formattedOutput = v;
ohair@286: }
ohair@286:
ohair@286:
ohair@286: /**
ohair@286: * Convenience method for getting the fragment flag.
ohair@286: *
ohair@286: * @return the current value of the fragment flag or false if
ohair@286: * it hasn't been set.
ohair@286: */
ohair@286: protected boolean isFragment() {
ohair@286: return fragment;
ohair@286: }
ohair@286:
ohair@286: /**
ohair@286: * Convenience method for setting the fragment flag.
ohair@286: *
ohair@286: * @param v value of the fragment flag.
ohair@286: */
ohair@286: protected void setFragment( boolean v ) {
ohair@286: fragment = v;
ohair@286: }
ohair@286:
ohair@286:
ohair@286: static String[] aliases = {
ohair@286: "UTF-8", "UTF8",
ohair@286: "UTF-16", "Unicode",
ohair@286: "UTF-16BE", "UnicodeBigUnmarked",
ohair@286: "UTF-16LE", "UnicodeLittleUnmarked",
ohair@286: "US-ASCII", "ASCII",
ohair@286: "TIS-620", "TIS620",
ohair@286:
ohair@286: // taken from the project-X parser
ohair@286: "ISO-10646-UCS-2", "Unicode",
ohair@286:
ohair@286: "EBCDIC-CP-US", "cp037",
ohair@286: "EBCDIC-CP-CA", "cp037",
ohair@286: "EBCDIC-CP-NL", "cp037",
ohair@286: "EBCDIC-CP-WT", "cp037",
ohair@286:
ohair@286: "EBCDIC-CP-DK", "cp277",
ohair@286: "EBCDIC-CP-NO", "cp277",
ohair@286: "EBCDIC-CP-FI", "cp278",
ohair@286: "EBCDIC-CP-SE", "cp278",
ohair@286:
ohair@286: "EBCDIC-CP-IT", "cp280",
ohair@286: "EBCDIC-CP-ES", "cp284",
ohair@286: "EBCDIC-CP-GB", "cp285",
ohair@286: "EBCDIC-CP-FR", "cp297",
ohair@286:
ohair@286: "EBCDIC-CP-AR1", "cp420",
ohair@286: "EBCDIC-CP-HE", "cp424",
ohair@286: "EBCDIC-CP-BE", "cp500",
ohair@286: "EBCDIC-CP-CH", "cp500",
ohair@286:
ohair@286: "EBCDIC-CP-ROECE", "cp870",
ohair@286: "EBCDIC-CP-YU", "cp870",
ohair@286: "EBCDIC-CP-IS", "cp871",
ohair@286: "EBCDIC-CP-AR2", "cp918",
ohair@286:
ohair@286: // IANA also defines two that JDK 1.2 doesn't handle:
ohair@286: // EBCDIC-CP-GR --> CP423
ohair@286: // EBCDIC-CP-TR --> CP905
ohair@286: };
ohair@286:
ohair@286: /**
ohair@286: * Gets the corresponding Java encoding name from an IANA name.
ohair@286: *
ohair@286: * This method is a helper method for the derived class to convert
ohair@286: * encoding names.
ohair@286: *
ohair@286: * @exception UnsupportedEncodingException
ohair@286: * If this implementation couldn't find the Java encoding name.
ohair@286: */
ohair@286: protected String getJavaEncoding( String encoding ) throws UnsupportedEncodingException {
ohair@286: try {
ohair@286: "1".getBytes(encoding);
ohair@286: return encoding;
ohair@286: } catch( UnsupportedEncodingException e ) {
ohair@286: // try known alias
ohair@286: for( int i=0; i void setAdapter(Class type, A adapter) {
ohair@286: throw new UnsupportedOperationException();
ohair@286: }
ohair@286:
ohair@286: public A getAdapter(Class type) {
ohair@286: throw new UnsupportedOperationException();
ohair@286: }
ohair@286:
ohair@286: public void setAttachmentMarshaller(AttachmentMarshaller am) {
ohair@286: throw new UnsupportedOperationException();
ohair@286: }
ohair@286:
ohair@286: public AttachmentMarshaller getAttachmentMarshaller() {
ohair@286: throw new UnsupportedOperationException();
ohair@286: }
ohair@286:
ohair@286: public void setListener(Listener listener) {
ohair@286: throw new UnsupportedOperationException();
ohair@286: }
ohair@286:
ohair@286: public Listener getListener() {
ohair@286: throw new UnsupportedOperationException();
ohair@286: }
ohair@286: }