aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.xml.internal.bind.v2.runtime; aoqi@0: aoqi@0: import java.io.BufferedWriter; aoqi@0: import java.io.Closeable; aoqi@0: import java.io.FileOutputStream; aoqi@0: import java.io.Flushable; aoqi@0: import java.io.IOException; aoqi@0: import java.io.OutputStream; aoqi@0: import java.io.OutputStreamWriter; aoqi@0: import java.io.UnsupportedEncodingException; aoqi@0: import java.io.Writer; aoqi@0: aoqi@0: import java.net.URI; aoqi@0: import javax.xml.bind.JAXBException; aoqi@0: import javax.xml.bind.MarshalException; aoqi@0: import javax.xml.bind.Marshaller; aoqi@0: import javax.xml.bind.PropertyException; aoqi@0: import javax.xml.bind.ValidationEvent; aoqi@0: import javax.xml.bind.ValidationEventHandler; aoqi@0: import javax.xml.bind.annotation.adapters.XmlAdapter; aoqi@0: import javax.xml.bind.attachment.AttachmentMarshaller; aoqi@0: import javax.xml.bind.helpers.AbstractMarshallerImpl; aoqi@0: import javax.xml.stream.XMLEventWriter; aoqi@0: import javax.xml.stream.XMLStreamException; aoqi@0: import javax.xml.stream.XMLStreamWriter; aoqi@0: import javax.xml.transform.Result; aoqi@0: import javax.xml.transform.dom.DOMResult; aoqi@0: import javax.xml.transform.sax.SAXResult; aoqi@0: import javax.xml.transform.stream.StreamResult; aoqi@0: import javax.xml.validation.Schema; aoqi@0: import javax.xml.validation.ValidatorHandler; aoqi@0: import javax.xml.namespace.NamespaceContext; aoqi@0: aoqi@0: import com.sun.xml.internal.bind.api.JAXBRIContext; aoqi@0: import com.sun.xml.internal.bind.marshaller.CharacterEscapeHandler; aoqi@0: import com.sun.xml.internal.bind.marshaller.DataWriter; aoqi@0: import com.sun.xml.internal.bind.marshaller.DumbEscapeHandler; aoqi@0: import com.sun.xml.internal.bind.marshaller.MinimumEscapeHandler; aoqi@0: import com.sun.xml.internal.bind.marshaller.NamespacePrefixMapper; aoqi@0: import com.sun.xml.internal.bind.marshaller.NioEscapeHandler; aoqi@0: import com.sun.xml.internal.bind.marshaller.SAX2DOMEx; aoqi@0: import com.sun.xml.internal.bind.marshaller.XMLWriter; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.output.C14nXmlOutput; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.output.Encoded; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.output.ForkXmlOutput; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.output.IndentingUTF8XmlOutput; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.output.NamespaceContextImpl; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.output.SAXOutput; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.output.UTF8XmlOutput; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.output.XMLEventWriterOutput; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.output.XMLStreamWriterOutput; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.output.XmlOutput; aoqi@0: import com.sun.xml.internal.bind.v2.util.FatalAdapter; aoqi@0: aoqi@0: import java.net.URISyntaxException; aoqi@0: import org.w3c.dom.Document; aoqi@0: import org.w3c.dom.Node; aoqi@0: import org.xml.sax.SAXException; aoqi@0: import org.xml.sax.helpers.XMLFilterImpl; aoqi@0: aoqi@0: /** aoqi@0: * Implementation of {@link Marshaller} interface for the JAXB RI. aoqi@0: * aoqi@0: *
aoqi@0: * Eventually all the {@link #marshal} methods call into
aoqi@0: * the {@link #write} method.
aoqi@0: *
aoqi@0: * @author Kohsuke Kawaguchi
aoqi@0: * @author Vivek Pandey
aoqi@0: */
aoqi@0: public /*to make unit tests happy*/ final class MarshallerImpl extends AbstractMarshallerImpl implements ValidationEventHandler
aoqi@0: {
aoqi@0: /** Indentation string. Default is four whitespaces. */
aoqi@0: private String indent = " ";
aoqi@0:
aoqi@0: /** Used to assign prefixes to namespace URIs. */
aoqi@0: private NamespacePrefixMapper prefixMapper = null;
aoqi@0:
aoqi@0: /** Object that handles character escaping. */
aoqi@0: private CharacterEscapeHandler escapeHandler = null;
aoqi@0:
aoqi@0: /** XML BLOB written after the XML declaration. */
aoqi@0: private String header=null;
aoqi@0:
aoqi@0: /** reference to the context that created this object */
aoqi@0: final JAXBContextImpl context;
aoqi@0:
aoqi@0: protected final XMLSerializer serializer;
aoqi@0:
aoqi@0: /**
aoqi@0: * Non-null if we do the marshal-time validation.
aoqi@0: */
aoqi@0: private Schema schema;
aoqi@0:
aoqi@0: /** Marshaller.Listener */
aoqi@0: private Listener externalListener = null;
aoqi@0:
aoqi@0: /** Configured for c14n? */
aoqi@0: private boolean c14nSupport;
aoqi@0:
aoqi@0: // while createing XmlOutput those values may be set.
aoqi@0: // if these are non-null they need to be cleaned up
aoqi@0: private Flushable toBeFlushed;
aoqi@0: private Closeable toBeClosed;
aoqi@0:
aoqi@0: /**
aoqi@0: * @param assoc
aoqi@0: * non-null if the marshaller is working inside {@link BinderImpl}.
aoqi@0: */
aoqi@0: public MarshallerImpl( JAXBContextImpl c, AssociationMap assoc ) {
aoqi@0: context = c;
aoqi@0: serializer = new XMLSerializer(this);
aoqi@0: c14nSupport = context.c14nSupport;
aoqi@0:
aoqi@0: try {
aoqi@0: setEventHandler(this);
aoqi@0: } catch (JAXBException e) {
aoqi@0: throw new AssertionError(e); // impossible
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: public JAXBContextImpl getContext() {
aoqi@0: return context;
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Marshals to {@link OutputStream} with the given in-scope namespaces
aoqi@0: * taken into account.
aoqi@0: *
aoqi@0: * @since 2.1.5
aoqi@0: */
aoqi@0: public void marshal(Object obj, OutputStream out, NamespaceContext inscopeNamespace) throws JAXBException {
aoqi@0: write(obj, createWriter(out), new StAXPostInitAction(inscopeNamespace,serializer));
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: public void marshal(Object obj, XMLStreamWriter writer) throws JAXBException {
aoqi@0: write(obj, XMLStreamWriterOutput.create(writer,context), new StAXPostInitAction(writer,serializer));
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: public void marshal(Object obj, XMLEventWriter writer) throws JAXBException {
aoqi@0: write(obj, new XMLEventWriterOutput(writer), new StAXPostInitAction(writer,serializer));
aoqi@0: }
aoqi@0:
aoqi@0: public void marshal(Object obj, XmlOutput output) throws JAXBException {
aoqi@0: write(obj, output, null );
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Creates {@link XmlOutput} from the given {@link Result} object.
aoqi@0: */
aoqi@0: final XmlOutput createXmlOutput(Result result) throws JAXBException {
aoqi@0: if (result instanceof SAXResult)
aoqi@0: return new SAXOutput(((SAXResult) result).getHandler());
aoqi@0:
aoqi@0: if (result instanceof DOMResult) {
aoqi@0: final Node node = ((DOMResult) result).getNode();
aoqi@0:
aoqi@0: if (node == null) {
aoqi@0: Document doc = JAXBContextImpl.createDom(getContext().disableSecurityProcessing);
aoqi@0: ((DOMResult) result).setNode(doc);
aoqi@0: return new SAXOutput(new SAX2DOMEx(doc));
aoqi@0: } else {
aoqi@0: return new SAXOutput(new SAX2DOMEx(node));
aoqi@0: }
aoqi@0: }
aoqi@0: if (result instanceof StreamResult) {
aoqi@0: StreamResult sr = (StreamResult) result;
aoqi@0:
aoqi@0: if (sr.getWriter() != null)
aoqi@0: return createWriter(sr.getWriter());
aoqi@0: else if (sr.getOutputStream() != null)
aoqi@0: return createWriter(sr.getOutputStream());
aoqi@0: else if (sr.getSystemId() != null) {
aoqi@0: String fileURL = sr.getSystemId();
aoqi@0:
aoqi@0: try {
aoqi@0: fileURL = new URI(fileURL).getPath();
aoqi@0: } catch (URISyntaxException use) {
aoqi@0: // otherwise assume that it's a file name
aoqi@0: }
aoqi@0:
aoqi@0: try {
aoqi@0: FileOutputStream fos = new FileOutputStream(fileURL);
aoqi@0: assert toBeClosed==null;
aoqi@0: toBeClosed = fos;
aoqi@0: return createWriter(fos);
aoqi@0: } catch (IOException e) {
aoqi@0: throw new MarshalException(e);
aoqi@0: }
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: // unsupported parameter type
aoqi@0: throw new MarshalException(Messages.UNSUPPORTED_RESULT.format());
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Creates an appropriate post-init action object.
aoqi@0: */
aoqi@0: final Runnable createPostInitAction(Result result) {
aoqi@0: if (result instanceof DOMResult) {
aoqi@0: Node node = ((DOMResult) result).getNode();
aoqi@0: return new DomPostInitAction(node,serializer);
aoqi@0: }
aoqi@0: return null;
aoqi@0: }
aoqi@0:
aoqi@0: public void marshal(Object target,Result result) throws JAXBException {
aoqi@0: write(target, createXmlOutput(result), createPostInitAction(result));
aoqi@0: }
aoqi@0:
aoqi@0:
aoqi@0: /**
aoqi@0: * Used by {@link BridgeImpl} to write an arbitrary object as a fragment.
aoqi@0: */
aoqi@0: protected final