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 void write(Name rootTagName, JaxBeanInfo bi, T obj, XmlOutput out,Runnable postInitAction) throws JAXBException { ohair@286: try { ohair@286: try { ohair@286: prewrite(out, true, postInitAction); ohair@286: serializer.startElement(rootTagName,null); ohair@286: if(bi.jaxbType==Void.class || bi.jaxbType==void.class) { ohair@286: // special case for void ohair@286: serializer.endNamespaceDecls(null); ohair@286: serializer.endAttributes(); ohair@286: } else { // normal cases ohair@286: if(obj==null) ohair@286: serializer.writeXsiNilTrue(); ohair@286: else ohair@286: serializer.childAsXsiType(obj,"root",bi, false); ohair@286: } ohair@286: serializer.endElement(); ohair@286: postwrite(); ohair@286: } catch( SAXException e ) { ohair@286: throw new MarshalException(e); ohair@286: } catch (IOException e) { ohair@286: throw new MarshalException(e); ohair@286: } catch (XMLStreamException e) { ohair@286: throw new MarshalException(e); ohair@286: } finally { ohair@286: serializer.close(); ohair@286: } ohair@286: } finally { ohair@286: cleanUp(); ohair@286: } ohair@286: } ohair@286: ohair@286: /** ohair@286: * All the marshal method invocation eventually comes down to this call. ohair@286: */ ohair@286: private void write(Object obj, XmlOutput out, Runnable postInitAction) throws JAXBException { ohair@286: try { ohair@286: if( obj == null ) ohair@286: throw new IllegalArgumentException(Messages.NOT_MARSHALLABLE.format()); ohair@286: ohair@286: if( schema!=null ) { ohair@286: // send the output to the validator as well ohair@286: ValidatorHandler validator = schema.newValidatorHandler(); ohair@286: validator.setErrorHandler(new FatalAdapter(serializer)); ohair@286: // work around a bug in JAXP validator in Tiger ohair@286: XMLFilterImpl f = new XMLFilterImpl() { ohair@286: @Override ohair@286: public void startPrefixMapping(String prefix, String uri) throws SAXException { ohair@286: super.startPrefixMapping(prefix.intern(), uri.intern()); ohair@286: } ohair@286: }; ohair@286: f.setContentHandler(validator); ohair@286: out = new ForkXmlOutput( new SAXOutput(f) { ohair@286: @Override ohair@286: public void startDocument(XMLSerializer serializer, boolean fragment, int[] nsUriIndex2prefixIndex, NamespaceContextImpl nsContext) throws SAXException, IOException, XMLStreamException { ohair@286: super.startDocument(serializer, false, nsUriIndex2prefixIndex, nsContext); ohair@286: } ohair@286: @Override ohair@286: public void endDocument(boolean fragment) throws SAXException, IOException, XMLStreamException { ohair@286: super.endDocument(false); ohair@286: } ohair@286: }, out ); ohair@286: } ohair@286: ohair@286: try { ohair@286: prewrite(out,isFragment(),postInitAction); ohair@286: serializer.childAsRoot(obj); ohair@286: postwrite(); ohair@286: } catch( SAXException e ) { ohair@286: throw new MarshalException(e); ohair@286: } catch (IOException e) { ohair@286: throw new MarshalException(e); ohair@286: } catch (XMLStreamException e) { ohair@286: throw new MarshalException(e); ohair@286: } finally { ohair@286: serializer.close(); ohair@286: } ohair@286: } finally { ohair@286: cleanUp(); ohair@286: } ohair@286: } ohair@286: ohair@286: private void cleanUp() { ohair@286: if(toBeFlushed!=null) ohair@286: try { ohair@286: toBeFlushed.flush(); ohair@286: } catch (IOException e) { ohair@286: // ignore ohair@286: } ohair@286: if(toBeClosed!=null) ohair@286: try { ohair@286: toBeClosed.close(); ohair@286: } catch (IOException e) { ohair@286: // ignore ohair@286: } ohair@286: toBeFlushed = null; ohair@286: toBeClosed = null; ohair@286: } ohair@286: ohair@286: // common parts between two write methods. ohair@286: ohair@286: private void prewrite(XmlOutput out, boolean fragment, Runnable postInitAction) throws IOException, SAXException, XMLStreamException { ohair@286: serializer.startDocument(out,fragment,getSchemaLocation(),getNoNSSchemaLocation()); ohair@286: if(postInitAction!=null) postInitAction.run(); ohair@286: if(prefixMapper!=null) { ohair@286: // be defensive as we work with the user's code ohair@286: String[] decls = prefixMapper.getContextualNamespaceDecls(); ohair@286: if(decls!=null) { // defensive check ohair@286: for( int i=0; i void setAdapter(Class type, A adapter) { ohair@286: if(type==null) ohair@286: throw new IllegalArgumentException(); ohair@286: serializer.putAdapter(type,adapter); ohair@286: } ohair@286: ohair@286: @Override ohair@286: public A getAdapter(Class type) { ohair@286: if(type==null) ohair@286: throw new IllegalArgumentException(); ohair@286: if(serializer.containsAdapter(type)) ohair@286: // so as not to create a new instance when this method is called ohair@286: return serializer.getAdapter(type); ohair@286: else ohair@286: return null; ohair@286: } ohair@286: ohair@286: @Override ohair@286: public void setAttachmentMarshaller(AttachmentMarshaller am) { ohair@286: serializer.attachmentMarshaller = am; ohair@286: } ohair@286: ohair@286: @Override ohair@286: public AttachmentMarshaller getAttachmentMarshaller() { ohair@286: return serializer.attachmentMarshaller; ohair@286: } ohair@286: ohair@286: @Override ohair@286: public Schema getSchema() { ohair@286: return schema; ohair@286: } ohair@286: ohair@286: @Override ohair@286: public void setSchema(Schema s) { ohair@286: this.schema = s; ohair@286: } ohair@286: ohair@286: /** ohair@286: * Default error handling behavior fot {@link Marshaller}. ohair@286: */ ohair@286: public boolean handleEvent(ValidationEvent event) { ohair@286: // draconian by default ohair@286: return false; ohair@286: } ohair@286: ohair@286: @Override ohair@286: public Listener getListener() { ohair@286: return externalListener; ohair@286: } ohair@286: ohair@286: @Override ohair@286: public void setListener(Listener listener) { ohair@286: externalListener = listener; ohair@286: } ohair@286: ohair@286: // features supported ohair@286: protected static final String INDENT_STRING = "com.sun.xml.internal.bind.indentString"; ohair@286: protected static final String PREFIX_MAPPER = "com.sun.xml.internal.bind.namespacePrefixMapper"; ohair@286: protected static final String ENCODING_HANDLER = "com.sun.xml.internal.bind.characterEscapeHandler"; ohair@286: protected static final String ENCODING_HANDLER2 = "com.sun.xml.internal.bind.marshaller.CharacterEscapeHandler"; ohair@286: protected static final String XMLDECLARATION = "com.sun.xml.internal.bind.xmlDeclaration"; ohair@286: protected static final String XML_HEADERS = "com.sun.xml.internal.bind.xmlHeaders"; ohair@286: protected static final String C14N = JAXBRIContext.CANONICALIZATION_SUPPORT; ohair@286: protected static final String OBJECT_IDENTITY_CYCLE_DETECTION = "com.sun.xml.internal.bind.objectIdentitityCycleDetection"; ohair@286: }