/* * Copyright 2003 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package com.sun.xml.bind.v2.runtime; import java.io.BufferedOutputStream; import java.io.BufferedWriter; import java.io.ByteArrayOutputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; import java.io.Writer; import javax.xml.bind.DatatypeConverter; import javax.xml.bind.JAXBException; import javax.xml.bind.MarshalException; import javax.xml.bind.PropertyException; import javax.xml.bind.ValidationEvent; import javax.xml.bind.ValidationEventHandler; import javax.xml.bind.annotation.adapters.XmlAdapter; import javax.xml.bind.attachment.AttachmentMarshaller; import javax.xml.bind.helpers.AbstractMarshallerImpl; import javax.xml.stream.XMLEventWriter; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamWriter; import javax.xml.transform.Result; import javax.xml.transform.dom.DOMResult; import javax.xml.transform.sax.SAXResult; import javax.xml.transform.stream.StreamResult; import javax.xml.validation.Schema; import javax.xml.validation.ValidatorHandler; import com.sun.xml.bind.DatatypeConverterImpl; import com.sun.xml.bind.marshaller.CharacterEscapeHandler; import com.sun.xml.bind.marshaller.DataWriter; import com.sun.xml.bind.marshaller.DumbEscapeHandler; import com.sun.xml.bind.marshaller.Messages; import com.sun.xml.bind.marshaller.MinimumEscapeHandler; import com.sun.xml.bind.marshaller.NamespacePrefixMapper; import com.sun.xml.bind.marshaller.NioEscapeHandler; import com.sun.xml.bind.marshaller.SAX2DOMEx; import com.sun.xml.bind.marshaller.XMLWriter; import com.sun.xml.bind.v2.AssociationMap; import com.sun.xml.bind.v2.FatalAdapter; import com.sun.xml.bind.v2.runtime.output.C14nXmlOutput; import com.sun.xml.bind.v2.runtime.output.Encoded; import com.sun.xml.bind.v2.runtime.output.FastInfosetStreamWriterOutput; import com.sun.xml.bind.v2.runtime.output.ForkXmlOutput; import com.sun.xml.bind.v2.runtime.output.IndentingUTF8XmlOutput; import com.sun.xml.bind.v2.runtime.output.SAXOutput; import com.sun.xml.bind.v2.runtime.output.UTF8XmlOutput; import com.sun.xml.bind.v2.runtime.output.XMLEventWriterOutput; import com.sun.xml.bind.v2.runtime.output.XMLStreamWriterOutput; import com.sun.xml.bind.v2.runtime.output.XmlOutput; import com.sun.xml.fastinfoset.stax.StAXDocumentSerializer; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.xml.sax.SAXException; import org.xml.sax.helpers.XMLFilterImpl; /** * Implementation of {@link Marshaller} interface for the JAXB RI. * *
* Eventually all the {@link #marshal} methods call into
* the {@link #write} method.
*
* @author Kohsuke Kawaguchi
* @author Vivek Pandey
*/
public /*to make unit tests happy*/ final class MarshallerImpl extends AbstractMarshallerImpl implements ValidationEventHandler
{
/** Indentation string. Default is four whitespaces. */
private String indent = " ";
/** Used to assign prefixes to namespace URIs. */
private NamespacePrefixMapper prefixMapper = null;
/** Object that handles character escaping. */
private CharacterEscapeHandler escapeHandler = null;
/** Whether the xml declaration will be printed or not. */
private boolean printXmlDeclaration = true;
/** XML BLOB written after the XML declaration. */
private String header=null;
/** reference to the context that created this object */
final JAXBContextImpl context;
protected final XMLSerializer serializer;
/**
* Non-null if we work inside a {@link BinderImpl}.
*/
private final AssociationMap assoc;
/**
* Non-null if we do the marshal-time validation.
*/
private Schema schema;
/**
* @param assoc
* non-null if the marshaller is working inside {@link BinderImpl}.
*/
public MarshallerImpl( JAXBContextImpl c, AssociationMap assoc ) {
// initialize datatype converter with ours
DatatypeConverter.setDatatypeConverter(DatatypeConverterImpl.theInstance);
context = c;
this.assoc = assoc;
serializer = new XMLSerializer(this);
try {
setEventHandler(this);
} catch (JAXBException e) {
throw new AssertionError(e); // impossible
}
}
public void marshal(Object obj, XMLStreamWriter writer) throws JAXBException {
if (writer instanceof StAXDocumentSerializer) {
write(obj, new FastInfosetStreamWriterOutput((StAXDocumentSerializer)writer),
new StAXPostInitAction(writer,serializer));
} else {
write(obj, new XMLStreamWriterOutput(writer), new StAXPostInitAction(writer,serializer));
}
}
public void marshal(Object obj, XMLEventWriter writer) throws JAXBException {
write(obj, new XMLEventWriterOutput(writer), new StAXPostInitAction(writer,serializer));
}
public void marshal(Object obj, XmlOutput output) throws JAXBException {
write(obj, output, null );
}
public void marshal(Object target,Result result) throws JAXBException {
if (result instanceof SAXResult) {
write(target, new SAXOutput(((SAXResult) result).getHandler()), null);
return;
}
if (result instanceof DOMResult) {
final Node node = ((DOMResult) result).getNode();
if (node == null) {
Document doc = JAXBContextImpl.createDom();
((DOMResult) result).setNode(doc);
write(target, new SAXOutput(new SAX2DOMEx(doc)), null );
} else {
write(target, new SAXOutput(new SAX2DOMEx(node)), new DomPostInitAction(node,serializer));
}
return;
}
if (result instanceof StreamResult) {
StreamResult sr = (StreamResult) result;
XmlOutput w = null;
if (sr.getWriter() != null)
w = createWriter(sr.getWriter());
else if (sr.getOutputStream() != null)
w = createWriter(sr.getOutputStream());
else if (sr.getSystemId() != null) {
String fileURL = sr.getSystemId();
if (fileURL.startsWith("file:///")) {
if (fileURL.substring(8).indexOf(":") > 0)
fileURL = fileURL.substring(8);
else
fileURL = fileURL.substring(7);
} // otherwise assume that it's a file name
try {
w = createWriter(new FileOutputStream(fileURL));
} catch (IOException e) {
throw new MarshalException(e);
}
}
if (w == null)
throw new IllegalArgumentException();
write(target, w, null);
return;
}
// unsupported parameter type
throw new MarshalException(
Messages.format( Messages.UNSUPPORTED_RESULT ) );
}
/**
* Used by {@link BridgeImpl} to write an arbitrary object.
*/
protected final