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