ohair@286: /* aefimov@1443: * Copyright (c) 1997, 2017, 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 com.sun.xml.internal.bind.v2.runtime; ohair@286: ohair@286: import java.io.BufferedWriter; ohair@286: import java.io.Closeable; ohair@286: import java.io.FileOutputStream; ohair@286: import java.io.Flushable; ohair@286: import java.io.IOException; ohair@286: import java.io.OutputStream; ohair@286: import java.io.OutputStreamWriter; ohair@286: import java.io.UnsupportedEncodingException; ohair@286: import java.io.Writer; ohair@286: ohair@286: import java.net.URI; ohair@286: import javax.xml.bind.JAXBException; ohair@286: import javax.xml.bind.MarshalException; ohair@286: import javax.xml.bind.Marshaller; ohair@286: import javax.xml.bind.PropertyException; ohair@286: import javax.xml.bind.ValidationEvent; 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.bind.helpers.AbstractMarshallerImpl; ohair@286: import javax.xml.stream.XMLEventWriter; ohair@286: import javax.xml.stream.XMLStreamException; ohair@286: import javax.xml.stream.XMLStreamWriter; ohair@286: import javax.xml.transform.Result; 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 javax.xml.validation.ValidatorHandler; ohair@286: import javax.xml.namespace.NamespaceContext; ohair@286: ohair@286: import com.sun.xml.internal.bind.api.JAXBRIContext; ohair@286: import com.sun.xml.internal.bind.marshaller.CharacterEscapeHandler; ohair@286: import com.sun.xml.internal.bind.marshaller.DataWriter; ohair@286: import com.sun.xml.internal.bind.marshaller.DumbEscapeHandler; ohair@286: import com.sun.xml.internal.bind.marshaller.MinimumEscapeHandler; ohair@286: import com.sun.xml.internal.bind.marshaller.NamespacePrefixMapper; ohair@286: import com.sun.xml.internal.bind.marshaller.NioEscapeHandler; ohair@286: import com.sun.xml.internal.bind.marshaller.SAX2DOMEx; ohair@286: import com.sun.xml.internal.bind.marshaller.XMLWriter; ohair@286: import com.sun.xml.internal.bind.v2.runtime.output.C14nXmlOutput; ohair@286: import com.sun.xml.internal.bind.v2.runtime.output.Encoded; ohair@286: import com.sun.xml.internal.bind.v2.runtime.output.ForkXmlOutput; ohair@286: import com.sun.xml.internal.bind.v2.runtime.output.IndentingUTF8XmlOutput; ohair@286: import com.sun.xml.internal.bind.v2.runtime.output.NamespaceContextImpl; ohair@286: import com.sun.xml.internal.bind.v2.runtime.output.SAXOutput; ohair@286: import com.sun.xml.internal.bind.v2.runtime.output.UTF8XmlOutput; ohair@286: import com.sun.xml.internal.bind.v2.runtime.output.XMLEventWriterOutput; ohair@286: import com.sun.xml.internal.bind.v2.runtime.output.XMLStreamWriterOutput; ohair@286: import com.sun.xml.internal.bind.v2.runtime.output.XmlOutput; ohair@286: import com.sun.xml.internal.bind.v2.util.FatalAdapter; ohair@286: ohair@286: import java.net.URISyntaxException; ohair@286: import org.w3c.dom.Document; ohair@286: import org.w3c.dom.Node; ohair@286: import org.xml.sax.SAXException; ohair@286: import org.xml.sax.helpers.XMLFilterImpl; ohair@286: ohair@286: /** ohair@286: * Implementation of {@link Marshaller} interface for the JAXB RI. ohair@286: * ohair@286: *
ohair@286: * Eventually all the {@link #marshal} methods call into
ohair@286: * the {@link #write} method.
ohair@286: *
ohair@286: * @author Kohsuke Kawaguchi
ohair@286: * @author Vivek Pandey
ohair@286: */
ohair@286: public /*to make unit tests happy*/ final class MarshallerImpl extends AbstractMarshallerImpl implements ValidationEventHandler
ohair@286: {
ohair@286: /** Indentation string. Default is four whitespaces. */
ohair@286: private String indent = " ";
ohair@286:
ohair@286: /** Used to assign prefixes to namespace URIs. */
ohair@286: private NamespacePrefixMapper prefixMapper = null;
ohair@286:
ohair@286: /** Object that handles character escaping. */
ohair@286: private CharacterEscapeHandler escapeHandler = null;
ohair@286:
ohair@286: /** XML BLOB written after the XML declaration. */
ohair@286: private String header=null;
ohair@286:
ohair@286: /** reference to the context that created this object */
ohair@286: final JAXBContextImpl context;
ohair@286:
ohair@286: protected final XMLSerializer serializer;
ohair@286:
ohair@286: /**
ohair@286: * Non-null if we do the marshal-time validation.
ohair@286: */
ohair@286: private Schema schema;
ohair@286:
ohair@286: /** Marshaller.Listener */
ohair@286: private Listener externalListener = null;
ohair@286:
ohair@286: /** Configured for c14n? */
ohair@286: private boolean c14nSupport;
ohair@286:
ohair@286: // while createing XmlOutput those values may be set.
ohair@286: // if these are non-null they need to be cleaned up
ohair@286: private Flushable toBeFlushed;
ohair@286: private Closeable toBeClosed;
ohair@286:
ohair@286: /**
ohair@286: * @param assoc
ohair@286: * non-null if the marshaller is working inside {@link BinderImpl}.
ohair@286: */
ohair@286: public MarshallerImpl( JAXBContextImpl c, AssociationMap assoc ) {
ohair@286: context = c;
ohair@286: serializer = new XMLSerializer(this);
ohair@286: c14nSupport = context.c14nSupport;
ohair@286:
ohair@286: try {
ohair@286: setEventHandler(this);
ohair@286: } catch (JAXBException e) {
ohair@286: throw new AssertionError(e); // impossible
ohair@286: }
ohair@286: }
ohair@286:
ohair@286: public JAXBContextImpl getContext() {
ohair@286: return context;
ohair@286: }
ohair@286:
ohair@286: /**
ohair@286: * Marshals to {@link OutputStream} with the given in-scope namespaces
ohair@286: * taken into account.
ohair@286: *
ohair@286: * @since 2.1.5
ohair@286: */
ohair@286: public void marshal(Object obj, OutputStream out, NamespaceContext inscopeNamespace) throws JAXBException {
ohair@286: write(obj, createWriter(out), new StAXPostInitAction(inscopeNamespace,serializer));
ohair@286: }
ohair@286:
ohair@286: @Override
ohair@286: public void marshal(Object obj, XMLStreamWriter writer) throws JAXBException {
aefimov@1443: write(obj, XMLStreamWriterOutput.create(writer,context, escapeHandler), new StAXPostInitAction(writer,serializer));
ohair@286: }
ohair@286:
ohair@286: @Override
ohair@286: public void marshal(Object obj, XMLEventWriter writer) throws JAXBException {
ohair@286: write(obj, new XMLEventWriterOutput(writer), new StAXPostInitAction(writer,serializer));
ohair@286: }
ohair@286:
ohair@286: public void marshal(Object obj, XmlOutput output) throws JAXBException {
ohair@286: write(obj, output, null );
ohair@286: }
ohair@286:
ohair@286: /**
ohair@286: * Creates {@link XmlOutput} from the given {@link Result} object.
ohair@286: */
ohair@286: final XmlOutput createXmlOutput(Result result) throws JAXBException {
ohair@286: if (result instanceof SAXResult)
ohair@286: return new SAXOutput(((SAXResult) result).getHandler());
ohair@286:
ohair@286: if (result instanceof DOMResult) {
ohair@286: final Node node = ((DOMResult) result).getNode();
ohair@286:
ohair@286: if (node == null) {
alanb@368: Document doc = JAXBContextImpl.createDom(getContext().disableSecurityProcessing);
ohair@286: ((DOMResult) result).setNode(doc);
ohair@286: return new SAXOutput(new SAX2DOMEx(doc));
ohair@286: } else {
ohair@286: return new SAXOutput(new SAX2DOMEx(node));
ohair@286: }
ohair@286: }
ohair@286: if (result instanceof StreamResult) {
ohair@286: StreamResult sr = (StreamResult) result;
ohair@286:
ohair@286: if (sr.getWriter() != null)
ohair@286: return createWriter(sr.getWriter());
ohair@286: else if (sr.getOutputStream() != null)
ohair@286: return createWriter(sr.getOutputStream());
ohair@286: else if (sr.getSystemId() != null) {
ohair@286: String fileURL = sr.getSystemId();
ohair@286:
ohair@286: try {
ohair@286: fileURL = new URI(fileURL).getPath();
ohair@286: } catch (URISyntaxException use) {
ohair@286: // otherwise assume that it's a file name
ohair@286: }
ohair@286:
ohair@286: try {
ohair@286: FileOutputStream fos = new FileOutputStream(fileURL);
ohair@286: assert toBeClosed==null;
ohair@286: toBeClosed = fos;
ohair@286: return createWriter(fos);
ohair@286: } catch (IOException e) {
ohair@286: throw new MarshalException(e);
ohair@286: }
ohair@286: }
ohair@286: }
ohair@286:
ohair@286: // unsupported parameter type
ohair@286: throw new MarshalException(Messages.UNSUPPORTED_RESULT.format());
ohair@286: }
ohair@286:
ohair@286: /**
ohair@286: * Creates an appropriate post-init action object.
ohair@286: */
ohair@286: final Runnable createPostInitAction(Result result) {
ohair@286: if (result instanceof DOMResult) {
ohair@286: Node node = ((DOMResult) result).getNode();
ohair@286: return new DomPostInitAction(node,serializer);
ohair@286: }
ohair@286: return null;
ohair@286: }
ohair@286:
ohair@286: public void marshal(Object target,Result result) throws JAXBException {
ohair@286: write(target, createXmlOutput(result), createPostInitAction(result));
ohair@286: }
ohair@286:
ohair@286:
ohair@286: /**
ohair@286: * Used by {@link BridgeImpl} to write an arbitrary object as a fragment.
ohair@286: */
ohair@286: protected final