1 /* 2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.xml.internal.bind.v2.runtime.output; 27 28 import java.io.IOException; 29 30 import javax.xml.bind.attachment.AttachmentMarshaller; 31 import javax.xml.stream.XMLStreamException; 32 33 import com.sun.xml.internal.bind.v2.WellKnownNamespace; 34 import com.sun.xml.internal.bind.v2.runtime.Name; 35 import com.sun.xml.internal.bind.v2.runtime.XMLSerializer; 36 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Base64Data; 37 38 import org.xml.sax.SAXException; 39 40 /** 41 * {@link XmlOutput} decorator that supports MTOM. 42 * 43 * @author Kohsuke Kawaguchi 44 */ 45 public final class MTOMXmlOutput extends XmlOutputAbstractImpl { 46 47 private final XmlOutput next; 48 49 /** 50 * Remembers the last namespace URI and local name so that we can pass them to 51 * {@link AttachmentMarshaller}. 52 */ 53 private String nsUri,localName; 54 55 public MTOMXmlOutput(XmlOutput next) { 56 this.next = next; 57 } 58 59 public void startDocument(XMLSerializer serializer, boolean fragment, int[] nsUriIndex2prefixIndex, NamespaceContextImpl nsContext) throws IOException, SAXException, XMLStreamException { 60 super.startDocument(serializer,fragment,nsUriIndex2prefixIndex, nsContext); 61 next.startDocument(serializer, fragment, nsUriIndex2prefixIndex, nsContext); 62 } 63 64 public void endDocument(boolean fragment) throws IOException, SAXException, XMLStreamException { 65 next.endDocument(fragment); 66 super.endDocument(fragment); 67 } 68 69 public void beginStartTag(Name name) throws IOException, XMLStreamException { 70 next.beginStartTag(name); 71 this.nsUri = name.nsUri; 72 this.localName = name.localName; 73 } 74 75 public void beginStartTag(int prefix, String localName) throws IOException, XMLStreamException { 76 next.beginStartTag(prefix, localName); 77 this.nsUri = nsContext.getNamespaceURI(prefix); 78 this.localName = localName; 79 } 80 81 public void attribute( Name name, String value ) throws IOException, XMLStreamException { 82 next.attribute(name, value); 83 } 84 85 public void attribute( int prefix, String localName, String value ) throws IOException, XMLStreamException { 86 next.attribute(prefix, localName, value); 87 } 88 89 public void endStartTag() throws IOException, SAXException { 90 next.endStartTag(); 91 } 92 93 public void endTag(Name name) throws IOException, SAXException, XMLStreamException { 94 next.endTag(name); 95 } 96 97 public void endTag(int prefix, String localName) throws IOException, SAXException, XMLStreamException { 98 next.endTag(prefix, localName); 99 } 100 101 public void text( String value, boolean needsSeparatingWhitespace ) throws IOException, SAXException, XMLStreamException { 102 next.text(value,needsSeparatingWhitespace); 103 } 104 105 public void text( Pcdata value, boolean needsSeparatingWhitespace ) throws IOException, SAXException, XMLStreamException { 106 if(value instanceof Base64Data && !serializer.getInlineBinaryFlag()) { 107 Base64Data b64d = (Base64Data) value; 108 String cid; 109 if(b64d.hasData()) 110 cid = serializer.attachmentMarshaller.addMtomAttachment( 111 b64d.get(),0,b64d.getDataLen(),b64d.getMimeType(),nsUri,localName); 112 else 113 cid = serializer.attachmentMarshaller.addMtomAttachment( 114 b64d.getDataHandler(),nsUri,localName); 115 116 if(cid!=null) { 117 nsContext.getCurrent().push(); 118 int prefix = nsContext.declareNsUri(WellKnownNamespace.XOP,"xop",false); 119 beginStartTag(prefix,"Include"); 120 attribute(-1,"href",cid); 121 endStartTag(); 122 endTag(prefix,"Include"); 123 nsContext.getCurrent().pop(); 124 return; 125 } 126 } 127 next.text(value, needsSeparatingWhitespace); 128 } 129 }