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