325N/A/*
325N/A * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A
325N/Apackage com.sun.xml.internal.bind.v2.runtime.output;
325N/A
325N/Aimport java.io.IOException;
325N/A
325N/Aimport javax.xml.bind.attachment.AttachmentMarshaller;
325N/Aimport javax.xml.stream.XMLStreamException;
325N/A
325N/Aimport com.sun.xml.internal.bind.v2.WellKnownNamespace;
325N/Aimport com.sun.xml.internal.bind.v2.runtime.Name;
325N/Aimport com.sun.xml.internal.bind.v2.runtime.XMLSerializer;
325N/Aimport com.sun.xml.internal.bind.v2.runtime.unmarshaller.Base64Data;
325N/A
325N/Aimport org.xml.sax.SAXException;
325N/A
325N/A/**
325N/A * {@link XmlOutput} decorator that supports MTOM.
325N/A *
325N/A * @author Kohsuke Kawaguchi
325N/A */
325N/Apublic final class MTOMXmlOutput extends XmlOutputAbstractImpl {
325N/A
325N/A private final XmlOutput next;
325N/A
325N/A /**
325N/A * Remembers the last namespace URI and local name so that we can pass them to
325N/A * {@link AttachmentMarshaller}.
325N/A */
325N/A private String nsUri,localName;
325N/A
325N/A public MTOMXmlOutput(XmlOutput next) {
325N/A this.next = next;
325N/A }
325N/A
325N/A public void startDocument(XMLSerializer serializer, boolean fragment, int[] nsUriIndex2prefixIndex, NamespaceContextImpl nsContext) throws IOException, SAXException, XMLStreamException {
325N/A super.startDocument(serializer,fragment,nsUriIndex2prefixIndex, nsContext);
325N/A next.startDocument(serializer, fragment, nsUriIndex2prefixIndex, nsContext);
325N/A }
325N/A
325N/A public void endDocument(boolean fragment) throws IOException, SAXException, XMLStreamException {
325N/A next.endDocument(fragment);
325N/A super.endDocument(fragment);
325N/A }
325N/A
325N/A public void beginStartTag(Name name) throws IOException, XMLStreamException {
325N/A next.beginStartTag(name);
325N/A this.nsUri = name.nsUri;
325N/A this.localName = name.localName;
325N/A }
325N/A
325N/A public void beginStartTag(int prefix, String localName) throws IOException, XMLStreamException {
325N/A next.beginStartTag(prefix, localName);
325N/A this.nsUri = nsContext.getNamespaceURI(prefix);
325N/A this.localName = localName;
325N/A }
325N/A
325N/A public void attribute( Name name, String value ) throws IOException, XMLStreamException {
325N/A next.attribute(name, value);
325N/A }
325N/A
325N/A public void attribute( int prefix, String localName, String value ) throws IOException, XMLStreamException {
325N/A next.attribute(prefix, localName, value);
325N/A }
325N/A
325N/A public void endStartTag() throws IOException, SAXException {
325N/A next.endStartTag();
325N/A }
325N/A
325N/A public void endTag(Name name) throws IOException, SAXException, XMLStreamException {
325N/A next.endTag(name);
325N/A }
325N/A
325N/A public void endTag(int prefix, String localName) throws IOException, SAXException, XMLStreamException {
325N/A next.endTag(prefix, localName);
325N/A }
325N/A
325N/A public void text( String value, boolean needsSeparatingWhitespace ) throws IOException, SAXException, XMLStreamException {
325N/A next.text(value,needsSeparatingWhitespace);
325N/A }
325N/A
325N/A public void text( Pcdata value, boolean needsSeparatingWhitespace ) throws IOException, SAXException, XMLStreamException {
325N/A if(value instanceof Base64Data && !serializer.getInlineBinaryFlag()) {
325N/A Base64Data b64d = (Base64Data) value;
325N/A String cid;
325N/A if(b64d.hasData())
325N/A cid = serializer.attachmentMarshaller.addMtomAttachment(
325N/A b64d.get(),0,b64d.getDataLen(),b64d.getMimeType(),nsUri,localName);
325N/A else
325N/A cid = serializer.attachmentMarshaller.addMtomAttachment(
325N/A b64d.getDataHandler(),nsUri,localName);
325N/A
325N/A if(cid!=null) {
325N/A nsContext.getCurrent().push();
325N/A int prefix = nsContext.declareNsUri(WellKnownNamespace.XOP,"xop",false);
325N/A beginStartTag(prefix,"Include");
325N/A attribute(-1,"href",cid);
325N/A endStartTag();
325N/A endTag(prefix,"Include");
325N/A nsContext.getCurrent().pop();
325N/A return;
325N/A }
325N/A }
325N/A next.text(value, needsSeparatingWhitespace);
325N/A }
325N/A}