src/share/jaxws_classes/com/sun/xml/internal/bind/v2/runtime/output/MTOMXmlOutput.java

changeset 0
373ffda63c9a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/bind/v2/runtime/output/MTOMXmlOutput.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,129 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.xml.internal.bind.v2.runtime.output;
    1.30 +
    1.31 +import java.io.IOException;
    1.32 +
    1.33 +import javax.xml.bind.attachment.AttachmentMarshaller;
    1.34 +import javax.xml.stream.XMLStreamException;
    1.35 +
    1.36 +import com.sun.xml.internal.bind.v2.WellKnownNamespace;
    1.37 +import com.sun.xml.internal.bind.v2.runtime.Name;
    1.38 +import com.sun.xml.internal.bind.v2.runtime.XMLSerializer;
    1.39 +import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Base64Data;
    1.40 +
    1.41 +import org.xml.sax.SAXException;
    1.42 +
    1.43 +/**
    1.44 + * {@link XmlOutput} decorator that supports MTOM.
    1.45 + *
    1.46 + * @author Kohsuke Kawaguchi
    1.47 + */
    1.48 +public final class MTOMXmlOutput extends XmlOutputAbstractImpl {
    1.49 +
    1.50 +    private final XmlOutput next;
    1.51 +
    1.52 +    /**
    1.53 +     * Remembers the last namespace URI and local name so that we can pass them to
    1.54 +     * {@link AttachmentMarshaller}.
    1.55 +     */
    1.56 +    private String nsUri,localName;
    1.57 +
    1.58 +    public MTOMXmlOutput(XmlOutput next) {
    1.59 +        this.next = next;
    1.60 +    }
    1.61 +
    1.62 +    public void startDocument(XMLSerializer serializer, boolean fragment, int[] nsUriIndex2prefixIndex, NamespaceContextImpl nsContext) throws IOException, SAXException, XMLStreamException {
    1.63 +        super.startDocument(serializer,fragment,nsUriIndex2prefixIndex, nsContext);
    1.64 +        next.startDocument(serializer, fragment, nsUriIndex2prefixIndex, nsContext);
    1.65 +    }
    1.66 +
    1.67 +    public void endDocument(boolean fragment) throws IOException, SAXException, XMLStreamException {
    1.68 +        next.endDocument(fragment);
    1.69 +        super.endDocument(fragment);
    1.70 +    }
    1.71 +
    1.72 +    public void beginStartTag(Name name) throws IOException, XMLStreamException {
    1.73 +        next.beginStartTag(name);
    1.74 +        this.nsUri = name.nsUri;
    1.75 +        this.localName = name.localName;
    1.76 +    }
    1.77 +
    1.78 +    public void beginStartTag(int prefix, String localName) throws IOException, XMLStreamException {
    1.79 +        next.beginStartTag(prefix, localName);
    1.80 +        this.nsUri = nsContext.getNamespaceURI(prefix);
    1.81 +        this.localName = localName;
    1.82 +    }
    1.83 +
    1.84 +    public void attribute( Name name, String value ) throws IOException, XMLStreamException {
    1.85 +        next.attribute(name, value);
    1.86 +    }
    1.87 +
    1.88 +    public void attribute( int prefix, String localName, String value ) throws IOException, XMLStreamException {
    1.89 +        next.attribute(prefix, localName, value);
    1.90 +    }
    1.91 +
    1.92 +    public void endStartTag() throws IOException, SAXException {
    1.93 +        next.endStartTag();
    1.94 +    }
    1.95 +
    1.96 +    public void endTag(Name name) throws IOException, SAXException, XMLStreamException {
    1.97 +        next.endTag(name);
    1.98 +    }
    1.99 +
   1.100 +    public void endTag(int prefix, String localName) throws IOException, SAXException, XMLStreamException {
   1.101 +        next.endTag(prefix, localName);
   1.102 +    }
   1.103 +
   1.104 +    public void text( String value, boolean needsSeparatingWhitespace ) throws IOException, SAXException, XMLStreamException {
   1.105 +        next.text(value,needsSeparatingWhitespace);
   1.106 +    }
   1.107 +
   1.108 +    public void text( Pcdata value, boolean needsSeparatingWhitespace ) throws IOException, SAXException, XMLStreamException {
   1.109 +        if(value instanceof Base64Data && !serializer.getInlineBinaryFlag()) {
   1.110 +            Base64Data b64d = (Base64Data) value;
   1.111 +            String cid;
   1.112 +            if(b64d.hasData())
   1.113 +                cid = serializer.attachmentMarshaller.addMtomAttachment(
   1.114 +                                b64d.get(),0,b64d.getDataLen(),b64d.getMimeType(),nsUri,localName);
   1.115 +            else
   1.116 +                cid = serializer.attachmentMarshaller.addMtomAttachment(
   1.117 +                    b64d.getDataHandler(),nsUri,localName);
   1.118 +
   1.119 +            if(cid!=null) {
   1.120 +                nsContext.getCurrent().push();
   1.121 +                int prefix = nsContext.declareNsUri(WellKnownNamespace.XOP,"xop",false);
   1.122 +                beginStartTag(prefix,"Include");
   1.123 +                attribute(-1,"href",cid);
   1.124 +                endStartTag();
   1.125 +                endTag(prefix,"Include");
   1.126 +                nsContext.getCurrent().pop();
   1.127 +                return;
   1.128 +            }
   1.129 +        }
   1.130 +        next.text(value, needsSeparatingWhitespace);
   1.131 +    }
   1.132 +}

mercurial